From 9b59be2733fd1fe72294588b1f2ddb692d9b6b4f Mon Sep 17 00:00:00 2001 From: vsv Date: Wed, 21 May 2014 18:08:28 +0400 Subject: [PATCH] Activate a Part on its creation (issue #23) --- src/XGUI/XGUI_DataTreeModel.h | 4 ++ src/XGUI/XGUI_DocumentDataModel.cpp | 33 +++++++++---- src/XGUI/XGUI_DocumentDataModel.h | 4 ++ src/XGUI/XGUI_ObjectsBrowser.cpp | 10 ++-- src/XGUI/XGUI_ObjectsBrowser.h | 2 +- src/XGUI/XGUI_PartDataModel.cpp | 73 ++++++++++++++++++++++------- src/XGUI/XGUI_PartDataModel.h | 10 +++- src/XGUI/XGUI_Workshop.cpp | 43 +++++++++++------ src/XGUI/XGUI_Workshop.h | 10 ++-- 9 files changed, 142 insertions(+), 47 deletions(-) diff --git a/src/XGUI/XGUI_DataTreeModel.h b/src/XGUI/XGUI_DataTreeModel.h index 42c609dac..05c5f7e12 100644 --- a/src/XGUI/XGUI_DataTreeModel.h +++ b/src/XGUI/XGUI_DataTreeModel.h @@ -25,6 +25,10 @@ public: //! Returns 0 if the given index is not index of a feature virtual FeaturePtr feature(const QModelIndex& theIndex) const = 0; + //! Returns QModelIndex which corresponds to the given feature + //! If the feature is not found then index is not valid + virtual QModelIndex featureIndex(const FeaturePtr& theFeature) const = 0; + //! Returns parent index of the given feature virtual QModelIndex findParent(const FeaturePtr& theFeature) const = 0; diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index e459f9cc2..30d3b9a0f 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -24,7 +24,7 @@ XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent) : QAbstractItemModel(theParent), myActivePart(0) { // Find Document object - boost::shared_ptr aMgr = ModelAPI_PluginManager::get(); + PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); myDocument = aMgr->currentDocument(); // Register in event loop @@ -49,8 +49,8 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) // Created object event ******************* if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) { const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); - boost::shared_ptr aFeature = aUpdMsg->feature(); - boost::shared_ptr aDoc = aFeature->document(); + FeaturePtr aFeature = aUpdMsg->feature(); + DocumentPtr aDoc = aFeature->document(); if (aDoc == myDocument) { // If root objects if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group @@ -86,7 +86,7 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) // Deleted object event *********************** } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED)) { const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast(theMessage); - boost::shared_ptr aDoc = aUpdMsg->document(); + DocumentPtr aDoc = aUpdMsg->document(); if (aDoc == myDocument) { // If root objects if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group @@ -119,8 +119,8 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) // Deleted object event *********************** } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) { //const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); - //boost::shared_ptr aFeature = aUpdMsg->feature(); - //boost::shared_ptr aDoc = aFeature->document(); + //FeaturePtr aFeature = aUpdMsg->feature(); + //DocumentPtr aDoc = aFeature->document(); // TODO: Identify the necessary index by the modified feature QModelIndex aIndex; @@ -465,7 +465,7 @@ void XGUI_DocumentDataModel::deactivatePart() myActivePart = 0; myModel->setItemsColor(ACTIVE_COLOR); } - + Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const { Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex); @@ -473,4 +473,21 @@ Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const aFlags |= Qt::ItemIsEditable; } return aFlags; -} \ No newline at end of file +} + +QModelIndex XGUI_DocumentDataModel::partIndex(const FeaturePtr& theFeature) const +{ + int aRow = -1; + XGUI_PartModel* aModel = 0; + foreach (XGUI_PartModel* aPartModel, myPartModels) { + aRow++; + if (aPartModel->part() == theFeature) { + aModel = aPartModel; + break; + } + } + if (aModel) { + return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex()))); + } + return QModelIndex(); +} diff --git a/src/XGUI/XGUI_DocumentDataModel.h b/src/XGUI/XGUI_DocumentDataModel.h index ee76b86ce..c43b99d19 100644 --- a/src/XGUI/XGUI_DocumentDataModel.h +++ b/src/XGUI/XGUI_DocumentDataModel.h @@ -56,6 +56,10 @@ public: //! Returns 0 if the given index is not index of a feature FeaturePtr feature(const QModelIndex& theIndex) const; + //! Returns QModelIndex which corresponds to the given feature if this is a part + //! If the feature is not found then index is not valid + QModelIndex partIndex(const FeaturePtr& theFeature) 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); diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index f526690b4..e5f8c8355 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -20,6 +20,7 @@ XGUI_DataTree::XGUI_DataTree(QWidget* theParent) { setHeaderHidden(true); setModel(new XGUI_DocumentDataModel(this)); + setEditTriggers(QAbstractItemView::NoEditTriggers); connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); @@ -191,7 +192,7 @@ bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent) QMouseEvent* aEvent = (QMouseEvent*) theEvent; QPoint aPnt = mapFromGlobal(aEvent->globalPos()); if (childAt(aPnt) != myActiveDocLbl) { - closeDocNameEditing(false); + closeDocNameEditing(true); } } else if (theEvent->type() == QEvent::KeyRelease) { QKeyEvent* aEvent = (QKeyEvent*) theEvent; @@ -226,10 +227,10 @@ void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave) } //*************************************************** -void XGUI_ObjectsBrowser::activateCurrentPart(bool toActivate) +void XGUI_ObjectsBrowser::activatePart(const FeaturePtr& thePart) { - if (toActivate) { - QModelIndex aIndex = myTreeView->currentIndex(); + if (thePart) { + QModelIndex aIndex = myDocModel->partIndex(thePart); if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) { myTreeView->setExpanded(myDocModel->activePartIndex(), false); @@ -237,6 +238,7 @@ void XGUI_ObjectsBrowser::activateCurrentPart(bool toActivate) bool isChanged = myDocModel->activatedIndex(aIndex); if (isChanged) { if (myDocModel->activePartIndex().isValid()) { + myTreeView->setExpanded(aIndex.parent(), true); myTreeView->setExpanded(aIndex, true); onActivePartChanged(myDocModel->feature(aIndex)); } else { diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index fa30d2b82..b8e275ae6 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -73,7 +73,7 @@ public: XGUI_DataTree* treeView() const { return myTreeView; } //! Activates currently selected part. Signal activePartChanged will not be sent - void activateCurrentPart(bool toActivate); + void activatePart(const FeaturePtr& thePart); signals: //! Emited when selection is changed diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp index ff0f8aad8..6610b64c5 100644 --- a/src/XGUI/XGUI_PartDataModel.cpp +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -162,13 +162,7 @@ FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const QModelIndex XGUI_TopDataModel::findParent(const FeaturePtr& theFeature) const { - QString aGroup(theFeature->getGroup().c_str()); - - if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0) - return createIndex(0, 0, (qint32) ParamsFolder); - if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0) - return createIndex(1, 0, (qint32) ConstructFolder); - return QModelIndex(); + return findGroup(theFeature->getGroup().c_str()); } QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const @@ -180,6 +174,30 @@ QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const return QModelIndex(); } +QModelIndex XGUI_TopDataModel::featureIndex(const FeaturePtr& theFeature) const +{ + QModelIndex aIndex; + if (theFeature) { + std::string aGroup = theFeature->getGroup(); + int aNb = myDocument->size(aGroup); + int aRow = -1; + for (int i = 0; i < aNb; i++) { + if (myDocument->feature(aGroup, i, true) == theFeature) { + aRow = i; + break; + } + } + if (aRow != -1) { + if (aGroup.compare(PARAMETERS_GROUP) == 0) + return createIndex(aRow, 0, (qint32) ParamObject); + if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0) + return createIndex(aRow, 0, (qint32) ConstructObject); + } + } + return aIndex; +} + + //****************************************************************** //****************************************************************** @@ -319,7 +337,7 @@ QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelInd case ConstructFolder: return createIndex(theRow, 0, (qint32) ConstructObject); case BodiesFolder: - return createIndex(theRow, 0, (qint32) BodieswObject); + return createIndex(theRow, 0, (qint32) BodiesObject); } return QModelIndex(); } @@ -364,11 +382,14 @@ FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const return featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3, true); case ParamsFolder: case ConstructFolder: + case BodiesFolder: return FeaturePtr(); case ParamObject: return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row(), true); case ConstructObject: return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row(), true); + //case BodiesObject: + // return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row(), true); } return FeaturePtr(); } @@ -381,13 +402,7 @@ bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const QModelIndex XGUI_PartDataModel::findParent(const FeaturePtr& theFeature) const { - QString aGroup(theFeature->getGroup().c_str()); - - if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0) - return createIndex(0, 0, (qint32) ParamsFolder); - if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0) - return createIndex(1, 0, (qint32) ConstructFolder); - return QModelIndex(); + return findGroup(theFeature->getGroup().c_str()); } QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const @@ -402,4 +417,30 @@ QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const FeaturePtr XGUI_PartDataModel::part() const { return myDocument->feature(PARTS_GROUP, myId, true); -} \ No newline at end of file +} + +QModelIndex XGUI_PartDataModel::featureIndex(const FeaturePtr& theFeature) const +{ + QModelIndex aIndex; + if (theFeature) { + if (part() == theFeature) + return aIndex; + + std::string aGroup = theFeature->getGroup(); + int aNb = myDocument->size(aGroup); + int aRow = -1; + for (int i = 0; i < aNb; i++) { + if (myDocument->feature(aGroup, i, true) == theFeature) { + aRow = i; + break; + } + } + if (aRow != -1) { + if (aGroup.compare(PARAMETERS_GROUP) == 0) + return createIndex(aRow, 0, (qint32) ParamObject); + if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0) + return createIndex(aRow, 0, (qint32) ConstructObject); + } + } + return aIndex; +} diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h index a44dfb03c..e1533ee0b 100644 --- a/src/XGUI/XGUI_PartDataModel.h +++ b/src/XGUI/XGUI_PartDataModel.h @@ -36,6 +36,10 @@ public: //! Returns 0 if the given index is not index of a feature virtual FeaturePtr feature(const QModelIndex& theIndex) const; + //! Returns QModelIndex which corresponds to the given feature + //! If the feature is not found then index is not valid + virtual QModelIndex featureIndex(const FeaturePtr& theFeature) const; + //! Returns parent index of the given feature virtual QModelIndex findParent(const FeaturePtr& theFeature) const; @@ -85,6 +89,10 @@ public: //! Returns 0 if the given index is not index of a feature virtual FeaturePtr feature(const QModelIndex& theIndex) const; + //! Returns QModelIndex which corresponds to the given feature + //! If the feature is not found then index is not valid + virtual QModelIndex featureIndex(const FeaturePtr& theFeature) const; + //! Returns true if the given document is a sub-document of this tree virtual bool hasDocument(const DocumentPtr& theDoc) const; @@ -108,7 +116,7 @@ private: ConstructFolder, ConstructObject, BodiesFolder, - BodieswObject, + BodiesObject, HistoryObject }; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 40c545b2c..9cef8d9b6 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #ifdef _DEBUG #include @@ -117,6 +118,8 @@ void XGUI_Workshop::startApplication() aLoop->registerListener(this, aPartSetId); Events_ID aFeatureUpdatedId = aLoop->eventByName(EVENT_FEATURE_UPDATED); aLoop->registerListener(this, aFeatureUpdatedId); + aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED)); + activateModule(); if (myMainWindow) { myMainWindow->show(); @@ -203,6 +206,17 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage) addFeature(aFeatureMsg); return; } + // Process creation of Part + if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) { + const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); + FeaturePtr aFeature = aUpdMsg->feature(); + if (aFeature->getKind() == "Part") { + //The created part will be created in Object Browser later and we have to activate that + // only when it created everywere + QTimer::singleShot(50, this, SLOT(activateLastPart())); + } + } + //Update property panel on corresponding message. If there is no current operation (no //property panel), or received message has different feature to the current - do nothing. static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED); @@ -708,25 +722,26 @@ XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const //************************************************************** void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked) { - if (theId == "ACTIVATE_PART_CMD") - activatePart(true); + QFeatureList aFeatures = mySelector->selectedFeatures(); + if ((theId == "ACTIVATE_PART_CMD") && (aFeatures.size() > 0)) + activatePart(aFeatures.first()); else if (theId == "DEACTIVATE_PART_CMD") - activatePart(false); + activatePart(FeaturePtr()); } //************************************************************** -void XGUI_Workshop::activatePart(bool toActivate) +void XGUI_Workshop::activatePart(FeaturePtr theFeature) { - if (toActivate) { - QFeatureList aFeatures = mySelector->selectedFeatures(); - if (aFeatures.size() > 0) { - changeCurrentDocument(aFeatures.first()); - myObjectBrowser->activateCurrentPart(true); - } - } else { - changeCurrentDocument(FeaturePtr()); - myObjectBrowser->activateCurrentPart(false); - } + changeCurrentDocument(theFeature); + myObjectBrowser->activatePart(theFeature); } +//************************************************************** +void XGUI_Workshop::activateLastPart() +{ + PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); + DocumentPtr aDoc = aMgr->rootDocument(); + FeaturePtr aLastPart = aDoc->feature(PARTS_GROUP, aDoc->size(PARTS_GROUP) - 1, true); + activatePart(aLastPart); +} diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index cd77d3b13..04920e9ea 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -124,6 +124,13 @@ public slots: signals: void errorOccurred(const QString&); +public slots: + //! Activates or deactivates a part + //! If PartPtr is Null pointer then PartSet will be activated + void activatePart(FeaturePtr theFeature); + + void activateLastPart(); + protected: //Event-loop processing methods: void addFeature(const Config_FeatureMessage*); @@ -153,9 +160,6 @@ private: // Creates Dock widgets: Object browser and Property panel void createDockWidgets(); - //! Activates or deactivates currently selected part - void activatePart(bool toActivate); - QString myCurrentFile; XGUI_MainWindow* myMainWindow; XGUI_Module* myPartSetModule; -- 2.39.2