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