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);
}
XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
{
+ clearModelIndexes();
}
for (int i = 0; i < myPartModels.size(); i++)
myPartModels.at(i)->setDocument(myDocument, i);
}
+ clearModelIndexes();
endResetModel();
}
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
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;
}
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;
}
{
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