1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "PartSet_TreeNodes.h"
23 #include <ModuleBase_IconFactory.h>
24 #include <ModuleBase_IWorkshop.h>
26 #include <PartSetPlugin_Part.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultParameter.h>
30 #include <ModelAPI_ResultField.h>
31 #include <ModelAPI_ResultGroup.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_ResultPart.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_Feature.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_ResultCompSolid.h>
38 #include <ModelAPI_CompositeFeature.h>
39 #include <ModelAPI_AttributeDouble.h>
45 #define ACTIVE_COLOR QColor(Qt::black)
46 #define SELECTABLE_COLOR QColor(100, 100, 100)
47 #define DISABLED_COLOR QColor(200, 200, 200)
49 Qt::ItemFlags aNullFlag;
50 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
51 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
54 ResultPartPtr getPartResult(const ObjectPtr& theObj)
56 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
58 ResultPtr aRes = aFeature->firstResult();
59 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
60 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
61 // Use only original parts, not a placement results
62 if (aPartRes == aPartRes->original())
66 return ResultPartPtr();
69 bool isCurrentFeature(const ObjectPtr& theObj)
71 SessionPtr aSession = ModelAPI_Session::get();
72 DocumentPtr aCurDoc = aSession->activeDocument();
73 FeaturePtr aFeature = aCurDoc->currentFeature(true);
74 return aFeature == theObj;
77 //////////////////////////////////////////////////////////////////////////////////
78 QVariant PartSet_TreeNode::data(int theColumn, int theRole) const
80 if ((theColumn == 1) && (theRole == Qt::ForegroundRole)) {
81 Qt::ItemFlags aFlags = flags(theColumn);
82 if (aFlags == Qt::ItemFlags())
83 return QBrush(DISABLED_COLOR);
84 if (!aFlags.testFlag(Qt::ItemIsEditable))
85 return QBrush(SELECTABLE_COLOR);
88 return ModuleBase_ITreeNode::data(theColumn, theRole);
92 //////////////////////////////////////////////////////////////////////////////////
93 QVariant PartSet_ObjectNode::data(int theColumn, int theRole) const
98 if (myObject->groupName() == ModelAPI_ResultParameter::group()) {
99 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myObject);
100 AttributeDoublePtr aValueAttribute =
101 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
102 QString aVal = QString::number(aValueAttribute->value());
103 QString aTitle = QString(myObject->data()->name().c_str());
104 return aTitle + " = " + aVal;
106 return myObject->data()->name().c_str();
109 case Qt::DecorationRole:
112 switch (getVisibilityState()) {
116 return QIcon(":pictures/eyeopen.png");
118 return QIcon(":pictures/eyemiclosed.png");
120 return QIcon(":pictures/eyeclosed.png");
123 return ModuleBase_IconFactory::get()->getIcon(myObject);
125 if (isCurrentFeature(myObject))
126 return QIcon(":pictures/arrow.png");
129 return PartSet_TreeNode::data(theColumn, theRole);
132 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
134 if (!myObject->isDisabled()) {
135 DocumentPtr aDoc = myObject->document();
136 SessionPtr aSession = ModelAPI_Session::get();
137 if (aSession->activeDocument() == aDoc)
143 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::getVisibilityState() const
145 Qt::ItemFlags aFlags = flags(1);
146 if (aFlags == Qt::ItemFlags())
149 if (myObject->groupName() == ModelAPI_ResultParameter::group())
151 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
153 ModuleBase_IWorkshop* aWork = workshop();
154 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
155 if (aCompRes.get()) {
156 VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
157 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
158 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
159 ResultPtr aSubRes = aCompRes->subResult(i, true);
160 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
161 if (aState == NoneState)
163 else if (aState != aS) {
164 aState = SemiVisible;
170 if (aWork->isVisible(aResObj))
180 //////////////////////////////////////////////////////////////////////////////////
181 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
183 : PartSet_TreeNode(theParent), myType(theType)
187 PartSet_FolderNode::~PartSet_FolderNode()
189 while (myChildren.length() > 0) {
190 ModuleBase_ITreeNode* aNode = myChildren.last();
191 myChildren.removeAll(aNode);
196 QString PartSet_FolderNode::name() const
199 case ParametersFolder:
200 return QObject::tr("Parameters");
201 case ConstructionFolder:
202 return QObject::tr("Constructions");
204 return QObject::tr("Parts");
206 return QObject::tr("Results");
208 return QObject::tr("Fields");
210 return QObject::tr("Groups");
216 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
218 static QIcon aParamsIco(":pictures/params_folder.png");
219 static QIcon aConstrIco(":pictures/constr_folder.png");
221 if (theColumn == 1) {
223 case Qt::DisplayRole:
224 return name() + QString(" (%1)").arg(childrenCount());
225 case Qt::DecorationRole:
227 case ParametersFolder:
229 case ConstructionFolder:
242 return PartSet_TreeNode::data(theColumn, theRole);
245 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
247 SessionPtr aSession = ModelAPI_Session::get();
248 DocumentPtr aActiveDoc = aSession->activeDocument();
249 if (theColumn == 1) {
250 if (document() == aActiveDoc)
256 void PartSet_FolderNode::update()
258 DocumentPtr aDoc = document();
262 // Remove extra sub-nodes
263 QTreeNodesList aDelList;
264 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
265 if (aDoc->index(aNode->object()) == -1)
266 aDelList.append(aNode);
268 foreach(ModuleBase_ITreeNode* aNode, aDelList) {
269 myChildren.removeAll(aNode);
274 std::string aGroup = groupName();
275 int aSize = aDoc->size(aGroup);
276 for (int i = 0; i < aSize; i++) {
277 ObjectPtr aObj = aDoc->object(aGroup, i);
278 if (i < myChildren.size()) {
279 if (myChildren.at(i)->object() != aObj) {
280 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(aObj, this);
281 myChildren.insert(i, aNode);
284 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(aObj, this);
285 myChildren.append(aNode);
290 std::string PartSet_FolderNode::groupName() const
293 case ParametersFolder:
294 return ModelAPI_ResultParameter::group();
295 case ConstructionFolder:
296 return ModelAPI_ResultConstruction::group();
298 return ModelAPI_ResultPart::group();
300 return ModelAPI_ResultBody::group();
302 return ModelAPI_ResultField::group();
304 return ModelAPI_ResultGroup::group();
309 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
311 QTreeNodesList aResult;
312 std::string aName = groupName();
313 DocumentPtr aDoc = document();
315 foreach(ObjectPtr aObj, theObjects) {
316 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
317 aIdx = aDoc->index(aObj);
319 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
321 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(aObj, this);
322 aResult.append(aNode);
323 if (aIdx < myChildren.size())
324 myChildren.insert(aIdx, aNode);
326 myChildren.append(aNode);
334 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
335 const QString& theGroup)
337 DocumentPtr aDoc = document();
338 QTreeNodesList aResult;
339 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
340 QTreeNodesList aDelList;
341 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
342 if (aDoc->index(aNode->object()) == -1)
343 aDelList.append(aNode);
345 if (aDelList.size() > 0) {
346 foreach(ModuleBase_ITreeNode* aNode, aDelList) {
347 myChildren.removeAll(aNode);
350 aResult.append(this);
356 //////////////////////////////////////////////////////////////////////////////////
357 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
359 QTreeNodesList aResult;
360 // Process all folders
361 ModuleBase_ITreeNode* aFoder = 0;
362 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
363 aFoder = dynamic_cast<PartSet_FolderNode*>(aNode);
365 aFoder = dynamic_cast<PartSet_FeatureFolderNode*>(aNode);
367 if (aFoder) { // aFolder node
368 QTreeNodesList aList = aNode->objectCreated(theObjects);
369 if (aList.size() > 0)
370 aResult.append(aList);
373 // Process the root sub-objects
374 DocumentPtr aDoc = document();
376 int aNb = numberOfFolders();
377 foreach(ObjectPtr aObj, theObjects) {
378 if (aDoc == aObj->document()) {
379 if (aObj->groupName() == ModelAPI_Feature::group()) {
380 ModuleBase_ITreeNode* aNode = createNode(aObj);
381 aIdx = aDoc->index(aObj) + aNb;
382 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
384 if (aIdx < myChildren.size())
385 myChildren.insert(aIdx, aNode);
387 myChildren.append(aNode);
388 aResult.append(aNode);
396 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
397 const QString& theGroup)
399 QTreeNodesList aResult;
400 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
401 if (aNode->childrenCount() > 0) { // aFolder node
402 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
403 if (aList.size() > 0)
404 aResult.append(aList);
407 DocumentPtr aDoc = document();
408 if ((theDoc == aDoc) && (theGroup.toStdString() == ModelAPI_Feature::group())) {
409 QTreeNodesList aDelList;
410 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
411 if (aNode->object().get()) {
412 if (aDoc->index(aNode->object()) == -1)
413 aDelList.append(aNode);
416 if (aDelList.size() > 0) {
417 foreach(ModuleBase_ITreeNode* aNode, aDelList) {
418 myChildren.removeAll(aNode);
421 aResult.append(this);
427 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
430 ModuleBase_ITreeNode* aResult = 0;
431 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
432 aResult = aNode->findParent(theDoc, theGroup);
437 if ((theDoc == document()) && (theGroup.toStdString() == ModelAPI_Feature::group()))
443 //////////////////////////////////////////////////////////////////////////////////
444 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
446 SessionPtr aSession = ModelAPI_Session::get();
447 DocumentPtr aDoc = aSession->moduleDocument();
449 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
450 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
451 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
453 myChildren.append(myParamsFolder);
454 myChildren.append(myConstrFolder);
455 myChildren.append(myPartsFolder);
460 PartSet_RootNode::~PartSet_RootNode()
462 delete myParamsFolder;
463 delete myConstrFolder;
464 delete myPartsFolder;
468 void PartSet_RootNode::update()
470 myParamsFolder->update();
471 myConstrFolder->update();
472 myPartsFolder->update();
474 // Update features content
475 DocumentPtr aDoc = document();
477 // Remove extra sub-nodes
478 QTreeNodesList aDelList;
479 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
480 if (aNode->object().get()) {
481 if (aDoc->index(aNode->object()) == -1)
482 aDelList.append(aNode);
485 foreach(ModuleBase_ITreeNode* aNode, aDelList) {
486 myChildren.removeAll(aNode);
491 std::string aGroup = ModelAPI_Feature::group();
492 int aSize = aDoc->size(aGroup);
495 int aNb = numberOfFolders();
496 for (int i = 0; i < aSize; i++) {
497 ObjectPtr aObj = aDoc->object(aGroup, i);
498 aId = i + aNb; // Take into account existing folders
499 if (aId < myChildren.size()) {
500 if (myChildren.at(aId)->object() != aObj) {
501 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
502 ModuleBase_ITreeNode* aNode;
503 if (aFeature->getKind() == PartSetPlugin_Part::ID())
504 aNode = new PartSet_PartRootNode(aObj, this);
506 aNode = new PartSet_ObjectNode(aObj, this);
507 myChildren.insert(aId, aNode);
510 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
511 ModuleBase_ITreeNode* aNode;
512 if (aFeature->getKind() == PartSetPlugin_Part::ID())
513 aNode = new PartSet_PartRootNode(aObj, this);
515 aNode = new PartSet_ObjectNode(aObj, this);
516 myChildren.append(aNode);
519 // Update sub-folders
520 ModuleBase_ITreeNode* aSubFolder = 0;
521 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
522 aSubFolder = dynamic_cast<PartSet_PartRootNode*>(aNode);
524 aSubFolder->update();
528 DocumentPtr PartSet_RootNode::document() const
530 return ModelAPI_Session::get()->moduleDocument();
533 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
535 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
536 if (aFeature->getKind() == PartSetPlugin_Part::ID())
537 return new PartSet_PartRootNode(theObj, this);
539 return new PartSet_ObjectNode(theObj, this);
542 //////////////////////////////////////////////////////////////////////////////////
543 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
544 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
546 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
547 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
548 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
549 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
550 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
552 myChildren.append(myParamsFolder);
553 myChildren.append(myConstrFolder);
554 myChildren.append(myResultsFolder);
559 PartSet_PartRootNode::~PartSet_PartRootNode()
561 delete myParamsFolder;
562 delete myConstrFolder;
563 delete myResultsFolder;
564 delete myFieldsFolder;
565 delete myGroupsFolder;
569 void PartSet_PartRootNode::update()
571 DocumentPtr aDoc = document();
575 myParamsFolder->update();
576 myConstrFolder->update();
577 myResultsFolder->update();
578 myFieldsFolder->update();
579 myGroupsFolder->update();
581 bool aHasFields = myFieldsFolder->childrenCount() > 0;
582 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
583 if (aHasFields && (!myChildren.contains(myFieldsFolder))) {
584 myChildren.insert(3, myFieldsFolder);
586 if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
587 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
590 // Update features content
591 int aRows = numberOfFolders();
593 // Remove extra sub-nodes
594 QTreeNodesList aDelList;
597 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
599 if (aNode->object().get()) {
600 aIndex = aDoc->index(aNode->object());
601 if ((aIndex == -1) || (aId != (aIndex + aRows)))
602 aDelList.append(aNode);
605 foreach(ModuleBase_ITreeNode* aNode, aDelList) {
606 myChildren.removeAll(aNode);
610 std::string aGroup = ModelAPI_Feature::group();
611 int aSize = aDoc->size(aGroup);
613 for (int i = 0; i < aSize; i++) {
614 ObjectPtr aObj = aDoc->object(aGroup, i);
615 aId = i + aRows; // Take into account existing folders
616 if (aId < myChildren.size()) {
617 if (myChildren.at(aId)->object() != aObj) {
618 ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(aObj, this);
619 myChildren.insert(aId, aNode);
622 ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(aObj, this);
623 myChildren.append(aNode);
628 DocumentPtr PartSet_PartRootNode::document() const
630 ResultPartPtr aPartRes = getPartResult(myObject);
632 return aPartRes->partDoc();
633 return DocumentPtr();
636 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
641 case Qt::DisplayRole:
642 return QString(myObject->data()->name().c_str());
643 case Qt::DecorationRole:
644 return ModuleBase_IconFactory::get()->getIcon(myObject);
647 if (theRole == Qt::DecorationRole)
648 if (isCurrentFeature(myObject))
649 return QIcon(":pictures/arrow.png");
651 return PartSet_TreeNode::data(theColumn, theRole);
654 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
656 SessionPtr aSession = ModelAPI_Session::get();
657 DocumentPtr aActiveDoc = aSession->activeDocument();
658 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
663 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
665 return new PartSet_ObjectNode(theObj, this);
668 int PartSet_PartRootNode::numberOfFolders() const
671 if (myFieldsFolder->childrenCount() > 0)
673 if (myGroupsFolder->childrenCount() > 0)
678 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
680 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
681 if (!myFieldsFolder->childrenCount()) {
682 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
684 myChildren.insert(3, myFieldsFolder);
685 aResult.append(myFieldsFolder);
686 aResult.append(aList);
689 if (!myGroupsFolder->childrenCount()) {
690 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
692 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
693 aResult.append(myGroupsFolder);
694 aResult.append(aList);
700 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
701 const QString& theGroup)
703 QTreeNodesList aResult;
704 if (myFieldsFolder->childrenCount()) {
705 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
707 aResult.append(aList);
708 if (!myFieldsFolder->childrenCount())
709 myChildren.removeAll(myFieldsFolder);
712 if (myGroupsFolder->childrenCount()) {
713 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
715 aResult.append(aList);
716 if (!myGroupsFolder->childrenCount())
717 myChildren.removeAll(myGroupsFolder);
720 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));