]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.0
authorvsv <vitaly.smetannikov@opencascade.com>
Tue, 19 May 2015 09:41:32 +0000 (12:41 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Tue, 19 May 2015 09:41:32 +0000 (12:41 +0300)
src/PartSet/PartSet_DataTreeModel.h
src/PartSet/PartSet_DocumentDataModel.cpp
src/PartSet/PartSet_DocumentDataModel.h
src/PartSet/PartSet_Module.cpp
src/PartSet/PartSet_PartDataModel.cpp
src/PartSet/PartSet_PartDataModel.h

index da29dea26de70fed3a298a8eff965495505b4ac2..ff96baf9b6deb42ce92800a1076806d7fdc51289 100644 (file)
@@ -8,6 +8,7 @@
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Session.h>
 
 #include <QAbstractItemModel>
 #include <QColor>
@@ -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
index bf29598a0b2c9d1a11606220617323d6eded2039..cd12d6ed9871a9d295f7dc08d6ed28fa2b70df21 100644 (file)
@@ -36,7 +36,7 @@ QMap<QString, QString> 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_ptr<Events_Messag
             FeaturePtr aPartFeature = ModelAPI_Feature::feature(aObject);
             PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this);
             int anId = aRootDoc->index(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_ptr<Events_Messag
       std::string aGroup = (*aIt);
       if (aDoc == aRootDoc) {  // If root objects
         if (aGroup == ModelAPI_ResultPart::group()) {  // Update only Parts group
-          ObjectPtr aObj;
-          FeaturePtr aFeature;
-          int aDelId = -1;
-          foreach (int aId, myPartModels.keys()) {
-            aObj = aDoc->object(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<PartSet_PartModel*> 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<ModelAPI_Feature>(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<const PartSet_PartModel*>(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<QTreeView*>(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
index 6692d085add1cd8b91147b4f77465914e15e3af0..360a1d686d71cf83e742fd53a9f6a449c5910644 100644 (file)
@@ -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<int, PartSet_PartModel*> myPartModels;
+  QList<PartSet_PartModel*> myPartModels;
 
   //! Active part in part editing mode
-  int myActivePartId;
+  PartSet_PartModel* myActivePartModel;
 
   QModelIndex myActivePartIndex;
 
index e94be9298c944b36d7da6b72b9acda9a8557f402..bb7c17e30ba51dda841546214d124e816507f806 100644 (file)
@@ -679,7 +679,7 @@ void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(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
index 402244d852d7bda4b1d6c94dc3f930557a5d3fc8..d124d80fc5272b0b2955a1074a38da7999387e5f 100644 (file)
@@ -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<ModelAPI_ResultPart>(aFeature->firstResult()); 
+  ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(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<ModelAPI_ResultPart>(aObj);
-}
-
 QModelIndex PartSet_PartDataModel::objectIndex(const ObjectPtr& theObject) const
 {
   QModelIndex aIndex;
index 08be66b308fbe523d5a3f61199fe5bdbd635cfa5..9789f79d8e940a84b7754fe3351f3f0b7d7e179e 100644 (file)
@@ -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);