X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FPartSet%2FPartSet_DocumentDataModel.cpp;h=84ac7a1697ea1820628022c43d0111c696a38c67;hb=857b1f72d9703c46c6c8c9bb239821d314344c86;hp=74df2229e96f3af191654653bf4d5c74e33c91dd;hpb=915fb2da2b16887c46305221513fceac0a1dab0f;p=modules%2Fshaper.git diff --git a/src/PartSet/PartSet_DocumentDataModel.cpp b/src/PartSet/PartSet_DocumentDataModel.cpp index 74df2229e..84ac7a169 100644 --- a/src/PartSet/PartSet_DocumentDataModel.cpp +++ b/src/PartSet/PartSet_DocumentDataModel.cpp @@ -2,6 +2,7 @@ #include "PartSet_DocumentDataModel.h" #include "PartSet_PartDataModel.h" +#include "PartSet_TopDataModel.h" #include "PartSet_Module.h" //#include "XGUI_Tools.h" @@ -36,7 +37,7 @@ QMap PartSet_DocumentDataModel::myIcons; PartSet_DocumentDataModel::PartSet_DocumentDataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent), - myActivePartId(-1) + myActivePartModel(0) { // Create a top part of data tree model myModel = new PartSet_TopDataModel(this); @@ -82,8 +83,8 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptrindex(aPartFeature); - aModel->setPartId(anId); - myPartModels[anId] = aModel; + aModel->setPart(aPartFeature); + myPartModels.append(aModel); insertRow(aStart, partFolderNode(0)); } } else { // Update top groups (other except parts @@ -123,27 +124,22 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptrobject(ModelAPI_Feature::group(), aId); - if (aObj.get()) { - aFeature = ModelAPI_Feature::feature(aObj); - if (aFeature.get()) { - if (aFeature->getKind() == PartSetPlugin_Part::ID()) - continue; - } + PartSet_PartModel* aDelPartModel = 0; + foreach (PartSet_PartModel* aPartModel, myPartModels) { + if (aPartModel->position() == -1) { + aDelPartModel = aPartModel; + break; } - aDelId = aId; - break; } - if (aDelId != -1) { + if (aDelPartModel) { deactivatePart(); int aStart = myPartModels.size() - 1; - removeSubModel(aDelId); + removeSubModel(aDelPartModel); removeRow(aStart, partFolderNode(0)); } + } if (aGroup == ModelAPI_Feature::group()) { // Update History node + int aRow = historyOffset() + aRootDoc->size(ModelAPI_Feature::group()); + removeRow(aRow); } else { // Update top groups (other except parts QModelIndex aIndex = myModel->findGroup(aGroup); int aStart = myModel->rowCount(aIndex); @@ -160,9 +156,15 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptrfindGroup(aGroup); - int aStart = aPartModel->rowCount(aIndex); - aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)); - removeRow(aStart, aIndex); + if (aIndex.isValid()) { + int aStart = aPartModel->rowCount(aIndex); + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)); + removeRow(aStart, aIndex); + } else { + int aRow = aPartModel->rowCount(); + aIndex = createIndex(aPartModel->position() + historyOffset(), 0, HistoryNode); + removeRow(aRow, aIndex); + } } } } @@ -201,21 +203,13 @@ void PartSet_DocumentDataModel::rebuildDataTree() // Delete extra models ObjectPtr aObj; FeaturePtr aFeature; - QIntList aDelList; - foreach (int aId, myPartModels.keys()) { - aObj = aRootDoc->object(ModelAPI_Feature::group(), aId); - if (aObj.get()) { - aFeature = ModelAPI_Feature::feature(aObj); - if (aFeature.get()) { - if (aFeature->getKind() == PartSetPlugin_Part::ID()) - continue; - } - } - aDelList.append(aId); - break; + QList aDelList; + foreach (PartSet_PartModel* aPartModel, myPartModels) { + if (aPartModel->position() == -1) + aDelList.append(aPartModel); } - foreach (int aId, aDelList) { - removeSubModel(aId); + foreach (PartSet_PartModel* aPartModel, aDelList) { + removeSubModel(aPartModel); } // Add non existing models int aHistNb = aRootDoc->size(ModelAPI_Feature::group()); @@ -223,10 +217,10 @@ void PartSet_DocumentDataModel::rebuildDataTree() aObj = aRootDoc->object(ModelAPI_Feature::group(), i); aFeature = std::dynamic_pointer_cast(aObj); if (aFeature->getKind() == PartSetPlugin_Part::ID()) { - if (!myPartModels.contains(i)) { + if (!findPartModel(aFeature)) { PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this); - aModel->setPartId(i); - myPartModels[i] = aModel; + aModel->setPart(aFeature); + myPartModels.append(aModel); } } } @@ -295,7 +289,20 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol case Qt::DecorationRole: return featureIcon(aFeature); case Qt::ToolTipRole: - return tr("Feature object"); + { + ResultPtr aResult = aFeature->firstResult(); + bool isResultAndNotLoaded = false; + if( aResult.get() ) + { + ResultPartPtr aResultPart = std::dynamic_pointer_cast( aResult ); + if( aResultPart.get() ) + isResultAndNotLoaded = !aResultPart->isActivated(); + } + if( isResultAndNotLoaded ) + return tr( "The part should be activated before the user may edit it" ); + else + return tr( "Feature object" ); + } case Qt::ForegroundRole: if (theIndex.row() > lastHistoryRow()) return QBrush(Qt::lightGray); @@ -336,7 +343,7 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol if (aParent.internalId() == HistoryNode) { int aId = aParent.row() - historyOffset(); QModelIndex* aIndex = toSourceModelIndex(theIndex); - return myPartModels[aId]->data(*aIndex, theRole); + return findPartModel(aId)->data(*aIndex, theRole); } return toSourceModelIndex(theIndex)->data(theRole); } @@ -349,7 +356,10 @@ QVariant PartSet_DocumentDataModel::headerData(int theSection, Qt::Orientation t int PartSet_DocumentDataModel::rowCount(const QModelIndex& theParent) const { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + SessionPtr aSession = ModelAPI_Session::get(); + if (!aSession->hasModuleDocument()) + return 0; + DocumentPtr aRootDoc = aSession->moduleDocument(); if (!theParent.isValid()) { // Size of external models int aVal = historyOffset(); @@ -364,8 +374,9 @@ int PartSet_DocumentDataModel::rowCount(const QModelIndex& theParent) const } if (theParent.internalId() == HistoryNode) { int aId = theParent.row() - historyOffset(); - if (myPartModels.contains(aId)) - return myPartModels[aId]->rowCount(QModelIndex()); + PartSet_PartModel* aModel = findPartModel(aId); + if (aModel) + return aModel->rowCount(QModelIndex()); return 0; } if (theParent.internalId() == PartResult) @@ -411,7 +422,7 @@ QModelIndex PartSet_DocumentDataModel::index(int theRow, int theColumn, } else { if (theParent.internalId() == HistoryNode) { int aId = theParent.row() - historyOffset(); - aIndex = myPartModels[aId]->index(theRow, theColumn, QModelIndex()); + aIndex = findPartModel(aId)->index(theRow, theColumn, QModelIndex()); } else { QModelIndex* aParent = (QModelIndex*) theParent.internalPointer(); aIndex = aParent->model()->index(theRow, theColumn, (*aParent)); @@ -436,8 +447,9 @@ QModelIndex PartSet_DocumentDataModel::parent(const QModelIndex& theIndex) const return QModelIndex(); QModelIndex aIndex1 = aModel->parent(*aIndex); - if (isPartSubModel(aModel) && (!aIndex1.isValid())) { - int aId = myPartModels.key((PartSet_PartModel*) aModel); + const PartSet_PartModel* aPartModel = dynamic_cast(aModel); + if (aPartModel && (!aIndex1.isValid())) { + int aId = aPartModel->position(); int aRow = aId + historyOffset(); return createIndex(aRow, 0, (qint32) HistoryNode); } @@ -494,6 +506,7 @@ void PartSet_DocumentDataModel::clearSubModels() foreach (PartSet_PartModel* aPart, myPartModels) delete aPart; myPartModels.clear(); + myActivePartModel = 0; } ObjectPtr PartSet_DocumentDataModel::object(const QModelIndex& theIndex) const @@ -539,10 +552,15 @@ bool PartSet_DocumentDataModel::removeRows(int theRow, int theCount, const QMode void PartSet_DocumentDataModel::removeSubModel(int theModelId) { - PartSet_PartModel* aModel = myPartModels[theModelId]; + PartSet_PartModel* aModel = myPartModels.at(theModelId); + removeSubModel(aModel); +} + +void PartSet_DocumentDataModel::removeSubModel(PartSet_PartModel* theModel) +{ QIntList aToRemove; for (int i = 0; i < myIndexes.size(); i++) { - if (myIndexes.at(i)->model() == aModel) + if (myIndexes.at(i)->model() == theModel) aToRemove.append(i); } int aId; @@ -552,10 +570,13 @@ void PartSet_DocumentDataModel::removeSubModel(int theModelId) myIndexes.removeAt(aId); aToRemove.removeLast(); } - delete aModel; - myPartModels.remove(theModelId); + if (theModel == myActivePartModel) + myActivePartModel = 0; + myPartModels.removeAll(theModel); + delete theModel; } + bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const { if (theModel == myModel) @@ -565,7 +586,7 @@ bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) c bool PartSet_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const { - return myPartModels.key((PartSet_PartModel*) theModel, -1) != -1; + return myPartModels.contains((PartSet_PartModel*) theModel); } QModelIndex PartSet_DocumentDataModel::partFolderNode(int theColumn) const @@ -588,15 +609,16 @@ bool PartSet_DocumentDataModel::activatePart(const QModelIndex& theIndex) if (theIndex.isValid() && (theIndex.internalId() == PartResult)) { myActivePartIndex = theIndex; myModel->setItemsColor(PASSIVE_COLOR); - if (myActivePartId != -1) - myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR); + if (myActivePartModel) + myActivePartModel->setItemsColor(PASSIVE_COLOR); // Find activated part feature by its ID ResultPartPtr aPartRes = activePart(); FeaturePtr aFeature = ModelAPI_Feature::feature(aPartRes); - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - myActivePartId = aRootDoc->index(aFeature); - myPartModels[myActivePartId]->setItemsColor(ACTIVE_COLOR); + if (aFeature.get()) { + myActivePartModel = findPartModel(aFeature); + myActivePartModel->setItemsColor(ACTIVE_COLOR); + } } return true; } @@ -613,8 +635,8 @@ ResultPartPtr PartSet_DocumentDataModel::activePart() const QModelIndex PartSet_DocumentDataModel::activePartTree() const { - if (myActivePartId != -1) { - return createIndex(myActivePartId + historyOffset(), 0, HistoryNode); + if (myActivePartModel) { + return createIndex(myActivePartModel->position() + historyOffset(), 0, HistoryNode); } return QModelIndex(); } @@ -622,9 +644,9 @@ QModelIndex PartSet_DocumentDataModel::activePartTree() const void PartSet_DocumentDataModel::deactivatePart() { if (myActivePartIndex.isValid()) { - if (myActivePartId != -1) - myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR); - myActivePartId = -1; + if (myActivePartModel) + myActivePartModel->setItemsColor(PASSIVE_COLOR); + myActivePartModel = 0; myActivePartIndex = QModelIndex(); myModel->setItemsColor(ACTIVE_COLOR); } @@ -634,7 +656,7 @@ Qt::ItemFlags PartSet_DocumentDataModel::flags(const QModelIndex& theIndex) cons { if ((theIndex.internalId() >= PartsFolder) && (theIndex.internalId() <= PartResult)) { Qt::ItemFlags aFlags = Qt::ItemIsSelectable; - if (object(theIndex)) { + if (object(theIndex).get()) { aFlags |= Qt::ItemIsEditable; } // Disable items which are below of last history row @@ -648,7 +670,12 @@ Qt::ItemFlags PartSet_DocumentDataModel::flags(const QModelIndex& theIndex) cons } else { QModelIndex* aIndex = toSourceModelIndex(theIndex); const QAbstractItemModel* aModel = aIndex->model(); - return aModel->flags(*aIndex); + Qt::ItemFlags aFlags = aModel->flags(*aIndex); + if (aModel == myModel) { + if (myModel->object(*aIndex).get()) + aFlags |= Qt::ItemIsEditable; + } + return aFlags; } } @@ -788,8 +815,13 @@ void PartSet_DocumentDataModel::onMouseDoubleClick(const QModelIndex& theIndex) { if (theIndex.column() != 1) return; + if (flags(theIndex) == 0) + return; QTreeView* aTreeView = dynamic_cast(sender()); if ((theIndex.internalId() >= PartsFolder) && (theIndex.internalId() <= PartResult)) { + if (myActivePartModel) + // It means that the root document is not active + return; QModelIndex aNewIndex; if (theIndex.internalId() == HistoryNode) aNewIndex = theIndex; @@ -823,3 +855,22 @@ void PartSet_DocumentDataModel::onMouseDoubleClick(const QModelIndex& theIndex) } } } + + +PartSet_PartModel* PartSet_DocumentDataModel::findPartModel(FeaturePtr thePart) const +{ + foreach (PartSet_PartModel* aModel, myPartModels) { + if (aModel->part() == thePart) + return aModel; + } + return 0; +} + +PartSet_PartModel* PartSet_DocumentDataModel::findPartModel(int thePosition) const +{ + foreach (PartSet_PartModel* aModel, myPartModels) { + if (aModel->position() == thePosition) + return aModel; + } + return 0; +} \ No newline at end of file