From f2c03527e6ee97d0253a1d496b118be658276d97 Mon Sep 17 00:00:00 2001 From: vsv Date: Wed, 6 May 2015 17:49:57 +0300 Subject: [PATCH] Transfer Part data from Part result sub-tree to Part feature sub-tree --- src/PartSet/PartSet_DataTreeModel.h | 5 +- src/PartSet/PartSet_DocumentDataModel.cpp | 317 ++++++++++++++-------- src/PartSet/PartSet_DocumentDataModel.h | 13 +- src/PartSet/PartSet_Module.cpp | 27 +- src/PartSet/PartSet_Module.h | 4 +- src/PartSet/PartSet_PartDataModel.cpp | 160 ++++++----- src/PartSet/PartSet_PartDataModel.h | 2 +- src/XGUI/XGUI_ObjectsBrowser.cpp | 50 ++-- src/XGUI/XGUI_ObjectsBrowser.h | 7 +- src/XGUI/XGUI_Workshop.cpp | 2 - 10 files changed, 354 insertions(+), 233 deletions(-) diff --git a/src/PartSet/PartSet_DataTreeModel.h b/src/PartSet/PartSet_DataTreeModel.h index cb71be87a..da29dea26 100644 --- a/src/PartSet/PartSet_DataTreeModel.h +++ b/src/PartSet/PartSet_DataTreeModel.h @@ -70,7 +70,7 @@ class PartSet_PartModel : public PartSet_FeaturesModel /// Constructor /// \param theParent a parent object PartSet_PartModel(QObject* theParent) - : PartSet_FeaturesModel(theParent) + : PartSet_FeaturesModel(theParent), myId(-1) { } @@ -81,6 +81,9 @@ class PartSet_PartModel : public PartSet_FeaturesModel myId = theId; } + /// Returns Id of the part + int partId() const { return myId; } + //! Returns true if the given document is a sub-document of this tree //! \param theDoc a document to check virtual bool hasDocument(const DocumentPtr& theDoc) const = 0; diff --git a/src/PartSet/PartSet_DocumentDataModel.cpp b/src/PartSet/PartSet_DocumentDataModel.cpp index 2041d5cb0..bb1c4ac90 100644 --- a/src/PartSet/PartSet_DocumentDataModel.cpp +++ b/src/PartSet/PartSet_DocumentDataModel.cpp @@ -19,6 +19,8 @@ #include #include +#include + #include #include #include @@ -33,7 +35,7 @@ QMap PartSet_DocumentDataModel::myIcons; PartSet_DocumentDataModel::PartSet_DocumentDataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent), - myActivePart(0), myHistoryBackOffset(0) + myActivePartId(-1), myHistoryBackOffset(0) { // Create a top part of data tree model myModel = new PartSet_TopDataModel(this); @@ -73,11 +75,16 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptrgroupName() == ModelAPI_ResultPart::group()) { // Update only Parts group // Add a new part - int aStart = myPartModels.size(); - PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this); - aModel->setPartId(myPartModels.count()); - myPartModels.append(aModel); - insertRow(aStart, partFolderNode(0)); + int aStart = aRootDoc->size(ModelAPI_ResultPart::group()); + if (aStart > 0) { + FeaturePtr aPartFeature = ModelAPI_Feature::feature(aObject); + PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this); + int anId = aRootDoc->index(aPartFeature); + aModel->setPartId(anId); + //aModel->setPartId(aRootDoc->index(aObject)); + myPartModels[anId] = aModel; + insertRow(aStart, partFolderNode(0)); + } } else { // Update top groups (other except parts QModelIndex aIndex = myModel->findParent(aObject); int aStart = myModel->rowCount(aIndex) - 1; @@ -115,15 +122,26 @@ void PartSet_DocumentDataModel::processEvent(const std::shared_ptr= 0) {// MPV: this could be reproduced on close - removeSubModel(aStart); - removeRow(aStart, partFolderNode(0)); - if (myActivePart && (!isPartSubModel(myActivePart))) { - myActivePart = 0; - myActivePartIndex = QModelIndex(); - myModel->setItemsColor(ACTIVE_COLOR); + ObjectPtr aObj; + FeaturePtr aFeature; + int aDelId = -1; + foreach (int aId, myPartModels.keys()) { + aObj = aDoc->object(ModelAPI_Feature::group(), aId); + if (aObj.get()) { + aFeature = ModelAPI_Feature::feature(aObj); + if (aFeature.get()) { + if (aFeature->getKind() == PartSetPlugin_Part::ID()) + continue; + } } + aDelId = aId; + break; + } + if (aDelId != -1) { + deactivatePart(); + int aStart = myPartModels.size() - 1; + removeSubModel(aDelId); + removeRow(aStart, partFolderNode(0)); } } else { // Update top groups (other except parts QModelIndex aIndex = myModel->findGroup(aGroup); @@ -180,17 +198,17 @@ void PartSet_DocumentDataModel::rebuildDataTree() clearModelIndexes(); int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group()); - if (myPartModels.size() != aNbParts) { // resize internal models - while (myPartModels.size() > aNbParts) { - delete myPartModels.last(); - myPartModels.removeLast(); - } - while (myPartModels.size() < aNbParts) { - myPartModels.append(new PartSet_PartDataModel(this)); - } - for (int i = 0; i < myPartModels.size(); i++) - myPartModels.at(i)->setPartId(i); - } + //if (myPartModels.size() != aNbParts) { // resize internal models + // while (myPartModels.size() > aNbParts) { + // delete myPartModels.last(); + // myPartModels.removeLast(); + // } + // while (myPartModels.size() < aNbParts) { + // myPartModels.append(new PartSet_PartDataModel(this)); + // } + // for (int i = 0; i < myPartModels.size(); i++) + // myPartModels.at(i)->setPartId(i); + //} endResetModel(); } @@ -198,19 +216,19 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol { if (!theIndex.isValid()) return QVariant(); - QModelIndex aParent = theIndex.parent(); - if ((theIndex.column() == 1) ) { - if ((theIndex.internalId() == HistoryNode) && (!aParent.isValid())) { - switch (theRole) { - case Qt::DecorationRole: - if (theIndex.row() == lastHistoryRow()) - return QIcon(":pictures/arrow.png"); - } - } - return QVariant(); - } + //if ((theIndex.column() == 1) ) { + // if ((theIndex.internalId() == HistoryNode) && (!aParent.isValid())) { + // switch (theRole) { + // case Qt::DecorationRole: + // if (theIndex.row() == lastHistoryRow()) + // return QIcon(":pictures/arrow.png"); + // } + // } + // return QVariant(); + //} + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); switch (theIndex.internalId()) { case PartsFolder: switch (theRole) { @@ -221,7 +239,7 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol case Qt::ToolTipRole: return tr("Parts folder"); case Qt::ForegroundRole: - if (myActivePart) + if (myActivePartIndex.isValid()) return QBrush(PASSIVE_COLOR); else return QBrush(ACTIVE_COLOR); @@ -232,7 +250,6 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol case HistoryNode: { int aOffset = historyOffset(); - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset); FeaturePtr aFeature = std::dynamic_pointer_cast(aObj); if (!aFeature) @@ -251,7 +268,7 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol if (theIndex.row() > lastHistoryRow()) return QBrush(Qt::lightGray); else { - if (myActivePart) + if (myActivePartIndex.isValid()) return QBrush(PASSIVE_COLOR); else return QBrush(ACTIVE_COLOR); @@ -261,9 +278,34 @@ QVariant PartSet_DocumentDataModel::data(const QModelIndex& theIndex, int theRol } } break; + case PartResult: + { + ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), theIndex.row()); + if (aObject) { + switch (theRole) { + case Qt::DisplayRole: + return std::dynamic_pointer_cast(aObject)->data()->name().c_str(); + case Qt::DecorationRole: + return QIcon(":pictures/part_ico.png"); + case Qt::ForegroundRole: + { + if (theIndex == myActivePartIndex) + return QBrush(ACTIVE_COLOR); + else + return QBrush(PASSIVE_COLOR); + } + default: + return QVariant(); + } + } + } + break; } - if (aParent.isValid() && (aParent.internalId() == PartsFolder)) { - return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole); + QModelIndex aParent = theIndex.parent(); + if (aParent.internalId() == HistoryNode) { + int aId = aParent.row() - historyOffset(); + QModelIndex* aIndex = toSourceModelIndex(theIndex); + return myPartModels[aId]->data(*aIndex, theRole); } return toSourceModelIndex(theIndex)->data(theRole); } @@ -276,8 +318,8 @@ QVariant PartSet_DocumentDataModel::headerData(int theSection, Qt::Orientation t int PartSet_DocumentDataModel::rowCount(const QModelIndex& theParent) const { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); if (!theParent.isValid()) { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); // Size of external models int aVal = historyOffset(); // Plus history size @@ -285,12 +327,19 @@ int PartSet_DocumentDataModel::rowCount(const QModelIndex& theParent) const return aVal; } if (theParent.internalId() == PartsFolder) { - int aSize = myPartModels.size(); - return myPartModels.size(); + return aRootDoc->size(ModelAPI_ResultPart::group()); + //int aSize = myPartModels.size(); + //return myPartModels.size(); } if (theParent.internalId() == HistoryNode) { + int aId = theParent.row() - historyOffset(); + if (myPartModels.contains(aId)) + return myPartModels[aId]->rowCount(QModelIndex()); return 0; } + if (theParent.internalId() == PartResult) + return 0; + QModelIndex* aParent = toSourceModelIndex(theParent); const QAbstractItemModel* aModel = aParent->model(); if (!isSubModel(aModel)) @@ -320,18 +369,28 @@ QModelIndex PartSet_DocumentDataModel::index(int theRow, int theColumn, } else { if (theRow == aOffs) // Create Parts node aIndex = partFolderNode(theColumn); - else - // create history node + else { + // create history node or a part sub-tree + int aId = theRow - historyOffset(); aIndex = createIndex(theRow, theColumn, HistoryNode); + //if (myPartModels.contains(aId)) { + // aIndex = myPartModels[aId]->index(theRow, theColumn, QModelIndex()); + //} + } } } else { if (theParent.internalId() == PartsFolder) { - 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, PartResult); + } else { + if (theParent.internalId() == HistoryNode) { + int aId = theParent.row() - historyOffset(); + aIndex = myPartModels[aId]->index(theRow, theColumn, QModelIndex()); + } else { + QModelIndex* aParent = (QModelIndex*) theParent.internalPointer(); + aIndex = aParent->model()->index(theRow, theColumn, (*aParent)); + } + aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex)); } - aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex)); } return aIndex; } @@ -341,18 +400,21 @@ QModelIndex PartSet_DocumentDataModel::parent(const QModelIndex& theIndex) const if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode)) return QModelIndex(); + if (theIndex.internalId() == PartResult) + return partFolderNode(theIndex.column()); + QModelIndex* aIndex = toSourceModelIndex(theIndex); const QAbstractItemModel* aModel = aIndex->model(); if (!isSubModel(aModel)) return QModelIndex(); - if (isPartSubModel(aModel)) { - if (!aModel->parent(*aIndex).isValid()) { - return partFolderNode(theIndex.column()); - } + QModelIndex aIndex1 = aModel->parent(*aIndex); + if (isPartSubModel(aModel) && (!aIndex1.isValid())) { + int aId = myPartModels.key((PartSet_PartModel*) aModel); + int aRow = aId + historyOffset(); + return createIndex(aRow, theIndex.column(), (qint32) HistoryNode); } - QModelIndex aIndex1 = aModel->parent(*aIndex); if (aIndex1.isValid()) return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1)); return aIndex1; @@ -411,11 +473,14 @@ ObjectPtr PartSet_DocumentDataModel::object(const QModelIndex& theIndex) const { if (theIndex.internalId() == PartsFolder) return ObjectPtr(); + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); if (theIndex.internalId() == HistoryNode) { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); int aOffset = historyOffset(); return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset); } + if (theIndex.internalId() == PartResult) { + return aRootDoc->object(ModelAPI_ResultPart::group(), theIndex.row()); + } QModelIndex* aIndex = toSourceModelIndex(theIndex); if (!isSubModel(aIndex->model())) return ObjectPtr(); @@ -447,7 +512,7 @@ bool PartSet_DocumentDataModel::removeRows(int theRow, int theCount, const QMode void PartSet_DocumentDataModel::removeSubModel(int theModelId) { - PartSet_PartModel* aModel = myPartModels.at(theModelId); + PartSet_PartModel* aModel = myPartModels[theModelId]; QIntList aToRemove; for (int i = 0; i < myIndexes.size(); i++) { if (myIndexes.at(i)->model() == aModel) @@ -461,7 +526,7 @@ void PartSet_DocumentDataModel::removeSubModel(int theModelId) aToRemove.removeLast(); } delete aModel; - myPartModels.removeAt(theModelId); + myPartModels.remove(theModelId); } bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const @@ -473,7 +538,7 @@ bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) c bool PartSet_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const { - return myPartModels.contains((PartSet_PartModel*) theModel); + return myPartModels.key((PartSet_PartModel*) theModel, -1) != -1; } QModelIndex PartSet_DocumentDataModel::partFolderNode(int theColumn) const @@ -493,52 +558,78 @@ bool PartSet_DocumentDataModel::activatePart(const QModelIndex& theIndex) if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode)) return false; - QModelIndex* aIndex = toSourceModelIndex(theIndex); - if (!aIndex) - return false; - - const QAbstractItemModel* aModel = aIndex->model(); - - if (isPartSubModel(aModel)) { - // if this is root node (Part item index) - if (!aIndex->parent().isValid()) { - if (myActivePart) - myActivePart->setItemsColor(PASSIVE_COLOR); - - if (myActivePart == aModel) { - myActivePart = 0; - myActivePartIndex = QModelIndex(); - } else { - myActivePart = (PartSet_PartModel*)aModel; - myActivePartIndex = theIndex; - } - - if (myActivePart) { - myActivePart->setItemsColor(ACTIVE_COLOR); - myModel->setItemsColor(PASSIVE_COLOR); - } else - myModel->setItemsColor(ACTIVE_COLOR); - return true; - } - } - return false; + if (theIndex.isValid() && (theIndex.internalId() == PartResult)) { + myActivePartIndex = theIndex; + myModel->setItemsColor(PASSIVE_COLOR); + if (myActivePartId != -1) + myPartModels[myActivePartId]->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); + } + //QModelIndex* aIndex = toSourceModelIndex(theIndex); + //if (!aIndex) + // return false; + + //const QAbstractItemModel* aModel = aIndex->model(); + + //if (isPartSubModel(aModel)) { + // // if this is root node (Part item index) + // if (!aIndex->parent().isValid()) { + // if (myActivePart) + // myActivePart->setItemsColor(PASSIVE_COLOR); + + // if (myActivePart == aModel) { + // myActivePart = 0; + // myActivePartIndex = QModelIndex(); + // } else { + // myActivePart = (PartSet_PartModel*)aModel; + // myActivePartIndex = theIndex; + // } + + // if (myActivePart) { + // myActivePart->setItemsColor(ACTIVE_COLOR); + // myModel->setItemsColor(PASSIVE_COLOR); + // } else + // myModel->setItemsColor(ACTIVE_COLOR); + // return true; + // } + // } + return true; } ResultPartPtr PartSet_DocumentDataModel::activePart() const { - if (myActivePart) - return myActivePart->part(); + if (myActivePartIndex.isValid()) { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + ObjectPtr aObj = aRootDoc->object(ModelAPI_ResultPart::group(), myActivePartIndex.row()); + return std::dynamic_pointer_cast(aObj); + } return ResultPartPtr(); } +QModelIndex PartSet_DocumentDataModel::activePartTree() const +{ + if (myActivePartId != -1) { + return createIndex(myActivePartId + historyOffset(), 0, HistoryNode); + } + return QModelIndex(); +} + void PartSet_DocumentDataModel::deactivatePart() { - if (myActivePart) - myActivePart->setItemsColor(PASSIVE_COLOR); - myActivePart = 0; + if (myActivePartIndex.isValid()) { + if (myActivePartId != -1) + myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR); + myActivePartId = -1; myActivePartIndex = QModelIndex(); myModel->setItemsColor(ACTIVE_COLOR); } +} Qt::ItemFlags PartSet_DocumentDataModel::flags(const QModelIndex& theIndex) const { @@ -556,18 +647,24 @@ Qt::ItemFlags PartSet_DocumentDataModel::flags(const QModelIndex& theIndex) cons QModelIndex PartSet_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const { - int aRow = -1; - PartSet_PartModel* aModel = 0; - foreach (PartSet_PartModel* aPartModel, myPartModels) - { - aRow++; - if (aPartModel->part() == theObject) { - aModel = aPartModel; - break; - } - } - if (aModel) { - return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex()))); + //int aRow = -1; + //PartSet_PartModel* aModel = 0; + //foreach (PartSet_PartModel* aPartModel, myPartModels) + //{ + // aRow++; + // if (aPartModel->part() == theObject) { + // aModel = aPartModel; + // break; + // } + //} + //if (aModel) { + // return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex()))); + //} + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + int aNb = aRootDoc->size(ModelAPI_ResultPart::group()); + for (int aId = 0; aId < aNb; aId++) { + if (theObject == aRootDoc->object(ModelAPI_ResultPart::group(), aId)) + return createIndex(aId, 0, PartResult); } return QModelIndex(); } @@ -598,8 +695,7 @@ QModelIndex PartSet_DocumentDataModel::objectIndex(const ObjectPtr theObject) co } } else { PartSet_PartModel* aPartModel = 0; - foreach(PartSet_PartModel* aModel, myPartModels) - { + foreach(PartSet_PartModel* aModel, myPartModels) { if (aModel->hasDocument(aDoc)) { aPartModel = aModel; break; @@ -607,8 +703,7 @@ QModelIndex PartSet_DocumentDataModel::objectIndex(const ObjectPtr theObject) co } if (aPartModel) { QModelIndex aIndex = aPartModel->objectIndex(theObject); - return - aIndex.isValid() ? + return aIndex.isValid() ? createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) : QModelIndex(); } @@ -621,7 +716,7 @@ void PartSet_DocumentDataModel::clear() { clearModelIndexes(); clearSubModels(); - myActivePart = 0; + //myActivePart = 0; myActivePartIndex = QModelIndex(); myModel->setItemsColor(ACTIVE_COLOR); } diff --git a/src/PartSet/PartSet_DocumentDataModel.h b/src/PartSet/PartSet_DocumentDataModel.h index 942d157ba..3631f7985 100644 --- a/src/PartSet/PartSet_DocumentDataModel.h +++ b/src/PartSet/PartSet_DocumentDataModel.h @@ -11,6 +11,7 @@ #include #include +#include class ModelAPI_Document; class PartSet_PartModel; @@ -116,6 +117,9 @@ Q_OBJECT return myActivePartIndex; } + //! Returns parent index of active part tree (index of Part feature) + QModelIndex activePartTree() const; + //! Deactivates a Part void deactivatePart(); @@ -138,8 +142,9 @@ Q_OBJECT enum { - PartsFolder, - HistoryNode + PartsFolder = -100, + HistoryNode, + PartResult }; //! Converts QModelIndex of this model to QModelIndex of a one of sub-models. @@ -178,10 +183,10 @@ Q_OBJECT PartSet_TopDataModel* myModel; //! Data models for Parts data tree representation (one data model per a one part) - QList myPartModels; + QMap myPartModels; //! Active part in part editing mode - PartSet_PartModel* myActivePart; + int myActivePartId; QModelIndex myActivePartIndex; diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 8fc18d64c..2091e3c26 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -12,7 +12,6 @@ #include "PartSet_WidgetEditor.h" #include "PartSet_SketcherMgr.h" #include "PartSet_MenuMgr.h" -#include "PartSet_DocumentDataModel.h" #include #include @@ -567,8 +566,10 @@ bool PartSet_Module::deleteObjects() FeaturePtr aFeature = aDoc->addFeature(PartSetPlugin_Remove::ID()); aFeature->execute(); aMgr->finishOperation(); - } - } + } else + return false; + } else + return false; } return true; } @@ -639,23 +640,18 @@ void PartSet_Module::onViewTransformed(int theTrsfType) aDisplayer->updateViewer(); } -ModuleBase_IDocumentDataModel* PartSet_Module::dataModel() const -{ - return myDataModel; -} - void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const { QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects(); int aSelected = aObjects.size(); + SessionPtr aMgr = ModelAPI_Session::get(); if (aSelected == 1) { bool hasResult = false; bool hasFeature = false; bool hasParameter = false; ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter); - SessionPtr aMgr = ModelAPI_Session::get(); ObjectPtr aObject = aObjects.first(); if (aObject) { ResultPartPtr aPart = std::dynamic_pointer_cast(aObject); @@ -672,6 +668,10 @@ void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const if (aMgr->activeDocument() != aMgr->moduleDocument()) theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD")); } + } else if (aSelected == 0) { + // if there is no selection then it means that upper label is selected + if (aMgr->activeDocument() != aMgr->moduleDocument()) + theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD")); } } @@ -687,7 +687,7 @@ void PartSet_Module::processEvent(const std::shared_ptr& theMess SessionPtr aMgr = ModelAPI_Session::get(); DocumentPtr aActiveDoc = aMgr->activeDocument(); DocumentPtr aDoc = aMgr->moduleDocument(); - QModelIndex aOldIndex = myDataModel->activePartIndex(); + QModelIndex aOldIndex = myDataModel->activePartTree(); if (aActiveDoc == aDoc) { if (aOldIndex.isValid()) aTreeView->setExpanded(aOldIndex, false); @@ -699,12 +699,9 @@ void PartSet_Module::processEvent(const std::shared_ptr& theMess ResultPartPtr aPart = std::dynamic_pointer_cast(aDoc->object(aGrpName, i)); if (aPart->partDoc() == aActiveDoc) { QModelIndex aIndex = myDataModel->partIndex(aPart); - if ((aOldIndex != aIndex) && aOldIndex.isValid()) { - aTreeView->setExpanded(aOldIndex, false); - } if (myDataModel->activatePart(aIndex)) { - aTreeView->setExpanded(aIndex.parent(), true); - aTreeView->setExpanded(aIndex, true); + aTreeView->setExpanded(aOldIndex, false); + aTreeView->setExpanded(myDataModel->activePartTree(), true); aPalet.setColor(QPalette::Text, Qt::black); } break; diff --git a/src/PartSet/PartSet_Module.h b/src/PartSet/PartSet_Module.h index 6da7807b2..3902138cb 100644 --- a/src/PartSet/PartSet_Module.h +++ b/src/PartSet/PartSet_Module.h @@ -5,6 +5,7 @@ #include "PartSet.h" #include "PartSet_Filters.h" +#include "PartSet_DocumentDataModel.h" #include #include @@ -29,7 +30,6 @@ class ModuleBase_Operation; class ModuleBase_IViewWindow; class PartSet_MenuMgr; class PartSet_SketcherMgr; -class PartSet_DocumentDataModel; class QAction; @@ -122,7 +122,7 @@ public: PartSet_SketcherMgr* sketchMgr() const { return mySketchMgr; } /// Returns data model object for representation of data tree in Object browser - virtual ModuleBase_IDocumentDataModel* dataModel() const; + virtual ModuleBase_IDocumentDataModel* dataModel() const { return myDataModel; } /// Event Listener method /// \param theMessage an event message diff --git a/src/PartSet/PartSet_PartDataModel.cpp b/src/PartSet/PartSet_PartDataModel.cpp index 21896e81a..14308ab40 100644 --- a/src/PartSet/PartSet_PartDataModel.cpp +++ b/src/PartSet/PartSet_PartDataModel.cpp @@ -257,12 +257,12 @@ QVariant PartSet_PartDataModel::data(const QModelIndex& theIndex, int theRole) c case Qt::DisplayRole: // return a name switch (theIndex.internalId()) { - case MyRoot: { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId); - if (aObject) - return std::dynamic_pointer_cast(aObject)->data()->name().c_str(); - } + //case MyRoot: { + // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + // ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId); + // if (aObject) + // return std::dynamic_pointer_cast(aObject)->data()->name().c_str(); + //} case ParamsFolder: return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex)); case ConstructFolder: @@ -311,8 +311,8 @@ QVariant PartSet_PartDataModel::data(const QModelIndex& theIndex, int theRole) c case Qt::DecorationRole: // return an Icon switch (theIndex.internalId()) { - case MyRoot: - return QIcon(":pictures/part_ico.png"); + //case MyRoot: + // return QIcon(":pictures/part_ico.png"); case ParamsFolder: return QIcon(":pictures/params_folder.png"); case ConstructFolder: @@ -358,21 +358,26 @@ QVariant PartSet_PartDataModel::headerData(int section, Qt::Orientation orientat int PartSet_PartDataModel::rowCount(const QModelIndex& parent) const { if (!parent.isValid()) { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - if (aRootDoc->object(ModelAPI_ResultPart::group(), myId)) - return 1; - else + //DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + //if (aRootDoc->object(ModelAPI_ResultPart::group(), myId)) + // return 1; + //else + // return 0; + DocumentPtr aDoc = partDocument(); + if (aDoc.get()) { + return getRowsNumber() + aDoc->size(ModelAPI_Feature::group()); + } else return 0; } switch (parent.internalId()) { - case MyRoot: - { - DocumentPtr aDoc = partDocument(); - if (aDoc) { - return getRowsNumber() + aDoc->size(ModelAPI_Feature::group()); - } else - return 0; - } + //case MyRoot: + // { + // DocumentPtr aDoc = partDocument(); + // if (aDoc) { + // return getRowsNumber() + aDoc->size(ModelAPI_Feature::group()); + // } else + // return 0; + // } case ParamsFolder: return partDocument()->size(ModelAPI_ResultParameter::group()); case ConstructFolder: @@ -392,38 +397,57 @@ int PartSet_PartDataModel::columnCount(const QModelIndex &parent) const QModelIndex PartSet_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const { - if (!theParent.isValid()) - return createIndex(theRow, 0, (qint32) MyRoot); - - int aId = (int) theParent.internalId(); - switch (aId) { - case MyRoot: - switch (theRow) { - case 0: - return createIndex(theRow, 0, (qint32) ParamsFolder); - case 1: - return createIndex(theRow, 0, (qint32) ConstructFolder); - case 2: - return createIndex(theRow, 0, (qint32) BodiesFolder); - case 3: - { - int aSize = partDocument()->size(ModelAPI_ResultGroup::group()); - if (aSize > 0) - return createIndex(theRow, 0, (qint32) GroupsFolder); - else - return createIndex(theRow, theColumn, (qint32) HistoryObject); - } - default: + if (!theParent.isValid()) { + // return createIndex(theRow, 0, (qint32) MyRoot); + switch (theRow) { + case 0: + return createIndex(theRow, theColumn, (qint32) ParamsFolder); + case 1: + return createIndex(theRow, theColumn, (qint32) ConstructFolder); + case 2: + return createIndex(theRow, theColumn, (qint32) BodiesFolder); + case 3: + { + int aSize = partDocument()->size(ModelAPI_ResultGroup::group()); + if (aSize > 0) + return createIndex(theRow, theColumn, (qint32) GroupsFolder); + else return createIndex(theRow, theColumn, (qint32) HistoryObject); - } - case ParamsFolder: - return createIndex(theRow, 0, (qint32) ParamObject); - case ConstructFolder: - return createIndex(theRow, 0, (qint32) ConstructObject); - case BodiesFolder: - return createIndex(theRow, 0, (qint32) BodiesObject); - case GroupsFolder: - return createIndex(theRow, 0, (qint32) GroupObject); + } + default: + return createIndex(theRow, theColumn, (qint32) HistoryObject); + } + } else { + int aId = (int) theParent.internalId(); + switch (aId) { + //case MyRoot: + // switch (theRow) { + // case 0: + // return createIndex(theRow, 0, (qint32) ParamsFolder); + // case 1: + // return createIndex(theRow, 0, (qint32) ConstructFolder); + // case 2: + // return createIndex(theRow, 0, (qint32) BodiesFolder); + // case 3: + // { + // int aSize = partDocument()->size(ModelAPI_ResultGroup::group()); + // if (aSize > 0) + // return createIndex(theRow, 0, (qint32) GroupsFolder); + // else + // return createIndex(theRow, theColumn, (qint32) HistoryObject); + // } + // default: + // return createIndex(theRow, theColumn, (qint32) HistoryObject); + // } + case ParamsFolder: + return createIndex(theRow, theColumn, (qint32) ParamObject); + case ConstructFolder: + return createIndex(theRow, theColumn, (qint32) ConstructObject); + case BodiesFolder: + return createIndex(theRow, theColumn, (qint32) BodiesObject); + case GroupsFolder: + return createIndex(theRow, theColumn, (qint32) GroupObject); + } } return QModelIndex(); } @@ -431,23 +455,24 @@ QModelIndex PartSet_PartDataModel::index(int theRow, int theColumn, const QModel QModelIndex PartSet_PartDataModel::parent(const QModelIndex& theIndex) const { switch (theIndex.internalId()) { - case MyRoot: - return QModelIndex(); + //case MyRoot: + // return QModelIndex(); case ParamsFolder: case ConstructFolder: case BodiesFolder: case GroupsFolder: case HistoryObject: - return createIndex(0, 0, (qint32) MyRoot); + return QModelIndex(); + //return createIndex(0, 0, (qint32) MyRoot); case ParamObject: - return createIndex(0, 0, (qint32) ParamsFolder); + return createIndex(0, theIndex.column(), (qint32) ParamsFolder); case ConstructObject: - return createIndex(1, 0, (qint32) ConstructFolder); + return createIndex(1, theIndex.column(), (qint32) ConstructFolder); case BodiesObject: - return createIndex(2, 0, (qint32) BodiesFolder); + return createIndex(2, theIndex.column(), (qint32) BodiesFolder); case GroupObject: - return createIndex(3, 0, (qint32) GroupsFolder); + return createIndex(3, theIndex.column(), (qint32) GroupsFolder); } return QModelIndex(); } @@ -460,20 +485,21 @@ bool PartSet_PartDataModel::hasChildren(const QModelIndex& theParent) const DocumentPtr PartSet_PartDataModel::partDocument() const { DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId); - ResultPartPtr aPart = std::dynamic_pointer_cast(aObject); - if (aPart) - return aPart->partDoc(); - return DocumentPtr(); // null if not found + //ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId); + ObjectPtr aObject = aRootDoc->object(ModelAPI_Feature::group(), myId); + FeaturePtr aFeature = ModelAPI_Feature::feature(aObject); + ResultPartPtr aPart = std::dynamic_pointer_cast(aFeature->firstResult()); + //std::dynamic_pointer_cast(aObject); + return aPart->partDoc(); } ObjectPtr PartSet_PartDataModel::object(const QModelIndex& theIndex) const { switch (theIndex.internalId()) { - case MyRoot: { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - return aRootDoc->object(ModelAPI_ResultPart::group(), myId); - } + //case MyRoot: { + // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + // return aRootDoc->object(ModelAPI_ResultPart::group(), myId); + //} case ParamsFolder: case ConstructFolder: case BodiesFolder: diff --git a/src/PartSet/PartSet_PartDataModel.h b/src/PartSet/PartSet_PartDataModel.h index fc61ec1c5..de1ef2274 100644 --- a/src/PartSet/PartSet_PartDataModel.h +++ b/src/PartSet/PartSet_PartDataModel.h @@ -174,7 +174,7 @@ Q_OBJECT //! Types of QModelIndexes enum DataIds { - MyRoot, + //MyRoot, ParamsFolder, ParamObject, ConstructFolder, diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 3851b9fbe..25e2db0f5 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -229,9 +229,9 @@ void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave) //*************************************************** void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) { - bool toEnable = mySelectedData.size() == 1; - foreach(QAction* aCmd, actions()) - { + QObjectPtrList aSelectedData = selectedObjects(); + bool toEnable = aSelectedData.size() == 1; + foreach(QAction* aCmd, actions()) { aCmd->setEnabled(toEnable); } emit contextMenuRequested(theEvent); @@ -240,12 +240,9 @@ void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) //*************************************************** void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt) { - mySelectedData.clear(); + myTreeView->selectionModel()->clearSelection(); //Empty feature pointer means that selected root document - mySelectedData.append(ObjectPtr()); - - foreach(QAction* aCmd, actions()) - { + foreach(QAction* aCmd, actions()) { aCmd->setEnabled(true); } QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt)); @@ -255,13 +252,13 @@ void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt) //*************************************************** void XGUI_ObjectsBrowser::onEditItem() { - if (mySelectedData.size() > 0) { - ObjectPtr aFeature = mySelectedData.first(); + QObjectPtrList aSelectedData = selectedObjects(); + if (aSelectedData.size() > 0) { + ObjectPtr aFeature = aSelectedData.first(); if (aFeature) { // Selection happens in TreeView // Find index which corresponds the feature QModelIndex aIndex; - foreach(QModelIndex aIdx, selectedIndexes()) - { + foreach(QModelIndex aIdx, selectedIndexes()) { ObjectPtr aFea = dataModel()->object(aIdx); if (dataModel()->object(aIdx)->isSame(aFeature)) { aIndex = aIdx; @@ -272,14 +269,15 @@ void XGUI_ObjectsBrowser::onEditItem() myTreeView->setCurrentIndex(aIndex); myTreeView->edit(aIndex); } - } else { //Selection happens in Upper label - myActiveDocLbl->setReadOnly(false); - myActiveDocLbl->setFocus(); - myActiveDocLbl->selectAll(); - myActiveDocLbl->grabMouse(); - myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text()); + return; } } + //Selection happens in Upper label + myActiveDocLbl->setReadOnly(false); + myActiveDocLbl->setFocus(); + myActiveDocLbl->selectAll(); + myActiveDocLbl->grabMouse(); + myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text()); } //*************************************************** @@ -308,7 +306,6 @@ void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects) //*************************************************** void XGUI_ObjectsBrowser::clearContent() { - mySelectedData.clear(); myTreeView->clear(); } @@ -324,16 +321,21 @@ void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel) void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected, const QItemSelection& theDeselected) { - mySelectedData.clear(); - QModelIndexList aIndexes = myTreeView->selectionModel()->selectedIndexes(); + emit selectionChanged(); +} + +QObjectPtrList XGUI_ObjectsBrowser::selectedObjects() const +{ + QObjectPtrList aList; + QModelIndexList aIndexes = selectedIndexes(); ModuleBase_IDocumentDataModel* aModel = dataModel(); QModelIndexList::const_iterator aIt; for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) { if ((*aIt).column() == 0) { ObjectPtr aObject = aModel->object(*aIt); if (aObject) - mySelectedData.append(aObject); + aList.append(aObject); } } - emit selectionChanged(); -} + return aList; +} \ No newline at end of file diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index 169615c3d..e5965b0dd 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -73,10 +73,7 @@ Q_OBJECT } //! Returns list of currently selected objects - QObjectPtrList selectedObjects() const - { - return mySelectedData; - } + QObjectPtrList selectedObjects() const; /// Set selected list of objects /// \param theObjects list of objects to select @@ -140,8 +137,6 @@ signals: QLineEdit* myActiveDocLbl; XGUI_DataTree* myTreeView; - - QObjectPtrList mySelectedData; }; #endif diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index ee91afe76..20d21f791 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -1139,8 +1139,6 @@ QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent) "::title { position: relative; padding-left: 5px; text-align: left center }"); myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock); myObjectBrowser->setDataModel(myModule->dataModel()); - //connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this, - // SLOT(changeCurrentDocument(ObjectPtr))); aObjDock->setWidget(myObjectBrowser); myContextMenuMgr->connectObjectBrowser(); -- 2.39.2