Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_PartDataModel.cpp
index 40f4671db277b9c204dc6da0ad184043f3f83b8f..ced60958b38a5855824c27001d6c39c258c155c2 100644 (file)
@@ -1,16 +1,28 @@
 #include "XGUI_PartDataModel.h"
+#include "XGUI_Workshop.h"
 
 #include <ModelAPI_PluginManager.h>
-#include <ModelAPI_Iterator.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_AttributeDocRef.h>
+#include <ModelAPI_Object.h>
 
 #include <QIcon>
+#include <QBrush>
 
-XGUI_TopDataModel::XGUI_TopDataModel(const std::shared_ptr<ModelAPI_Document>& theDocument, QObject* theParent)
-  : XGUI_FeaturesModel(theDocument, theParent)
+
+//FeaturePtr featureObj(const FeaturePtr& theFeature)
+//{
+//  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
+//  if (aObject)
+//    return aObject->featureRef();
+//  return theFeature;
+//}
+
+
+XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
+  : XGUI_FeaturesModel(theParent)
 {
 }
   
@@ -26,20 +38,22 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
     // return a name
     switch (theIndex.internalId()) {
     case ParamsFolder:
-      return tr("Parameters");
+      return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
     case ParamObject:
       {
-        std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row());
+        DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+        FeaturePtr aFeature = aRootDoc->feature(PARAMETERS_GROUP, theIndex.row());
         if (aFeature)
-          return aFeature->data()->getName().c_str();
+          return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
       } 
     case ConstructFolder:
-        return tr("Constructions");
+        return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
     case ConstructObject:
       {
-        std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+        DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+        FeaturePtr aFeature = aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
         if (aFeature)
-          return aFeature->data()->getName().c_str();
+          return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
       }
     }
     break;
@@ -48,17 +62,25 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
     // return an Icon
     switch (theIndex.internalId()) {
     case ParamsFolder:
-        return QIcon(":pictures/params_folder.png");
+      return QIcon(":pictures/params_folder.png");
     case ConstructFolder:
-        return QIcon(":pictures/constr_folder.png");
+      return QIcon(":pictures/constr_folder.png");
     case ConstructObject:
-        return QIcon(":pictures/point_ico.png");
+      {
+        DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+        FeaturePtr aFeature = aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+        if (aFeature)
+          return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
+      }
     }
     break;
 
   case Qt::ToolTipRole:
     // return Tooltip
     break;
+  case Qt::ForegroundRole:
+    return QBrush(myItemsColor);
+    break;
   }
   return QVariant();
 }
@@ -73,11 +95,12 @@ int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
   if (!theParent.isValid()) 
     return 2;
 
+  DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
   if (theParent.internalId() == ParamsFolder)
-    return myDocument->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
+    return aRootDoc->size(PARAMETERS_GROUP);
 
   if (theParent.internalId() == ConstructFolder)
-    return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
+    return aRootDoc->size(CONSTRUCTIONS_GROUP);
 
   return 0;
 }
@@ -133,40 +156,65 @@ FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
   case ConstructFolder:
     return FeaturePtr();
   case ParamObject:
-    return myDocument->feature(PARAMETERS_GROUP, theIndex.row());
+    {
+      DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+      return aRootDoc->feature(PARAMETERS_GROUP, theIndex.row());
+    }
   case ConstructObject:
-    return myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+    {
+      DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+      return aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+    }
   }
   return FeaturePtr();
 }
 
 
-QModelIndex XGUI_TopDataModel::findParent(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
+QModelIndex XGUI_TopDataModel::findParent(const FeaturePtr& theFeature) const
 {
-  QString aGroup(theFeature->getGroup().c_str());
-
-  if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0)
-    return createIndex(0, 0, (quintptr) ParamsFolder);
-  if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0)
-    return createIndex(1, 0, (quintptr) ConstructFolder);
-  return QModelIndex();
+  return findGroup(theFeature->getGroup().c_str());
 }
 
 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
 {
   if (theGroup.compare(PARAMETERS_GROUP) == 0)
-    return createIndex(0, 0, (quintptr) ParamsFolder);
+    return createIndex(0, 0, (qint32) ParamsFolder);
   if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
-    return createIndex(1, 0, (quintptr) ConstructFolder);
+    return createIndex(1, 0, (qint32) ConstructFolder);
   return QModelIndex();
 }
 
