1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
4 #include <ModelAPI_PluginManager.h>
5 #include <ModelAPI_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Data.h>
8 #include <Model_Events.h>
10 #include <Events_Loop.h>
17 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
18 : QAbstractItemModel(theParent)
20 // Find Document object
21 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
22 myDocument = aMgr->currentDocument();
24 // Register in event loop
25 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
26 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
27 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
29 // Create a top part of data tree model
30 myModel = new XGUI_TopDataModel(myDocument, this);
34 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
40 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
42 // Created object event *******************
43 if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
44 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
45 boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
46 boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
48 if (aDoc == myDocument) { // If root objects
49 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
51 int aStart = myPartModels.size() + 1;
52 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
53 aModel->setPartId(myPartModels.count());
54 myPartModels.append(aModel);
55 insertRow(aStart, partFolderNode());
56 } else { // Update top groups (other except parts
57 QModelIndex aIndex = myModel->findParent(aFeature);
58 int aStart = myModel->rowCount(aIndex) - 1;
59 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
60 insertRow(aStart, aIndex);
62 } else { // if sub-objects of first level nodes
63 XGUI_PartModel* aPartModel = 0;
64 QList<XGUI_PartModel*>::const_iterator aIt;
65 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
66 if ((*aIt)->hasDocument(aDoc)) {
72 QModelIndex aIndex = aPartModel->findParent(aFeature);
73 int aStart = aPartModel->rowCount(aIndex) - 1;
74 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
75 insertRow(aStart, aIndex);
79 // Deleted object event ***********************
80 } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
81 const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
82 boost::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
84 if (aDoc == myDocument) { // If root objects
85 if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
86 int aStart = myPartModels.size();
87 removeSubModel(myPartModels.size() - 1);
88 removeRow(aStart - 1, partFolderNode());
89 } else { // Update top groups (other except parts
90 QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
91 int aStart = myModel->rowCount(aIndex);
92 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
93 removeRow(aStart - 1, aIndex);
96 XGUI_PartModel* aPartModel = 0;
97 QList<XGUI_PartModel*>::const_iterator aIt;
98 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
99 if ((*aIt)->hasDocument(aDoc)) {
105 QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
106 int aStart = aPartModel->rowCount(aIndex);
107 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
108 removeRow(aStart - 1, aIndex);
112 // Deleted object event ***********************
113 } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_UPDATED) {
114 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
115 boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
116 boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
119 emit dataChanged(aIndex, aIndex);
121 // Reset whole tree **************************
124 int aNbParts = myDocument->size(PARTS_GROUP);
125 if (myPartModels.size() != aNbParts) { // resize internal models
126 while (myPartModels.size() > aNbParts) {
127 delete myPartModels.last();
128 myPartModels.removeLast();
130 while (myPartModels.size() < aNbParts) {
131 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
133 for (int i = 0; i < myPartModels.size(); i++)
134 myPartModels.at(i)->setPartId(i);
141 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
143 if (!theIndex.isValid())
145 switch (theIndex.internalId()) {
148 case Qt::DisplayRole:
149 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
150 case Qt::DecorationRole:
151 return QIcon(":pictures/constr_folder.png");
152 case Qt::ToolTipRole:
153 return tr("Parts folder");
160 int aOffset = historyOffset();
161 FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
163 case Qt::DisplayRole:
165 return aFeature->data()->getName().c_str();
168 case Qt::DecorationRole:
170 std::string aType = aFeature->getKind();
171 if (aType.compare("Point") == 0)
172 return QIcon(":pictures/point_ico.png");
173 if (aType.compare("Part") == 0)
174 return QIcon(":pictures/part_ico.png");
175 if (aType.compare("Sketch") == 0)
176 return QIcon(":icons/sketch.png");
178 case Qt::ToolTipRole:
179 return tr("Feature object");
186 QModelIndex aParent = theIndex.parent();
187 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
188 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
190 return toSourceModelIndex(theIndex).data(theRole);
194 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
199 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
201 if (!theParent.isValid()) {
202 // Size of external models
203 int aVal = historyOffset();
205 aVal += myDocument->size(FEATURES_GROUP);
208 if (theParent.internalId() == PartsFolder) {
209 return myPartModels.size();
211 if (theParent.internalId() == HistoryNode) {
214 QModelIndex aParent = toSourceModelIndex(theParent);
215 if (!isSubModel(aParent.model()))
218 return aParent.model()->rowCount(aParent);
221 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
226 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
229 if (!theParent.isValid()) {
230 int aOffs = myModel->rowCount();
231 if (theRow < aOffs) {
232 aIndex = myModel->index(theRow, theColumn, theParent);
233 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
235 if (theRow == aOffs) // Create Parts node
236 aIndex = partFolderNode();
237 else // create history node
238 aIndex = createIndex(theRow, theColumn, HistoryNode);
241 if (theParent.internalId() == PartsFolder) {
242 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
244 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
245 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
247 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
253 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
255 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
256 return QModelIndex();
258 QModelIndex aIndex = toSourceModelIndex(theIndex);
259 const QAbstractItemModel* aModel = aIndex.model();
260 if (!isSubModel(aModel))
261 return QModelIndex();
263 if (isPartSubModel(aModel)) {
264 if (!aModel->parent(aIndex).isValid()) {
265 return partFolderNode();
269 aIndex = aModel->parent(aIndex);
270 if (aIndex.isValid())
271 return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
276 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
278 if (!theParent.isValid())
280 return rowCount(theParent) > 0;
284 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
286 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
291 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
293 QList<QModelIndex*>::const_iterator aIt;
294 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
295 QModelIndex* aIndex = (*aIt);
296 if ((*aIndex) == theIndex)
302 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
304 QModelIndex* aIndexPtr = findModelIndex(theIndex);
306 aIndexPtr = new QModelIndex(theIndex);
307 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
308 that->myIndexes.append(aIndexPtr);
313 void XGUI_DocumentDataModel::clearModelIndexes()
315 QList<QModelIndex*>::const_iterator aIt;
316 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
321 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
323 if (theIndex.internalId() == PartsFolder)
326 QModelIndex aIndex = toSourceModelIndex(theIndex);
327 if (!isSubModel(aIndex.model()))
330 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
331 return aModel->feature(aIndex);
334 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
336 beginInsertRows(theParent, theRow, theRow + theCount - 1);
341 int aRow = rowCount(aRoot);
342 beginInsertRows(aRoot, aRow, aRow);
348 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
350 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
356 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
358 XGUI_PartModel* aModel = myPartModels.at(theModelId);
360 for (int i = 0; i < myIndexes.size(); i++) {
361 if (myIndexes.at(i)->model() == aModel)
365 while(aToRemove.size() > 0) {
366 aId = aToRemove.last();
367 delete myIndexes.at(aId);
368 myIndexes.removeAt(aId);
369 aToRemove.removeLast();
372 myPartModels.removeAt(theModelId);
375 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
377 if (theModel == myModel)
379 return isPartSubModel(theModel);
382 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
384 return myPartModels.contains((XGUI_PartModel*)theModel);
387 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
389 int aPos = myModel->rowCount(QModelIndex());
390 return createIndex(aPos, columnCount() - 1, PartsFolder);
393 int XGUI_DocumentDataModel::historyOffset() const
395 // Nb of rows of top model + Parts folder
396 return myModel->rowCount(QModelIndex()) + 1;