QmlPuppet: Register fonts at scene creation

Automatically register all fonts from 'fonts' folder at scene creation.

Fixes: QDS-3624
Change-Id: I38728e458952ea52c941244daaca715102a93c55
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Miikka Heikkinen
2021-01-25 17:33:05 +02:00
parent 59d24fa268
commit a57820f055
6 changed files with 27 additions and 0 deletions

View File

@@ -41,6 +41,7 @@ QDebug operator <<(QDebug debug, const CreateSceneCommand &command)
<< "imports: " << command.imports << ", " << "imports: " << command.imports << ", "
<< "mockupTypes: " << command.mockupTypes << ", " << "mockupTypes: " << command.mockupTypes << ", "
<< "fileUrl: " << command.fileUrl << ", " << "fileUrl: " << command.fileUrl << ", "
<< "resourceUrl: " << command.resourceUrl << ", "
<< "edit3dToolStates: " << command.edit3dToolStates << ", " << "edit3dToolStates: " << command.edit3dToolStates << ", "
<< "language: " << command.language << ")"; << "language: " << command.language << ")";
} }

View File

@@ -52,6 +52,7 @@ public:
const QVector<AddImportContainer> &importVector, const QVector<AddImportContainer> &importVector,
const QVector<MockupTypeContainer> &mockupTypeVector, const QVector<MockupTypeContainer> &mockupTypeVector,
const QUrl &fileUrl, const QUrl &fileUrl,
const QUrl &resourceUrl,
const QHash<QString, QVariantMap> &edit3dToolStates, const QHash<QString, QVariantMap> &edit3dToolStates,
const QString &language, const QString &language,
qint32 stateInstanceId) qint32 stateInstanceId)
@@ -64,6 +65,7 @@ public:
, imports(importVector) , imports(importVector)
, mockupTypes(mockupTypeVector) , mockupTypes(mockupTypeVector)
, fileUrl(fileUrl) , fileUrl(fileUrl)
, resourceUrl(resourceUrl)
, edit3dToolStates(edit3dToolStates) , edit3dToolStates(edit3dToolStates)
, language(language) , language(language)
, stateInstanceId{stateInstanceId} , stateInstanceId{stateInstanceId}
@@ -80,6 +82,7 @@ public:
out << command.imports; out << command.imports;
out << command.mockupTypes; out << command.mockupTypes;
out << command.fileUrl; out << command.fileUrl;
out << command.resourceUrl;
out << command.edit3dToolStates; out << command.edit3dToolStates;
out << command.language; out << command.language;
out << command.stateInstanceId; out << command.stateInstanceId;
@@ -98,6 +101,7 @@ public:
in >> command.imports; in >> command.imports;
in >> command.mockupTypes; in >> command.mockupTypes;
in >> command.fileUrl; in >> command.fileUrl;
in >> command.resourceUrl;
in >> command.edit3dToolStates; in >> command.edit3dToolStates;
in >> command.language; in >> command.language;
in >> command.stateInstanceId; in >> command.stateInstanceId;
@@ -115,6 +119,7 @@ public:
QVector<AddImportContainer> imports; QVector<AddImportContainer> imports;
QVector<MockupTypeContainer> mockupTypes; QVector<MockupTypeContainer> mockupTypes;
QUrl fileUrl; QUrl fileUrl;
QUrl resourceUrl;
QHash<QString, QVariantMap> edit3dToolStates; QHash<QString, QVariantMap> edit3dToolStates;
QString language; QString language;
qint32 stateInstanceId = 0; qint32 stateInstanceId = 0;

View File

@@ -88,6 +88,9 @@
#include <QUrl> #include <QUrl>
#include <QVariant> #include <QVariant>
#include <qqmllist.h> #include <qqmllist.h>
#include <QFontDatabase>
#include <QFileInfo>
#include <QDirIterator>
#include <algorithm> #include <algorithm>
@@ -322,6 +325,7 @@ void NodeInstanceServer::stopRenderTimer()
void NodeInstanceServer::createScene(const CreateSceneCommand &command) void NodeInstanceServer::createScene(const CreateSceneCommand &command)
{ {
registerFonts(command.resourceUrl);
setTranslationLanguage(command.language); setTranslationLanguage(command.language);
initializeView(); initializeView();
@@ -1502,4 +1506,13 @@ void NodeInstanceServer::setupState(qint32 stateInstanceId)
} }
} }
void NodeInstanceServer::registerFonts(const QUrl &resourceUrl) const
{
// Autoregister all fonts found inside the project
QDirIterator it {QFileInfo(resourceUrl.toLocalFile()).absoluteFilePath(),
{"*.ttf", "*.otf"}, QDir::Files, QDirIterator::Subdirectories};
while (it.hasNext())
QFontDatabase::addApplicationFont(it.next());
}
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -298,6 +298,7 @@ protected:
virtual void resizeCanvasToRootItem() = 0; virtual void resizeCanvasToRootItem() = 0;
void setupState(qint32 stateInstanceId); void setupState(qint32 stateInstanceId);
void registerFonts(const QUrl &resourceUrl) const;
private: private:
void setupOnlyWorkingImports(const QStringList &workingImportStatementList); void setupOnlyWorkingImports(const QStringList &workingImportStatementList);

View File

@@ -46,6 +46,7 @@ Qt5PreviewNodeInstanceServer::Qt5PreviewNodeInstanceServer(NodeInstanceClientInt
void Qt5PreviewNodeInstanceServer::createScene(const CreateSceneCommand &command) void Qt5PreviewNodeInstanceServer::createScene(const CreateSceneCommand &command)
{ {
registerFonts(command.resourceUrl);
setTranslationLanguage(command.language); setTranslationLanguage(command.language);
initializeView(); initializeView();
setupScene(command); setupScene(command);

View File

@@ -1071,6 +1071,12 @@ CreateSceneCommand NodeInstanceView::createCreateSceneCommand()
importVector, importVector,
mockupTypesVector, mockupTypesVector,
model()->fileUrl(), model()->fileUrl(),
#ifndef QMLDESIGNER_TEST
QUrl::fromLocalFile(QmlDesigner::DocumentManager::currentResourcePath()
.toFileInfo().absoluteFilePath()),
#else
QUrl::fromLocalFile(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath()),
#endif
m_edit3DToolStates[model()->fileUrl()], m_edit3DToolStates[model()->fileUrl()],
lastUsedLanguage, lastUsedLanguage,
stateInstanceId); stateInstanceId);