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 "XGUI_DataModel.h"
22 #include "XGUI_ObjectsBrowser.h"
24 #include <ModuleBase_IconFactory.h>
25 #include <ModuleBase_ITreeNode.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_ResultField.h>
30 #include <Config_FeatureMessage.h>
32 #include <Events_Loop.h>
38 // Constructor *************************************************
39 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
40 //myIsEventsProcessingBlocked(false)
42 XGUI_ObjectsBrowser* aOB = qobject_cast<XGUI_ObjectsBrowser*>(theParent);
43 myWorkshop = aOB->workshop();
45 Events_Loop* aLoop = Events_Loop::loop();
46 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
47 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
48 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
49 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
50 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
51 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
54 XGUI_DataModel::~XGUI_DataModel()
59 //******************************************************
60 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
62 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
63 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
64 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
65 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
66 QObjectPtrList aCreated;
67 std::set<ObjectPtr>::const_iterator aIt;
68 for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
69 if ((*aIt)->isInHistory())
70 aCreated.append(*aIt);
72 QTreeNodesList aNodes = myRoot->objectCreated(aCreated);
73 ModuleBase_ITreeNode* aParent;
75 QModelIndex aParentIndex1, aParentIndex2;
76 foreach(ModuleBase_ITreeNode* aNode, aNodes) {
77 aParent = aNode->parent();
78 aRow = aParent->nodeRow(aNode);
79 aParentIndex1 = getParentIndex(aNode, 0);
80 aParentIndex2 = getParentIndex(aNode, 2);
81 insertRows(aRow, 1, aParentIndex1);
82 dataChanged(aParentIndex1, aParentIndex2);
85 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
86 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
87 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
88 const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aMsgGroups =
91 std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aIt;
92 for (aIt = aMsgGroups.cbegin(); aIt != aMsgGroups.cend(); aIt++) {
93 aList.append(myRoot->objectsDeleted(aIt->first, aIt->second.c_str()));
95 foreach(ModuleBase_ITreeNode* aNode, aList) {
97 aNode->parent()->update();
101 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
102 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
103 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
104 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
106 QObjectPtrList aCreated;
107 std::set<ObjectPtr>::const_iterator aIt;
108 bool aRebuildAll = false;
109 for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
110 ObjectPtr aObj = (*aIt);
111 if (!aObj->isInHistory())
114 if (aObj->data()->isValid()) {
115 if (aObj->groupName() == ModelAPI_Folder::group()) {
119 aCreated.append(*aIt);
127 foreach(ObjectPtr aObj, aCreated) {
128 ModuleBase_ITreeNode* aNode = myRoot->subNode(aObj);
130 int aOldNb = aNode->childrenCount();
132 int aNewNb = aNode->childrenCount();
134 QModelIndex aFirstIdx = getIndex(aNode, 0);
135 QModelIndex aLastIdx = getIndex(aNode, 2);
137 if (aNewNb > aOldNb) {
138 insertRows(aOldNb - 1, aNewNb - aOldNb, aFirstIdx);
140 else if (aNewNb < aOldNb) {
142 removeRows(aNewNb - 1, aOldNb - aNewNb, aFirstIdx);
144 removeRows(0, aOldNb, aFirstIdx);
146 dataChanged(aFirstIdx, aLastIdx);
151 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
152 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
153 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
154 if (aUpdMsg->reordered().get()) {
155 DocumentPtr aDoc = aUpdMsg->reordered()->document();
156 std::string aGroup = aUpdMsg->reordered()->group();
157 ModuleBase_ITreeNode* aNode = myRoot->findParent(aDoc, aGroup.c_str());
164 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
165 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
166 ModuleBase_ITreeNode* aRoot = myRoot->findRoot(aDoc);
168 updateSubTree(aRoot);
171 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
172 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
173 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
174 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
176 QObjectPtrList aCreated;
177 std::set<ObjectPtr>::const_iterator aIt;
178 bool aRebuildAll = false;
179 for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
180 ObjectPtr aObj = (*aIt);
181 if (aObj->groupName() == ModelAPI_ResultField::group()) {
182 aCreated.append(aObj);
185 foreach(ObjectPtr aObj, aCreated) {
186 ModuleBase_ITreeNode* aNode = myRoot->subNode(aObj);
188 int aOldNb = aNode->childrenCount();
190 int aNewNb = aNode->childrenCount();
192 QModelIndex aFirstIdx = getIndex(aNode, 0);
193 QModelIndex aLastIdx = getIndex(aNode, 2);
195 if (aNewNb > aOldNb) {
196 insertRows(aOldNb - 1, aNewNb - aOldNb, aFirstIdx);
198 else if (aNewNb < aOldNb) {
200 removeRows(aNewNb - 1, aOldNb - aNewNb, aFirstIdx);
202 removeRows(0, aOldNb, aFirstIdx);
204 dataChanged(aFirstIdx, aLastIdx);
210 //******************************************************
211 void XGUI_DataModel::clear()
217 //******************************************************
218 void XGUI_DataModel::rebuildDataTree()
225 //******************************************************
226 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
228 if (theIndex.isValid()) {
229 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
230 return aNode->object();
235 //******************************************************
236 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
238 ModuleBase_ITreeNode* aNode = myRoot->subNode(theObject);
240 return getIndex(aNode, theColumn);
242 return QModelIndex();
245 //******************************************************
246 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
248 if (theIndex.isValid()) {
249 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
250 return aNode->data(theIndex.column(), theRole);
255 //******************************************************
256 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
261 //******************************************************
262 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
264 ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
265 (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
266 return aParentNode->childrenCount();
269 //******************************************************
270 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
275 //******************************************************
276 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
278 ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
279 (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
280 ModuleBase_ITreeNode* aSubNode = aParentNode->subNode(theRow);
282 return createIndex(theRow, theColumn, aSubNode);
285 //******************************************************
286 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
288 if (theIndex.isValid()) {
289 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
290 return getParentIndex(aNode, 1);
292 return QModelIndex();
295 //******************************************************
296 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
298 ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
299 (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
300 return aParentNode->childrenCount() > 0;
303 //******************************************************
304 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
306 beginInsertRows(theParent, theRow, theRow + theCount - 1);
311 //******************************************************
312 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
314 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
319 //******************************************************
320 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
322 if (theIndex.isValid()) {
323 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
324 return aNode->flags(theIndex.column());
326 return Qt::ItemFlags();
330 //******************************************************
331 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
333 SessionPtr aSession = ModelAPI_Session::get();
334 DocumentPtr aRootDoc = aSession->moduleDocument();
335 if (theDoc == aRootDoc)
336 return QModelIndex();
338 ModuleBase_ITreeNode* aDocNode = 0;
339 foreach(ModuleBase_ITreeNode* aNode, myRoot->children()) {
340 if (aNode->document() == theDoc) {
346 return getIndex(aDocNode, theColumn);
348 return QModelIndex();
351 //******************************************************
352 bool XGUI_DataModel::hasHiddenState(const QModelIndex& theIndex)
354 if (theIndex.isValid()) {
355 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
356 return aNode->visibilityState() == ModuleBase_ITreeNode::Hidden;
361 //******************************************************
362 bool XGUI_DataModel::hasIndex(const QModelIndex& theIndex) const
364 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
365 return myRoot->hasSubNode(aNode);
368 //******************************************************
369 QModelIndex XGUI_DataModel::getParentIndex(ModuleBase_ITreeNode* theNode, int thCol) const
371 ModuleBase_ITreeNode* aParent = theNode->parent();
372 if (aParent == myRoot) {
373 return QModelIndex();
375 return getIndex(aParent, thCol);
379 //******************************************************
380 QModelIndex XGUI_DataModel::getIndex(ModuleBase_ITreeNode* theNode, int thCol) const
382 if (theNode == myRoot)
383 return QModelIndex();
384 int aRow = theNode->parent()->nodeRow(theNode);
385 return createIndex(aRow, thCol, theNode);
389 //******************************************************
390 void XGUI_DataModel::updateSubTree(ModuleBase_ITreeNode* theParent)
392 int aRows = theParent->childrenCount();
394 QModelIndex aParent = getIndex(theParent, 0);
395 QModelIndex aFirstIdx = aParent.child(0, 0);
396 QModelIndex aLastIdx = aParent.child(aRows - 1, 2);
397 dataChanged(aFirstIdx, aLastIdx);
402 //******************************************************
403 DocumentPtr XGUI_DataModel::document(const QModelIndex& theIndex) const
405 ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
406 return aNode->document();