Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_model_index_converter.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
12#include "kis_node_model.h"
13#include "kis_node_manager.h"
16
17
19 KisNodeModel *model,
20 bool showGlobalSelection)
21 : m_dummiesFacade(dummiesFacade),
22 m_model(model),
23 m_showGlobalSelection(showGlobalSelection)
24{
25}
26
31
33{
34 const QString selectionMaskType = KisSelectionMask::staticMetaObject.className();
35 const QString referencesLayerType = KisReferenceImagesLayer::staticMetaObject.className();
36 const QString decorationsLayerType = KisDecorationsWrapperLayer::staticMetaObject.className();
37
38 return (type != selectionMaskType || m_showGlobalSelection) &&
39 type != referencesLayerType &&
40 type != decorationsLayerType;
41}
42
44{
45
46 KisNodeDummy *parentDummy = parent.isValid() ?
48
49 if(!parentDummy) return 0;
50
51 KisNodeDummy *resultDummy = 0;
52
53 // a child of the root node
54 if(!parentDummy->parent()) {
55 KisNodeDummy *currentDummy = parentDummy->lastChild();
56 while(currentDummy) {
57 if(checkDummyType(currentDummy)) {
58 if(!row) {
59 resultDummy = currentDummy;
60 break;
61 }
62 row--;
63 }
64 currentDummy = currentDummy->prevSibling();
65 }
66 }
67 // a child of other layer
68 else {
69 int rowCount = parentDummy->childCount();
70 int index = rowCount - row - 1;
71 resultDummy = parentDummy->at(index);
72 }
73
74
75 return resultDummy;
76}
77
79{
80 Q_ASSERT(index.isValid());
81 Q_ASSERT(index.internalPointer());
82 return static_cast<KisNodeDummy*>(index.internalPointer());
83}
84
86{
87 Q_ASSERT(dummy);
88 KisNodeDummy *parentDummy = dummy->parent();
89
90 // a root node
91 if(!parentDummy) return QModelIndex();
92
93 int row = 0;
94
95 // a child of the root node
96 if(!parentDummy->parent()) {
97 if(!checkDummyType(dummy)) return QModelIndex();
98
99 KisNodeDummy *currentDummy = parentDummy->lastChild();
100 while(currentDummy && currentDummy != dummy) {
101 if(checkDummyType(currentDummy)) {
102 row++;
103 }
104 currentDummy = currentDummy->prevSibling();
105 }
106 }
107 // a child of other layer
108 else {
109 int rowCount = parentDummy->childCount();
110 int index = parentDummy->indexOf(dummy);
111 row = rowCount - index - 1;
112 }
113
114 return m_model->createIndex(row, 0, (void*)dummy);
115}
116
118 int index,
119 const QString &newNodeMetaObjectType,
120 QModelIndex &parentIndex,
121 int &row)
122{
123 // adding a root node
124 if(!parentDummy) {
125 Q_ASSERT(!index);
126 return false;
127 }
128
129 // adding a child of the root node
130 if(!parentDummy->parent()) {
131 if(!checkDummyMetaObjectType(newNodeMetaObjectType)) {
132 return false;
133 }
134
135 row = 0;
136
137 parentIndex = QModelIndex();
138 KisNodeDummy *dummy = parentDummy->lastChild();
139 int toScan = parentDummy->childCount() - index;
140 while(dummy && toScan > 0) {
141 if(checkDummyType(dummy)) {
142 row++;
143 }
144 dummy = dummy->prevSibling();
145 toScan--;
146 }
147 }
148 // everything else
149 else {
150 parentIndex = indexFromDummy(parentDummy);
151 int rowCount = parentDummy->childCount();
152 row = rowCount - index;
153 }
154
155 return true;
156}
157
158int KisModelIndexConverter::rowCount(QModelIndex parent)
159{
160 KisNodeDummy *dummy = parent.isValid() ?
162
163 // a root node (hidden)
164 if(!dummy) return 0;
165
166 int numChildren = 0;
167
168 // children of the root node
169 if(!dummy->parent()) {
170 KisNodeDummy *currentDummy = dummy->lastChild();
171 while(currentDummy) {
172 if(checkDummyType(currentDummy)) {
173 numChildren++;
174 }
175
176 currentDummy = currentDummy->prevSibling();
177 }
178 }
179 // children of other nodes
180 else {
181 numChildren = dummy->childCount();
182 }
183
184 return numChildren;
185}
virtual KisNodeDummy * rootDummy() const =0
KisDummiesFacadeBase * m_dummiesFacade
bool indexFromAddedDummy(KisNodeDummy *parentDummy, int index, const QString &newNodeMetaObjectType, QModelIndex &parentIndex, int &row) override
KisNodeDummy * dummyFromIndex(QModelIndex index) override
bool checkDummyType(KisNodeDummy *dummy)
int rowCount(QModelIndex parent) override
KisModelIndexConverter(KisDummiesFacadeBase *dummiesFacade, KisNodeModel *model, bool showGlobalSelection)
KisNodeDummy * dummyFromRow(int row, QModelIndex parent) override
bool checkDummyMetaObjectType(const QString &type)
QModelIndex indexFromDummy(KisNodeDummy *dummy) override
KisNodeDummy * at(int index) const
bool isGUIVisible(bool showGlobalSelection) const
KisNodeDummy * lastChild() const
KisNodeDummy * parent() const
int indexOf(KisNodeDummy *child) const
KisNodeDummy * prevSibling() const