Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
index e1d4aee48882c96744264646633499a0e1896267..b4cc62c0c9f4d5f8bc6a6fba6cd41dffd6301903 100644 (file)
@@ -1,33 +1,38 @@
 #include "XGUI_DocumentDataModel.h"
 #include "XGUI_PartDataModel.h"
+#include "XGUI_Workshop.h"
+#include "XGUI_Tools.h"
 
 #include <ModelAPI_PluginManager.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Data.h>
 #include <Model_Events.h>
+#include <ModelAPI_Object.h>
 
 #include <Events_Loop.h>
 
+#include <Config_FeatureMessage.h>
 
 #include <QIcon>
 #include <QString>
+#include <QBrush>
 
 
+#define ACTIVE_COLOR QColor(0,72,140)
+#define PASSIVE_COLOR Qt::black
+
 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
-  : QAbstractItemModel(theParent)
+  : QAbstractItemModel(theParent), myActivePart(0)
 {
-  // Find Document object
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-  myDocument = aMgr->currentDocument();
-
   // Register in event loop
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
 
   // Create a top part of data tree model
-  myModel = new XGUI_TopDataModel(myDocument, this);
+  myModel = new XGUI_TopDataModel(this);
+  myModel->setItemsColor(ACTIVE_COLOR);
 }
 
 
@@ -39,103 +44,126 @@ XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
 
 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
 {
+  DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+
   // Created object event *******************
-  if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
+  if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
-    boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
-    boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
-
-    if (aDoc == myDocument) {  // If root objects
-      if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
-        // Add a new part
-        int aStart = myPartModels.size() + 1;
-        XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
-        aModel->setPartId(myPartModels.count());
-        myPartModels.append(aModel);
-        insertRow(aStart, partFolderNode());
-      } else { // Update top groups (other except parts
-        QModelIndex aIndex = myModel->findParent(aFeature);
-        int aStart = myModel->rowCount(aIndex) - 1;
-        aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
-        insertRow(aStart, aIndex);
-      }
-    } else { // if sub-objects of first level nodes
-      XGUI_PartModel* aPartModel = 0;
-      QList<XGUI_PartModel*>::const_iterator aIt;
-      for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
-        if ((*aIt)->hasDocument(aDoc)) {
-          aPartModel = (*aIt);
-          break;
+    std::set<FeaturePtr> aFeatures = aUpdMsg->features();
+
+    std::set<FeaturePtr>::const_iterator aIt;
+    for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
+      FeaturePtr aFeature = (*aIt);
+      DocumentPtr aDoc = aFeature->document();
+      if (aDoc == aRootDoc) {  // If root objects
+        if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
+          // Add a new part
+          int aStart = myPartModels.size();
+          XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
+          aModel->setPartId(myPartModels.count());
+          myPartModels.append(aModel);
+          insertRow(aStart, partFolderNode());
+        } else { // Update top groups (other except parts
+          QModelIndex aIndex = myModel->findParent(aFeature);
+          int aStart = myModel->rowCount(aIndex) - 1;
+          aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+          insertRow(aStart, aIndex);
+        }
+      } else { // if sub-objects of first level nodes
+        XGUI_PartModel* aPartModel = 0;
+        QList<XGUI_PartModel*>::const_iterator aIt;
+        for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
+          if ((*aIt)->hasDocument(aDoc)) {
+            aPartModel = (*aIt);
+            break;
+          }
+        }
+        if (aPartModel) {
+          QModelIndex aIndex = aPartModel->findParent(aFeature);
+          int aStart = aPartModel->rowCount(aIndex) - 1;
+          aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+          insertRow(aStart, aIndex);
         }
-      }
-      if (aPartModel) {
-        QModelIndex aIndex = aPartModel->findParent(aFeature);
-        int aStart = aPartModel->rowCount(aIndex) - 1;
-        aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
-        insertRow(aStart, aIndex);
       }
     }
-
   // Deleted object event ***********************
