Debian Codegraph @

utils/fgqcanvas/canvastreemodel.cpp

raw symbols SCIP
1 //
2 // Copyright (C) 2017 James Turner [email protected]
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 #include "canvastreemodel.h"
19
20 #include <QDebug>
21
22 #include "localprop.h"
23
24 CanvasTreeModel::CanvasTreeModel(FGCanvasGroup* root) :
25 _root(root)
26 {
27 connect(_root, &FGCanvasGroup::childAdded, this, &CanvasTreeModel::onGroupChildAdded);
28 }
29
30 FGCanvasElement* CanvasTreeModel::elementFromIndex(const QModelIndex &index) const
31 {
32 if (!index.isValid()) {
33 return nullptr;
34 }
35
36 FGCanvasElement* e = static_cast<FGCanvasElement*>(index.internalPointer());
37 return e;
38 }
39
40 int CanvasTreeModel::rowCount(const QModelIndex &parent) const
41 {
42 FGCanvasElement* e = static_cast<FGCanvasElement*>(parent.internalPointer());
43 if (!e) {
44 return _root->childCount();
45 }
46
47 FGCanvasGroup* group = qobject_cast<FGCanvasGroup*>(e);
48 if (group) {
49 return group->childCount();
50 }
51
52 return 0;
53 }
54
55 int CanvasTreeModel::columnCount(const QModelIndex &parent) const
56 {
57 return 1;
58 }
59
60 QVariant CanvasTreeModel::data(const QModelIndex &index, int role) const
61 {
62 FGCanvasElement* e = static_cast<FGCanvasElement*>(index.internalPointer());
63 if (!e) {
64 return QVariant();
65 }
66
67 switch (role) {
68 case Qt::DisplayRole:
69 return e->property()->value("id", QVariant("<noid>"));
70
71 case Qt::CheckStateRole:
72 return e->property()->value("visible", true).toBool() ? Qt::Checked : Qt::Unchecked;
73
74 default:
75 break;
76 }
77
78 return QVariant();
79 }
80
81 bool CanvasTreeModel::hasChildren(const QModelIndex &parent) const
82 {
83 FGCanvasElement* e;
84 if (parent.isValid()) {
85 e = static_cast<FGCanvasElement*>(parent.internalPointer());
86 } else {
87 e = _root;
88 }
89
90 FGCanvasGroup* group = qobject_cast<FGCanvasGroup*>(e);
91 if (group) {
92 return group->hasChilden();
93 }
94
95 return false;
96 }
97
98 QModelIndex CanvasTreeModel::index(int row, int column, const QModelIndex &parent) const
99 {
100 FGCanvasGroup* group;
101 if (parent.isValid()) {
102 group = qobject_cast<FGCanvasGroup*>(static_cast<FGCanvasElement*>(parent.internalPointer()));
103 } else {
104 group = _root;
105 }
106
107 if (!group) {
108 return QModelIndex(); // invalid
109 }
110
111 if ((row < 0) || (row >= (int) group->childCount())) {
112 return QModelIndex(); // invalid
113 }
114
115 return createIndex(row, column, group->childAt(row));
116 }
117
118 QModelIndex CanvasTreeModel::parent(const QModelIndex &child) const
119 {
120 FGCanvasElement* e = static_cast<FGCanvasElement*>(child.internalPointer());
121 if (!child.isValid() || !e) {
122 return QModelIndex();
123 }
124
125 return indexForGroup(const_cast<FGCanvasGroup*>(e->parentGroup()));
126 }
127
128 Qt::ItemFlags CanvasTreeModel::flags(const QModelIndex &index) const
129 {
130 return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
131 }
132
133 bool CanvasTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
134 {
135 FGCanvasElement* e = static_cast<FGCanvasElement*>(index.internalPointer());
136 if (!e) {
137 return false;
138 }
139
140 qDebug() << Q_FUNC_INFO;
141 if (role == Qt::CheckStateRole) {
142 e->property()->changeValue("visible", (value.toInt() == Qt::Checked));
143 emit dataChanged(index, index, QVector<int>() << Qt::CheckStateRole);
144 return true;
145 }
146
147 return false;
148 }
149
150 QModelIndex CanvasTreeModel::indexForGroup(FGCanvasGroup* group) const
151 {
152 if (!group) {
153 return QModelIndex();
154 }
155
156 if (group->parentGroup()) {
157 int prIndex = group->parentGroup()->indexOfChild(group);
158 return createIndex(prIndex, 0, group);
159 } else {
160 return createIndex(0, 0, group);
161 }
162 }
163
164 void CanvasTreeModel::onGroupChildAdded()
165 {
166 FGCanvasGroup* group = qobject_cast<FGCanvasGroup*>(sender());
167 int newChild = group->childCount() - 1;
168
169 FGCanvasGroup* childGroup = qobject_cast<FGCanvasGroup*>(group->childAt(newChild));
170 if (childGroup) {
171 connect(childGroup, &FGCanvasGroup::childAdded, this, &CanvasTreeModel::onGroupChildAdded);
172 }
173
174 beginInsertRows(indexForGroup(group),
175 newChild, newChild);
176 endInsertRows();
177 }
178
179 void CanvasTreeModel::onGroupChildRemoved(int index)
180 {
181 FGCanvasGroup* group = qobject_cast<FGCanvasGroup*>(sender());
182 beginRemoveRows(indexForGroup(group), index, index);
183 }