From 174b6a109cca75c8d264794ef230a73f0656caf3 Mon Sep 17 00:00:00 2001 From: vsv Date: Wed, 7 May 2014 16:55:36 +0400 Subject: [PATCH] Correction of data tree according to Issue #22 --- src/XGUI/XGUI_DocumentDataModel.cpp | 114 +++++++++++++++++++--------- src/XGUI/XGUI_DocumentDataModel.h | 10 ++- src/XGUI/XGUI_PartDataModel.cpp | 20 +++-- src/XGUI/XGUI_PartDataModel.h | 4 +- 4 files changed, 106 insertions(+), 42 deletions(-) diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index 1ee46e331..a9bb8b61b 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -47,13 +47,13 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) boost::shared_ptr aDoc = aFeature->document(); if (aDoc == myDocument) { // If root objects - if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Updsate only Parts group + if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group // Add a new part - int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size() + 1; + int aStart = myPartModels.size() + 1; XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this); aModel->setPartId(myPartModels.count()); myPartModels.append(aModel); - insertRows(QModelIndex(), aStart, aStart); + insertRows(partFolderNode(), aStart, aStart); } else { // Update top groups (other except parts QModelIndex aIndex = myModel->findParent(aFeature); int aStart = myModel->rowCount(aIndex) - 1; @@ -77,15 +77,15 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) } } - // Deteted object event *********************** + // Deleted object event *********************** } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) { const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast(theMessage); boost::shared_ptr aDoc = aUpdMsg->document(); if (aDoc == myDocument) { // If root objects if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group - int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size() - 1; - beginRemoveRows(QModelIndex(), aStart, aStart); + int aStart = myPartModels.size(); + beginRemoveRows(partFolderNode(), aStart, aStart); removeSubModel(myPartModels.size() - 1); endRemoveRows(); } else { // Update top groups (other except parts @@ -113,6 +113,15 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) } } + // Deleted object event *********************** + } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_UPDATED) { + const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); + boost::shared_ptr aFeature = aUpdMsg->feature(); + boost::shared_ptr aDoc = aFeature->document(); + + QModelIndex aIndex; + emit dataChanged(aIndex, aIndex); + // Reset whole tree ************************** } else { beginResetModel(); @@ -137,6 +146,22 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) { if (!theIndex.isValid()) return QVariant(); + if (theIndex.internalId() == 0){ + switch (theRole) { + case Qt::DisplayRole: + return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex)); + case Qt::DecorationRole: + return QIcon(":pictures/constr_folder.png"); + case Qt::ToolTipRole: + return tr("Parts folder"); + default: + return QVariant(); + } + } + QModelIndex aParent = theIndex.parent(); + if (aParent.isValid() && (aParent.internalId() == 0)) { + return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole); + } return toSourceModelIndex(theIndex).data(theRole); } @@ -150,10 +175,13 @@ int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const { if (!theParent.isValid()) { int aVal = myModel->rowCount(theParent) + myPartModels.size(); - return myModel->rowCount(theParent) + myPartModels.size(); + return myModel->rowCount(theParent) + 1;//myPartModels.size(); + } + if (theParent.internalId() == 0) { + return myPartModels.size(); } QModelIndex aParent = toSourceModelIndex(theParent); - if (!hasSubModel(aParent.model())) + if (!isSubModel(aParent.model())) return 0; return aParent.model()->rowCount(aParent); @@ -169,22 +197,20 @@ QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QMode QModelIndex aIndex; if (!theParent.isValid()) { int aOffs = myModel->rowCount(); - if (theRow < aOffs) + if (theRow < aOffs) { aIndex = myModel->index(theRow, theColumn, theParent); - else { - if (myPartModels.size() > 0) { - int aPos = theRow - aOffs; - if (aPos >= myPartModels.size()) - aPos = 0; - aIndex = myPartModels.at(aPos)->index(aPos, theColumn, theParent); - } else - return aIndex; + aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex)); + } else { + // Create Parts node + aIndex = partFolderNode(); } - aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex)); } else { - QModelIndex* aParent = (QModelIndex*)theParent.internalPointer(); - aIndex = aParent->model()->index(theRow, theColumn, (*aParent)); - + if (theParent.internalId() == 0) { + aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex()); + } else { + QModelIndex* aParent = (QModelIndex*)theParent.internalPointer(); + aIndex = aParent->model()->index(theRow, theColumn, (*aParent)); + } aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex)); } return aIndex; @@ -193,14 +219,24 @@ QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QMode QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const { - QModelIndex aParent = toSourceModelIndex(theIndex); - if (!hasSubModel(aParent.model())) + if (theIndex.internalId() == 0) return QModelIndex(); - aParent = aParent.model()->parent(aParent); - if (aParent.isValid()) - return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent)); - return aParent; + QModelIndex aIndex = toSourceModelIndex(theIndex); + const QAbstractItemModel* aModel = aIndex.model(); + if (!isSubModel(aModel)) + return QModelIndex(); + + if (isPartSubModel(aModel)) { + 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; } @@ -251,8 +287,11 @@ void XGUI_DocumentDataModel::clearModelIndexes() FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const { + if (theIndex.internalId() == 0) + return FeaturePtr(); + QModelIndex aIndex = toSourceModelIndex(theIndex); - if (!hasSubModel(aIndex.model())) + if (!isSubModel(aIndex.model())) return FeaturePtr(); const XGUI_FeaturesModel* aModel = dynamic_cast(aIndex.model()); @@ -286,13 +325,20 @@ void XGUI_DocumentDataModel::removeSubModel(int theModelId) myPartModels.removeAt(theModelId); } -bool XGUI_DocumentDataModel::hasSubModel(const QAbstractItemModel* theModel) const +bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const { if (theModel == myModel) return true; - QList::const_iterator aIt; - for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) - if ((*aIt) == theModel) - return true; - return false; + return isPartSubModel(theModel); +} + +bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const +{ + return myPartModels.contains((XGUI_PartModel*)theModel); } + +QModelIndex XGUI_DocumentDataModel::partFolderNode() const +{ + int aPos = myModel->rowCount(QModelIndex()); + return createIndex(aPos, columnCount() - 1, 0); +} \ No newline at end of file diff --git a/src/XGUI/XGUI_DocumentDataModel.h b/src/XGUI/XGUI_DocumentDataModel.h index 80d69f3b6..e389cd800 100644 --- a/src/XGUI/XGUI_DocumentDataModel.h +++ b/src/XGUI/XGUI_DocumentDataModel.h @@ -70,8 +70,14 @@ private: //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model void removeSubModel(int theModelId); - //! - bool hasSubModel(const QAbstractItemModel* theModel) const; + //! Returns true if the given model is a one of sub-models (of both types) + bool isSubModel(const QAbstractItemModel* theModel) const; + + //! Returns true if the given model is a one of sub-models of Part type + bool isPartSubModel(const QAbstractItemModel* theModel) const; + + QModelIndex partFolderNode() const; + //! Document boost::shared_ptr myDocument; diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp index ba5bbbea3..3ca210d76 100644 --- a/src/XGUI/XGUI_PartDataModel.cpp +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -26,7 +26,7 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const // return a name switch (theIndex.internalId()) { case ParamsFolder: - return tr("Parameters"); + return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex)); case ParamObject: { boost::shared_ptr aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row()); @@ -34,7 +34,7 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const return aFeature->data()->getName().c_str(); } case ConstructFolder: - return tr("Constructions"); + return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex)); case ConstructObject: { boost::shared_ptr aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); @@ -188,9 +188,11 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons return aFeature->data()->getName().c_str(); } case ParamsFolder: - return tr("Parameters"); + return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex)); case ConstructFolder: - return tr("Constructions"); + return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex)); + case BodiesFolder: + return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex)); case ParamObject: { boost::shared_ptr aFeature = @@ -215,6 +217,7 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons case ParamsFolder: return QIcon(":pictures/params_folder.png"); case ConstructFolder: + case BodiesFolder: return QIcon(":pictures/constr_folder.png"); case ConstructObject: return QIcon(":pictures/point_ico.png"); @@ -241,11 +244,13 @@ int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const return 0; switch (parent.internalId()) { case MyRoot: - return 2; + return 3; case ParamsFolder: return featureDocument()->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft(); case ConstructFolder: return featureDocument()->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft(); + case BodiesFolder: + return 0; } return 0; } @@ -268,11 +273,15 @@ QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelInd return createIndex(0, 0, (qint32) ParamsFolder); case 1: return createIndex(1, 0, (qint32) ConstructFolder); + case 2: + return createIndex(1, 0, (qint32) BodiesFolder); } case ParamsFolder: return createIndex(theRow, 0, (qint32) ParamObject); case ConstructFolder: return createIndex(theRow, 0, (qint32) ConstructObject); + case BodiesFolder: + return createIndex(theRow, 0, (qint32) BodieswObject); } return QModelIndex(); } @@ -284,6 +293,7 @@ QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const return QModelIndex(); case ParamsFolder: case ConstructFolder: + case BodiesFolder: return createIndex(0, 0, (qint32) MyRoot); case ParamObject: return createIndex(0, 0, (qint32) ParamsFolder); diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h index e1f0246ef..ace36a743 100644 --- a/src/XGUI/XGUI_PartDataModel.h +++ b/src/XGUI/XGUI_PartDataModel.h @@ -103,7 +103,9 @@ private: ParamsFolder, ParamObject, ConstructFolder, - ConstructObject + ConstructObject, + BodiesFolder, + BodieswObject }; }; -- 2.39.2