Salome HOME
Validator for partition
[modules/shaper.git] / src / PartSet / PartSet_PartDataModel.cpp
index 1398e59575c4a3378e6d08e4bb8462e4a5b47e1b..fff14f9d4b0ae97eae84c6443eff4f5511391caa 100644 (file)
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Tools.h>
+
+#include <Events_Loop.h>
 
 #include <QIcon>
 #include <QBrush>
 
 
-PartSet_TopDataModel::PartSet_TopDataModel(QObject* theParent)
-    : PartSet_FeaturesModel(theParent)
-{
-}
-
-PartSet_TopDataModel::~PartSet_TopDataModel()
-{
-}
-
-QVariant PartSet_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
-{
-  if (theIndex.column() == 1)
-    return QVariant();
-
-  switch (theRole) {
-    case Qt::DisplayRole:
-      // return a name
-      switch (theIndex.internalId()) {
-        case ParamsFolder:
-          return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
-        case ParamObject: {
-          DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-          ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row());
-          if (aObject) {
-            ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObject);
-            AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
-            QString aVal = QString::number(aValueAttribute->value());
-            QString aTitle = QString(aObject->data()->name().c_str());
-            return aTitle + " = " + aVal;
-          }
-        }
-          break;
-        case ConstructFolder:
-          return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
-        case ConstructObject: {
-          DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-          ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(),
-                                               theIndex.row());
-          if (aObject)
-            return aObject->data()->name().c_str();
-        }
-          break;
-        //case GroupsFolder:
-        //  return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex));
-        //case GroupObject: {
-        //  DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-        //  ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultGroup::group(),
-        //                                       theIndex.row());
-        //  if (aObject)
-        //    return aObject->data()->name().c_str();
-        //}
-        //  break;
-      }
-      break;
-
-    case Qt::DecorationRole:
-      {
-      // return an Icon
-      switch (theIndex.internalId()) {
-        case ParamsFolder:
-          return QIcon(":pictures/params_folder.png");
-        case ConstructFolder:
-          return QIcon(":pictures/constr_folder.png");
-        case ConstructObject:
-          return QIcon(":pictures/constr_object.png");
-        //case GroupsFolder:
-        //  return QIcon(":pictures/constr_folder.png");
-        }
-      }
-      break;
-
-    case Qt::ToolTipRole:
-      // return Tooltip
-      break;
-    case Qt::ForegroundRole:
-      return QBrush(myItemsColor);
-      break;
-  }
-  return QVariant();
-}
-
-QVariant PartSet_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
-{
-  return QVariant();
-}
-
-int PartSet_TopDataModel::rowCount(const QModelIndex& theParent) const
-{
-  if (!theParent.isValid())
-    return 2;  // In case of groups using it has to be +1
-
-  DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-  if (theParent.internalId() == ParamsFolder)
-    return aRootDoc->size(ModelAPI_ResultParameter::group());
-
-  if (theParent.internalId() == ConstructFolder)
-    return aRootDoc->size(ModelAPI_ResultConstruction::group());
-
-  //if (theParent.internalId() == GroupsFolder)
-  //  return aRootDoc->size(ModelAPI_ResultGroup::group());
-
-  return 0;
-}
-
-int PartSet_TopDataModel::columnCount(const QModelIndex &parent) const
-{
-  return 1;
-}
-
-QModelIndex PartSet_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
-{
-  if (!theParent.isValid()) {
-    switch (theRow) {
-      case 0:
-        return createIndex(theRow, theColumn, (qint32) ParamsFolder);
-      case 1:
-        return createIndex(theRow, theColumn, (qint32) ConstructFolder);
-      //case 2:
-      //  return createIndex(theRow, theColumn, (qint32) GroupsFolder);
-    }
-  } else {
-    if (theParent.internalId() == ParamsFolder)
-      return createIndex(theRow, theColumn, (qint32) ParamObject);
-
-    if (theParent.internalId() == ConstructFolder)
-      return createIndex(theRow, theColumn, (qint32) ConstructObject);
-
-    //if (theParent.internalId() == GroupsFolder)
-    //  return createIndex(theRow, theColumn, (qint32) GroupObject);
-  }
-  return QModelIndex();
-}
-
-QModelIndex PartSet_TopDataModel::parent(const QModelIndex& theIndex) const
-{
-  int aId = (int) theIndex.internalId();
-  switch (aId) {
-    case ParamsFolder:
-    case ConstructFolder:
-    //case GroupsFolder:
-      return QModelIndex();
-    case ParamObject:
-      return createIndex(0, 0, (qint32) ParamsFolder);
-    case ConstructObject:
-      return createIndex(1, 0, (qint32) ConstructFolder);
-    //case GroupObject:
-    //  return createIndex(2, 0, (qint32) GroupsFolder);
-  }
-  return QModelIndex();
-}
-
-bool PartSet_TopDataModel::hasChildren(const QModelIndex& theParent) const
-{
-  return rowCount(theParent) > 0;
-}
-
-ObjectPtr PartSet_TopDataModel::object(const QModelIndex& theIndex) const
-{
-  switch (theIndex.internalId()) {
-    case ParamsFolder:
-    case ConstructFolder:
-      return ObjectPtr();
-    case ParamObject: {
-      DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-      return aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row());
-    }
-    case ConstructObject: {
-      DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-      return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
-    }
-    //case GroupObject: {
-    //  DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-    //  return aRootDoc->object(ModelAPI_ResultGroup::group(), theIndex.row());
-    //}
-  }
-  return ObjectPtr();
-}
-
-QModelIndex PartSet_TopDataModel::findParent(const ObjectPtr& theObject) const
-{
-  return findGroup(theObject->groupName().c_str());
-}
-
-QModelIndex PartSet_TopDataModel::findGroup(const std::string& theGroup) const
-{
-  if (theGroup == ModelAPI_ResultParameter::group())
-    return createIndex(0, 0, (qint32) ParamsFolder);
-  if (theGroup == ModelAPI_ResultConstruction::group())
-    return createIndex(1, 0, (qint32) ConstructFolder);
-  //if (theGroup == ModelAPI_ResultGroup::group())
-  //  return createIndex(2, 0, (qint32) ConstructFolder);
-  return QModelIndex();
-}
-
-QModelIndex PartSet_TopDataModel::objectIndex(const ObjectPtr& theObject) const
-{
-  QModelIndex aIndex;
-  if (theObject) {
-    DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
-    std::string aGroup = theObject->groupName();
-    int aNb = aRootDoc->size(aGroup);
-    int aRow = -1;
-    for (int i = 0; i < aNb; i++) {
-      if (aRootDoc->object(aGroup, i) == theObject) {
-        aRow = i;
-        break;
-      }
-    }
-    if (aRow != -1) {
-      if (aGroup == ModelAPI_ResultParameter::group())
-        return createIndex(aRow, 0, (qint32) ParamObject);
-      if (aGroup == ModelAPI_ResultConstruction::group())
-        return createIndex(aRow, 0, (qint32) ConstructObject);
-      //if (aGroup == ModelAPI_ResultGroup::group())
-      //  return createIndex(aRow, 0, (qint32) GroupObject);
-    }
-  }
-  return aIndex;
-}
-
-//******************************************************************
-//******************************************************************
-//******************************************************************
 PartSet_PartDataModel::PartSet_PartDataModel(QObject* theParent)
     : PartSet_PartModel(theParent)
 {
@@ -261,7 +42,7 @@ QVariant PartSet_PartDataModel::data(const QModelIndex& theIndex, int theRole) c
     DocumentPtr aActiveDoc = ModelAPI_Session::get()->activeDocument();
     QModelIndex aParent = theIndex.parent();
     if (aActiveDoc == aPartDoc) {
-      if ((theIndex.internalId() == HistoryObject) && (!aParent.isValid())) {
+      if (!aParent.isValid()) {
         switch (theRole) {
         case Qt::DecorationRole:
           if (theIndex.row() == lastHistoryRow())
@@ -272,6 +53,25 @@ QVariant PartSet_PartDataModel::data(const QModelIndex& theIndex, int theRole) c
     return QVariant();
   }
 
+  if (theIndex.internalId() >= 0) {
+    ObjectPtr aObj = object(theIndex);
+    switch (theRole) {
+      case Qt::DisplayRole:
+        return aObj->data()->name().c_str();
+      case Qt::DecorationRole: 
+        {
+          FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+          if (aFeature)
+            return PartSet_DocumentDataModel::featureIcon(aFeature);
+        }
+        break;
+      case Qt::ForegroundRole:
+        if (theIndex.internalId() > lastHistoryRow())
+          return QBrush(Qt::lightGray);
+        return QBrush(myItemsColor);
+    }
+  }
+
   switch (theRole) {
     case Qt::DisplayRole:
       // return a name
@@ -340,12 +140,19 @@ QVariant PartSet_PartDataModel::data(const QModelIndex& theIndex, int theRole) c
         case ConstructObject:
         case GroupObject:
         case BodiesObject: {
-          std::string aGroup = theIndex.internalId() == ConstructObject ?
-            ModelAPI_ResultConstruction::group() : ModelAPI_ResultBody::group();
-          ObjectPtr anObject = aPartDoc->object(aGroup, theIndex.row());
-          if (anObject && anObject->data() && 
-              anObject->data()->execState() == ModelAPI_StateMustBeUpdated) {
-            return QIcon(":pictures/constr_object_modified.png");
+          std::string aGroup;
+          if (theIndex.internalId() == ConstructObject)
+            aGroup = ModelAPI_ResultConstruction::group();
+          else if (theIndex.internalId() == BodiesObject)
+            aGroup = ModelAPI_ResultBody::group();
+          else if (theIndex.internalId() == GroupObject)
+            aGroup = ModelAPI_ResultGroup::group();
+          if (aGroup.length() > 0) {
+            ObjectPtr anObject = aPartDoc->object(aGroup, theIndex.row());
+            if (anObject && anObject->data() && 
+                anObject->data()->execState() == ModelAPI_StateMustBeUpdated) {
+              return QIcon(":pictures/constr_object_modified.png");
+            }
           }
           return QIcon(":pictures/constr_object.png");
         }
@@ -393,6 +200,15 @@ int PartSet_PartDataModel::rowCount(const QModelIndex& parent) const
       return partDocument()->size(ModelAPI_ResultBody::group());
     case GroupsFolder:
       return partDocument()->size(ModelAPI_ResultGroup::group());
+    case HistoryObject:
+      {
+        ObjectPtr aObj = object(parent);
+        CompositeFeaturePtr aCompFeature = 
+          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aObj);
+        if (aCompFeature.get()) {
+          return aCompFeature->numberOfSubs(true);
+        }
+      }
   }
   return 0;
 }
@@ -434,6 +250,10 @@ QModelIndex PartSet_PartDataModel::index(int theRow, int theColumn, const QModel
         return createIndex(theRow, theColumn, (qint32) BodiesObject);
       case GroupsFolder:
         return createIndex(theRow, theColumn, (qint32) GroupObject);
+      case HistoryObject:
+        {
+          return createIndex(theRow, theColumn, (qint32) theParent.row());
+        }
     }
   }
   return QModelIndex();
@@ -441,6 +261,10 @@ QModelIndex PartSet_PartDataModel::index(int theRow, int theColumn, const QModel
 
 QModelIndex PartSet_PartDataModel::parent(const QModelIndex& theIndex) const
 {
+  if (theIndex.internalId() >= 0) {
+    int aPRow = theIndex.internalId();
+    return createIndex(aPRow, 0, (qint32) HistoryObject);
+  }
   switch (theIndex.internalId()) {
     case ParamsFolder:
     case ConstructFolder:
@@ -468,15 +292,25 @@ 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()); 
-  return aPart->partDoc();
+  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();
 }
 
 ObjectPtr PartSet_PartDataModel::object(const QModelIndex& theIndex) const
 {
+  if (theIndex.internalId() >= 0) {
+    int aPRow = theIndex.internalId();
+    ObjectPtr aObj = 
+      partDocument()->object(ModelAPI_Feature::group(), aPRow - getRowsNumber());
+    CompositeFeaturePtr aCompFeature = 
+      std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aObj);
+    if (aCompFeature.get()) {
+      return aCompFeature->subFeature(theIndex.row(), true);
+    }
+    return ObjectPtr();
+  }
   switch (theIndex.internalId()) {
     case ParamsFolder:
     case ConstructFolder:
@@ -521,13 +355,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;
@@ -573,18 +400,26 @@ int PartSet_PartDataModel::getRowsNumber() const
 int PartSet_PartDataModel::lastHistoryRow() const
 {
   DocumentPtr aDoc = partDocument();
-  FeaturePtr aFeature = aDoc->currentFeature();
-  return getRowsNumber() + aDoc->index(aFeature);
+  FeaturePtr aFeature = aDoc->currentFeature(true);
+  if (aFeature.get())
+    return getRowsNumber() + aDoc->index(aFeature);
+  else
+    return getRowsNumber() - 1;
 }
 
 void PartSet_PartDataModel::setLastHistoryItem(const QModelIndex& theIndex)
 {
+  SessionPtr aMgr = ModelAPI_Session::get();
+  DocumentPtr aDoc = partDocument();
+  std::string aOpName = tr("History change").toStdString();
   if (theIndex.internalId() == HistoryObject) {
-    SessionPtr aMgr = ModelAPI_Session::get();
     ObjectPtr aObject = object(theIndex);
-    DocumentPtr aDoc = partDocument();
-    aMgr->startOperation(tr("History change").toStdString());
-    aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObject));
+    aMgr->startOperation(aOpName);
+    aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObject), true);
+    aMgr->finishOperation();
+  } else {
+    aMgr->startOperation(aOpName);
+    aDoc->setCurrentFeature(FeaturePtr(), true);
     aMgr->finishOperation();
   }
 }
@@ -596,10 +431,15 @@ QModelIndex PartSet_PartDataModel::lastHistoryItem() const
 
 Qt::ItemFlags PartSet_PartDataModel::flags(const QModelIndex& theIndex) const
 {
+  // Disable sub-features at column 1
+  if ((theIndex.column() == 1)  && (theIndex.internalId() >= 0))
+    return 0;
+
   Qt::ItemFlags aFlags = Qt::ItemIsSelectable;
   if (object(theIndex)) {
     aFlags |= Qt::ItemIsEditable;
   }
+
   if (theIndex.internalId() == HistoryObject) {
     if (theIndex.row() <= lastHistoryRow() || (theIndex.column() == 1))
       aFlags |= Qt::ItemIsEnabled;