KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
quickrecursiveinstantiator.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Kevin Funk <kevin.funk@kdab.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8
9 Licensees holding valid commercial KDAB State Machine Editor Library
10 licenses may use this file in accordance with the KDAB State Machine Editor
11 Library License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14*/
15
17
18#include "objecttreemodel.h"
19
20#include <QAbstractItemModel>
21#include "debug.h"
22#include <QQmlComponent>
23#include <QQmlContext>
24#include <QQmlEngine>
25#include <QQuickItem>
26
27using namespace KDSME;
28
30 : QQuickItem(parent)
31 , m_model(nullptr)
32 , m_delegate(nullptr)
33{
34}
35
37{
38 return m_rootItems;
39}
40
41QAbstractItemModel *QuickRecursiveInstantiator::model() const
42{
43 return m_model;
44}
45
46void QuickRecursiveInstantiator::setModel(QAbstractItemModel *model)
47{
48 if (m_model == model) {
49 return;
50 }
51 if (m_model) {
52 disconnect(m_model, &QAbstractItemModel::modelReset, this, &QuickRecursiveInstantiator::modelReset);
53 disconnect(m_model, &QAbstractItemModel::rowsInserted, this, &QuickRecursiveInstantiator::rowsInserted);
54 disconnect(m_model, &QAbstractItemModel::rowsRemoved, this, &QuickRecursiveInstantiator::rowsAboutToBeRemoved);
55 disconnect(m_model, &QAbstractItemModel::destroyed, this, &QuickRecursiveInstantiator::modelDestroyed);
56 }
57 m_model = model;
58 if (m_model) {
59 connect(m_model, &QAbstractItemModel::modelReset, this, &QuickRecursiveInstantiator::modelReset);
60 connect(m_model, &QAbstractItemModel::rowsInserted, this, &QuickRecursiveInstantiator::rowsInserted);
61 connect(m_model, &QAbstractItemModel::rowsAboutToBeRemoved, this, &QuickRecursiveInstantiator::rowsAboutToBeRemoved);
62 connect(m_model, &QAbstractItemModel::destroyed, this, &QuickRecursiveInstantiator::modelDestroyed);
63 }
64 Q_EMIT modelChanged(m_model);
65}
66
68{
69 return m_delegate;
70}
71
72void QuickRecursiveInstantiator::setDelegate(QQmlComponent *delegate)
73{
74 if (m_delegate == delegate) {
75 return;
76 }
77
78 m_delegate = delegate;
79 Q_EMIT delegateChanged(m_delegate);
80}
81
82QObject *QuickRecursiveInstantiator::itemForIndex(const QModelIndex &index) const
83{
84 return m_createdItems.value(index);
85}
86
87void QuickRecursiveInstantiator::modelReset()
88{
89 Q_ASSERT(m_model);
90
91 std::for_each(m_rootItems.begin(), m_rootItems.end(), [](QObject *obj) { obj->deleteLater(); });
92 m_rootItems.clear();
93
94 for (int i = 0; i < m_model->rowCount(); ++i) {
95 auto rootIndex = m_model->index(0, 0);
96 m_rootItems << createItems(rootIndex, this);
97 }
98}
99
100void QuickRecursiveInstantiator::modelDestroyed()
101{
102 // qDeleteAll(m_createdItems.values());
103 m_createdItems.clear();
104 qDeleteAll(m_rootItems);
105 m_rootItems.clear();
106
107 m_model = nullptr;
108}
109
110QObject *QuickRecursiveInstantiator::createItems(const QModelIndex &index, QObject *parent)
111{
112 Q_ASSERT(m_delegate);
113 Q_ASSERT(m_model);
114 Q_ASSERT(index.isValid());
115
116 auto object = index.data(ObjectTreeModel::ObjectRole).value<QObject *>();
117 Q_ASSERT(object);
118 auto creationContext = m_delegate->creationContext();
119 auto context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
120 context->setContextProperty(QStringLiteral("object"), object);
121
122 auto createdObject = m_delegate->create(context);
123 createdObject->setParent(parent);
124 context->setParent(createdObject);
125 if (auto quickItem = qobject_cast<QQuickItem *>(createdObject)) {
126 if (auto quickParentItem = qobject_cast<QQuickItem *>(parent)) {
127 quickItem->setParentItem(quickParentItem);
128 } else {
129 quickItem->setParentItem(this);
130 }
131 }
132
133 // register
134 m_createdItems[QPersistentModelIndex(index)] = createdObject;
135
136 // create items for child indices recursively
137 for (int i = 0; i < m_model->rowCount(index); ++i) {
138 auto childIndex = m_model->index(i, 0, index);
139 createItems(childIndex, createdObject);
140 }
141
142 return createdObject;
143}
144
145void QuickRecursiveInstantiator::removeItems(const QModelIndex &index, QObject *parent)
146{
147 Q_UNUSED(parent);
148 Q_ASSERT(m_createdItems.contains(index));
149 auto createdObject = m_createdItems.take(index);
150 Q_ASSERT(createdObject);
151 qDebug() << createdObject << index;
152
153 createdObject->deleteLater();
154}
155
156void QuickRecursiveInstantiator::rowsInserted(const QModelIndex &parent, int first, int last)
157{
158 auto parentItem = itemForIndex(parent);
159 Q_ASSERT(parentItem);
160 for (int i = first; i <= last; ++i) {
161 const auto currentIndex = m_model->index(i, 0, parent);
162 createItems(currentIndex, parentItem);
163 }
164}
165
166void QuickRecursiveInstantiator::rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
167{
168 auto parentItem = itemForIndex(parent);
169 Q_ASSERT(parentItem);
170 for (int i = first; i <= last; ++i) {
171 const auto currentIndex = m_model->index(i, 0, parent);
172 removeItems(currentIndex, parentItem);
173 }
174}
@ ObjectRole
return QObject*
virtual void setModel(QAbstractItemModel *model)
void modelChanged(QAbstractItemModel *model)
QuickRecursiveInstantiator(QQuickItem *parent=nullptr)
QObject * itemForIndex(const QModelIndex &index) const override
void setDelegate(QQmlComponent *delegate)
void delegateChanged(QQmlComponent *delegate)
QList< QObject * > rootItems() const

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8