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 insertRows(partFolderNode(), aStart, aStart);
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 insertRows(aIndex, aStart, aStart);
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 insertRows(aIndex, aStart, aStart);
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 beginRemoveRows(partFolderNode(), aStart, aStart);
88 removeSubModel(myPartModels.size() - 1);
90 } else { // Update top groups (other except parts
91 QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
92 int aStart = myModel->rowCount(aIndex);
93 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
94 beginRemoveRows(aIndex, aStart, aStart);
98 XGUI_PartModel* aPartModel = 0;
99 QList<XGUI_PartModel*>::const_iterator aIt;
100 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
101 if ((*aIt)->hasDocument(aDoc)) {
107 QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
108 int aStart = aPartModel->rowCount(aIndex);
109 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
110 beginRemoveRows(aIndex, aStart, aStart);
115 // Deleted object event ***********************
116 } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_UPDATED) {
117 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
118 boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
119 boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
122 emit dataChanged(aIndex, aIndex);
124 // Reset whole tree **************************
127 int aNbParts = myDocument->size(PARTS_GROUP);
128 if (myPartModels.size() != aNbParts) { // resize internal models
129 while (myPartModels.size() > aNbParts) {
130 delete myPartModels.last();
131 myPartModels.removeLast();
133 while (myPartModels.size() < aNbParts) {
134 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
136 for (int i = 0; i < myPartModels.size(); i++)
137 myPartModels.at(i)->setPartId(i);
144 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
146 if (!theIndex.isValid())
148 if (theIndex.internalId() == 0){
150 case Qt::DisplayRole:
151 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
152 case Qt::DecorationRole:
153 return QIcon(":pictures/constr_folder.png");
154 case Qt::ToolTipRole:
155 return tr("Parts folder");
160 QModelIndex aParent = theIndex.parent();
161 if (aParent.isValid() && (aParent.internalId() == 0)) {
162 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
164 return toSourceModelIndex(theIndex).data(theRole);
168 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
173 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
175 if (!theParent.isValid()) {
176 int aVal = myModel->rowCount(theParent) + myPartModels.size();
177 return myModel->rowCount(theParent) + 1;//myPartModels.size();
179 if (theParent.internalId() == 0) {
180 return myPartModels.size();
182 QModelIndex aParent = toSourceModelIndex(theParent);
183 if (!isSubModel(aParent.model()))
186 return aParent.model()->rowCount(aParent);
189 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
194 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
197 if (!theParent.isValid()) {
198 int aOffs = myModel->rowCount();
199 if (theRow < aOffs) {
200 aIndex = myModel->index(theRow, theColumn, theParent);
201 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
204 aIndex = partFolderNode();
207 if (theParent.internalId() == 0) {
208 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
210 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
211 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
213 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
219 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
221 if (theIndex.internalId() == 0)
222 return QModelIndex();
224 QModelIndex aIndex = toSourceModelIndex(theIndex);
225 const QAbstractItemModel* aModel = aIndex.model();
226 if (!isSubModel(aModel))
227 return QModelIndex();
229 if (isPartSubModel(aModel)) {
230 if (!aModel->parent(aIndex).isValid()) {
231 return partFolderNode();
235 aIndex = aModel->parent(aIndex);
236 if (aIndex.isValid())
237 return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
242 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
244 if (!theParent.isValid())
246 return rowCount(theParent) > 0;
250 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
252 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
257 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
259 QList<QModelIndex*>::const_iterator aIt;
260 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
261 QModelIndex* aIndex = (*aIt);
262 if ((*aIndex) == theIndex)
268 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
270 QModelIndex* aIndexPtr = findModelIndex(theIndex);
272 aIndexPtr = new QModelIndex(theIndex);
273 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
274 that->myIndexes.append(aIndexPtr);
279 void XGUI_DocumentDataModel::clearModelIndexes()
281 QList<QModelIndex*>::const_iterator aIt;
282 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
287 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
289 if (theIndex.internalId() == 0)
292 QModelIndex aIndex = toSourceModelIndex(theIndex);
293 if (!isSubModel(aIndex.model()))
296 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
297 return aModel->feature(aIndex);
300 void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
302 beginInsertRows(theParent, theStart, theEnd);
304 if (theStart == 0) // Update parent if this is a first child in order to update node decoration
305 emit dataChanged(theParent, theParent);
308 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
310 XGUI_PartModel* aModel = myPartModels.at(theModelId);
312 for (int i = 0; i < myIndexes.size(); i++) {
313 if (myIndexes.at(i)->model() == aModel)
317 while(aToRemove.size() > 0) {
318 aId = aToRemove.last();
319 delete myIndexes.at(aId);
320 myIndexes.removeAt(aId);
321 aToRemove.removeLast();
324 myPartModels.removeAt(theModelId);
327 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
329 if (theModel == myModel)
331 return isPartSubModel(theModel);
334 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
336 return myPartModels.contains((XGUI_PartModel*)theModel);
339 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
341 int aPos = myModel->rowCount(QModelIndex());
342 return createIndex(aPos, columnCount() - 1, 0);