From 94f9544df5138105271ac34415b7abd1fdc58919 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Wed, 1 Jul 2020 14:46:15 +0200 Subject: [PATCH] QmlDesigner: Rename CppTypeData to QmlTypeData Also adding getQMLSingletons(). The Backendmodel now also knows about QML singletons. Change-Id: I96e130678ef956e569960c431f45bd362d026f5f Reviewed-by: Aleksei German Reviewed-by: Thomas Hartmann --- .../connectioneditor/addnewbackenddialog.cpp | 8 ++--- .../connectioneditor/addnewbackenddialog.h | 4 +-- .../connectioneditor/backendmodel.cpp | 14 ++++---- .../connectioneditor/backendmodel.h | 2 +- .../designercore/include/rewriterview.h | 5 +-- .../instances/nodeinstanceview.cpp | 2 +- .../designercore/model/rewriterview.cpp | 35 ++++++++++--------- .../designercore/model/texttomodelmerger.cpp | 31 ++++++++++++++++ .../designercore/model/texttomodelmerger.h | 6 +++- 9 files changed, 73 insertions(+), 34 deletions(-) diff --git a/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.cpp b/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.cpp index 49f47dfef27..dcd22bc06b1 100644 --- a/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.cpp +++ b/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.cpp @@ -51,11 +51,11 @@ AddNewBackendDialog::~AddNewBackendDialog() delete m_ui; } -void AddNewBackendDialog::setupPossibleTypes(const QList &types) +void AddNewBackendDialog::setupPossibleTypes(const QList &types) { QSignalBlocker blocker(this); m_typeData = types; - for (const CppTypeData &typeData : types) + for (const QmlTypeData &typeData : types) m_ui->comboBox->addItem(typeData.typeName); m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_ui->comboBox->count() > 0); @@ -67,7 +67,7 @@ QString AddNewBackendDialog::importString() const if (m_ui->comboBox->currentIndex() < 0) return QString(); - CppTypeData typeData = m_typeData.at(m_ui->comboBox->currentIndex()); + QmlTypeData typeData = m_typeData.at(m_ui->comboBox->currentIndex()); return typeData.importUrl + " " + typeData.versionString; } @@ -100,7 +100,7 @@ void AddNewBackendDialog::invalidate() if (m_ui->comboBox->currentIndex() < 0) return; - CppTypeData typeData = m_typeData.at(m_ui->comboBox->currentIndex()); + QmlTypeData typeData = m_typeData.at(m_ui->comboBox->currentIndex()); m_ui->importLabel->setText(importString()); m_ui->checkBox->setChecked(false); diff --git a/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.h b/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.h index 4cfa46d66e9..363721c14b9 100644 --- a/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.h +++ b/src/plugins/qmldesigner/components/connectioneditor/addnewbackenddialog.h @@ -42,7 +42,7 @@ class AddNewBackendDialog : public QDialog public: explicit AddNewBackendDialog(QWidget *parent = nullptr); ~AddNewBackendDialog() override; - void setupPossibleTypes(const QList &types); + void setupPossibleTypes(const QList &types); QString importString() const; QString type() const; bool applied() const; @@ -53,7 +53,7 @@ private: void invalidate(); Ui::AddNewBackendDialog *m_ui; - QList m_typeData; + QList m_typeData; bool m_applied = false; bool m_isSingleton = false; diff --git a/src/plugins/qmldesigner/components/connectioneditor/backendmodel.cpp b/src/plugins/qmldesigner/components/connectioneditor/backendmodel.cpp index e17f827025b..d64a9898a82 100644 --- a/src/plugins/qmldesigner/components/connectioneditor/backendmodel.cpp +++ b/src/plugins/qmldesigner/components/connectioneditor/backendmodel.cpp @@ -75,7 +75,7 @@ void BackendModel::resetModel() static const PropertyTypeList simpleTypes = {"int", "real", "color", "string"}; if (rewriterView) - for (const CppTypeData &cppTypeData : rewriterView->getCppTypes()) + for (const QmlTypeData &cppTypeData : rewriterView->getQMLTypes()) if (cppTypeData.isSingleton) { NodeMetaInfo metaInfo = m_connectionView->model()->metaInfo(cppTypeData.typeName.toUtf8()); if (metaInfo.isValid() && !metaInfo.isSubclassOf("QtQuick.Item")) { @@ -146,20 +146,20 @@ QStringList BackendModel::possibleCppTypes() const QStringList list; if (rewriterView) - foreach (const CppTypeData &cppTypeData, rewriterView->getCppTypes()) + foreach (const QmlTypeData &cppTypeData, rewriterView->getQMLTypes()) list.append(cppTypeData.typeName); return list; } -CppTypeData BackendModel::cppTypeDataForType(const QString &typeName) const +QmlTypeData BackendModel::cppTypeDataForType(const QString &typeName) const { RewriterView *rewriterView = m_connectionView->model()->rewriterView(); if (!rewriterView) - return CppTypeData(); + return QmlTypeData(); - return Utils::findOr(rewriterView->getCppTypes(), CppTypeData(), [&typeName](const CppTypeData &data) { + return Utils::findOr(rewriterView->getQMLTypes(), QmlTypeData(), [&typeName](const QmlTypeData &data) { return typeName == data.typeName; }); } @@ -173,7 +173,7 @@ void BackendModel::deletePropertyByRow(int rowNumber) /* singleton case remove the import */ if (data(index(rowNumber, 0), Qt::UserRole + 1).toBool()) { const QString typeName = data(index(rowNumber, 0), Qt::UserRole + 1).toString(); - CppTypeData cppTypeData = cppTypeDataForType(typeName); + QmlTypeData cppTypeData = cppTypeDataForType(typeName); if (cppTypeData.isSingleton) { @@ -214,7 +214,7 @@ void BackendModel::addNewBackend() QStringList availableTypes; if (rewriterView) - dialog.setupPossibleTypes(Utils::filtered(rewriterView->getCppTypes(), [model](const CppTypeData &cppTypeData) { + dialog.setupPossibleTypes(Utils::filtered(rewriterView->getQMLTypes(), [model](const QmlTypeData &cppTypeData) { return !cppTypeData.isSingleton || !model->metaInfo(cppTypeData.typeName.toUtf8()).isValid(); /* Only show singletons if the import is missing */ })); diff --git a/src/plugins/qmldesigner/components/connectioneditor/backendmodel.h b/src/plugins/qmldesigner/components/connectioneditor/backendmodel.h index 8abbbe77fa1..2f355c71922 100644 --- a/src/plugins/qmldesigner/components/connectioneditor/backendmodel.h +++ b/src/plugins/qmldesigner/components/connectioneditor/backendmodel.h @@ -52,7 +52,7 @@ public: void resetModel(); QStringList possibleCppTypes() const; - CppTypeData cppTypeDataForType(const QString &typeName) const; + QmlTypeData cppTypeDataForType(const QString &typeName) const; void deletePropertyByRow(int rowNumber); diff --git a/src/plugins/qmldesigner/designercore/include/rewriterview.h b/src/plugins/qmldesigner/designercore/include/rewriterview.h index dcd85c950d0..0601da3acf4 100644 --- a/src/plugins/qmldesigner/designercore/include/rewriterview.h +++ b/src/plugins/qmldesigner/designercore/include/rewriterview.h @@ -53,7 +53,7 @@ class ModelNodePositionStorage; } //Internal -struct CppTypeData +struct QmlTypeData { QString superClassName; QString importUrl; @@ -61,6 +61,7 @@ struct CppTypeData QString cppClassName; QString typeName; bool isSingleton = false; + bool isCppType = false; }; class QMLDESIGNERCORE_EXPORT RewriterView : public AbstractView @@ -158,7 +159,7 @@ public: QStringList autoComplete(const QString &text, int pos, bool explicitComplete = true); - QList getCppTypes(); + QList getQMLTypes() const; void setWidgetStatusCallback(std::function setWidgetStatusCallback); diff --git a/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp b/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp index 2e3b5be32b4..7cf2784a7d3 100644 --- a/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp +++ b/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp @@ -958,7 +958,7 @@ CreateSceneCommand NodeInstanceView::createCreateSceneCommand() QVector mockupTypesVector; - for (const CppTypeData &cppTypeData : model()->rewriterView()->getCppTypes()) { + for (const QmlTypeData &cppTypeData : model()->rewriterView()->getQMLTypes()) { const QString versionString = cppTypeData.versionString; int majorVersion = -1; int minorVersion = -1; diff --git a/src/plugins/qmldesigner/designercore/model/rewriterview.cpp b/src/plugins/qmldesigner/designercore/model/rewriterview.cpp index 5d0cd5da351..eb9c6507469 100644 --- a/src/plugins/qmldesigner/designercore/model/rewriterview.cpp +++ b/src/plugins/qmldesigner/designercore/model/rewriterview.cpp @@ -960,27 +960,31 @@ QStringList RewriterView::autoComplete(const QString &text, int pos, bool explic return list; } -QList RewriterView::getCppTypes() +QList RewriterView::getQMLTypes() const { - QList cppDataList; - for (const QmlJS::ModelManagerInterface::CppData &cppData : QmlJS::ModelManagerInterface::instance()->cppData().values()) + QList qmlDataList; + + qmlDataList.append(m_textToModelMerger->getQMLSingletons()); + + for (const QmlJS::ModelManagerInterface::CppData &cppData : + QmlJS::ModelManagerInterface::instance()->cppData().values()) for (const LanguageUtils::FakeMetaObject::ConstPtr &fakeMetaObject : cppData.exportedTypes) { - for (const LanguageUtils::FakeMetaObject::Export &exportItem : fakeMetaObject->exports()) { + for (const LanguageUtils::FakeMetaObject::Export &exportItem : + fakeMetaObject->exports()) { + QmlTypeData qmlData; + qmlData.cppClassName = fakeMetaObject->className(); + qmlData.typeName = exportItem.type; + qmlData.importUrl = exportItem.package; + qmlData.versionString = exportItem.version.toString(); + qmlData.superClassName = fakeMetaObject->superclassName(); + qmlData.isSingleton = fakeMetaObject->isSingleton(); - CppTypeData cppData; - cppData.cppClassName = fakeMetaObject->className(); - cppData.typeName = exportItem.type; - cppData.importUrl = exportItem.package; - cppData.versionString = exportItem.version.toString(); - cppData.superClassName = fakeMetaObject->superclassName(); - cppData.isSingleton = fakeMetaObject->isSingleton(); - - if (cppData.importUrl != "") //ignore pure unregistered cpp types - cppDataList.append(cppData); + if (qmlData.importUrl != "") //ignore pure unregistered cpp types + qmlDataList.append(qmlData); } } - return cppDataList; + return qmlDataList; } void RewriterView::setWidgetStatusCallback(std::function setWidgetStatusCallback) @@ -990,7 +994,6 @@ void RewriterView::setWidgetStatusCallback(std::function setWidgetS void RewriterView::qmlTextChanged() { - getCppTypes(); if (inErrorState()) return; diff --git a/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp b/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp index fafa408a468..94814b74367 100644 --- a/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp +++ b/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp @@ -2176,6 +2176,37 @@ QSet > TextToModelMerger::qrcMapping() const return m_qrcMapping; } +QList TextToModelMerger::getQMLSingletons() const +{ + QList list; + const QmlJS::Imports *imports = m_scopeChain->context()->imports( + m_scopeChain->document().data()); + + if (!imports) + return list; + + for (const QmlJS::Import &import : imports->all()) { + if (import.info.type() == ImportType::Library && !import.libraryPath.isEmpty()) { + const LibraryInfo &libraryInfo = m_scopeChain->context()->snapshot().libraryInfo( + import.libraryPath); + + for (const QmlDirParser::Component &component : libraryInfo.components()) { + if (component.singleton) { + QmlTypeData qmlData; + + qmlData.typeName = component.typeName; + qmlData.importUrl = import.info.name(); + qmlData.versionString = import.info.version().toString(); + qmlData.isSingleton = component.singleton; + + list.append(qmlData); + } + } + } + } + return list; +} + QString TextToModelMerger::textAt(const Document::Ptr &doc, const SourceLocation &location) { diff --git a/src/plugins/qmldesigner/designercore/model/texttomodelmerger.h b/src/plugins/qmldesigner/designercore/model/texttomodelmerger.h index 2884453f8c5..c729d2aeddc 100644 --- a/src/plugins/qmldesigner/designercore/model/texttomodelmerger.h +++ b/src/plugins/qmldesigner/designercore/model/texttomodelmerger.h @@ -41,6 +41,8 @@ namespace QmlDesigner { class RewriterView; class DocumentMessage; +struct QmlTypeData; + namespace Internal { class DifferenceHandler; @@ -131,7 +133,9 @@ public: void delayedSetup(); - QSet > qrcMapping() const; + QSet> qrcMapping() const; + + QList getQMLSingletons() const; private: void setupCustomParserNode(const ModelNode &node);