]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_DataModel.cpp
Salome HOME
935b74ec40a6094d91a30da94a27e69cfdb86cbd
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModuleBase_IDocumentDataModel.cpp
4 // Created:     28 Apr 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "XGUI_DataModel.h"
8
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Events.h>
12
13 #include <Config_FeatureMessage.h>
14
15 #include <Events_Loop.h>
16
17 #include <QIcon>
18
19 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
20 {
21   myXMLReader.readAll();
22
23   Events_Loop* aLoop = Events_Loop::loop();
24   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
25   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
26   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
27   aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
28 }
29
30 //******************************************************
31 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
32 {
33   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
34   std::string aRootType = myXMLReader.rootType();
35   int aNbFolders = myXMLReader.rootFoldersNumber();
36
37   // Created object event *******************
38   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
39     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
40         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
41     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
42
43     std::set<ObjectPtr>::const_iterator aIt;
44     std::string aObjType;
45     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
46       ObjectPtr aObject = (*aIt);
47       aObjType = aObject->groupName();
48       DocumentPtr aDoc = aObject->document();
49       if (aDoc == aRootDoc) {
50         int aRow = aRootDoc->size(aObjType) - 1;
51         if (aRow != -1) {
52           if (aObjType == aRootType) {
53             insertRow(aRow + aNbFolders);
54           } else {
55             int aFolderId = myXMLReader.rootFolderId(aObjType);
56             if (aFolderId != -1) {
57               insertRow(aRow, createIndex(aFolderId, 0, -1));
58             }
59           } 
60         }
61       }
62     }
63   }
64 }
65
66 //******************************************************
67 void XGUI_DataModel::clear()
68 {
69
70 }
71
72 //******************************************************
73 void XGUI_DataModel::rebuildDataTree()
74 {
75
76 }
77
78 //******************************************************
79 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
80 {
81   return ObjectPtr();
82 }
83
84 //******************************************************
85 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
86 {
87   //std::string aType = theObject->groupName();
88   //DocumentPtr aDoc = theObject->document();
89   //int aRow = aDoc->index(theObject);
90   //if (aRow == -1)
91     return QModelIndex();
92   //return createIndex(aRow, 0, theObject.get());
93 }
94
95 //******************************************************
96 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
97 {
98   SessionPtr aSession = ModelAPI_Session::get();
99   DocumentPtr aRootDoc = aSession->moduleDocument();
100   int aNbFolders = myXMLReader.rootFoldersNumber();
101   int theIndexRow = theIndex.row();
102
103   if ((theIndex.column() == 1) ) {
104     //if (theIndexRow >= aNbFolders) {
105     //  if (theRole == Qt::DecorationRole) {
106     //    return QIcon(":pictures/arrow.png");
107     //  }
108     //}
109     return QVariant();
110   }
111
112   int aParentRow = theIndex.internalId();
113   if (aParentRow == -1) { // First level of items
114     if (theIndexRow < aNbFolders) { // A folder
115       switch (theRole) {
116         case Qt::DisplayRole:
117           return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) + 
118             QString(" (%1)").arg(rowCount(theIndex));
119         case Qt::DecorationRole:
120           return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
121       }
122     } else {
123       ObjectPtr aObj = aRootDoc->object(myXMLReader.rootType(), theIndexRow - aNbFolders);
124       switch (theRole) {
125         case Qt::DisplayRole:
126           return aObj->data()->name().c_str();
127         //case Qt::DecorationRole:
128         //  return featureIcon(aFeature);
129       }
130     }
131   } else { // Content of folders
132     if (aParentRow < aNbFolders) {
133       std::string aType = myXMLReader.rootFolderType(aParentRow);
134       ObjectPtr aObj = aRootDoc->object(aType, theIndexRow);
135       switch (theRole) {
136         case Qt::DisplayRole:
137           return aObj->data()->name().c_str();
138       }
139     }
140   }
141   return QVariant();
142 }
143
144 //******************************************************
145 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
146 {
147   return QVariant();
148 }
149
150 //******************************************************
151 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
152 {
153   SessionPtr aSession = ModelAPI_Session::get();
154   if (!aSession->hasModuleDocument())
155     return 0;
156   DocumentPtr aRootDoc = aSession->moduleDocument();
157   int aNbFolders = myXMLReader.rootFoldersNumber();
158
159   int aNbItems = 0;
160   std::string aType = myXMLReader.rootType();
161   if (!aType.empty())
162     aNbItems = aRootDoc->size(aType);
163
164   if (!theParent.isValid())
165     return aNbFolders + aNbItems;
166
167   int aParentPos = theParent.row();
168   if (aParentPos < aNbFolders) {
169     // This is number of items under folder
170     aType = myXMLReader.rootFolderType(aParentPos);
171     return aRootDoc->size(aType);
172   }
173   return 0;
174 }
175
176 //******************************************************
177 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
178 {
179   return 2;
180 }
181
182 //******************************************************
183 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
184 {
185   if (!theParent.isValid())
186     return createIndex(theRow, theColumn, -1);
187
188   return createIndex(theRow, theColumn, theParent.row());
189 }
190
191 //******************************************************
192 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
193 {
194   if (theIndex.isValid() && (theIndex.internalId() != -1)) {
195     return createIndex(theIndex.internalId(), theIndex.column(), -1);
196   }
197   return QModelIndex();
198 }
199
200 //******************************************************
201 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
202 {
203   int aNbFolders = myXMLReader.rootFoldersNumber();
204   if (!theParent.isValid() && aNbFolders)
205     return true;
206   if (theParent.internalId() == -1) {
207     if (theParent.row() < aNbFolders) {
208       std::string aType = myXMLReader.rootFolderType(theParent.row());
209       if (!aType.empty()) {
210         SessionPtr aSession = ModelAPI_Session::get();
211         DocumentPtr aRootDoc = aSession->moduleDocument();
212         return aRootDoc->size(aType) > 0;
213       }
214     }
215   }
216   return false;
217 }
218
219 //******************************************************
220 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
221 {
222   beginInsertRows(theParent, theRow, theRow + theCount - 1);
223   endInsertRows();
224
225   return true;
226 }
227
228 //******************************************************
229 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
230 {
231   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
232   endRemoveRows();
233   return true;
234 }