From dd1c89aa2217c4909c2a853ff150461cc49582d8 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 26 Jan 2021 14:03:58 +0200 Subject: [PATCH] QmlDesigner: Allow using assets from entire project in subfolders If editing a subcomponent qml, assets tab now lists assets from the entire project, and url/font choosers list appropriate resources from entire project with correct relative paths. Fixes: QDS-3638 Change-Id: I4a066c30d1bd696e8edbd17754bb9df2b7880873 Reviewed-by: Thomas Hartmann Reviewed-by: Mahmoud Badri --- .../imports/HelperWidgets/FontComboBox.qml | 2 +- .../itemlibrary/itemlibraryview.cpp | 2 +- .../propertyeditor/fileresourcesmodel.cpp | 42 +++++++++---------- .../propertyeditor/fileresourcesmodel.h | 7 ++-- src/plugins/qmldesigner/documentmanager.cpp | 7 ++++ src/plugins/qmldesigner/documentmanager.h | 1 + 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/FontComboBox.qml b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/FontComboBox.qml index 49f53c79987..4fce3153d2e 100644 --- a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/FontComboBox.qml +++ b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/FontComboBox.qml @@ -60,7 +60,7 @@ StudioControls.ComboBox { var familyNames = ["Arial", "Times New Roman", "Courier", "Verdana", "Tahoma"] // default fonts for (var i = 0; i < fileModel.fullPathModel.length; ++i) { // add custom fonts - var fontLoader = createFontLoader(fileModel.dirPath + "/" + fileModel.fullPathModel[i]); + var fontLoader = createFontLoader(fileModel.docPath + "/" + fileModel.fullPathModel[i]); familyNames.push(fontLoader.name); } diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp index 2117eb9e952..823baa5d149 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp @@ -143,7 +143,7 @@ void ItemLibraryView::modelAttached(Model *model) model->attachView(m_importManagerView); m_hasErrors = !rewriterView()->errors().isEmpty(); m_widget->setFlowMode(QmlItemNode(rootModelNode()).isFlowView()); - setResourcePath(QmlDesignerPlugin::instance()->documentManager().currentDesignDocument()->fileName().toFileInfo().absolutePath()); + setResourcePath(DocumentManager::currentResourcePath().toFileInfo().absoluteFilePath()); } void ItemLibraryView::modelAboutToBeDetached(Model *model) diff --git a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp index 71e2c996277..6c1bc578e49 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -53,8 +54,12 @@ void FileResourcesModel::setModelNodeBackend(const QVariant &modelNodeBackend) const auto backendObjectCasted = qobject_cast(modelNodeBackendObject); - if (backendObjectCasted) - m_path = backendObjectCasted->qmlObjectNode().modelNode().model()->fileUrl(); + if (backendObjectCasted) { + QmlDesigner::Model *model = backendObjectCasted->qmlObjectNode().modelNode().model(); + m_docPath = QDir{QFileInfo{model->fileUrl().toLocalFile()}.absolutePath()}; + m_path = QUrl::fromLocalFile(QmlDesigner::DocumentManager::currentResourcePath() + .toFileInfo().absoluteFilePath()); + } setupModel(); emit modelNodeBackendChanged(); @@ -91,9 +96,9 @@ QUrl FileResourcesModel::path() const return m_path; } -QUrl FileResourcesModel::dirPath() const +QUrl FileResourcesModel::docPath() const { - return QUrl::fromLocalFile(m_dirPath.path()); + return QUrl::fromLocalFile(m_docPath.path()); } void FileResourcesModel::setFilter(const QString &filter) @@ -121,28 +126,24 @@ QStringList FileResourcesModel::fileNameModel() const void FileResourcesModel::openFileDialog() { - QString modelPath = m_path.toLocalFile(); + QString resourcePath = m_path.toLocalFile(); + bool resourcePathChanged = m_lastResourcePath != resourcePath; + m_lastResourcePath = resourcePath; - m_lastModelPath = modelPath; + // First we try the last path this browser widget was opened with within current project + QString path = resourcePathChanged ? QString() : m_currentPath; - bool documentChanged = m_lastModelPath == modelPath; - - //First we try the last path this browser widget was opened with if the document was not changed - QString path = documentChanged ? QString() : m_currentPath; - - - //If that one is not valid we try the path for the current file + // If that one is not valid we try the path for the current file if (path.isEmpty() && !m_fileName.isEmpty()) - path = QFileInfo(modelPath + '/' + m_fileName.toString()).absolutePath(); + path = QFileInfo(m_fileName.toString()).absolutePath(); - - //Next we try to fall back to the path any file browser was opened with + // Next we try to fall back to the path any file browser was opened with if (!QFileInfo::exists(path)) path = s_lastBrowserPath; - //The last fallback is to try the path of the document + // The last fallback is to try the resource path if (!QFileInfo::exists(path)) - path = modelPath; + path = resourcePath; QString newFile = QFileDialog::getOpenFileName(Core::ICore::dialogParent(), tr("Open File"), @@ -151,7 +152,6 @@ void FileResourcesModel::openFileDialog() if (!newFile.isEmpty()) { setFileNameStr(newFile); - m_currentPath = QFileInfo(newFile).absolutePath(); s_lastBrowserPath = m_currentPath; } @@ -194,7 +194,7 @@ bool filterMetaIcons(const QString &fileName) void FileResourcesModel::setupModel() { - m_dirPath = QFileInfo(m_path.toLocalFile()).dir(); + m_dirPath = QDir(m_path.toLocalFile()); refreshModel(); @@ -214,7 +214,7 @@ void FileResourcesModel::refreshModel() while (it.hasNext()) { QString absolutePath = it.next(); if (filterMetaIcons(absolutePath)) { - QString filePath = m_dirPath.relativeFilePath(absolutePath); + QString filePath = m_docPath.relativeFilePath(absolutePath); m_fullPathModel.append(filePath); } } diff --git a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h index 226421f441f..c1ebd6a8ad9 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h +++ b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h @@ -43,7 +43,7 @@ class FileResourcesModel : public QObject Q_PROPERTY(QString filter READ filter WRITE setFilter) Q_PROPERTY(QVariant modelNodeBackendProperty READ modelNodeBackend WRITE setModelNodeBackend NOTIFY modelNodeBackendChanged) Q_PROPERTY(QUrl path READ path WRITE setPath) - Q_PROPERTY(QUrl dirPath READ dirPath) + Q_PROPERTY(QUrl docPath READ docPath) Q_PROPERTY(QStringList fullPathModel READ fullPathModel NOTIFY fullPathModelChanged) Q_PROPERTY(QStringList fileNameModel READ fileNameModel NOTIFY fileNameModelChanged) @@ -56,7 +56,7 @@ public: void setFileNameStr(const QString &fileName); void setPath(const QUrl &url); QUrl path() const; - QUrl dirPath() const; + QUrl docPath() const; void setFilter(const QString &filter); QString filter() const; QStringList fullPathModel() const; @@ -81,9 +81,10 @@ private: QUrl m_fileName; QUrl m_path; QDir m_dirPath; + QDir m_docPath; QString m_filter; QString m_currentPath; - QString m_lastModelPath; + QString m_lastResourcePath; QStringList m_fullPathModel; QStringList m_fileNameModel; Utils::FileSystemWatcher *m_fileSystemWatcher; diff --git a/src/plugins/qmldesigner/documentmanager.cpp b/src/plugins/qmldesigner/documentmanager.cpp index 109b6577b4f..49350f634f9 100644 --- a/src/plugins/qmldesigner/documentmanager.cpp +++ b/src/plugins/qmldesigner/documentmanager.cpp @@ -495,5 +495,12 @@ bool DocumentManager::belongsToQmakeProject() return proNode; } +Utils::FilePath DocumentManager::currentResourcePath() +{ + Utils::FilePath resourcePath = currentProjectDirPath(); + if (resourcePath.isEmpty()) + return currentFilePath().absolutePath(); + return resourcePath; +} } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/documentmanager.h b/src/plugins/qmldesigner/documentmanager.h index 8cd67eca9f6..f8b29fee5f9 100644 --- a/src/plugins/qmldesigner/documentmanager.h +++ b/src/plugins/qmldesigner/documentmanager.h @@ -65,6 +65,7 @@ public: static bool isoProFileSupportsAddingExistingFiles(const QString &resourceFileProPath); static bool addResourceFileToIsoProject(const QString &resourceFileProPath, const QString &resourceFilePath); static bool belongsToQmakeProject(); + static Utils::FilePath currentResourcePath(); private: QHash > m_designDocumentHash;