-  } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
+  } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED)) {
     const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
-    boost::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
-
-    if (aDoc == myDocument) {  // If root objects
-      if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
-        int aStart = myPartModels.size();
-        removeSubModel(myPartModels.size() - 1);
-        removeRow(aStart - 1, partFolderNode());
-      } else { // Update top groups (other except parts
-        QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
-        int aStart = myModel->rowCount(aIndex);
-        aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
-        removeRow(aStart - 1, aIndex);
-      }
-    } else {
-      XGUI_PartModel* aPartModel = 0;
-      QList<XGUI_PartModel*>::const_iterator aIt;
-      for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
-        if ((*aIt)->hasDocument(aDoc)) {
-          aPartModel = (*aIt);
-          break;
+    DocumentPtr aDoc = aUpdMsg->document();
+    std::set<std::string> aGroups = aUpdMsg->groups();
+
+    std::set<std::string>::const_iterator aIt;
+    for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
+      std::string aGroup = (*aIt);
+      if (aDoc == aRootDoc) {  // If root objects
+        if (aGroup.compare(PARTS_GROUP) == 0) { // Updsate only Parts group
+          int aStart = myPartModels.size() - 1;
+          removeSubModel(aStart);
+          removeRow(aStart, partFolderNode());
+          if (myActivePart && (!isPartSubModel(myActivePart))) {
+            myActivePart = 0;
+            myActivePartIndex = QModelIndex();
+            myModel->setItemsColor(ACTIVE_COLOR);
+          }
+        } else { // Update top groups (other except parts
+          QModelIndex aIndex = myModel->findGroup(aGroup);
+          int aStart = myModel->rowCount(aIndex);
+          aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+          removeRow(aStart, aIndex);
+        }
+      } else {
+        XGUI_PartModel* aPartModel = 0;
+        QList<XGUI_PartModel*>::const_iterator aIt;
+        for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
+          if ((*aIt)->hasDocument(aDoc)) {
+            aPartModel = (*aIt);
+            break;
+          }
+        }
+        if (aPartModel) {
+          QModelIndex aIndex = aPartModel->findGroup(aGroup);
+          int aStart = aPartModel->rowCount(aIndex);
+          aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+          removeRow(aStart, aIndex);
         }
-      }
-      if (aPartModel) {
-        QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
-        int aStart = aPartModel->rowCount(aIndex);
-        aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
-        removeRow(aStart - 1, aIndex);
       }
     }
-
   // Deleted object event ***********************
-  } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_UPDATED) {
-    const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
-    boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
-    boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
+  } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) {
+    //const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
+    //FeaturePtr aFeature = aUpdMsg->feature();
+    //DocumentPtr aDoc = aFeature->document();
     
+    // TODO: Identify the necessary index by the modified feature
     QModelIndex aIndex;
     emit dataChanged(aIndex, aIndex);
 
   // Reset whole tree **************************
   } else {  
-    beginResetModel();
-    int aNbParts = myDocument->size(PARTS_GROUP);
-    if (myPartModels.size() != aNbParts) { // resize internal models
-      while (myPartModels.size() > aNbParts) {
-        delete myPartModels.last();
-        myPartModels.removeLast();
-      }
-      while (myPartModels.size() < aNbParts) {
-        myPartModels.append(new XGUI_PartDataModel(myDocument, this));
-      }
-      for (int i = 0; i < myPartModels.size(); i++)
-        myPartModels.at(i)->setPartId(i);
+    rebuildDataTree();
+  }
+}
+
+void XGUI_DocumentDataModel::rebuildDataTree()
+{
+  DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+
+  beginResetModel();
+  clearModelIndexes();
+
+  int aNbParts = aRootDoc->size(PARTS_GROUP);
+  if (myPartModels.size() != aNbParts) { // resize internal models
+    while (myPartModels.size() > aNbParts) {
+      delete myPartModels.last();
+      myPartModels.removeLast();
+    }
+    while (myPartModels.size() < aNbParts) {
+      myPartModels.append(new XGUI_PartDataModel(this));
     }
-    clearModelIndexes();
-    endResetModel();
+    for (int i = 0; i < myPartModels.size(); i++)
+      myPartModels.at(i)->setPartId(i);
   }
+  endResetModel();
 }
 
 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
@@ -151,6 +179,11 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole)
       return QIcon(":pictures/constr_folder.png");
     case Qt::ToolTipRole:
       return tr("Parts folder");
