QmlDesigner: Add ItemFilterModel

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 <tim.jenssen@qt.io>
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
This commit is contained in:
Thomas Hartmann
2019-09-10 16:56:58 +02:00
parent 2509dd4c32
commit da0601e470
7 changed files with 180 additions and 3 deletions

View File

@@ -298,6 +298,7 @@ extend_qtc_plugin(QmlDesigner
SOURCES SOURCES
designerpropertymap.cpp designerpropertymap.h designerpropertymap.cpp designerpropertymap.h
fileresourcesmodel.cpp fileresourcesmodel.h fileresourcesmodel.cpp fileresourcesmodel.h
itemfiltermodel.cpp itemfiltermodel.h
gradientmodel.cpp gradientmodel.h gradientmodel.cpp gradientmodel.h
gradientpresetcustomlistmodel.cpp gradientpresetcustomlistmodel.h gradientpresetcustomlistmodel.cpp gradientpresetcustomlistmodel.h
gradientpresetdefaultlistmodel.cpp gradientpresetdefaultlistmodel.h gradientpresetdefaultlistmodel.cpp gradientpresetdefaultlistmodel.h

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.

View File

@@ -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 <abstractview.h>
#include <model.h>
#include <nodemetainfo.h>
#include <QFileDialog>
#include <QDirIterator>
#include <qmlmodelnodeproxy.h>
ItemFilterModel::ItemFilterModel(QObject *parent) :
QObject(parent), m_typeFilter("QtQuick.Item"), m_lock(false)
{
}
void ItemFilterModel::setModelNodeBackend(const QVariant &modelNodeBackend)
{
auto modelNodeBackendObject = modelNodeBackend.value<QObject*>();
const auto backendObjectCasted =
qobject_cast<const QmlDesigner::QmlModelNodeProxy *>(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<ItemFilterModel>("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;
}

View File

@@ -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 <qmlitemnode.h>
#include <QDir>
#include <QObject>
#include <QStringList>
#include <QUrl>
#include <QtQml>
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)

View File

@@ -18,7 +18,8 @@ SOURCES += propertyeditorview.cpp \
gradientpresetcustomlistmodel.cpp \ gradientpresetcustomlistmodel.cpp \
simplecolorpalette.cpp \ simplecolorpalette.cpp \
simplecolorpalettemodel.cpp \ simplecolorpalettemodel.cpp \
simplecolorpalettesingleton.cpp simplecolorpalettesingleton.cpp \
itemfiltermodel.cpp
HEADERS += propertyeditorview.h \ HEADERS += propertyeditorview.h \
qmlanchorbindingproxy.h \ qmlanchorbindingproxy.h \
@@ -38,6 +39,7 @@ HEADERS += propertyeditorview.h \
gradientpresetcustomlistmodel.h \ gradientpresetcustomlistmodel.h \
simplecolorpalette.h \ simplecolorpalette.h \
simplecolorpalettemodel.h \ simplecolorpalettemodel.h \
simplecolorpalettesingleton.h simplecolorpalettesingleton.h \
itemfiltermodel.h
QT += qml quick QT += qml quick

View File

@@ -30,6 +30,7 @@
#include "gradientmodel.h" #include "gradientmodel.h"
#include "gradientpresetdefaultlistmodel.h" #include "gradientpresetdefaultlistmodel.h"
#include "gradientpresetcustomlistmodel.h" #include "gradientpresetcustomlistmodel.h"
#include "itemfiltermodel.h"
#include "simplecolorpalettemodel.h" #include "simplecolorpalettemodel.h"
#include "bindingeditor/bindingeditor.h" #include "bindingeditor/bindingeditor.h"
#include "qmlanchorbindingproxy.h" #include "qmlanchorbindingproxy.h"
@@ -54,6 +55,7 @@ void Quick2PropertyEditorView::registerQmlTypes()
GradientModel::registerDeclarativeType(); GradientModel::registerDeclarativeType();
GradientPresetDefaultListModel::registerDeclarativeType(); GradientPresetDefaultListModel::registerDeclarativeType();
GradientPresetCustomListModel::registerDeclarativeType(); GradientPresetCustomListModel::registerDeclarativeType();
ItemFilterModel::registerDeclarativeType();
SimpleColorPaletteModel::registerDeclarativeType(); SimpleColorPaletteModel::registerDeclarativeType();
Internal::QmlAnchorBindingProxy::registerDeclarativeType(); Internal::QmlAnchorBindingProxy::registerDeclarativeType();
BindingEditor::registerDeclarativeType(); BindingEditor::registerDeclarativeType();

View File

@@ -564,6 +564,8 @@ Project {
"propertyeditor/designerpropertymap.h", "propertyeditor/designerpropertymap.h",
"propertyeditor/fileresourcesmodel.cpp", "propertyeditor/fileresourcesmodel.cpp",
"propertyeditor/fileresourcesmodel.h", "propertyeditor/fileresourcesmodel.h",
"propertyeditor/itemfiltermodel.cpp",
"propertyeditor/itemfiltermodel.h",
"propertyeditor/gradientmodel.cpp", "propertyeditor/gradientmodel.cpp",
"propertyeditor/gradientmodel.h", "propertyeditor/gradientmodel.h",
"propertyeditor/gradientpresetcustomlistmodel.cpp", "propertyeditor/gradientpresetcustomlistmodel.cpp",