From da0601e470d6a1f6e5f81eaf8e842a6fd6f241c7 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Tue, 10 Sep 2019 16:56:58 +0200 Subject: [PATCH] QmlDesigner: Add ItemFilterModel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This exposes a model to QML that allows to get a list of all ids of items of a specific type. This allows implementing a combobox that can e.g. set the id of any image element in the file. Change-Id: Ibc825cb5a66c951d29df12676f9433c9232fd8af Reviewed-by: Tim Jenssen Reviewed-by: Henning Gründl --- src/plugins/qmldesigner/CMakeLists.txt | 1 + .../propertyeditor/fileresourcesmodel.h | 2 +- .../propertyeditor/itemfiltermodel.cpp | 101 ++++++++++++++++++ .../propertyeditor/itemfiltermodel.h | 69 ++++++++++++ .../propertyeditor/propertyeditor.pri | 6 +- .../quick2propertyeditorview.cpp | 2 + src/plugins/qmldesigner/qmldesignerplugin.qbs | 2 + 7 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp create mode 100644 src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h diff --git a/src/plugins/qmldesigner/CMakeLists.txt b/src/plugins/qmldesigner/CMakeLists.txt index 3d8f5ce3cb2..c677105d6b0 100644 --- a/src/plugins/qmldesigner/CMakeLists.txt +++ b/src/plugins/qmldesigner/CMakeLists.txt @@ -298,6 +298,7 @@ extend_qtc_plugin(QmlDesigner SOURCES designerpropertymap.cpp designerpropertymap.h fileresourcesmodel.cpp fileresourcesmodel.h + itemfiltermodel.cpp itemfiltermodel.h gradientmodel.cpp gradientmodel.h gradientpresetcustomlistmodel.cpp gradientpresetcustomlistmodel.h gradientpresetdefaultlistmodel.cpp gradientpresetdefaultlistmodel.h diff --git a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h index f6866310796..dc124b5739e 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h +++ b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt Creator. diff --git a/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp new file mode 100644 index 00000000000..fab5366e6de --- /dev/null +++ b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "itemfiltermodel.h" + +#include +#include +#include + +#include +#include +#include + +ItemFilterModel::ItemFilterModel(QObject *parent) : + QObject(parent), m_typeFilter("QtQuick.Item"), m_lock(false) +{ +} + +void ItemFilterModel::setModelNodeBackend(const QVariant &modelNodeBackend) +{ + + auto modelNodeBackendObject = modelNodeBackend.value(); + + const auto backendObjectCasted = + qobject_cast(modelNodeBackendObject); + + if (backendObjectCasted) + m_modelNode = backendObjectCasted->qmlObjectNode().modelNode(); + + setupModel(); + emit modelNodeBackendChanged(); +} + +void ItemFilterModel::setTypeFilter(const QString &filter) +{ + if (m_typeFilter != filter) { + m_typeFilter = filter; + setupModel(); + } +} + +QString ItemFilterModel::typeFilter() const +{ + return m_typeFilter; +} + +void ItemFilterModel::registerDeclarativeType() +{ + qmlRegisterType("HelperWidgets",2,0,"ItemFilterModel"); +} + +QVariant ItemFilterModel::modelNodeBackend() const +{ + return QVariant(); +} + + +void ItemFilterModel::setupModel() +{ + if (!m_modelNode.isValid()) + return; + + m_lock = true; + m_model.clear(); + + for (const QmlDesigner::ModelNode &node : m_modelNode.view()->allModelNodes()) { + if (node.hasId() && node.metaInfo().isValid() && node.metaInfo().isSubclassOf(m_typeFilter.toUtf8())) + m_model.append(node.id()); + } + + m_lock = false; + + emit itemModelChanged(); +} + +QStringList ItemFilterModel::itemModel() const +{ + return m_model; +} diff --git a/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h new file mode 100644 index 00000000000..032bd2baff2 --- /dev/null +++ b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include + +#include +#include +#include +#include +#include + +class ItemFilterModel : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString typeFilter READ typeFilter WRITE setTypeFilter) + Q_PROPERTY(QVariant modelNodeBackendProperty READ modelNodeBackend WRITE setModelNodeBackend NOTIFY modelNodeBackendChanged) + Q_PROPERTY(QStringList itemModel READ itemModel NOTIFY itemModelChanged) + +public: + explicit ItemFilterModel(QObject *parent = nullptr); + + void setModelNodeBackend(const QVariant &modelNodeBackend); + void setTypeFilter(const QString &typeFilter); + QString typeFilter() const; + void setupModel(); + QStringList itemModel() const; + + static void registerDeclarativeType(); + +signals: + void modelNodeBackendChanged(); + void itemModelChanged(); + +private: + QVariant modelNodeBackend() const; + +private: + QString m_typeFilter; + bool m_lock; + QStringList m_model; + QmlDesigner::ModelNode m_modelNode; +}; + +QML_DECLARE_TYPE(ItemFilterModel) diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri b/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri index b32a744016f..0777202db4f 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri @@ -18,7 +18,8 @@ SOURCES += propertyeditorview.cpp \ gradientpresetcustomlistmodel.cpp \ simplecolorpalette.cpp \ simplecolorpalettemodel.cpp \ - simplecolorpalettesingleton.cpp + simplecolorpalettesingleton.cpp \ + itemfiltermodel.cpp HEADERS += propertyeditorview.h \ qmlanchorbindingproxy.h \ @@ -38,6 +39,7 @@ HEADERS += propertyeditorview.h \ gradientpresetcustomlistmodel.h \ simplecolorpalette.h \ simplecolorpalettemodel.h \ - simplecolorpalettesingleton.h + simplecolorpalettesingleton.h \ + itemfiltermodel.h QT += qml quick diff --git a/src/plugins/qmldesigner/components/propertyeditor/quick2propertyeditorview.cpp b/src/plugins/qmldesigner/components/propertyeditor/quick2propertyeditorview.cpp index e6288ea861c..7ff25cad8ea 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/quick2propertyeditorview.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/quick2propertyeditorview.cpp @@ -30,6 +30,7 @@ #include "gradientmodel.h" #include "gradientpresetdefaultlistmodel.h" #include "gradientpresetcustomlistmodel.h" +#include "itemfiltermodel.h" #include "simplecolorpalettemodel.h" #include "bindingeditor/bindingeditor.h" #include "qmlanchorbindingproxy.h" @@ -54,6 +55,7 @@ void Quick2PropertyEditorView::registerQmlTypes() GradientModel::registerDeclarativeType(); GradientPresetDefaultListModel::registerDeclarativeType(); GradientPresetCustomListModel::registerDeclarativeType(); + ItemFilterModel::registerDeclarativeType(); SimpleColorPaletteModel::registerDeclarativeType(); Internal::QmlAnchorBindingProxy::registerDeclarativeType(); BindingEditor::registerDeclarativeType(); diff --git a/src/plugins/qmldesigner/qmldesignerplugin.qbs b/src/plugins/qmldesigner/qmldesignerplugin.qbs index 662b38ac3a4..a89bd8bb9e6 100644 --- a/src/plugins/qmldesigner/qmldesignerplugin.qbs +++ b/src/plugins/qmldesigner/qmldesignerplugin.qbs @@ -564,6 +564,8 @@ Project { "propertyeditor/designerpropertymap.h", "propertyeditor/fileresourcesmodel.cpp", "propertyeditor/fileresourcesmodel.h", + "propertyeditor/itemfiltermodel.cpp", + "propertyeditor/itemfiltermodel.h", "propertyeditor/gradientmodel.cpp", "propertyeditor/gradientmodel.h", "propertyeditor/gradientpresetcustomlistmodel.cpp",