+QModelIndex XGUI_TopDataModel::featureIndex(const FeaturePtr& theFeature) const
+{
+  QModelIndex aIndex;
+  if (theFeature) {
+    DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+    std::string aGroup = theFeature->getGroup();
+    int aNb = aRootDoc->size(aGroup);
+    int aRow = -1;
+    for (int i = 0; i < aNb; i++) {
+      if (aRootDoc->feature(aGroup, i) == theFeature) {
+        aRow = i;
+        break;
+      }
+    }
+    if (aRow != -1) {
+      if (aGroup.compare(PARAMETERS_GROUP) == 0)
+        return createIndex(aRow, 0, (qint32) ParamObject);
+      if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
+        return createIndex(aRow, 0, (qint32) ConstructObject);
+    }
+  }
+  return aIndex;
+}
+
+
 
 //******************************************************************
 //******************************************************************
 //******************************************************************
-XGUI_PartDataModel::XGUI_PartDataModel(const std::shared_ptr<ModelAPI_Document>& theDocument, QObject* theParent)
-  : XGUI_PartModel(theDocument, theParent)
+XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
+  : XGUI_PartModel(theParent)
 {
 }
 
@@ -183,25 +231,32 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons
     switch (theIndex.internalId()) {
     case MyRoot:
       {
-        std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
+        DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+        FeaturePtr aFeature = aRootDoc->feature(PARTS_GROUP, myId);
         if (aFeature)
-          return aFeature->data()->getName().c_str();
+          return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
       }
     case ParamsFolder:
-      return tr("Parameters");
+      return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
     case ConstructFolder:
-      return tr("Constructions");
+      return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
+    case BodiesFolder:
+      return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
     case ParamObject:
       {
-        std::shared_ptr<ModelAPI_Feature> aFeature = 
-          featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
+        FeaturePtr aFeature = featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
         if (aFeature)
-          return aFeature->data()->getName().c_str();
+          return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
       }
     case ConstructObject:
       {
-        std::shared_ptr<ModelAPI_Feature> aFeature = 
-          featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+        FeaturePtr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+        if (aFeature)
+          return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
+      }
+    case HistoryObject:
+      {
+        FeaturePtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
         if (aFeature)
           return aFeature->data()->getName().c_str();
       }
@@ -215,14 +270,28 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons
     case ParamsFolder:
       return QIcon(":pictures/params_folder.png");
     case ConstructFolder:
+    case BodiesFolder:
       return QIcon(":pictures/constr_folder.png");
     case ConstructObject:
-        return QIcon(":pictures/point_ico.png");
+      {
+        FeaturePtr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+        if (aFeature)
+          return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
+      }
+    case HistoryObject:
+      {
+        FeaturePtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
+        if (aFeature)
+          return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
+      }
     }
    break;
   case Qt::ToolTipRole:
     // return Tooltip
     break;
+  case Qt::ForegroundRole:
+    return QBrush(myItemsColor);
+    break;
   }
   return QVariant();
 }
@@ -234,18 +303,22 @@ QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation
 
 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
 {
-  if (!parent.isValid()) 
-    if (myDocument->feature(PARTS_GROUP, myId))
+  if (!parent.isValid()) {
+    DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+    if (aRootDoc->feature(PARTS_GROUP, myId))
       return 1;
     else 
       return 0;
+  }
   switch (parent.internalId()) {
   case MyRoot:
-    return 2;
+    return 3 + featureDocument()->size(FEATURES_GROUP);
   case ParamsFolder:
-    return featureDocument()->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
+    return featureDocument()->size(PARAMETERS_GROUP);
   case ConstructFolder:
-    return featureDocument()->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
+    return featureDocument()->size(CONSTRUCTIONS_GROUP);
+  case BodiesFolder:
+    return 0;
   }
   return 0;
 }
@@ -268,11 +341,17 @@ QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelInd
       return createIndex(0, 0, (qint32) ParamsFolder);
     case 1:
       return createIndex(1, 0, (qint32) ConstructFolder);
