Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
index b2376b9aa2e79c9e091d6114acc7ebe31ed52d6a..ba23d472d96ad5b660d5cdd01126aeb3805a0dfe 100644 (file)
 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
   : QAbstractItemModel(theParent)
 {
+  // 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);
 }
@@ -29,6 +32,7 @@ XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
 
 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
 {
+  clearModelIndexes();
 }
 
 
@@ -47,6 +51,7 @@ void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
     for (int i = 0; i < myPartModels.size(); i++)
       myPartModels.at(i)->setDocument(myDocument, i);
   }
+  clearModelIndexes();
   endResetModel();
 }
 
@@ -68,7 +73,8 @@ int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
   if (!theParent.isValid()) 
     return myModel->rowCount(theParent) + myPartModels.size();
 
-  return 0;
+  QModelIndex aParent = toSourceModel(theParent);
+  return aParent.model()->rowCount(aParent);
 }
 
 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
@@ -85,7 +91,13 @@ QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QMode
       aIndex = myModel->index(theRow, theColumn, theParent);
     else
       aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
-    aIndex = createIndex(theRow, theColumn, aIndex.internalId());
+
+    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 aIndex;
 }
@@ -93,7 +105,11 @@ QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QMode
 
 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;
 }
 
 
@@ -101,22 +117,43 @@ bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
 {
   if (!theParent.isValid())
     return true;
-  return false;
+  return rowCount(theParent) > 0;
 }
 
 
 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
 {
-  int aRow = theProxy.row();
-  if (!theProxy.parent().isValid()) {
-    if (aRow < myModel->rowCount()) {
-      return myModel->index(aRow, 0);
-    } else {
-      int aOffs = aRow - myModel->rowCount();
-      return myPartModels.at(aOffs)->index(aOffs, 0);
-    }
-  } 
-  return QModelIndex();
+  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