From 00740b42f9c46511b2c880857372e37195cae304 Mon Sep 17 00:00:00 2001 From: vsv Date: Thu, 10 Apr 2014 14:20:58 +0400 Subject: [PATCH] Selection manager added --- src/XGUI/CMakeLists.txt | 3 ++ src/XGUI/XGUI_Constants.h | 7 ++++- src/XGUI/XGUI_DataTreeModel.h | 48 +++++++++++++++++++++++++++++ src/XGUI/XGUI_DocumentDataModel.cpp | 14 ++++++--- src/XGUI/XGUI_DocumentDataModel.h | 10 ++++-- src/XGUI/XGUI_ObjectsBrowser.cpp | 22 +++++++++++-- src/XGUI/XGUI_ObjectsBrowser.h | 16 ++++++++-- src/XGUI/XGUI_PartDataModel.cpp | 43 +++++++++++++++++++++----- src/XGUI/XGUI_PartDataModel.h | 41 ++++++++---------------- src/XGUI/XGUI_SelectionMgr.cpp | 40 ++++++++++++++++++++++++ src/XGUI/XGUI_SelectionMgr.h | 31 +++++++++++++++++++ src/XGUI/XGUI_ViewWindow.cpp | 4 +-- src/XGUI/XGUI_Workshop.cpp | 2 ++ src/XGUI/XGUI_Workshop.h | 6 ++++ 14 files changed, 239 insertions(+), 48 deletions(-) create mode 100644 src/XGUI/XGUI_DataTreeModel.h create mode 100644 src/XGUI/XGUI_SelectionMgr.cpp create mode 100644 src/XGUI/XGUI_SelectionMgr.h diff --git a/src/XGUI/CMakeLists.txt b/src/XGUI/CMakeLists.txt index e1993f5d9..33d7a9765 100644 --- a/src/XGUI/CMakeLists.txt +++ b/src/XGUI/CMakeLists.txt @@ -22,6 +22,8 @@ SET(PROJECT_HEADERS XGUI_DocumentDataModel.h XGUI_PartDataModel.h XGUI_ObjectsBrowser.h + XGUI_DataTreeModel.h + XGUI_SelectionMgr.h ) SET(PROJECT_AUTOMOC @@ -46,6 +48,7 @@ SET(PROJECT_SOURCES XGUI_DocumentDataModel.cpp XGUI_PartDataModel.cpp XGUI_ObjectsBrowser.cpp + XGUI_SelectionMgr.cpp ) SET(PROJECT_RESOURCES diff --git a/src/XGUI/XGUI_Constants.h b/src/XGUI/XGUI_Constants.h index 643dbbae8..f523c3ed8 100644 --- a/src/XGUI/XGUI_Constants.h +++ b/src/XGUI/XGUI_Constants.h @@ -2,14 +2,19 @@ #define XGUI_Constants_H #include +#include -//! This file contains varioous constants used in the application +//! This file contains various constants used in the application typedef QList QIntList; //!< list of int values typedef QList QShortList; //!< list of short int values typedef QList QDoubleList; //!< list of double values +//! Pointer on feature object +typedef std::shared_ptr FeaturePtr; +typedef QList QFeatureList; //!< List of features + namespace XGUI { diff --git a/src/XGUI/XGUI_DataTreeModel.h b/src/XGUI/XGUI_DataTreeModel.h new file mode 100644 index 000000000..3be26bd2a --- /dev/null +++ b/src/XGUI/XGUI_DataTreeModel.h @@ -0,0 +1,48 @@ + + +#ifndef XGUI_DataTreeModel_H +#define XGUI_DataTreeModel_H + +#include +#include + +#include "XGUI_Constants.h" + +/**\class XGUI_FeaturesModel + * \ingroup GUI + * \brief Abstaract class of model object which operates with features data. + */ +class XGUI_FeaturesModel : public QAbstractItemModel +{ +public: + XGUI_FeaturesModel(const std::shared_ptr& theDocument, QObject* theParent): + QAbstractItemModel(theParent), myDocument(theDocument) {} + + //! Returns Feature object by the given Model index. + //! Returns 0 if the given index is not index of a feature + virtual FeaturePtr feature(const QModelIndex& theIndex) const = 0; + +protected: + std::shared_ptr myDocument; +}; + + +/**\class XGUI_PartModel + * \ingroup GUI + * \brief Abstaract class of model object which operates with parts data. + */ +class XGUI_PartModel : public XGUI_FeaturesModel +{ +public: + XGUI_PartModel(const std::shared_ptr& theDocument, QObject* theParent): + XGUI_FeaturesModel(theDocument, theParent) {} + + void setPartId(int theId) { myId = theId; } + +protected: + //! Id of the current part object in the document + int myId; +}; + + +#endif \ No newline at end of file diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index ba23d472d..0cb623369 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -25,8 +25,7 @@ XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent) 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); + myModel = new XGUI_TopDataModel(myDocument, this); } @@ -46,10 +45,10 @@ void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage) myPartModels.removeLast(); } while (myPartModels.size() < aNbParts) { - myPartModels.append(new XGUI_PartDataModel(this)); + myPartModels.append(new XGUI_PartDataModel(myDocument, this)); } for (int i = 0; i < myPartModels.size(); i++) - myPartModels.at(i)->setDocument(myDocument, i); + myPartModels.at(i)->setPartId(i); } clearModelIndexes(); endResetModel(); @@ -156,4 +155,11 @@ void XGUI_DocumentDataModel::clearModelIndexes() for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) delete (*aIt); myIndexes.clear(); +} + +FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const +{ + QModelIndex aIndex = toSourceModel(theIndex); + const XGUI_FeaturesModel* aModel = dynamic_cast(aIndex.model()); + return aModel->feature(aIndex); } \ No newline at end of file diff --git a/src/XGUI/XGUI_DocumentDataModel.h b/src/XGUI/XGUI_DocumentDataModel.h index 6e4a8969e..8c57a61de 100644 --- a/src/XGUI/XGUI_DocumentDataModel.h +++ b/src/XGUI/XGUI_DocumentDataModel.h @@ -2,13 +2,15 @@ #ifndef XGUI_DocumentDataModel_H #define XGUI_DocumentDataModel_H +#include "XGUI_Constants.h" + #include #include #include class ModelAPI_Document; -class XGUI_PartDataModel; +class XGUI_PartModel; class XGUI_TopDataModel; /**\class XGUI_DocumentDataModel @@ -43,6 +45,10 @@ public: virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + //! Returns Feature object by the given Model index. + //! Returns 0 if the given index is not index of a feature + FeaturePtr feature(const QModelIndex& theIndex) const; + private: //! Converts QModelIndex of this model to QModelIndex of a one of sub-models. @@ -64,7 +70,7 @@ private: XGUI_TopDataModel* myModel; //! Data models for Parts data tree representation (one data model per a one part) - QList myPartModels; + QList myPartModels; //! 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 53e804401..8355754d6 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -5,8 +5,11 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) : QTreeView(theParent) { setHeaderHidden(true); - XGUI_DocumentDataModel* aDocModel = new XGUI_DocumentDataModel(this); - setModel(aDocModel); + myDocModel = new XGUI_DocumentDataModel(this); + setModel(myDocModel); + + connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), + this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); } @@ -14,3 +17,18 @@ XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser() { } + + +void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected, + const QItemSelection& theDeselected) +{ + mySelectedData.clear(); + QModelIndexList aIndexes = selectionModel()->selectedIndexes(); + QModelIndexList::const_iterator aIt; + for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) { + FeaturePtr aFeature = myDocModel->feature(*aIt); + if (aFeature) + mySelectedData.append(aFeature); + } + emit selectionChanged(); +} \ No newline at end of file diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index c2d56ee38..dff46d5ae 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -2,6 +2,8 @@ #ifndef XGUI_ObjectsBrowser_H #define XGUI_ObjectsBrowser_H +#include "XGUI_Constants.h" + #include class XGUI_DocumentDataModel; @@ -13,11 +15,21 @@ public: XGUI_ObjectsBrowser(QWidget* theParent); virtual ~XGUI_ObjectsBrowser(); - QAbstractItemModel* dataModel() const { return myDocModel; } + XGUI_DocumentDataModel* dataModel() const { return myDocModel; } + + QFeatureList selectedData() const { return mySelectedData; } + +signals: + void selectionChanged(); + + +private slots: + void onSelectionChanged(const QItemSelection& theSelected, const QItemSelection& theDeselected); private: + XGUI_DocumentDataModel* myDocModel; - QAbstractItemModel* myDocModel; + QFeatureList mySelectedData; }; #endif \ No newline at end of file diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp index f48a27a7c..4f8423f46 100644 --- a/src/XGUI/XGUI_PartDataModel.cpp +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -9,8 +9,8 @@ #include -XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent) - : QAbstractItemModel(theParent) +XGUI_TopDataModel::XGUI_TopDataModel(const std::shared_ptr& theDocument, QObject* theParent) + : XGUI_FeaturesModel(theDocument, theParent) { } @@ -124,12 +124,26 @@ bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const return rowCount(theParent) > 0; } +FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const +{ + switch (theIndex.internalId()) { + case ParamsFolder: + case ConstructFolder: + return 0; + case ParamObject: + return myDocument->feature(PARAMETERS_GROUP, theIndex.row()); + case ConstructObject: + return myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + } + return 0; +} + //****************************************************************** //****************************************************************** //****************************************************************** -XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent) - : QAbstractItemModel(theParent) +XGUI_PartDataModel::XGUI_PartDataModel(const std::shared_ptr& theDocument, QObject* theParent) + : XGUI_PartModel(theDocument, theParent) { } @@ -239,8 +253,7 @@ QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelInd QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const { - int aId = (int)theIndex.internalId(); - switch (aId) { + switch (theIndex.internalId()) { case MyRoot: return QModelIndex(); case ParamsFolder: @@ -264,4 +277,20 @@ std::shared_ptr XGUI_PartDataModel::featureDocument() const { std::shared_ptr aFeature = myDocument->feature(PARTS_GROUP, myId); return aFeature->data()->docRef("PartDocument")->value(); -} \ No newline at end of file +} + +FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const +{ + switch (theIndex.internalId()) { + case MyRoot: + return myDocument->feature(PARTS_GROUP, myId); + case ParamsFolder: + case ConstructFolder: + return 0; + case ParamObject: + return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row()); + case ConstructObject: + return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + } + return 0; +} diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h index c8fb16629..559f00732 100644 --- a/src/XGUI/XGUI_PartDataModel.h +++ b/src/XGUI/XGUI_PartDataModel.h @@ -2,28 +2,19 @@ #ifndef XGUI_PartDataModel_H #define XGUI_PartDataModel_H -#include - -class ModelAPI_Feature; -class ModelAPI_Document; +#include "XGUI_DataTreeModel.h" /**\class XGUI_TopDataModel * \ingroup GUI * \brief This is a data model for Object Browser (QTreeView). * It represents only upper part of data tree (non-parts tree items) */ -class XGUI_TopDataModel : public QAbstractItemModel +class XGUI_TopDataModel : public XGUI_FeaturesModel { Q_OBJECT public: - XGUI_TopDataModel(QObject* theParent); + XGUI_TopDataModel(const std::shared_ptr& theDocument, QObject* theParent); virtual ~XGUI_TopDataModel(); - - //! Set a document object - virtual void setDocument(const std::shared_ptr& theDoc) - { - myDocument = theDoc; - } // Reimplementation from QAbstractItemModel virtual QVariant data(const QModelIndex& theIndex, int theRole) const; @@ -40,6 +31,10 @@ public: virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + //! Returns Feature object by the given Model index. + //! Returns 0 if the given index is not index of a feature + virtual FeaturePtr feature(const QModelIndex& theIndex) const; + private: //! Types of QModelIndexes enum DataIds { @@ -49,8 +44,6 @@ private: ConstructObject }; - //! Document object - std::shared_ptr myDocument; }; @@ -59,20 +52,13 @@ private: * \brief This is a data model for Object Browser (QTreeView). * It represents data tree only of a one part */ -class XGUI_PartDataModel : public QAbstractItemModel +class XGUI_PartDataModel : public XGUI_PartModel { Q_OBJECT public: - XGUI_PartDataModel(QObject* theParent); + XGUI_PartDataModel(const std::shared_ptr& theDocument, QObject* theParent); virtual ~XGUI_PartDataModel(); - //! Set a document object and Id of a part in the document - virtual void setDocument(const std::shared_ptr& theDoc, int theId) - { - myDocument = theDoc; - myId = theId; - } - // Reimplementation from QAbstractItemModel virtual QVariant data(const QModelIndex& theIndex, int theRole) const; virtual QVariant headerData(int section, Qt::Orientation orientation, @@ -88,6 +74,10 @@ public: virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + //! Returns Feature object by the given Model index. + //! Returns 0 if the given index is not index of a feature + virtual FeaturePtr feature(const QModelIndex& theIndex) const; + private: std::shared_ptr featureDocument() const; @@ -100,11 +90,6 @@ private: ConstructObject }; - //! Document object - std::shared_ptr myDocument; - - //! Id of the current part object in the document - int myId; }; #endif \ No newline at end of file diff --git a/src/XGUI/XGUI_SelectionMgr.cpp b/src/XGUI/XGUI_SelectionMgr.cpp new file mode 100644 index 000000000..9d1d1e724 --- /dev/null +++ b/src/XGUI/XGUI_SelectionMgr.cpp @@ -0,0 +1,40 @@ +#include "XGUI_SelectionMgr.h" +#include "XGUI_Workshop.h" +#include "XGUI_MainWindow.h" +#include "XGUI_ObjectsBrowser.h" + +#include +#include +#include +#include + + + +XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent) : + QObject(theParent), myWorkshop(theParent) +{ + XGUI_ObjectsBrowser* aObjBrowser = myWorkshop->mainWindow()->objectBrowser(); + + connect(aObjBrowser, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); +} + + +XGUI_SelectionMgr::~XGUI_SelectionMgr() +{ +} + +void XGUI_SelectionMgr::onSelectionChanged() +{ + XGUI_ObjectsBrowser* aObjBrowser = myWorkshop->mainWindow()->objectBrowser(); + mySelectedData = aObjBrowser->selectedData(); + + // Set current document + if (mySelectedData.size() > 0) { + FeaturePtr aFeature = mySelectedData.first(); + + std::shared_ptr aMgr = ModelAPI_PluginManager::get(); + aMgr->setCurrentDocument(aFeature->data()->docRef("PartDocument")->value()); + } + + emit selectionChanged(); +} \ No newline at end of file diff --git a/src/XGUI/XGUI_SelectionMgr.h b/src/XGUI/XGUI_SelectionMgr.h new file mode 100644 index 000000000..5f50ca3d8 --- /dev/null +++ b/src/XGUI/XGUI_SelectionMgr.h @@ -0,0 +1,31 @@ +#ifndef XGUI_SelectionMgr_H +#define XGUI_SelectionMgr_H + +#include "XGUI_Constants.h" +#include + +class XGUI_Workshop; + +class XGUI_SelectionMgr : public QObject +{ + Q_OBJECT +public: + XGUI_SelectionMgr(XGUI_Workshop* theParent); + virtual ~XGUI_SelectionMgr(); + + QFeatureList selectedData() const { return mySelectedData; } + + +signals: + void selectionChanged(); + +public slots: + void onSelectionChanged(); + +private: + XGUI_Workshop* myWorkshop; + + QFeatureList mySelectedData; +}; + +#endif; \ No newline at end of file diff --git a/src/XGUI/XGUI_ViewWindow.cpp b/src/XGUI/XGUI_ViewWindow.cpp index ef9e5403f..9f8edf365 100644 --- a/src/XGUI/XGUI_ViewWindow.cpp +++ b/src/XGUI/XGUI_ViewWindow.cpp @@ -961,9 +961,9 @@ void XGUI_ViewWindow::dumpView() Handle(Visual3d_View) a3dView = myViewPort->getView()->View(); if (aFmt == "PS") - a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_PostScript); + a3dView->Export(_strdup(qPrintable(aFileName)), Graphic3d_EF_PostScript); else if (aFmt == "EPS") - a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_EnhPostScript); + a3dView->Export(_strdup(qPrintable(aFileName)), Graphic3d_EF_EnhPostScript); else aPicture.save( aFileName, aFmt.toLatin1() ); QApplication::restoreOverrideCursor(); diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index d92350a07..e5552b506 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -9,6 +9,7 @@ #include "XGUI_Workshop.h" #include "XGUI_Viewer.h" #include "XGUI_WidgetFactory.h" +#include "XGUI_SelectionMgr.h" #include #include @@ -43,6 +44,7 @@ XGUI_Workshop::XGUI_Workshop() myPartSetModule(NULL) { myMainWindow = new XGUI_MainWindow(); + mySelector = new XGUI_SelectionMgr(this); } //****************************************************** diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index 6628d12ef..16c542543 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -13,6 +13,7 @@ class XGUI_MainWindow; class XGUI_Command; class XGUI_Module; class XGUI_Workbench; +class XGUI_SelectionMgr; class ModuleBase_Operation; class Config_FeatureMessage; @@ -39,6 +40,9 @@ public: return myMainWindow; } + //! Returns selection manager object + XGUI_SelectionMgr* selector() const { return mySelector; } + //! Creates and adds a new workbench (menu group) with the given name and returns it XGUI_Workbench* addWorkbench(const QString& theName); @@ -68,6 +72,8 @@ private: XGUI_MainWindow* myMainWindow; XGUI_Module* myPartSetModule; + XGUI_SelectionMgr* mySelector; + ModuleBase_Operation* myCurrentOperation; }; -- 2.39.2