+    case 2:
+      return createIndex(2, 0, (qint32) BodiesFolder);
+    default:
+      return createIndex(theRow, theColumn, (qint32) HistoryObject);
     }
   case ParamsFolder:
     return createIndex(theRow, 0, (qint32) ParamObject);
   case ConstructFolder:
     return createIndex(theRow, 0, (qint32) ConstructObject);
+  case BodiesFolder:
+    return createIndex(theRow, 0, (qint32) BodiesObject);
   }
   return QModelIndex();
 }
@@ -284,6 +363,8 @@ QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
     return QModelIndex();
   case ParamsFolder:
   case ConstructFolder:
+  case BodiesFolder:
+  case HistoryObject:
     return createIndex(0, 0, (qint32) MyRoot);
   case ParamObject:
     return createIndex(0, 0, (qint32) ParamsFolder);
@@ -299,50 +380,87 @@ bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
 }
 
 
-std::shared_ptr<ModelAPI_Document> XGUI_PartDataModel::featureDocument() const
+DocumentPtr XGUI_PartDataModel::featureDocument() const
 {
-  std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
+  DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+  FeaturePtr aFeature = aRootDoc->feature(PARTS_GROUP, myId, true);
   return aFeature->data()->docRef("PartDocument")->value();
 }
-
 FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
 {
   switch (theIndex.internalId()) {
   case MyRoot:
-    return myDocument->feature(PARTS_GROUP, myId);
+    {
+      DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+      return aRootDoc->feature(PARTS_GROUP, myId);
+    }
   case ParamsFolder:
   case ConstructFolder:
+  case BodiesFolder:
     return FeaturePtr();
   case ParamObject:
     return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
   case ConstructObject:
     return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+  //case BodiesObject:
+  //  return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
+  case HistoryObject:
+    return featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3); 
   }
   return FeaturePtr();
 }
 
-bool XGUI_PartDataModel::hasDocument(const std::shared_ptr<ModelAPI_Document>& theDoc) const
+bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
 {
   return (featureDocument() == theDoc);
 }
 
 
-QModelIndex XGUI_PartDataModel::findParent(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
+QModelIndex XGUI_PartDataModel::findParent(const FeaturePtr& theFeature) const
 {
-  QString aGroup(theFeature->getGroup().c_str());
-
-  if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0)
-    return createIndex(0, 0, (quintptr) ParamsFolder);
-  if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0)
-    return createIndex(1, 0, (quintptr) ConstructFolder);
-  return QModelIndex();
+  return findGroup(theFeature->getGroup().c_str());
 }
 
 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
 {
   if (theGroup.compare(PARAMETERS_GROUP) == 0)
-    return createIndex(0, 0, (quintptr) ParamsFolder);
+    return createIndex(0, 0, (qint32) ParamsFolder);
   if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
-    return createIndex(1, 0, (quintptr) ConstructFolder);
+    return createIndex(1, 0, (qint32) ConstructFolder);
   return QModelIndex();
 }
+
+FeaturePtr XGUI_PartDataModel::part() const
+{
+  DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+  return aRootDoc->feature(PARTS_GROUP, myId, true);
+}
+
+QModelIndex XGUI_PartDataModel::featureIndex(const FeaturePtr& theFeature) const
+{
+  QModelIndex aIndex;
+  if (theFeature) {
+    if (part() == theFeature) 
+      return aIndex;
+
+    //std::string aGroup = theFeature->getGroup();
+    DocumentPtr aDoc = theFeature->document();
+    int aNb = aDoc->size(FEATURES_GROUP);
+    int aRow = -1;
+    for (int i = 0; i < aNb; i++) {
+      if (aDoc->feature(FEATURES_GROUP, i) == theFeature) {
+        aRow = i;
+        break;
+      }
+    }
+    if (aRow != -1) {
+      return createIndex(aRow + 3, 0, (qint32) HistoryObject);
+/*      if (aGroup.compare(PARAMETERS_GROUP) == 0)
+        return createIndex(aRow, 0, (qint32) ParamObject);
+      if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
+        return createIndex(aRow, 0, (qint32) ConstructObject);*/
+    }
+  }
+  return aIndex;
+}