Debian Codegraph @

src/Gui/propertyeditor/PropertyModel.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24 #include <limits>
25 #include <boost/algorithm/string/predicate.hpp>
26
27 #include <Base/Tools.h>
28
29 #include "PropertyItem.h"
30 #include "PropertyModel.h"
31 #include "PropertyView.h"
32
33
34 using namespace Gui;
35 using namespace Gui::PropertyEditor;
36
37
38 /* TRANSLATOR Gui::PropertyEditor::PropertyModel */
39
40 PropertyModel::PropertyModel(QObject* parent)
41 : QAbstractItemModel(parent)
42 {
43 rootItem = static_cast<PropertyItem*>(PropertyItem::create());
44 }
45
46 PropertyModel::~PropertyModel()
47 {
48 delete rootItem;
49 }
50
51 QModelIndex PropertyModel::buddy(const QModelIndex& index) const
52 {
53 if (index.column() == 1) {
54 return index;
55 }
56 return index.sibling(index.row(), 1);
57 }
58
59 int PropertyModel::columnCount(const QModelIndex& parent) const
60 {
61 // <property, value>, hence always 2
62 if (parent.isValid()) {
63 return static_cast<PropertyItem*>(parent.internalPointer())->columnCount();
64 }
65 else {
66 return rootItem->columnCount();
67 }
68 }
69
70 QVariant PropertyModel::data(const QModelIndex& index, int role) const
71 {
72 if (!index.isValid()) {
73 return {};
74 }
75
76 auto item = static_cast<PropertyItem*>(index.internalPointer());
77 return item->data(index.column(), role);
78 }
79
80 bool PropertyModel::setData(const QModelIndex& index, const QVariant& value, int role)
81 {
82 if (!index.isValid()) {
83 return false;
84 }
85
86 // we check whether the data has really changed, otherwise we ignore it
87 if (role == Qt::EditRole) {
88 auto item = static_cast<PropertyItem*>(index.internalPointer());
89 QVariant data = item->data(index.column(), role);
90 if (data.userType() == QMetaType::Double && value.userType() == QMetaType::Double) {
91 // since we store some properties as floats we get some round-off
92 // errors here. Thus, we use an epsilon here.
93 // NOTE: Since 0.14 PropertyFloat uses double precision, so this is maybe unnecessary
94 // now?
95 double d = data.toDouble();
96 double v = value.toDouble();
97 if (fabs(d - v) > std::numeric_limits<double>::epsilon()) {
98 return item->setData(value);
99 }
100 }
101 // Special case handling for quantities
102 else if (data.canConvert<Base::Quantity>() && value.canConvert<Base::Quantity>()) {
103 const Base::Quantity& val1 = data.value<Base::Quantity>();
104 const Base::Quantity& val2 = value.value<Base::Quantity>();
105 if (!(val1 == val2)) {
106 return item->setData(value);
107 }
108 }
109 else if (data != value) {
110 return item->setData(value);
111 }
112 }
113
114 return true;
115 }
116
117 Qt::ItemFlags PropertyModel::flags(const QModelIndex& index) const
118 {
119 auto item = static_cast<PropertyItem*>(index.internalPointer());
120 return item->flags(index.column());
121 }
122
123 QModelIndex PropertyModel::index(int row, int column, const QModelIndex& parent) const
124 {
125 PropertyItem* parentItem;
126
127 if (!parent.isValid()) {
128 parentItem = rootItem;
129 }
130 else {
131 parentItem = static_cast<PropertyItem*>(parent.internalPointer());
132 }
133
134 PropertyItem* childItem = parentItem->child(row);
135 if (childItem) {
136 return createIndex(row, column, childItem);
137 }
138 else {
139 return {};
140 }
141 }
142
143 QModelIndex PropertyModel::parent(const QModelIndex& index) const
144 {
145 if (!index.isValid()) {
146 return {};
147 }
148
149 auto childItem = static_cast<PropertyItem*>(index.internalPointer());
150 PropertyItem* parentItem = childItem->parent();
151
152 if (parentItem == rootItem) {
153 return {};
154 }
155
156 return createIndex(parentItem->row(), 0, parentItem);
157 }
158
159 int PropertyModel::rowCount(const QModelIndex& parent) const
160 {
161 PropertyItem* parentItem;
162
163 if (!parent.isValid()) {
164 parentItem = rootItem;
165 }
166 else {
167 parentItem = static_cast<PropertyItem*>(parent.internalPointer());
168 }
169
170 return parentItem->childCount();
171 }
172
173 QVariant PropertyModel::headerData(int section, Qt::Orientation orientation, int role) const
174 {
175 if (orientation == Qt::Horizontal) {
176 if (role != Qt::DisplayRole) {
177 return {};
178 }
179 if (section == 0) {
180 return tr("Property");
181 }
182 if (section == 1) {
183 return tr("Value");
184 }
185 }
186
187 return {};
188 }
189
190 bool PropertyModel::setHeaderData(int, Qt::Orientation, const QVariant&, int)
191 {
192 return false;
193 }
194
195 QStringList PropertyModel::propertyPathFromIndex(const QModelIndex& index) const
196 {
197 QStringList path;
198 if (index.isValid()) {
199 auto item = static_cast<PropertyItem*>(index.internalPointer());
200 while (item && item != this->rootItem) {
201 path.push_front(item->propertyName());
202 item = item->parent();
203 }
204 }
205
206 return path;
207 }
208
209 QModelIndex PropertyModel::propertyIndexFromPath(const QStringList& path) const
210 {
211 if (path.size() < 2) {
212 return {};
213 }
214
215 auto it = groupItems.find(path.front());
216 if (it == groupItems.end()) {
217 return {};
218 }
219
220 PropertyItem* item = it->second.groupItem;
221 QModelIndex index = this->index(item->row(), 0, QModelIndex());
222
223 for (int j = 1; j < path.size(); ++j) {
224 bool found = false;
225 for (int i = 0, c = item->childCount(); i < c; ++i) {
226 auto child = item->child(i);
227 if (child->propertyName() == path[j]) {
228 index = this->index(i, 1, index);
229 item = child;
230 found = true;
231 break;
232 }
233 }
234 if (!found) {
235 return j == 1 ? QModelIndex() : index;
236 }
237 }
238 return index;
239 }
240
241 static void setPropertyItemName(PropertyItem* item, const char* propName, QString groupName)
242 {
243 QString name = QString::fromUtf8(propName);
244 QString realName = name;
245 if (name.size() > groupName.size() + 1 && name.startsWith(groupName + QLatin1Char('_'))) {
246 name = name.right(name.size() - groupName.size() - 1);
247 }
248
249 item->setPropertyName(name, realName);
250 }
251
252 static PropertyItem* createPropertyItem(App::Property* prop)
253 {
254 const char* editor = prop->getEditorName();
255 if (!editor || !editor[0]) {
256 if (PropertyView::showAll()) {
257 editor = "Gui::PropertyEditor::PropertyItem";
258 }
259 else {
260 return nullptr;
261 }
262 }
263 auto item = static_cast<PropertyItem*>(PropertyItemFactory::instance().createPropertyItem(editor));
264 if (!item) {
265 qWarning("No property item for type %s found\n", editor);
266 }
267 return item;
268 }
269
270 PropertyModel::GroupInfo& PropertyModel::getGroupInfo(App::Property* prop)
271 {
272 const char* group = prop->getGroup();
273 bool isEmpty = Base::Tools::isNullOrEmpty(group);
274 QString groupName = QString::fromUtf8(isEmpty ? QT_TRANSLATE_NOOP("App::Property", "Base") : group);
275
276 auto res = groupItems.insert(std::make_pair(groupName, GroupInfo()));
277 if (res.second) {
278 auto& groupInfo = res.first->second;
279 groupInfo.groupItem = static_cast<PropertySeparatorItem*>(PropertySeparatorItem::create());
280 groupInfo.groupItem->setReadOnly(true);
281 groupInfo.groupItem->setExpanded(true);
282 groupInfo.groupItem->setParent(rootItem);
283 groupInfo.groupItem->setPropertyName(groupName);
284
285 auto it = res.first;
286 int row = 0;
287 if (it != groupItems.begin()) {
288 --it;
289 row = it->second.groupItem->_row + 1;
290 ++it;
291 }
292 groupInfo.groupItem->_row = row;
293 beginInsertRows(QModelIndex(), row, row);
294 rootItem->insertChild(row, groupInfo.groupItem);
295 // update row index for all group items behind
296 for (++it; it != groupItems.end(); ++it) {
297 ++it->second.groupItem->_row;
298 }
299 endInsertRows();
300 }
301
302 return res.first->second;
303 }
304
305 void PropertyModel::buildUp(const PropertyModel::PropertyList& props)
306 {
307 // If props empty, then simply reset all property items, but keep the group
308 // items.
309 if (props.empty()) {
310 resetGroups();
311 return;
312 }
313
314 // First step, init group info
315 initGroups();
316
317 // Second step, either find existing items or create new items for the given
318 // properties. There is no signaling of model change at this stage. The
319 // change information is kept pending in GroupInfo::children
320 findOrCreateChildren(props);
321
322 // Third step, signal item insertion and movement.
323 insertOrMoveChildren();
324
325
326 // Final step, signal item removal. This is separated from the above because
327 // of the possibility of moving items between groups.
328 removeChildren();
329 }
330
331 void PropertyModel::resetGroups()
332 {
333 beginResetModel();
334 for (auto& v : groupItems) {
335 auto& groupInfo = v.second;
336 groupInfo.groupItem->reset();
337 groupInfo.children.clear();
338 }
339 itemMap.clear();
340 endResetModel();
341 }
342
343 void PropertyModel::initGroups()
344 {
345 for (auto& v : groupItems) {
346 auto& groupInfo = v.second;
347 groupInfo.children.clear();
348 }
349 }
350
351 void PropertyModel::findOrCreateChildren(const PropertyModel::PropertyList& props)
352 {
353 for (const auto& jt : props) {
354 App::Property* prop = jt.second.front();
355
356 PropertyItem* item = nullptr;
357 for (auto prop : jt.second) {
358 auto it = itemMap.find(prop);
359 if (it == itemMap.end() || !it->second) {
360 continue;
361 }
362 item = it->second;
363 break;
364 }
365
366 if (!item) {
367 item = createPropertyItem(prop);
368 if (!item) {
369 continue;
370 }
371 }
372
373 GroupInfo& groupInfo = getGroupInfo(prop);
374 groupInfo.children.push_back(item);
375
376 item->setLinked(boost::ends_with(jt.first, "*"));
377 setPropertyItemName(item, prop->getName(), groupInfo.groupItem->propertyName());
378
379 if (jt.second != item->getPropertyData()) {
380 for (auto prop : item->getPropertyData()) {
381 itemMap.erase(prop);
382 }
383 for (auto prop : jt.second) {
384 itemMap[prop] = item;
385 }
386 // TODO: is it necessary to make sure the item has no pending commit?
387 item->setPropertyData(jt.second);
388 }
389 else {
390 item->updateData();
391 }
392 }
393 }
394
395 void PropertyModel::insertOrMoveChildren()
396 {
397 for (auto& v : groupItems) {
398 auto& groupInfo = v.second;
399 int beginChange = -1;
400 int endChange = 0;
401 int beginInsert = -1;
402 int endInsert = 0;
403 int row = -1;
404 QModelIndex midx = this->index(groupInfo.groupItem->_row, 0, QModelIndex());
405
406 auto flushInserts = [&]() {
407 if (beginInsert < 0) {
408 return;
409 }
410 beginInsertRows(midx, beginInsert, endInsert);
411 for (int i = beginInsert; i <= endInsert; ++i) {
412 groupInfo.groupItem->insertChild(i, groupInfo.children[i]);
413 }
414 endInsertRows();
415 beginInsert = -1;
416 };
417
418 auto flushChanges = [&]() {
419 if (beginChange < 0) {
420 return;
421 }
422 (void)endChange;
423 // There is no need to signal dataChange(), because PropertyEditor
424 // will call PropertyModel::updateProperty() on any property
425 // changes.
426 //
427 // dataChanged(this->index(beginChange,0,midx), this->index(endChange,1,midx));
428 beginChange = -1;
429 };
430
431 for (auto item : groupInfo.children) {
432 ++row;
433 if (!item->parent()) {
434 flushChanges();
435 item->setParent(groupInfo.groupItem);
436 if (beginInsert < 0) {
437 beginInsert = row;
438 }
439 endInsert = row;
440 }
441 else {
442 flushInserts();
443 int oldRow = item->row();
444 // Dynamic property can rename group, so must check
445 auto groupItem = item->parent();
446 assert(groupItem);
447 if (oldRow == row && groupItem == groupInfo.groupItem) {
448 if (beginChange < 0) {
449 beginChange = row;
450 }
451 endChange = row;
452 }
453 else {
454 flushChanges();
455 beginMoveRows(createIndex(groupItem->row(), 0, groupItem), oldRow, oldRow, midx, row);
456 if (groupItem == groupInfo.groupItem) {
457 groupInfo.groupItem->moveChild(oldRow, row);
458 }
459 else {
460 groupItem->takeChild(oldRow);
461 item->setParent(groupInfo.groupItem);
462 groupInfo.groupItem->insertChild(row, item);
463 }
464 endMoveRows();
465 }
466 }
467 }
468
469 flushChanges();
470 flushInserts();
471 }
472 }
473
474 void PropertyModel::removeChildren()
475 {
476 for (auto& v : groupItems) {
477 auto& groupInfo = v.second;
478 int first, last;
479 getRange(groupInfo, first, last);
480 if (last > first) {
481 QModelIndex midx = this->index(groupInfo.groupItem->_row, 0, QModelIndex());
482 // This can trigger a recursive call of PropertyView::onTimer()
483 beginRemoveRows(midx, first, last - 1);
484 groupInfo.groupItem->removeChildren(first, last - 1);
485 endRemoveRows();
486 }
487 else {
488 assert(last == first);
489 }
490 }
491 }
492
493 void PropertyModel::getRange(const PropertyModel::GroupInfo& groupInfo, int& first, int& last) const
494 {
495 first = static_cast<int>(groupInfo.children.size());
496 last = groupInfo.groupItem->childCount();
497 }
498
499 void PropertyModel::updateProperty(const App::Property& prop)
500 {
501 auto it = itemMap.find(const_cast<App::Property*>(&prop));
502 if (it == itemMap.end() || !it->second || !it->second->parent()) {
503 return;
504 }
505
506 int column = 1;
507 PropertyItem* item = it->second;
508 item->updateData();
509 QModelIndex parent = this->index(item->parent()->row(), 0, QModelIndex());
510 item->assignProperty(&prop);
511 QModelIndex data = this->index(item->row(), column, parent);
512 Q_EMIT dataChanged(data, data);
513 updateChildren(item, column, data);
514 }
515
516 void PropertyModel::appendProperty(const App::Property& _prop)
517 {
518 auto prop = const_cast<App::Property*>(&_prop);
519 if (!prop->getName()) {
520 return;
521 }
522 auto it = itemMap.find(prop);
523 if (it == itemMap.end() || !it->second) {
524 return;
525 }
526
527 PropertyItem* item = createPropertyItem(prop);
528 GroupInfo& groupInfo = getGroupInfo(prop);
529
530 int row = 0;
531 for (int c = groupInfo.groupItem->childCount(); row < c; ++row) {
532 PropertyItem* child = groupInfo.groupItem->child(row);
533 App::Property* firstProp = item->getFirstProperty();
534 if (firstProp && firstProp->testStatus(App::Property::PropDynamic)
535 && item->propertyName() >= child->propertyName()) {
536 break;
537 }
538 }
539
540 QModelIndex midx = this->index(groupInfo.groupItem->_row, 0, QModelIndex());
541 beginInsertRows(midx, row, row);
542 groupInfo.groupItem->insertChild(row, item);
543 setPropertyItemName(item, prop->getName(), groupInfo.groupItem->propertyName());
544 item->setPropertyData({prop});
545 endInsertRows();
546 }
547
548 void PropertyModel::removeProperty(const App::Property& _prop)
549 {
550 auto prop = const_cast<App::Property*>(&_prop);
551 auto it = itemMap.find(prop);
552 if (it == itemMap.end() || !it->second) {
553 return;
554 }
555
556 PropertyItem* item = it->second;
557 if (item->removeProperty(prop)) {
558 PropertyItem* parent = item->parent();
559 int row = item->row();
560 beginRemoveRows(this->index(parent->row(), 0, QModelIndex()), row, row);
561 parent->removeChildren(row, row);
562 endRemoveRows();
563 }
564 }
565
566 void PropertyModel::renameProperty(const App::Property& _prop)
567 {
568 auto prop = const_cast<App::Property*>(&_prop);
569 auto it = itemMap.find(prop);
570 if (it == itemMap.end() || !it->second) {
571 return;
572 }
573
574 PropertyItem* item = it->second;
575 item->renameProperty(prop);
576 QModelIndex parent = this->index(item->parent()->row(), 0, QModelIndex());
577 QModelIndex nameIndex = this->index(item->row(), 0, parent);
578 QModelIndex dataIndex = this->index(item->row(), 1, parent);
579 QVector<int> roles;
580 roles << Qt::DisplayRole;
581 Q_EMIT dataChanged(nameIndex, dataIndex, roles);
582 }
583
584 void PropertyModel::updateChildren(PropertyItem* item, int column, const QModelIndex& parent)
585 {
586 int numChild = item->childCount();
587 if (numChild > 0) {
588 QModelIndex topLeft = this->index(0, column, parent);
589 QModelIndex bottomRight = this->index(numChild - 1, column, parent);
590 Q_EMIT dataChanged(topLeft, bottomRight);
591 }
592 }
593
594 bool PropertyModel::removeRows(int row, int count, const QModelIndex& parent)
595 {
596 PropertyItem* item;
597 if (!parent.isValid()) {
598 item = rootItem;
599 }
600 else {
601 item = static_cast<PropertyItem*>(parent.internalPointer());
602 }
603
604 int start = row;
605 int end = row + count - 1;
606 beginRemoveRows(parent, start, end);
607 item->removeChildren(start, end);
608 endRemoveRows();
609 return true;
610 }
611
612 #include "moc_PropertyModel.cpp"