From a319904887d7def795af910efc8fc52a746eb56f Mon Sep 17 00:00:00 2001 From: vsv Date: Wed, 14 May 2014 15:28:24 +0400 Subject: [PATCH] Issue #23: current document management --- src/XGUI/XGUI_DataTreeModel.h | 3 ++ src/XGUI/XGUI_DocumentDataModel.cpp | 71 +++++++++++++++++++++-------- src/XGUI/XGUI_DocumentDataModel.h | 11 ++++- src/XGUI/XGUI_ObjectsBrowser.cpp | 12 +++++ src/XGUI/XGUI_ObjectsBrowser.h | 5 ++ src/XGUI/XGUI_PartDataModel.cpp | 5 ++ src/XGUI/XGUI_PartDataModel.h | 3 ++ src/XGUI/XGUI_Workshop.cpp | 17 +++---- src/XGUI/XGUI_Workshop.h | 3 +- 9 files changed, 100 insertions(+), 30 deletions(-) diff --git a/src/XGUI/XGUI_DataTreeModel.h b/src/XGUI/XGUI_DataTreeModel.h index 676c9d8fd..b7313a922 100644 --- a/src/XGUI/XGUI_DataTreeModel.h +++ b/src/XGUI/XGUI_DataTreeModel.h @@ -48,6 +48,9 @@ public: //! Returns true if the given document is a sub-document of this tree virtual bool hasDocument(const boost::shared_ptr& theDoc) const = 0; + //! Return a Part object + virtual FeaturePtr part() const = 0; + protected: //! Id of the current part object in the document int myId; diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index 16a56174e..b5cc13697 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -17,7 +17,7 @@ XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent) - : QAbstractItemModel(theParent) + : QAbstractItemModel(theParent), myActivePart(0) { // Find Document object boost::shared_ptr aMgr = ModelAPI_PluginManager::get(); @@ -184,7 +184,7 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) if (aParent.isValid() && (aParent.internalId() == PartsFolder)) { return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole); } - return toSourceModelIndex(theIndex).data(theRole); + return toSourceModelIndex(theIndex)->data(theRole); } @@ -208,11 +208,16 @@ int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const if (theParent.internalId() == HistoryNode) { return 0; } - QModelIndex aParent = toSourceModelIndex(theParent); - if (!isSubModel(aParent.model())) + QModelIndex* aParent = toSourceModelIndex(theParent); + const QAbstractItemModel* aModel = aParent->model(); + if (!isSubModel(aModel)) return 0; - return aParent.model()->rowCount(aParent); + if (isPartSubModel(aModel)) { + if (aModel != myActivePart) + return 0; + } + return aModel->rowCount(*aParent); } int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const @@ -252,21 +257,21 @@ QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode)) return QModelIndex(); - QModelIndex aIndex = toSourceModelIndex(theIndex); - const QAbstractItemModel* aModel = aIndex.model(); + QModelIndex* aIndex = toSourceModelIndex(theIndex); + const QAbstractItemModel* aModel = aIndex->model(); if (!isSubModel(aModel)) return QModelIndex(); if (isPartSubModel(aModel)) { - if (!aModel->parent(aIndex).isValid()) { + 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; + QModelIndex aIndex1 = aModel->parent(*aIndex); + if (aIndex1.isValid()) + return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1)); + return aIndex1; } @@ -278,10 +283,10 @@ bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const } -QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const +QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const { QModelIndex* aIndexPtr = static_cast(theProxy.internalPointer()); - return (*aIndexPtr); + return aIndexPtr; } @@ -323,12 +328,12 @@ FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const int aOffset = historyOffset(); return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset); } - QModelIndex aIndex = toSourceModelIndex(theIndex); - if (!isSubModel(aIndex.model())) + QModelIndex* aIndex = toSourceModelIndex(theIndex); + if (!isSubModel(aIndex->model())) return FeaturePtr(); - const XGUI_FeaturesModel* aModel = dynamic_cast(aIndex.model()); - return aModel->feature(aIndex); + const XGUI_FeaturesModel* aModel = dynamic_cast(aIndex->model()); + return aModel->feature(*aIndex); } bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent) @@ -394,4 +399,34 @@ int XGUI_DocumentDataModel::historyOffset() const { // Nb of rows of top model + Parts folder return myModel->rowCount(QModelIndex()) + 1; +} + +bool XGUI_DocumentDataModel::activatedIndex(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()) { + beginResetModel(); + myActivePart = (myActivePart == aModel)? 0 : (XGUI_PartModel*)aModel; + endResetModel(); + return true; + } + } + return false; +} + +FeaturePtr XGUI_DocumentDataModel::activePart() const +{ + if (myActivePart) + return myActivePart->part(); + return FeaturePtr(); } \ No newline at end of file diff --git a/src/XGUI/XGUI_DocumentDataModel.h b/src/XGUI/XGUI_DocumentDataModel.h index 7214c82f4..68bfa4062 100644 --- a/src/XGUI/XGUI_DocumentDataModel.h +++ b/src/XGUI/XGUI_DocumentDataModel.h @@ -54,13 +54,19 @@ public: //! Returns 0 if the given index is not index of a feature FeaturePtr feature(const QModelIndex& theIndex) const; + //! Activates a part data model if the index is a Part node index. + //! Returns true if active part changed. + bool activatedIndex(const QModelIndex& theIndex); + + FeaturePtr activePart() const; + private: enum {PartsFolder, HistoryNode}; //! Converts QModelIndex of this model to QModelIndex of a one of sub-models. - QModelIndex toSourceModelIndex(const QModelIndex& theProxy) const; + QModelIndex* toSourceModelIndex(const QModelIndex& theProxy) const; //! Finds a pointer on QModelIndex which is equal to the given one QModelIndex* findModelIndex(const QModelIndex& theIndex) const; @@ -95,6 +101,9 @@ private: //! Data models for Parts data tree representation (one data model per a one part) QList myPartModels; + //! Active part in part editing mode + XGUI_PartModel* myActivePart; + //! List of saved QModelIndexes created by sub-models QList myIndexes; diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 8355754d6..fb43e7612 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -31,4 +31,16 @@ void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected, mySelectedData.append(aFeature); } emit selectionChanged(); +} + +void XGUI_ObjectsBrowser::mouseDoubleClickEvent(QMouseEvent* theEvent) +{ + QModelIndex aIndex = currentIndex(); + bool isChanged = myDocModel->activatedIndex(aIndex); + QTreeView::mouseDoubleClickEvent(theEvent); + if (isChanged) { + setExpanded(aIndex.parent(), true); + setExpanded(aIndex, myDocModel->hasChildren(aIndex)); + emit activePartChanged(myDocModel->activePart()); + } } \ No newline at end of file diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index b0949534b..de43e5272 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -29,7 +29,10 @@ public: signals: //! Emited when selection is changed void selectionChanged(); + void activePartChanged(FeaturePtr thePart); +protected: + virtual void mouseDoubleClickEvent(QMouseEvent* theEvent); private slots: //! Called when selection in Data Tree is changed @@ -41,6 +44,8 @@ private: //! List of currently selected data QFeatureList mySelectedData; + + //QModelIndex myActivePartIndex; }; #endif \ No newline at end of file diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp index cc5b7395c..44b74b320 100644 --- a/src/XGUI/XGUI_PartDataModel.cpp +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -384,3 +384,8 @@ QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const return createIndex(1, 0, (qint32) ConstructFolder); return QModelIndex(); } + +FeaturePtr XGUI_PartDataModel::part() const +{ + return myDocument->feature(PARTS_GROUP, myId); +} \ No newline at end of file diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h index abda0bfde..0c857f95b 100644 --- a/src/XGUI/XGUI_PartDataModel.h +++ b/src/XGUI/XGUI_PartDataModel.h @@ -94,6 +94,9 @@ public: //! Returns index corresponded to the group virtual QModelIndex findGroup(const std::string& theGroup) const; + //! Return a Part object + virtual FeaturePtr part() const; + private: boost::shared_ptr featureDocument() const; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 48a4615d2..48daf4524 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -77,7 +77,6 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) myDisplayer = new XGUI_Displayer(this); mySelector = new XGUI_SelectionMgr(this); - connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(changeCurrentDocument())); myOperationMgr = new XGUI_OperationMgr(this); myActionsMgr = new XGUI_ActionsMgr(this); @@ -597,6 +596,7 @@ QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent) aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); aObjDock->setWindowTitle(tr("Object browser")); myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock); + connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr))); aObjDock->setWidget(myObjectBrowser); return aObjDock; } @@ -662,18 +662,15 @@ void XGUI_Workshop::onFeatureTriggered() } //****************************************************** -void XGUI_Workshop::changeCurrentDocument() +void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart) { - QFeatureList aFeatures = objectBrowser()->selectedFeatures(); - - // Set current document - if (aFeatures.size() > 0) { - FeaturePtr aFeature = aFeatures.first(); - - boost::shared_ptr aMgr = ModelAPI_PluginManager::get(); - boost::shared_ptr aDocRef = aFeature->data()->docRef("PartDocument"); + boost::shared_ptr aMgr = ModelAPI_PluginManager::get(); + if (thePart) { + boost::shared_ptr aDocRef = thePart->data()->docRef("PartDocument"); if (aDocRef) aMgr->setCurrentDocument(aDocRef->value()); + } else { + aMgr->setCurrentDocument(aMgr->rootDocument()); } } diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index 922a4dd89..737b36876 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -2,6 +2,7 @@ #define XGUI_WORKSHOP_H #include "XGUI.h" +#include "XGUI_Constants.h" #include #include @@ -114,7 +115,7 @@ public slots: void hideObjectBrowser(); void onFeatureTriggered(); - void changeCurrentDocument(); + void changeCurrentDocument(FeaturePtr thePart); signals: void errorOccurred(const QString&); -- 2.39.2