Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
index ccb90aae2a3e3afe9393656009888dc288ce2169..ba23d472d96ad5b660d5cdd01126aeb3805a0dfe 100644 (file)
 #include "XGUI_DocumentDataModel.h"
+#include "XGUI_PartDataModel.h"
 
 #include <ModelAPI_PluginManager.h>
 #include <ModelAPI_Iterator.h>
 #include <ModelAPI_Document.h>
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_Object.h>
+#include <Model_Document.h>
 
+#include <Event_Loop.h>
 
 
+#include <QIcon>
+
 
 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
-  : QAbstractItemModel(theParent),
-  myParamsFolder(0),
-  myConstructFolder(0)
+  : QAbstractItemModel(theParent)
 {
-  //std::shared_ptr<ModelAPI_Feature> myRoot = aMgr->createFeature("Point");
+  // Find Document object
   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
   myDocument = aMgr->currentDocument();
+
+  // Register in event loop
+  Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
+
+  // Create a top part of data tree model
+  myModel = new XGUI_TopDataModel(this);
+  myModel->setDocument(myDocument);
 }
 
 
 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
 {
+  clearModelIndexes();
 }
 
 
-QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
+void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
 {
-  switch (theRole) {
-  case Qt::DisplayRole:
-    // return a name
-    if (theIndex.internalId() == quintptr(&myParamsFolder))
-      return "Parameters";
-    else if (theIndex.internalId() == quintptr(&myConstructFolder))
-      return "Constructions";
-    else if (theIndex.internalId() == 0) {
-      return "Part";
+  beginResetModel();
+  int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
+  if (myPartModels.size() != aNbParts) { // resize internal models
+    while (myPartModels.size() > aNbParts) {
+      delete myPartModels.last();
+      myPartModels.removeLast();
     }
-    break;
-  case Qt::DecorationRole:
-    // return an Icon
-    break;
-  case Qt::ToolTipRole:
-    // return Tooltip
-    break;
+    while (myPartModels.size() < aNbParts) {
+      myPartModels.append(new XGUI_PartDataModel(this));
+    }
+    for (int i = 0; i < myPartModels.size(); i++)
+      myPartModels.at(i)->setDocument(myDocument, i);
   }
-  return QVariant();
+  clearModelIndexes();
+  endResetModel();
+}
+
+QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
+{
+  if (!theIndex.isValid())
+    return QVariant();
+  return toSourceModel(theIndex).data(theRole);
 }
 
 
-QVariant XGUI_DocumentDataModel::headerData(int section, Qt::Orientation orientation, int role) const
+QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
 {
   return QVariant();
 }
 
-int XGUI_DocumentDataModel::rowCount(const QModelIndex &parent) const
+int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
 {
-  std::shared_ptr<ModelAPI_Iterator> aIt = myDocument->featuresIterator(PARTS_GROUP);
-  int a = aIt->numIterationsLeft();
-  return aIt->numIterationsLeft() + 2;
+  if (!theParent.isValid()) 
+    return myModel->rowCount(theParent) + myPartModels.size();
+
+  QModelIndex aParent = toSourceModel(theParent);
+  return aParent.model()->rowCount(aParent);
 }
 
-int XGUI_DocumentDataModel::columnCount(const QModelIndex &parent) const
+int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
 {
   return 1;
 }
 
 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
 {
-  switch (theRow) {
-  case 0:
-    return createIndex(theRow, theColumn, (quintptr) &myParamsFolder);
-  case 1:
-    return createIndex(theRow, theColumn, (quintptr) &myConstructFolder);
-  default:
-    {
-      std::shared_ptr<ModelAPI_Iterator> aIt = myDocument->featuresIterator(PARTS_GROUP);
-      if (aIt->numIterationsLeft() <= (theRow - 1)) {
-        return createIndex(theRow, theColumn, (quintptr) 0);
-      }
-    }
+  QModelIndex aIndex;
+  if (!theParent.isValid()) {
+    int aOffs = myModel->rowCount();
+    if (theRow < aOffs) 
+      aIndex = myModel->index(theRow, theColumn, theParent);
+    else
+      aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
+
+    aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
+  } else {
+    QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
+    aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
+
+    aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
   }
-  return QModelIndex();
+  return aIndex;
 }
 
 
-QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex &index) const
+QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
 {
-  return QModelIndex();
+  QModelIndex aParent = toSourceModel(theIndex);
+  aParent = aParent.model()->parent(aParent);
+  if (aParent.isValid())
+    return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
+  return aParent;
 }
 
+
 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
 {
   if (!theParent.isValid())
     return true;
+  return rowCount(theParent) > 0;
+}
 
-  if (theParent.internalId() == quintptr(&myParamsFolder)) 
-    return myDocument->featuresIterator(PARAMETERS_GROUP)->more();
-  if (theParent.internalId() == quintptr(&myConstructFolder))
-    return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->more();
-  return false;
+
+QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
+{
+  QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
+  return (*aIndexPtr);
+}
+
+
+QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
+{
+  QList<QModelIndex*>::const_iterator aIt;
+  for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
+    QModelIndex* aIndex = (*aIt);
+    if ((*aIndex) == theIndex)
+      return aIndex;
+  }
+  return 0;
+}
+
+QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
+{
+  QModelIndex* aIndexPtr = findModelIndex(theIndex);
+  if (!aIndexPtr) {
+    aIndexPtr = new QModelIndex(theIndex);
+    XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
+    that->myIndexes.append(aIndexPtr);
+  }
+  return aIndexPtr;
+}
+
+void XGUI_DocumentDataModel::clearModelIndexes()
+{
+  QList<QModelIndex*>::const_iterator aIt;
+  for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
+    delete (*aIt);
+  myIndexes.clear();
 }
\ No newline at end of file