From 366e89e9cfb7d110b9b8d08a044e4d34ca5a3cae Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Tue, 15 Sep 2015 11:46:20 +0200 Subject: [PATCH] QmlDesigner: Adding helper function for icon browser Change-Id: I6f333c897802c8bf1d93b168f5d108b77bfa33c2 Reviewed-by: Eike Ziller Reviewed-by: Tim Jenssen --- src/plugins/qmldesigner/documentmanager.cpp | 138 ++++++++++++++++++ src/plugins/qmldesigner/documentmanager.h | 14 +- .../qmldesigner/qmldesigner_dependencies.pri | 5 +- 3 files changed, 155 insertions(+), 2 deletions(-) diff --git a/src/plugins/qmldesigner/documentmanager.cpp b/src/plugins/qmldesigner/documentmanager.cpp index ccfe398f6c9..e287dc95dae 100644 --- a/src/plugins/qmldesigner/documentmanager.cpp +++ b/src/plugins/qmldesigner/documentmanager.cpp @@ -45,11 +45,19 @@ #include #include #include +#include +#include +#include + +#include +#include #include namespace QmlDesigner { +Q_LOGGING_CATEGORY(documentManagerLog, "qtc.qtquickdesigner.documentmanager") + static inline DesignDocument* currentDesignDocument() { return QmlDesignerPlugin::instance()->documentManager().currentDesignDocument(); @@ -344,5 +352,135 @@ void DocumentManager::addFileToVersionControl(const QString &directoryPath, cons } } +Utils::FileName DocumentManager::currentFilePath() +{ + return QmlDesignerPlugin::instance()->documentManager().currentDesignDocument()->fileName(); +} + +QStringList DocumentManager::isoIconsQmakeVariableValue(const QString &proPath) +{ + ProjectExplorer::Node *node = ProjectExplorer::SessionManager::nodeForFile(Utils::FileName::fromString(proPath)); + if (!node) { + qCWarning(documentManagerLog) << "No node for .pro:" << proPath; + return QStringList(); + } + + ProjectExplorer::Node *parentNode = node->parentFolderNode(); + if (!parentNode) { + qCWarning(documentManagerLog) << "No parent node for node at" << proPath; + return QStringList(); + } + + QmakeProjectManager::QmakeProFileNode *proNode = dynamic_cast(parentNode); + if (!proNode) { + qCWarning(documentManagerLog) << "Parent node for node at" << proPath << "could not be converted to a QmakeProFileNode"; + return QStringList(); + } + + return proNode->variableValue(QmakeProjectManager::IsoIconsVar); +} + +bool DocumentManager::setIsoIconsQmakeVariableValue(const QString &proPath, const QStringList &value) +{ + ProjectExplorer::Node *node = ProjectExplorer::SessionManager::nodeForFile(Utils::FileName::fromString(proPath)); + if (!node) { + qCWarning(documentManagerLog) << "No node for .pro:" << proPath; + return false; + } + + ProjectExplorer::Node *parentNode = node->parentFolderNode(); + if (!parentNode) { + qCWarning(documentManagerLog) << "No parent node for node at" << proPath; + return false; + } + + QmakeProjectManager::QmakeProFileNode *proNode = dynamic_cast(parentNode); + if (!proNode) { + qCWarning(documentManagerLog) << "Node for" << proPath << "could not be converted to a QmakeProFileNode"; + return false; + } + + int flags = QmakeProjectManager::Internal::ProWriter::ReplaceValues | QmakeProjectManager::Internal::ProWriter::MultiLine; + return proNode->setProVariable("ISO_ICONS", value, QString(), flags); +} + +void DocumentManager::findPathToIsoProFile(bool *iconResourceFileAlreadyExists, QString *resourceFilePath, + QString *resourceFileProPath, const QString &isoIconsQrcFile) +{ + Utils::FileName qmlFileName = QmlDesignerPlugin::instance()->currentDesignDocument()->fileName(); + ProjectExplorer::Project *project = ProjectExplorer::SessionManager::projectForFile(qmlFileName); + ProjectExplorer::Node *node = ProjectExplorer::SessionManager::nodeForFile(qmlFileName)->parentFolderNode(); + ProjectExplorer::Node *iconQrcFileNode = 0; + + while (node && !iconQrcFileNode) { + qCDebug(documentManagerLog) << "Checking" << node->displayName() << "(" << node << node->nodeType() << ")"; + + if (node->nodeType() == ProjectExplorer::VirtualFolderNodeType && node->displayName() == "Resources") { + ProjectExplorer::VirtualFolderNode *virtualFolderNode = dynamic_cast(node); + + for (int subFolderIndex = 0; subFolderIndex < virtualFolderNode->subFolderNodes().size() && !iconQrcFileNode; ++subFolderIndex) { + ProjectExplorer::FolderNode *subFolderNode = virtualFolderNode->subFolderNodes().at(subFolderIndex); + + qCDebug(documentManagerLog) << "Checking if" << subFolderNode->displayName() << "(" + << subFolderNode << subFolderNode->nodeType() << ") is" << isoIconsQrcFile; + + if (subFolderNode->nodeType() == ProjectExplorer::FolderNodeType + && subFolderNode->displayName() == isoIconsQrcFile) { + qCDebug(documentManagerLog) << "Found" << isoIconsQrcFile << "in" << virtualFolderNode->path(); + + iconQrcFileNode = subFolderNode; + *resourceFileProPath = iconQrcFileNode->projectNode()->path().toString(); + } + } + } + + if (!iconQrcFileNode) { + qCDebug(documentManagerLog) << "Didn't find" << isoIconsQrcFile + << "in" << node->displayName() << "; checking parent"; + node = node->parentFolderNode(); + } + } + + if (!iconQrcFileNode) { + // The QRC file that we want doesn't exist or is not listed under RESOURCES in the .pro. + *resourceFilePath = project->projectDirectory().toString() + "/" + isoIconsQrcFile; + + // We assume that the .pro containing the QML file is an acceptable place to add the .qrc file. + ProjectExplorer::ProjectNode *projectNode = ProjectExplorer::SessionManager::nodeForFile(qmlFileName)->projectNode(); + *resourceFileProPath = projectNode->path().toString(); + } else { + // We found the QRC file that we want. + QString projectDirectory = ProjectExplorer::SessionManager::projectForNode(iconQrcFileNode)->projectDirectory().toString(); + *resourceFilePath = projectDirectory + "/" + isoIconsQrcFile; + } + + *iconResourceFileAlreadyExists = iconQrcFileNode != 0; +} + +bool DocumentManager::isoProFileSupportsAddingExistingFiles(const QString &resourceFileProPath) +{ + ProjectExplorer::Node *node = ProjectExplorer::SessionManager::nodeForFile(Utils::FileName::fromString(resourceFileProPath)); + ProjectExplorer::ProjectNode *projectNode = dynamic_cast(node->parentFolderNode()); + + if (!projectNode->supportedActions(projectNode).contains(ProjectExplorer::AddExistingFile)) { + qCWarning(documentManagerLog) << "Project" << projectNode->displayName() << "does not support adding existing files"; + return false; + } + + return true; +} + +bool DocumentManager::addResourceFileToIsoProject(const QString &resourceFileProPath, const QString &resourceFilePath) +{ + ProjectExplorer::Node *node = ProjectExplorer::SessionManager::nodeForFile(Utils::FileName::fromString(resourceFileProPath)); + ProjectExplorer::ProjectNode *projectNode = dynamic_cast(node->parentFolderNode()); + + if (!projectNode->addFiles(QStringList() << resourceFilePath)) { + qCWarning(documentManagerLog) << "Failed to add resource file to" << projectNode->displayName(); + return false; + } + return true; +} + } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/documentmanager.h b/src/plugins/qmldesigner/documentmanager.h index c20be264cb9..99795b543db 100644 --- a/src/plugins/qmldesigner/documentmanager.h +++ b/src/plugins/qmldesigner/documentmanager.h @@ -34,13 +34,17 @@ #include #include +#include #include namespace Core { class IEditor; } - +namespace ProjectExplorer { class Node; } +namespace ProjectExplorer { class Project; } namespace QmlDesigner { +Q_DECLARE_LOGGING_CATEGORY(documentManagerLog) + class QMLDESIGNERCORE_EXPORT DocumentManager : public QObject { Q_OBJECT @@ -58,6 +62,14 @@ public: static bool createFile(const QString &filePath, const QString &contents); static void addFileToVersionControl(const QString &directoryPath, const QString &newFilePath); + static Utils::FileName currentFilePath(); + + static QStringList isoIconsQmakeVariableValue(const QString &proPath); + static bool setIsoIconsQmakeVariableValue(const QString &proPath, const QStringList &value); + static void findPathToIsoProFile(bool *iconResourceFileAlreadyExists, QString *resourceFilePath, + QString *resourceFileProPath, const QString &isoIconsQrcFile); + static bool isoProFileSupportsAddingExistingFiles(const QString &resourceFileProPath); + static bool addResourceFileToIsoProject(const QString &resourceFileProPath, const QString &resourceFilePath); private: QHash > m_designDocumentHash; diff --git a/src/plugins/qmldesigner/qmldesigner_dependencies.pri b/src/plugins/qmldesigner/qmldesigner_dependencies.pri index f8357252343..1028b6d4003 100644 --- a/src/plugins/qmldesigner/qmldesigner_dependencies.pri +++ b/src/plugins/qmldesigner/qmldesigner_dependencies.pri @@ -8,7 +8,10 @@ QTC_PLUGIN_DEPENDS += \ texteditor \ qmljseditor \ qtsupport \ - projectexplorer + projectexplorer \ + qmakeprojectmanager \ + resourceeditor + INCLUDEPATH *= \ $$PWD \ $$PWD/../../../share/qtcreator/qml/qmlpuppet/interfaces \