1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "XGUI_DataModel.h"
21 #include "XGUI_ObjectsBrowser.h"
23 #include <ModuleBase_IconFactory.h>
24 #include <ModuleBase_ITreeNode.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_ResultField.h>
29 #include <Config_FeatureMessage.h>
31 #include <Events_Loop.h>
37 // Constructor *************************************************
38 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
39 //myIsEventsProcessingBlocked(false)
41 XGUI_ObjectsBrowser* aOB = qobject_cast<XGUI_ObjectsBrowser*>(theParent);
42 myWorkshop = aOB->workshop();
44 Events_Loop* aLoop = Events_Loop::loop();
45 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
46 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
47 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
48 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
49 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
50 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
53 XGUI_DataModel::~XGUI_DataModel()
58 //******************************************************
59 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
61 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
62 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
63 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
64 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
65 QObjectPtrList aCreated;
66 std::set<ObjectPtr>::const_iterator aIt;
67 for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
68 if ((*aIt)->isInHistory())
69 aCreated.append(*aIt);
71 if (aCreated.length() == 0)
74 emit beforeTreeRebuild();
75 QTreeNodesList aNodes = myRoot->objectCreated(aCreated);
76 ModuleBase_ITreeNode* aParent;
78 QModelIndex aParentIndex1, aParentIndex2;
80 bool aRebuildAll = false;
81 bool isInserted = false;
83 foreach(ModuleBase_ITreeNode* aNode, aNodes) {
84 aObj = aNode->object();
85 aParent = aNode->parent();
86 if (aObj.get() && (aObj->groupName() == ModelAPI_Folder::group())) {
91 aRow = aParent->nodeRow(aNode);
92 aParentIndex1 = getParentIndex(aNode, 0);
93 aParentIndex2 = getParentIndex(aNode, 2);
94 insertRows(aRow, 1, aParentIndex1);
95 dataChanged(aParentIndex1, aParentIndex2);
106 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
107 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
108 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
109 const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aMsgGroups =
111 QTreeNodesList aList;
112 std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aIt;
113 emit beforeTreeRebuild();
114 for (aIt = aMsgGroups.cbegin(); aIt != aMsgGroups.cend(); aIt++) {
115 aList.append(myRoot->objectsDeleted(aIt->first, aIt->second.c_str()));
117 // Remove obsolete nodes
118 QTreeNodesList aRemaining;
119 foreach(ModuleBase_ITreeNode* aNode, aList) {
120 if (myRoot->hasSubNode(aNode))
121 aRemaining.append(aNode);
123 // Update remaining nodes
124 foreach(ModuleBase_ITreeNode* aNode, aRemaining) {
126 aNode->parent()->update();
131 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
132 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
133 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
134 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
136 QObjectPtrList aCreated;
137 std::set<ObjectPtr>::const_iterator aIt;
138 bool aRebuildAll = false;
139 emit beforeTreeRebuild();
140 for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
141 ObjectPtr aObj = (*aIt);
142 if (!aObj->isInHistory())
145 if (aObj->data()->isValid()) {
146 if (aObj->groupName() == ModelAPI_Folder::group()) {
150 aCreated.append(*aIt);
157 QSet<ModuleBase_ITreeNode*> aParents;
158 foreach(ObjectPtr aObj, aCreated) {
159 ModuleBase_ITreeNode* aNode = myRoot->subNode(aObj);
161 if (aNode->parent()) {
162 if (aNode->parent() == myRoot) {
164 aParents.insert(myRoot);
168 aNode = aNode->parent();
171 aParents.insert(aNode);
174 foreach(ModuleBase_ITreeNode* aNode, aParents) {
181 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
182 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
183 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
184 if (aUpdMsg->reordered().get()) {
185 DocumentPtr aDoc = aUpdMsg->reordered()->document();
186 std::string aGroup = aUpdMsg->reordered()->group();
187 ModuleBase_ITreeNode* aNode = myRoot->findParent(aDoc, aGroup.c_str());
189 emit beforeTreeRebuild();
196 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
197 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
198 ModuleBase_ITreeNode* aRoot = myRoot->findRoot(aDoc);
200 updateSubTree(aRoot);
203 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
204 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
205 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
206 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
208 QObjectPtrList aCreated;
209 std::set<ObjectPtr>::const_iterator aIt;
210 bool aRebuildAll = false;
211 for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
212 ObjectPtr aObj = (*aIt);
213 if (aObj->groupName() == ModelAPI_ResultField::group()) {
214 aCreated.append(aObj);
217 if (aCreated.length() == 0)
219 bool isInsert = false;
220 bool isRemove = false;
221 emit beforeTreeRebuild();
222 foreach(ObjectPtr aObj, aCreated) {
223 ModuleBase_ITreeNode* aNode = myRoot->subNode(aObj);
225 int aOldNb = aNode->childrenCount();
227 int aNewNb = aNode->childrenCount();
229 QModelIndex aFirstIdx = getIndex(aNode, 0);
230 QModelIndex aLastIdx = getIndex(aNode, 2);
232 if (aNewNb > aOldNb) {
233 insertRows(aOldNb - 1, aNewNb - aOldNb, aFirstIdx);
236 else if (aNewNb < aOldNb) {
238 removeRows(aNewNb - 1, aOldNb - aNewNb, aFirstIdx);
240 removeRows(0, aOldNb, aFirstIdx);
241 isRemove = aNewNb || aOldNb;
243 dataChanged(aFirstIdx, aLastIdx);
254 //******************************************************
255 void XGUI_DataModel::clear()
261 //******************************************************
262 void XGUI_DataModel::rebuildDataTree()
268 //******************************************************
269 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
271 if (theIndex.isValid()) {
272 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
273 return aNode->object();
278 //******************************************************
279 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
281 ModuleBase_ITreeNode* aNode = myRoot->subNode(theObject);
283 return getIndex(aNode, theColumn);
285 return QModelIndex();
288 //******************************************************
289 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
291 if (theIndex.isValid()) {
292 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
293 return aNode->data(theIndex.column(), theRole);
298 //******************************************************
299 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
304 //******************************************************
305 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
307 ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
308 (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
309 return aParentNode->childrenCount();
312 //******************************************************
313 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
318 //******************************************************
319 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
321 ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
322 (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
323 ModuleBase_ITreeNode* aSubNode = aParentNode->subNode(theRow);
325 return createIndex(theRow, theColumn, aSubNode);
328 //******************************************************
329 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
331 if (theIndex.isValid()) {
332 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
333 return getParentIndex(aNode, 1);
335 return QModelIndex();
338 //******************************************************
339 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
341 ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
342 (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
343 return aParentNode->childrenCount() > 0;
346 //******************************************************
347 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
349 beginInsertRows(theParent, theRow, theRow + theCount - 1);
353 //******************************************************
354 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
356 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
360 //******************************************************
361 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
363 if (theIndex.isValid()) {
364 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
365 return aNode->flags(theIndex.column());
367 return Qt::ItemFlags();
371 //******************************************************
372 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
374 SessionPtr aSession = ModelAPI_Session::get();
375 DocumentPtr aRootDoc = aSession->moduleDocument();
376 if (theDoc == aRootDoc)
377 return QModelIndex();
379 ModuleBase_ITreeNode* aDocNode = 0;
380 foreach(ModuleBase_ITreeNode* aNode, myRoot->children()) {
381 if (aNode->document() == theDoc) {
387 return getIndex(aDocNode, theColumn);
389 return QModelIndex();
392 //******************************************************
393 bool XGUI_DataModel::hasHiddenState(const QModelIndex& theIndex)
395 if (theIndex.isValid()) {
396 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
397 return aNode->visibilityState() == ModuleBase_ITreeNode::Hidden;
402 //******************************************************
403 bool XGUI_DataModel::hasIndex(const QModelIndex& theIndex) const
405 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
406 return myRoot->hasSubNode(aNode);
409 //******************************************************
410 QModelIndex XGUI_DataModel::getParentIndex(ModuleBase_ITreeNode* theNode, int thCol) const
412 ModuleBase_ITreeNode* aParent = theNode->parent();
413 if (aParent == myRoot) {
414 return QModelIndex();
416 return getIndex(aParent, thCol);
420 //******************************************************
421 QModelIndex XGUI_DataModel::getIndex(ModuleBase_ITreeNode* theNode, int thCol) const
423 if (theNode == myRoot)
424 return QModelIndex();
425 int aRow = theNode->parent()->nodeRow(theNode);
426 return createIndex(aRow, thCol, theNode);
430 //******************************************************
431 void XGUI_DataModel::updateSubTree(ModuleBase_ITreeNode* theParent)
433 int aRows = theParent->childrenCount();
435 QModelIndex aParent = getIndex(theParent, 0);
436 QModelIndex aFirstIdx = aParent.child(0, 0);
437 QModelIndex aLastIdx = aParent.child(aRows - 1, 2);
438 dataChanged(aFirstIdx, aLastIdx);
443 //******************************************************
444 DocumentPtr XGUI_DataModel::document(const QModelIndex& theIndex) const
446 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
447 return aNode->document();