+    case Qt::ForegroundRole:
+      if (myActivePart)
+        return QBrush(PASSIVE_COLOR);
+      else
+        return QBrush(ACTIVE_COLOR);
     default:
       return QVariant();
     }
@@ -158,7 +191,10 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole)
   case HistoryNode:
     {
       int aOffset = historyOffset();
-      FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
+      DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+      FeaturePtr aFeature = aRootDoc->feature(FEATURES_GROUP, theIndex.row() - aOffset);
+      if (!aFeature)
+        return QVariant();
       switch (theRole) {
       case Qt::DisplayRole:
         if (aFeature)
@@ -166,17 +202,14 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole)
         else 
           return QVariant();
       case Qt::DecorationRole:
-        {
-          std::string aType = aFeature->getKind();
-          if (aType.compare("Point") == 0)
-            return QIcon(":pictures/point_ico.png");
-          if (aType.compare("Part") == 0)
-            return QIcon(":pictures/part_ico.png");
-          if (aType.compare("Sketch") == 0)
-            return QIcon(":icons/sketch.png");
-        }
+        return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
       case Qt::ToolTipRole:
         return tr("Feature object");
+      case Qt::ForegroundRole:
+        if (myActivePart)
+          return QBrush(PASSIVE_COLOR);
+        else
+          return QBrush(ACTIVE_COLOR);
       default:
         return QVariant();
       }
@@ -187,7 +220,7 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole)
   if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
     return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
   }
-  return toSourceModelIndex(theIndex).data(theRole);
+  return toSourceModelIndex(theIndex)->data(theRole);
 }
 
 
@@ -199,10 +232,11 @@ QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theO
 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
 {
   if (!theParent.isValid()) {
+    DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
     // Size of external models
     int aVal = historyOffset();
     // Plus history size
-    aVal += myDocument->size(FEATURES_GROUP);
+    aVal += aRootDoc->size(FEATURES_GROUP);
     return aVal;
   }
   if (theParent.internalId() == PartsFolder) {
@@ -211,11 +245,16 @@ int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
   if (theParent.internalId() == HistoryNode) {
     return 0;
   }
-  QModelIndex aParent = toSourceModelIndex(theParent);
-  if (!isSubModel(aParent.model())) 
+  QModelIndex* aParent = toSourceModelIndex(theParent);
+  const QAbstractItemModel* aModel = aParent->model();
+  if (!isSubModel(aModel)) 
     return 0;
 
-  return aParent.model()->rowCount(aParent);
+  /*if (isPartSubModel(aModel)) {
+    if (aModel != myActivePart)
+      return 0;
+  }*/
+  return aModel->rowCount(*aParent);
 }
 
 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
@@ -255,21 +294,21 @@ QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
     return QModelIndex();
 
-  QModelIndex aIndex = toSourceModelIndex(theIndex);
-  const QAbstractItemModel* aModel = aIndex.model();
+  QModelIndex* aIndex = toSourceModelIndex(theIndex);
+  const QAbstractItemModel* aModel = aIndex->model();
   if (!isSubModel(aModel)) 
     return QModelIndex();
 
   if (isPartSubModel(aModel)) {
-    if (!aModel->parent(aIndex).isValid()) {
+    if (!aModel->parent(*aIndex).isValid()) {
       return partFolderNode();
     }
   }
 
-  aIndex = aModel->parent(aIndex);
-  if (aIndex.isValid())
-    return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
-  return aIndex;
+  QModelIndex aIndex1 = aModel->parent(*aIndex);
+  if (aIndex1.isValid())
+    return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
+  return aIndex1;
 }
 
 
@@ -281,10 +320,10 @@ bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
 }
 
 
-QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
+QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
 {
   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
-  return (*aIndexPtr);
+  return aIndexPtr;
 }
 
 
