| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <QApplication> |
| 24 |
#include <QFont> |
| 25 |
|
| 26 |
#include <App/Document.h> |
| 27 |
#include <App/DocumentObject.h> |
| 28 |
#include <App/PropertyLinks.h> |
| 29 |
|
| 30 |
#include "DocumentModel.h" |
| 31 |
#include "Application.h" |
| 32 |
#include "BitmapFactory.h" |
| 33 |
#include "Document.h" |
| 34 |
#include "ViewProviderDocumentObject.h" |
| 35 |
|
| 36 |
|
| 37 |
using namespace Gui; |
| 38 |
namespace sp = std::placeholders; |
| 39 |
|
| 40 |
namespace Gui |
| 41 |
{ |
| 42 |
// forward declaration |
| 43 |
class ViewProviderIndex; |
| 44 |
|
| 45 |
// Base class |
| 46 |
class DocumentModelIndex: public Base::BaseClass |
| 47 |
{ |
| 48 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 49 |
|
| 50 |
public: |
| 51 |
~DocumentModelIndex() override |
| 52 |
{ |
| 53 |
qDeleteAll(childItems); |
| 54 |
} |
| 55 |
|
| 56 |
void setParent(DocumentModelIndex* parent) |
| 57 |
{ |
| 58 |
parentItem = parent; |
| 59 |
} |
| 60 |
DocumentModelIndex* parent() const |
| 61 |
{ |
| 62 |
return parentItem; |
| 63 |
} |
| 64 |
void appendChild(DocumentModelIndex* child) |
| 65 |
{ |
| 66 |
childItems.append(child); |
| 67 |
child->setParent(this); |
| 68 |
} |
| 69 |
void removeChild(int row) |
| 70 |
{ |
| 71 |
childItems.removeAt(row); |
| 72 |
} |
| 73 |
QList<DocumentModelIndex*> removeAll() |
| 74 |
{ |
| 75 |
QList<DocumentModelIndex*> list = childItems; |
| 76 |
childItems.clear(); |
| 77 |
return list; |
| 78 |
} |
| 79 |
DocumentModelIndex* child(int row) |
| 80 |
{ |
| 81 |
return childItems.value(row); |
| 82 |
} |
| 83 |
int row() const |
| 84 |
{ |
| 85 |
if (parentItem) { |
| 86 |
return parentItem->childItems.indexOf(const_cast<DocumentModelIndex*>(this)); |
| 87 |
} |
| 88 |
return 0; |
| 89 |
} |
| 90 |
int childCount() const |
| 91 |
{ |
| 92 |
return childItems.count(); |
| 93 |
} |
| 94 |
virtual QVariant data(int role) const |
| 95 |
{ |
| 96 |
Q_UNUSED(role); |
| 97 |
return {}; |
| 98 |
} |
| 99 |
virtual bool setData(const QVariant& value, int role) |
| 100 |
{ |
| 101 |
Q_UNUSED(value); |
| 102 |
if (role == Qt::EditRole) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
return true; |
| 107 |
} |
| 108 |
virtual Qt::ItemFlags flags() const |
| 109 |
{ |
| 110 |
return Qt::ItemIsSelectable | Qt::ItemIsEnabled; |
| 111 |
} |
| 112 |
|
| 113 |
protected: |
| 114 |
void reset() |
| 115 |
{ |
| 116 |
qDeleteAll(childItems); |
| 117 |
childItems.clear(); |
| 118 |
} |
| 119 |
|
| 120 |
protected: |
| 121 |
DocumentModelIndex() = default; |
| 122 |
DocumentModelIndex* parentItem {nullptr}; |
| 123 |
QList<DocumentModelIndex*> childItems; |
| 124 |
}; |
| 125 |
|
| 126 |
// ------------------------------------------------------------------------ |
| 127 |
|
| 128 |
// Root node |
| 129 |
class ApplicationIndex: public DocumentModelIndex |
| 130 |
{ |
| 131 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 132 |
|
| 133 |
public: |
| 134 |
ApplicationIndex() = default; |
| 135 |
int findChild(const Gui::Document& d) const; |
| 136 |
Qt::ItemFlags flags() const override; |
| 137 |
QVariant data(int role) const override; |
| 138 |
}; |
| 139 |
|
| 140 |
// ------------------------------------------------------------------------ |
| 141 |
|
| 142 |
// Document nodes |
| 143 |
class DocumentIndex: public DocumentModelIndex |
| 144 |
{ |
| 145 |
friend class ViewProviderIndex; |
| 146 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 147 |
static QIcon* documentIcon; |
| 148 |
using IndexSet = boost::unordered_set<ViewProviderIndex*>; |
| 149 |
std::map<const ViewProviderDocumentObject*, IndexSet> vp_nodes; |
| 150 |
void addToDocument(ViewProviderIndex*); |
| 151 |
void removeFromDocument(ViewProviderIndex*); |
| 152 |
|
| 153 |
public: |
| 154 |
const Gui::Document& d; |
| 155 |
explicit DocumentIndex(const Gui::Document& d) |
| 156 |
: d(d) |
| 157 |
{ |
| 158 |
if (!documentIcon) { |
| 159 |
documentIcon = new QIcon(Gui::BitmapFactory().pixmap("Document")); |
| 160 |
} |
| 161 |
} |
| 162 |
~DocumentIndex() override |
| 163 |
{ |
| 164 |
qDeleteAll(childItems); |
| 165 |
childItems.clear(); |
| 166 |
} |
| 167 |
ViewProviderIndex* cloneViewProvider(const ViewProviderDocumentObject&) const; |
| 168 |
int rowOfViewProvider(const ViewProviderDocumentObject&) const; |
| 169 |
void findViewProviders(const ViewProviderDocumentObject&, QList<ViewProviderIndex*>&) const; |
| 170 |
QVariant data(int role) const override; |
| 171 |
}; |
| 172 |
|
| 173 |
// ------------------------------------------------------------------------ |
| 174 |
|
| 175 |
// Object nodes |
| 176 |
class ViewProviderIndex: public DocumentModelIndex |
| 177 |
{ |
| 178 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 179 |
|
| 180 |
public: |
| 181 |
const Gui::ViewProviderDocumentObject& v; |
| 182 |
ViewProviderIndex(const Gui::ViewProviderDocumentObject& v, DocumentIndex* d); |
| 183 |
~ViewProviderIndex() override; |
| 184 |
ViewProviderIndex* clone() const; |
| 185 |
void findViewProviders(const ViewProviderDocumentObject&, QList<ViewProviderIndex*>&) const; |
| 186 |
QVariant data(int role) const override; |
| 187 |
|
| 188 |
private: |
| 189 |
DocumentIndex* d; |
| 190 |
}; |
| 191 |
|
| 192 |
// ------------------------------------------------------------------------ |
| 193 |
|
| 194 |
int ApplicationIndex::findChild(const Gui::Document& d) const |
| 195 |
{ |
| 196 |
int child = 0; |
| 197 |
QList<DocumentModelIndex*>::const_iterator it; |
| 198 |
for (it = childItems.begin(); it != childItems.end(); ++it, ++child) { |
| 199 |
auto doc = static_cast<DocumentIndex*>(*it); |
| 200 |
if (&doc->d == &d) { |
| 201 |
return child; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
Qt::ItemFlags ApplicationIndex::flags() const |
| 209 |
{ |
| 210 |
return Qt::ItemIsEnabled; |
| 211 |
} |
| 212 |
|
| 213 |
QVariant ApplicationIndex::data(int role) const |
| 214 |
{ |
| 215 |
if (role == Qt::DecorationRole) { |
| 216 |
return qApp->windowIcon(); |
| 217 |
} |
| 218 |
else if (role == Qt::DisplayRole) { |
| 219 |
return DocumentModel::tr("Application"); |
| 220 |
} |
| 221 |
return {}; |
| 222 |
} |
| 223 |
|
| 224 |
// ------------------------------------------------------------------------ |
| 225 |
|
| 226 |
QIcon* DocumentIndex::documentIcon = nullptr; |
| 227 |
|
| 228 |
void DocumentIndex::addToDocument(ViewProviderIndex* vp) |
| 229 |
{ |
| 230 |
vp_nodes[&vp->v].insert(vp); |
| 231 |
} |
| 232 |
|
| 233 |
void DocumentIndex::removeFromDocument(ViewProviderIndex* vp) |
| 234 |
{ |
| 235 |
vp_nodes[&vp->v].erase(vp); |
| 236 |
} |
| 237 |
|
| 238 |
ViewProviderIndex* DocumentIndex::cloneViewProvider(const ViewProviderDocumentObject& vp) const |
| 239 |
{ |
| 240 |
std::map<const ViewProviderDocumentObject*, boost::unordered_set<ViewProviderIndex*>>::const_iterator it; |
| 241 |
it = vp_nodes.find(&vp); |
| 242 |
if (it != vp_nodes.end()) { |
| 243 |
boost::unordered_set<ViewProviderIndex*>::const_iterator v; |
| 244 |
if (!it->second.empty()) { |
| 245 |
v = it->second.begin(); |
| 246 |
if (*v) { |
| 247 |
return (*v)->clone(); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
return new ViewProviderIndex(vp, const_cast<DocumentIndex*>(this)); |
| 252 |
} |
| 253 |
|
| 254 |
void DocumentIndex::findViewProviders( |
| 255 |
const ViewProviderDocumentObject& vp, |
| 256 |
QList<ViewProviderIndex*>& index |
| 257 |
) const |
| 258 |
{ |
| 259 |
QList<DocumentModelIndex*>::const_iterator it; |
| 260 |
for (it = childItems.begin(); it != childItems.end(); ++it) { |
| 261 |
auto v = static_cast<ViewProviderIndex*>(*it); |
| 262 |
v->findViewProviders(vp, index); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
int DocumentIndex::rowOfViewProvider(const ViewProviderDocumentObject& vp) const |
| 267 |
{ |
| 268 |
QList<DocumentModelIndex*>::const_iterator it; |
| 269 |
int index = 0; |
| 270 |
for (it = childItems.begin(); it != childItems.end(); ++it, ++index) { |
| 271 |
auto v = static_cast<ViewProviderIndex*>(*it); |
| 272 |
if (&v->v == &vp) { |
| 273 |
return index; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
QVariant DocumentIndex::data(int role) const |
| 281 |
{ |
| 282 |
if (role == Qt::DecorationRole) { |
| 283 |
return *documentIcon; |
| 284 |
} |
| 285 |
else if (role == Qt::DisplayRole) { |
| 286 |
App::Document* doc = d.getDocument(); |
| 287 |
return QString::fromUtf8(doc->Label.getValue()); |
| 288 |
} |
| 289 |
else if (role == Qt::FontRole) { |
| 290 |
Document* doc = Application::Instance->activeDocument(); |
| 291 |
QFont font; |
| 292 |
font.setBold(doc == &d); |
| 293 |
return static_cast<QVariant>(font); |
| 294 |
} |
| 295 |
|
| 296 |
return {}; |
| 297 |
} |
| 298 |
|
| 299 |
// ------------------------------------------------------------------------ |
| 300 |
|
| 301 |
ViewProviderIndex::ViewProviderIndex(const Gui::ViewProviderDocumentObject& v, DocumentIndex* d) |
| 302 |
: v(v) |
| 303 |
, d(d) |
| 304 |
{ |
| 305 |
if (d) { |
| 306 |
d->addToDocument(this); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
ViewProviderIndex::~ViewProviderIndex() |
| 311 |
{ |
| 312 |
if (d) { |
| 313 |
d->removeFromDocument(this); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
ViewProviderIndex* ViewProviderIndex::clone() const |
| 318 |
{ |
| 319 |
auto copy = new ViewProviderIndex(this->v, this->d); |
| 320 |
for (const auto& childItem : childItems) { |
| 321 |
ViewProviderIndex* c = static_cast<ViewProviderIndex*>(childItem)->clone(); |
| 322 |
copy->appendChild(c); |
| 323 |
} |
| 324 |
return copy; |
| 325 |
} |
| 326 |
|
| 327 |
void ViewProviderIndex::findViewProviders( |
| 328 |
const ViewProviderDocumentObject& vp, |
| 329 |
QList<ViewProviderIndex*>& index |
| 330 |
) const |
| 331 |
{ |
| 332 |
if (&this->v == &vp) { |
| 333 |
index.push_back(const_cast<ViewProviderIndex*>(this)); |
| 334 |
} |
| 335 |
QList<DocumentModelIndex*>::const_iterator it; |
| 336 |
for (it = childItems.begin(); it != childItems.end(); ++it) { |
| 337 |
auto v = static_cast<ViewProviderIndex*>(*it); |
| 338 |
v->findViewProviders(vp, index); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
QVariant ViewProviderIndex::data(int role) const |
| 343 |
{ |
| 344 |
if (role == Qt::DecorationRole) { |
| 345 |
return v.getIcon(); |
| 346 |
} |
| 347 |
else if (role == Qt::DisplayRole) { |
| 348 |
App::DocumentObject* obj = v.getObject(); |
| 349 |
return QString::fromUtf8(obj->Label.getValue()); |
| 350 |
} |
| 351 |
else if (role == Qt::FontRole) { |
| 352 |
App::DocumentObject* obj = v.getObject(); |
| 353 |
App::DocumentObject* act = obj->getDocument()->getActiveObject(); |
| 354 |
QFont font; |
| 355 |
font.setBold(obj == act); |
| 356 |
return static_cast<QVariant>(font); |
| 357 |
} |
| 358 |
|
| 359 |
return {}; |
| 360 |
} |
| 361 |
|
| 362 |
// ------------------------------------------------------------------------ |
| 363 |
|
| 364 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::DocumentModelIndex, Base::BaseClass) |
| 365 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::ApplicationIndex, Gui::DocumentModelIndex) |
| 366 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::DocumentIndex, Gui::DocumentModelIndex) |
| 367 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::ViewProviderIndex, Gui::DocumentModelIndex) |
| 368 |
|
| 369 |
struct DocumentModelP |
| 370 |
{ |
| 371 |
DocumentModelP() |
| 372 |
{ |
| 373 |
rootItem = new ApplicationIndex(); |
| 374 |
} |
| 375 |
~DocumentModelP() |
| 376 |
{ |
| 377 |
delete rootItem; |
| 378 |
} |
| 379 |
ApplicationIndex* rootItem; |
| 380 |
}; |
| 381 |
} // namespace Gui |
| 382 |
|
| 383 |
// ----------------------------------------------------------------- |
| 384 |
|
| 385 |
DocumentModel::DocumentModel(QObject* parent) |
| 386 |
: QAbstractItemModel(parent) |
| 387 |
, d(new DocumentModelP) |
| 388 |
{ |
| 389 |
static bool inittype = false; |
| 390 |
if (!inittype) { |
| 391 |
inittype = true; |
| 392 |
DocumentModelIndex ::init(); |
| 393 |
ApplicationIndex ::init(); |
| 394 |
DocumentIndex ::init(); |
| 395 |
ViewProviderIndex ::init(); |
| 396 |
} |
| 397 |
|
| 398 |
// NOLINTBEGIN |
| 399 |
// Setup connections |
| 400 |
Application::Instance->signalNewDocument.connect( |
| 401 |
std::bind(&DocumentModel::slotNewDocument, this, sp::_1) |
| 402 |
); |
| 403 |
Application::Instance->signalDeleteDocument.connect( |
| 404 |
std::bind(&DocumentModel::slotDeleteDocument, this, sp::_1) |
| 405 |
); |
| 406 |
Application::Instance->signalRenameDocument.connect( |
| 407 |
std::bind(&DocumentModel::slotRenameDocument, this, sp::_1) |
| 408 |
); |
| 409 |
Application::Instance->signalActiveDocument.connect( |
| 410 |
std::bind(&DocumentModel::slotActiveDocument, this, sp::_1) |
| 411 |
); |
| 412 |
Application::Instance->signalRelabelDocument.connect( |
| 413 |
std::bind(&DocumentModel::slotRelabelDocument, this, sp::_1) |
| 414 |
); |
| 415 |
// NOLINTEND |
| 416 |
} |
| 417 |
|
| 418 |
DocumentModel::~DocumentModel() |
| 419 |
{ |
| 420 |
delete d; |
| 421 |
d = nullptr; |
| 422 |
} |
| 423 |
|
| 424 |
void DocumentModel::slotNewDocument(const Gui::Document& Doc) |
| 425 |
{ |
| 426 |
// NOLINTBEGIN |
| 427 |
Doc.signalNewObject.connect(std::bind(&DocumentModel::slotNewObject, this, sp::_1)); |
| 428 |
Doc.signalDeletedObject.connect(std::bind(&DocumentModel::slotDeleteObject, this, sp::_1)); |
| 429 |
Doc.signalChangedObject.connect(std::bind(&DocumentModel::slotChangeObject, this, sp::_1, sp::_2)); |
| 430 |
Doc.signalRelabelObject.connect(std::bind(&DocumentModel::slotRenameObject, this, sp::_1)); |
| 431 |
Doc.signalActivatedObject.connect(std::bind(&DocumentModel::slotActiveObject, this, sp::_1)); |
| 432 |
Doc.signalInEdit.connect(std::bind(&DocumentModel::slotInEdit, this, sp::_1)); |
| 433 |
Doc.signalResetEdit.connect(std::bind(&DocumentModel::slotResetEdit, this, sp::_1)); |
| 434 |
// NOLINTEND |
| 435 |
|
| 436 |
QModelIndex parent = createIndex(0, 0, d->rootItem); |
| 437 |
int count_docs = d->rootItem->childCount(); |
| 438 |
beginInsertRows(parent, count_docs, count_docs); |
| 439 |
d->rootItem->appendChild(new DocumentIndex(Doc)); |
| 440 |
endInsertRows(); |
| 441 |
} |
| 442 |
|
| 443 |
void DocumentModel::slotDeleteDocument(const Gui::Document& Doc) |
| 444 |
{ |
| 445 |
int row = d->rootItem->findChild(Doc); |
| 446 |
if (row > -1) { |
| 447 |
QModelIndex parent = createIndex(0, 0, d->rootItem); |
| 448 |
beginRemoveRows(parent, row, row); |
| 449 |
DocumentModelIndex* item = d->rootItem->child(row); |
| 450 |
d->rootItem->removeChild(row); |
| 451 |
delete item; |
| 452 |
endRemoveRows(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
void DocumentModel::slotRenameDocument(const Gui::Document& Doc) |
| 457 |
{ |
| 458 |
Q_UNUSED(Doc); |
| 459 |
// do nothing here |
| 460 |
} |
| 461 |
|
| 462 |
void DocumentModel::slotRelabelDocument(const Gui::Document& Doc) |
| 463 |
{ |
| 464 |
int row = d->rootItem->findChild(Doc); |
| 465 |
if (row > -1) { |
| 466 |
QModelIndex parent = createIndex(0, 0, d->rootItem); |
| 467 |
QModelIndex item = index(row, 0, parent); |
| 468 |
Q_EMIT dataChanged(item, item); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
void DocumentModel::slotActiveDocument(const Gui::Document& /*Doc*/) |
| 473 |
{ |
| 474 |
// don't know which was the previous active document, so check simply all |
| 475 |
QModelIndex parent = createIndex(0, 0, d->rootItem); |
| 476 |
QModelIndex top = index(0, 0, parent); |
| 477 |
QModelIndex bottom = index(d->rootItem->childCount() - 1, 0, parent); |
| 478 |
Q_EMIT dataChanged(top, bottom); |
| 479 |
} |
| 480 |
|
| 481 |
void DocumentModel::slotInEdit(const Gui::ViewProviderDocumentObject& v) |
| 482 |
{ |
| 483 |
Q_UNUSED(v); |
| 484 |
} |
| 485 |
|
| 486 |
void DocumentModel::slotResetEdit(const Gui::ViewProviderDocumentObject& v) |
| 487 |
{ |
| 488 |
Q_UNUSED(v); |
| 489 |
} |
| 490 |
|
| 491 |
void DocumentModel::slotNewObject(const Gui::ViewProviderDocumentObject& obj) |
| 492 |
{ |
| 493 |
App::Document* doc = obj.getObject()->getDocument(); |
| 494 |
Gui::Document* gdc = Application::Instance->getDocument(doc); |
| 495 |
int row = d->rootItem->findChild(*gdc); |
| 496 |
if (row > -1) { |
| 497 |
auto index = static_cast<DocumentIndex*>(d->rootItem->child(row)); |
| 498 |
QModelIndex parent = createIndex(index->row(), 0, index); |
| 499 |
int count_obj = index->childCount(); |
| 500 |
beginInsertRows(parent, count_obj, count_obj); |
| 501 |
index->appendChild(new ViewProviderIndex(obj, index)); |
| 502 |
endInsertRows(); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
void DocumentModel::slotDeleteObject(const Gui::ViewProviderDocumentObject& obj) |
| 507 |
{ |
| 508 |
App::Document* doc = obj.getObject()->getDocument(); |
| 509 |
Gui::Document* gdc = Application::Instance->getDocument(doc); |
| 510 |
int row = d->rootItem->findChild(*gdc); |
| 511 |
if (row > -1) { |
| 512 |
auto doc_index = static_cast<DocumentIndex*>(d->rootItem->child(row)); |
| 513 |
QList<ViewProviderIndex*> views; |
| 514 |
doc_index->findViewProviders(obj, views); |
| 515 |
for (auto& view : views) { |
| 516 |
DocumentModelIndex* parentitem = view->parent(); |
| 517 |
QModelIndex parent = createIndex(doc_index->row(), 0, parentitem); |
| 518 |
int row = view->row(); |
| 519 |
beginRemoveRows(parent, row, row); |
| 520 |
parentitem->removeChild(row); |
| 521 |
delete view; |
| 522 |
endRemoveRows(); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
void DocumentModel::slotChangeObject(const Gui::ViewProviderDocumentObject& obj, const App::Property& Prop) |
| 528 |
{ |
| 529 |
App::DocumentObject* fea = obj.getObject(); |
| 530 |
if (&fea->Label == &Prop) { |
| 531 |
App::Document* doc = fea->getDocument(); |
| 532 |
Gui::Document* gdc = Application::Instance->getDocument(doc); |
| 533 |
int row = d->rootItem->findChild(*gdc); |
| 534 |
if (row > -1) { |
| 535 |
auto doc_index = static_cast<DocumentIndex*>(d->rootItem->child(row)); |
| 536 |
QList<ViewProviderIndex*> views; |
| 537 |
doc_index->findViewProviders(obj, views); |
| 538 |
for (const auto& view : std::as_const(views)) { |
| 539 |
DocumentModelIndex* parentitem = view->parent(); |
| 540 |
QModelIndex parent = createIndex(0, 0, parentitem); |
| 541 |
int row = view->row(); |
| 542 |
QModelIndex item = index(row, 0, parent); |
| 543 |
Q_EMIT dataChanged(item, item); |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
else if (isPropertyLink(Prop)) { |
| 548 |
App::Document* doc = fea->getDocument(); |
| 549 |
Gui::Document* gdc = Application::Instance->getDocument(doc); |
| 550 |
std::vector<ViewProviderDocumentObject*> views = claimChildren(*gdc, obj); |
| 551 |
|
| 552 |
int row = d->rootItem->findChild(*gdc); |
| 553 |
if (row > -1) { |
| 554 |
QList<DocumentModelIndex*> del_items; |
| 555 |
auto doc_index = static_cast<DocumentIndex*>(d->rootItem->child(row)); |
| 556 |
for (const auto& view : views) { |
| 557 |
int row = doc_index->rowOfViewProvider(*view); |
| 558 |
// is it a top-level child in the document |
| 559 |
if (row >= 0) { |
| 560 |
DocumentModelIndex* child = doc_index->child(row); |
| 561 |
del_items.push_back(child); |
| 562 |
QModelIndex parent = createIndex(doc_index->row(), 0, doc_index); |
| 563 |
beginRemoveRows(parent, row, row); |
| 564 |
doc_index->removeChild(row); |
| 565 |
endRemoveRows(); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
// get all occurrences of the view provider in the tree structure |
| 570 |
QList<ViewProviderIndex*> obj_index; |
| 571 |
doc_index->findViewProviders(obj, obj_index); |
| 572 |
for (const auto& it : std::as_const(obj_index)) { |
| 573 |
QModelIndex parent = createIndex(it->row(), 0, it); |
| 574 |
int count_obj = it->childCount(); |
| 575 |
beginRemoveRows(parent, 0, count_obj); |
| 576 |
// remove all children but do not yet delete them |
| 577 |
QList<DocumentModelIndex*> items = it->removeAll(); |
| 578 |
endRemoveRows(); |
| 579 |
|
| 580 |
beginInsertRows(parent, 0, (int)views.size()); |
| 581 |
for (const auto& view : views) { |
| 582 |
ViewProviderIndex* clone = doc_index->cloneViewProvider(*view); |
| 583 |
it->appendChild(clone); |
| 584 |
} |
| 585 |
endInsertRows(); |
| 586 |
|
| 587 |
del_items.append(items); |
| 588 |
} |
| 589 |
|
| 590 |
qDeleteAll(del_items); |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
void DocumentModel::slotRenameObject(const Gui::ViewProviderDocumentObject& obj) |
| 596 |
{ |
| 597 |
Q_UNUSED(obj); |
| 598 |
// renaming of objects not supported at the moment |
| 599 |
} |
| 600 |
|
| 601 |
void DocumentModel::slotActiveObject(const Gui::ViewProviderDocumentObject& obj) |
| 602 |
{ |
| 603 |
Q_UNUSED(obj); |
| 604 |
// do nothing here because this is automatically done by calling |
| 605 |
// ViewProviderIndex::data() |
| 606 |
} |
| 607 |
|
| 608 |
const Document* DocumentModel::getDocument(const QModelIndex& index) const |
| 609 |
{ |
| 610 |
if (!index.isValid()) { |
| 611 |
return nullptr; |
| 612 |
} |
| 613 |
Base::BaseClass* item = nullptr; |
| 614 |
item = static_cast<Base::BaseClass*>(index.internalPointer()); |
| 615 |
if (item->is<DocumentIndex>()) { |
| 616 |
const Gui::Document& d = static_cast<DocumentIndex*>(item)->d; |
| 617 |
return (&d); |
| 618 |
} |
| 619 |
|
| 620 |
return nullptr; |
| 621 |
} |
| 622 |
|
| 623 |
bool DocumentModel::isPropertyLink(const App::Property& prop) const |
| 624 |
{ |
| 625 |
return prop.isDerivedFrom<App::PropertyLink>() || prop.isDerivedFrom<App::PropertyLinkSub>() |
| 626 |
|| prop.isDerivedFrom<App::PropertyLinkList>() |
| 627 |
|| prop.isDerivedFrom<App::PropertyLinkSubList>(); |
| 628 |
} |
| 629 |
|
| 630 |
std::vector<ViewProviderDocumentObject*> DocumentModel::claimChildren( |
| 631 |
const Gui::Document& doc, |
| 632 |
const ViewProviderDocumentObject& obj |
| 633 |
) const |
| 634 |
{ |
| 635 |
std::vector<ViewProviderDocumentObject*> views; |
| 636 |
std::vector<App::DocumentObject*> childs = obj.claimChildren(); |
| 637 |
for (const auto& child : childs) { |
| 638 |
ViewProvider* view = doc.getViewProvider(child); |
| 639 |
if (view && view->isDerivedFrom<ViewProviderDocumentObject>()) { |
| 640 |
views.push_back(static_cast<ViewProviderDocumentObject*>(view)); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
return views; |
| 645 |
} |
| 646 |
|
| 647 |
int DocumentModel::columnCount(const QModelIndex& /*parent*/) const |
| 648 |
{ |
| 649 |
return 1; |
| 650 |
} |
| 651 |
|
| 652 |
QVariant DocumentModel::data(const QModelIndex& index, int role) const |
| 653 |
{ |
| 654 |
if (!index.isValid()) { |
| 655 |
return {}; |
| 656 |
} |
| 657 |
return static_cast<DocumentModelIndex*>(index.internalPointer())->data(role); |
| 658 |
} |
| 659 |
|
| 660 |
bool DocumentModel::setData(const QModelIndex& index, const QVariant& value, int role) |
| 661 |
{ |
| 662 |
if (!index.isValid()) { |
| 663 |
return false; |
| 664 |
} |
| 665 |
return static_cast<DocumentModelIndex*>(index.internalPointer())->setData(value, role); |
| 666 |
} |
| 667 |
|
| 668 |
Qt::ItemFlags DocumentModel::flags(const QModelIndex& index) const |
| 669 |
{ |
| 670 |
// if (index.internalPointer() == d->rootItem) |
| 671 |
// return Qt::ItemIsEnabled; |
| 672 |
// return QAbstractItemModel::flags(index); |
| 673 |
if (!index.isValid()) { |
| 674 |
return {}; |
| 675 |
} |
| 676 |
return static_cast<DocumentModelIndex*>(index.internalPointer())->flags(); |
| 677 |
} |
| 678 |
|
| 679 |
QModelIndex DocumentModel::index(int row, int column, const QModelIndex& parent) const |
| 680 |
{ |
| 681 |
DocumentModelIndex* item = nullptr; |
| 682 |
if (!parent.isValid()) { |
| 683 |
item = d->rootItem; |
| 684 |
} |
| 685 |
else { |
| 686 |
item = static_cast<DocumentModelIndex*>(parent.internalPointer())->child(row); |
| 687 |
} |
| 688 |
if (!item) { |
| 689 |
return {}; |
| 690 |
} |
| 691 |
return createIndex(row, column, item); |
| 692 |
} |
| 693 |
|
| 694 |
QModelIndex DocumentModel::parent(const QModelIndex& index) const |
| 695 |
{ |
| 696 |
if (!index.isValid() || index.internalPointer() == d->rootItem) { |
| 697 |
return {}; |
| 698 |
} |
| 699 |
DocumentModelIndex* item = nullptr; |
| 700 |
item = static_cast<DocumentModelIndex*>(index.internalPointer()); |
| 701 |
DocumentModelIndex* parent = item->parent(); |
| 702 |
return createIndex(parent->row(), 0, parent); |
| 703 |
} |
| 704 |
|
| 705 |
int DocumentModel::rowCount(const QModelIndex& parent) const |
| 706 |
{ |
| 707 |
if (!parent.isValid()) { |
| 708 |
return 1; // the root item |
| 709 |
} |
| 710 |
DocumentModelIndex* item = nullptr; |
| 711 |
item = static_cast<DocumentModelIndex*>(parent.internalPointer()); |
| 712 |
return item->childCount(); |
| 713 |
} |
| 714 |
|
| 715 |
QVariant DocumentModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 716 |
{ |
| 717 |
Q_UNUSED(section); |
| 718 |
if (orientation == Qt::Horizontal) { |
| 719 |
if (role != Qt::DisplayRole) { |
| 720 |
return {}; |
| 721 |
} |
| 722 |
return tr("Labels & Attributes"); |
| 723 |
} |
| 724 |
|
| 725 |
return {}; |
| 726 |
} |
| 727 |
|
| 728 |
bool DocumentModel::setHeaderData(int, Qt::Orientation, const QVariant&, int) |
| 729 |
{ |
| 730 |
return false; |
| 731 |
} |