From 82cec10b6f678f068e1d3fbc0243a374a18c7afe Mon Sep 17 00:00:00 2001 From: vsv Date: Tue, 19 May 2015 12:41:24 +0300 Subject: [PATCH] Take into account that position of Part feature could be changed --- src/PartSet/PartSet_DataTreeModel.h | 19 ++-- src/PartSet/PartSet_DocumentDataModel.cpp | 126 ++++++++++++---------- src/PartSet/PartSet_DocumentDataModel.h | 11 +- src/PartSet/PartSet_Module.cpp | 2 +- src/PartSet/PartSet_PartDataModel.cpp | 12 +-- src/PartSet/PartSet_PartDataModel.h | 3 - 6 files changed, 92 insertions(+), 81 deletions(-) diff --git a/src/PartSet/PartSet_DataTreeModel.h b/src/PartSet/PartSet_DataTreeModel.h index da29dea26..ff96baf9b 100644 --- a/src/PartSet/PartSet_DataTreeModel.h +++ b/src/PartSet/PartSet_DataTreeModel.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -70,30 +71,34 @@ class PartSet_PartModel : public PartSet_FeaturesModel /// Constructor /// \param theParent a parent object PartSet_PartModel(QObject* theParent) - : PartSet_FeaturesModel(theParent), myId(-1) + : PartSet_FeaturesModel(theParent) { } /// Set part id /// \param theId a new id - void setPartId(int theId) + void setPart(FeaturePtr thePart) { - myId = theId; + myPart = thePart; } /// Returns Id of the part - int partId() const { return myId; } + FeaturePtr part() const { return myPart; } //! Returns true if the given document is a sub-document of this tree //! \param theDoc a document to check virtual bool hasDocument(const DocumentPtr& theDoc) const = 0; - //! Return a Part object - virtual ResultPartPtr part() const = 0; + /// Returns position of the part in history + int position() const + { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + return aRootDoc->index(myPart); + } protected: //! Id of the current part object in the document - int myId; + FeaturePtr myPart; }; #endif diff --git a/src/PartSet/PartSet_DocumentDataModel.cpp b/src/PartSet/PartSet_DocumentDataModel.cpp index bf29598a0..cd12d6ed9 100644 --- a/src/PartSet/PartSet_DocumentDataModel.cpp +++ b/src/PartSet/PartSet_DocumentDataModel.cpp @@ -36,7 +36,7 @@ QMap PartSet_DocumentDataModel::myIcons; PartSet_DocumentDataModel::PartSet_DocumentDataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent), - myActivePartId(-1) + myActivePartModel(0) { // Create a top part of data tree model myModel = new PartSet_TopDataModel(this); @@ -82,8 +82,8 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptrindex(aPartFeature); - aModel->setPartId(anId); - myPartModels[anId] = aModel; + aModel->setPart(aPartFeature); + myPartModels.append(aModel); insertRow(aStart, partFolderNode(0)); } } else { // Update top groups (other except parts @@ -123,25 +123,17 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptrobject(ModelAPI_Feature::group(), aId); - if (aObj.get()) { - aFeature = ModelAPI_Feature::feature(aObj); - if (aFeature.get()) { - if (aFeature->getKind() == PartSetPlugin_Part::ID()) - continue; - } + PartSet_PartModel* aDelPartModel = 0; + foreach (PartSet_PartModel* aPartModel, myPartModels) { + if (aPartModel->position() == -1) { + aDelPartModel = aPartModel; + break; } - aDelId = aId; - break; } - if (aDelId != -1) { + if (aDelPartModel) { deactivatePart(); int aStart = myPartModels.size() - 1; - removeSubModel(aDelId); + removeSubModel(aDelPartModel); removeRow(aStart, partFolderNode(0)); } } else { // Update top groups (other except parts @@ -201,21 +193,13 @@ void PartSet_DocumentDataModel::rebuildDataTree() // Delete extra models ObjectPtr aObj; FeaturePtr aFeature; - QIntList aDelList; - foreach (int aId, myPartModels.keys()) { - aObj = aRootDoc->object(ModelAPI_Feature::group(), aId); - if (aObj.get()) { - aFeature = ModelAPI_Feature::feature(aObj); - if (aFeature.get()) { - if (aFeature->getKind() == PartSetPlugin_Part::ID()) - continue; - } - } - aDelList.append(aId); - break; + QList aDelList; + foreach (PartSet_PartModel* aPartModel, myPartModels) { + if (aPartModel->position() == -1) + aDelList.append(aPartModel); } - foreach (int aId, aDelList) { - removeSubModel(aId); + foreach (PartSet_PartModel* aPartModel, aDelList) { + removeSubModel(aPartModel); } // Add non existing models int aHistNb = aRootDoc->size(ModelAPI_Feature::group()); @@ -223,10 +207,10 @@ void PartSet_DocumentDataModel::rebuildDataTree() aObj = aRootDoc->object(ModelAPI_Feature::group(), i); aFeature = std::dynamic_pointer_cast(aObj); if (aFeature->getKind() == PartSetPlugin_Part::ID()) { - if (!myPartModels.contains(i)) { + if (!findPartModel(aFeature)) { PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this); - aModel->setPartId(i); - myPartModels[i] = aModel; + aModel->setPart(aFeature); + myPartModels.append(aModel); } } } @@ -336,7 +320,7 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol if (aParent.internalId() == HistoryNode) { int aId = aParent.row() - historyOffset(); QModelIndex* aIndex = toSourceModelIndex(theIndex); - return myPartModels[aId]->data(*aIndex, theRole); + return findPartModel(aId)->data(*aIndex, theRole); } return toSourceModelIndex(theIndex)->data(theRole); } @@ -364,8 +348,9 @@ int PartSet_DocumentDataModel::rowCount(const QModelIndex& theParent) const } if (theParent.internalId() == HistoryNode) { int aId = theParent.row() - historyOffset(); - if (myPartModels.contains(aId)) - return myPartModels[aId]->rowCount(QModelIndex()); + PartSet_PartModel* aModel = findPartModel(aId); + if (aModel) + return aModel->rowCount(QModelIndex()); return 0; } if (theParent.internalId() == PartResult) @@ -411,7 +396,7 @@ QModelIndex PartSet_DocumentDataModel::index(int theRow, int theColumn, } else { if (theParent.internalId() == HistoryNode) { int aId = theParent.row() - historyOffset(); - aIndex = myPartModels[aId]->index(theRow, theColumn, QModelIndex()); + aIndex = findPartModel(aId)->index(theRow, theColumn, QModelIndex()); } else { QModelIndex* aParent = (QModelIndex*) theParent.internalPointer(); aIndex = aParent->model()->index(theRow, theColumn, (*aParent)); @@ -436,8 +421,9 @@ QModelIndex PartSet_DocumentDataModel::parent(const QModelIndex& theIndex) const return QModelIndex(); QModelIndex aIndex1 = aModel->parent(*aIndex); - if (isPartSubModel(aModel) && (!aIndex1.isValid())) { - int aId = myPartModels.key((PartSet_PartModel*) aModel); + const PartSet_PartModel* aPartModel = dynamic_cast(aModel); + if (aPartModel && (!aIndex1.isValid())) { + int aId = aPartModel->position(); int aRow = aId + historyOffset(); return createIndex(aRow, 0, (qint32) HistoryNode); } @@ -539,10 +525,15 @@ bool PartSet_DocumentDataModel::removeRows(int theRow, int theCount, const QMode void PartSet_DocumentDataModel::removeSubModel(int theModelId) { - PartSet_PartModel* aModel = myPartModels[theModelId]; + PartSet_PartModel* aModel = myPartModels.at(theModelId); + removeSubModel(aModel); +} + +void PartSet_DocumentDataModel::removeSubModel(PartSet_PartModel* theModel) +{ QIntList aToRemove; for (int i = 0; i < myIndexes.size(); i++) { - if (myIndexes.at(i)->model() == aModel) + if (myIndexes.at(i)->model() == theModel) aToRemove.append(i); } int aId; @@ -552,10 +543,11 @@ void PartSet_DocumentDataModel::removeSubModel(int theModelId) myIndexes.removeAt(aId); aToRemove.removeLast(); } - delete aModel; - myPartModels.remove(theModelId); + delete theModel; + myPartModels.removeAll(theModel); } + bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const { if (theModel == myModel) @@ -565,7 +557,7 @@ bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) c bool PartSet_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const { - return myPartModels.key((PartSet_PartModel*) theModel, -1) != -1; + return myPartModels.contains((PartSet_PartModel*) theModel); } QModelIndex PartSet_DocumentDataModel::partFolderNode(int theColumn) const @@ -588,15 +580,16 @@ bool PartSet_DocumentDataModel::activatePart(const QModelIndex& theIndex) if (theIndex.isValid() && (theIndex.internalId() == PartResult)) { myActivePartIndex = theIndex; myModel->setItemsColor(PASSIVE_COLOR); - if (myActivePartId != -1) - myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR); + if (myActivePartModel) + myActivePartModel->setItemsColor(PASSIVE_COLOR); // Find activated part feature by its ID ResultPartPtr aPartRes = activePart(); FeaturePtr aFeature = ModelAPI_Feature::feature(aPartRes); - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - myActivePartId = aRootDoc->index(aFeature); - myPartModels[myActivePartId]->setItemsColor(ACTIVE_COLOR); + if (aFeature.get()) { + myActivePartModel = findPartModel(aFeature); + myActivePartModel->setItemsColor(ACTIVE_COLOR); + } } return true; } @@ -613,8 +606,8 @@ ResultPartPtr PartSet_DocumentDataModel::activePart() const QModelIndex PartSet_DocumentDataModel::activePartTree() const { - if (myActivePartId != -1) { - return createIndex(myActivePartId + historyOffset(), 0, HistoryNode); + if (myActivePartModel) { + return createIndex(myActivePartModel->position() + historyOffset(), 0, HistoryNode); } return QModelIndex(); } @@ -622,9 +615,9 @@ QModelIndex PartSet_DocumentDataModel::activePartTree() const void PartSet_DocumentDataModel::deactivatePart() { if (myActivePartIndex.isValid()) { - if (myActivePartId != -1) - myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR); - myActivePartId = -1; + if (myActivePartModel) + myActivePartModel->setItemsColor(PASSIVE_COLOR); + myActivePartModel = 0; myActivePartIndex = QModelIndex(); myModel->setItemsColor(ACTIVE_COLOR); } @@ -790,7 +783,7 @@ void PartSet_DocumentDataModel::onMouseDoubleClick(const QModelIndex& theIndex) return; QTreeView* aTreeView = dynamic_cast(sender()); if ((theIndex.internalId() >= PartsFolder) && (theIndex.internalId() <= PartResult)) { - if (myActivePartId != -1) + if (myActivePartModel) // It means that the root document is not active return; QModelIndex aNewIndex; @@ -826,3 +819,22 @@ void PartSet_DocumentDataModel::onMouseDoubleClick(const QModelIndex& theIndex) } } } + + +PartSet_PartModel* PartSet_DocumentDataModel::findPartModel(FeaturePtr thePart) const +{ + foreach (PartSet_PartModel* aModel, myPartModels) { + if (aModel->part() == thePart) + return aModel; + } + return 0; +} + +PartSet_PartModel* PartSet_DocumentDataModel::findPartModel(int thePosition) const +{ + foreach (PartSet_PartModel* aModel, myPartModels) { + if (aModel->position() == thePosition) + return aModel; + } + return 0; +} \ No newline at end of file diff --git a/src/PartSet/PartSet_DocumentDataModel.h b/src/PartSet/PartSet_DocumentDataModel.h index 6692d085a..360a1d686 100644 --- a/src/PartSet/PartSet_DocumentDataModel.h +++ b/src/PartSet/PartSet_DocumentDataModel.h @@ -169,6 +169,9 @@ Q_OBJECT //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model void removeSubModel(int theModelId); + //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model + void removeSubModel(PartSet_PartModel* theModel); + //! Returns true if the given model is a one of sub-models (of both types) bool isSubModel(const QAbstractItemModel* theModel) const; @@ -183,14 +186,18 @@ Q_OBJECT int historyOffset() const; + PartSet_PartModel* findPartModel(FeaturePtr thePart) const; + + PartSet_PartModel* findPartModel(int thePosition) const; + //! Data model of top part of data tree (not parts object) PartSet_TopDataModel* myModel; //! Data models for Parts data tree representation (one data model per a one part) - QMap myPartModels; + QList myPartModels; //! Active part in part editing mode - int myActivePartId; + PartSet_PartModel* myActivePartModel; QModelIndex myActivePartIndex; diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 6cc3f94e8..61cc5145f 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -678,7 +678,7 @@ void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const aPart = std::dynamic_pointer_cast(aPartFeature->firstResult()); } if (aPart.get()) // this may be null is Part feature is disabled - aPartDoc; + aPartDoc = aPart->partDoc(); if (aMgr->activeDocument() == aPartDoc) theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD")); else diff --git a/src/PartSet/PartSet_PartDataModel.cpp b/src/PartSet/PartSet_PartDataModel.cpp index 402244d85..d124d80fc 100644 --- a/src/PartSet/PartSet_PartDataModel.cpp +++ b/src/PartSet/PartSet_PartDataModel.cpp @@ -468,10 +468,7 @@ bool PartSet_PartDataModel::hasChildren(const QModelIndex& theParent) const DocumentPtr PartSet_PartDataModel::partDocument() const { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - ObjectPtr aObject = aRootDoc->object(ModelAPI_Feature::group(), myId); - FeaturePtr aFeature = ModelAPI_Feature::feature(aObject); - ResultPartPtr aPart = std::dynamic_pointer_cast(aFeature->firstResult()); + ResultPartPtr aPart = std::dynamic_pointer_cast(myPart->firstResult()); if (aPart.get()) // this may be null is Part feature is disabled return aPart->partDoc(); return DocumentPtr(); @@ -523,13 +520,6 @@ QModelIndex PartSet_PartDataModel::findGroup(const std::string& theGroup) const return QModelIndex(); } -ResultPartPtr PartSet_PartDataModel::part() const -{ - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - ObjectPtr aObj = aRootDoc->object(ModelAPI_ResultPart::group(), myId); - return std::dynamic_pointer_cast(aObj); -} - QModelIndex PartSet_PartDataModel::objectIndex(const ObjectPtr& theObject) const { QModelIndex aIndex; diff --git a/src/PartSet/PartSet_PartDataModel.h b/src/PartSet/PartSet_PartDataModel.h index 08be66b30..9789f79d8 100644 --- a/src/PartSet/PartSet_PartDataModel.h +++ b/src/PartSet/PartSet_PartDataModel.h @@ -164,9 +164,6 @@ Q_OBJECT //! Returns index corresponded to the group virtual QModelIndex findGroup(const std::string& theGroup) const; - //! Return a Part object - virtual ResultPartPtr part() const; - //! Set an Index which will be considered as a last history index //! \param theIndex a last index for history void setLastHistoryItem(const QModelIndex& theIndex); -- 2.39.2