@@ -322,13 +361,17 @@ FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
 {
   if (theIndex.internalId() == PartsFolder)
     return FeaturePtr();
-
-  QModelIndex aIndex = toSourceModelIndex(theIndex);
-  if (!isSubModel(aIndex.model())) 
+  if (theIndex.internalId() == HistoryNode) {
+    DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+    int aOffset = historyOffset();
+    return aRootDoc->feature(FEATURES_GROUP, theIndex.row() - aOffset);
+  }
+  QModelIndex* aIndex = toSourceModelIndex(theIndex);
+  if (!isSubModel(aIndex->model())) 
     return FeaturePtr();
 
-  const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
-  return aModel->feature(aIndex);
+  const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
+  return aModel->feature(*aIndex);
 }
 
 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
@@ -394,4 +437,121 @@ int XGUI_DocumentDataModel::historyOffset() const
 {
   // Nb of rows of top model + Parts folder
   return myModel->rowCount(QModelIndex()) + 1;
+}
+
+bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
+{
+  if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
+    return false;
+
+  QModelIndex* aIndex = toSourceModelIndex(theIndex);
+  if (!aIndex)
+    return false;
+
+  const QAbstractItemModel* aModel = aIndex->model();
+
+  if (isPartSubModel(aModel)) {
+    // if this is root node (Part item index)
+    if (!aIndex->parent().isValid()) {
+      if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
+
+      if (myActivePart == aModel) {
+        myActivePart = 0;
+        myActivePartIndex = QModelIndex();
+      } else {
+        myActivePart = (XGUI_PartModel*)aModel;
+        myActivePartIndex = theIndex;
+      }
+
+      if (myActivePart) {
+        myActivePart->setItemsColor(ACTIVE_COLOR);
+        myModel->setItemsColor(PASSIVE_COLOR);
+      } else 
+         myModel->setItemsColor(ACTIVE_COLOR);
+     return true;
+    }
+  }
+  return false;
+}
+
+FeaturePtr XGUI_DocumentDataModel::activePart() const
+{
+  if (myActivePart) 
+    return myActivePart->part();
+  return FeaturePtr();
+}
+
+void XGUI_DocumentDataModel::deactivatePart() 
+{ 
+  if (myActivePart) 
+    myActivePart->setItemsColor(PASSIVE_COLOR);
+  myActivePart = 0;
+  myActivePartIndex = QModelIndex();
+  myModel->setItemsColor(ACTIVE_COLOR);
+}
+Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
+{
+  Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
+  if (feature(theIndex)) {
+    aFlags |= Qt::ItemIsEditable;
+  }
+  return aFlags;
+}
+
+QModelIndex XGUI_DocumentDataModel::partIndex(const FeaturePtr& theFeature) const 
+{
+  FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
+
+  int aRow = -1;
+  XGUI_PartModel* aModel = 0;
+  foreach (XGUI_PartModel* aPartModel, myPartModels) {
+    aRow++;
+    if (aPartModel->part() == aFeature) {
+      aModel = aPartModel;
+      break;
+    }
+  }
+  if (aModel) {
+    return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
+  }
+  return QModelIndex();
+}
+
+QModelIndex XGUI_DocumentDataModel::featureIndex(const FeaturePtr theFeature) const
+{
+  // Check that this feature belongs to root document
+  DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
+  DocumentPtr aDoc = theFeature->document();
+  if (aDoc == aRootDoc) {
+    // This feature belongs to histrory or top model
+    if (theFeature->isInHistory()) {
+      int aId;
+      for (aId = 0; aId < aRootDoc->size(FEATURES_GROUP); aId++) {
+        if (theFeature == aRootDoc->feature(FEATURES_GROUP, aId))
+          break;
+      }
+      return index(aId + historyOffset(), 0, QModelIndex());
+    } else {
+      QModelIndex aIndex = myModel->featureIndex(theFeature);
+      return aIndex.isValid()? 
+        createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
+        QModelIndex();
+    }
+  } else {
+    XGUI_PartModel* aPartModel = 0;
+    foreach(XGUI_PartModel* aModel, myPartModels) {
+      if (aModel->hasDocument(aDoc)) {
+        aPartModel = aModel;
+        break;
+      }
+    }
+    if (aPartModel) {
+      QModelIndex aIndex = aPartModel->featureIndex(theFeature);
+      return aIndex.isValid()? 
+        createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
+        QModelIndex();
+    }
+  }
+  return QModelIndex();
 }
\ No newline at end of file