From d766d27e75f1c8af8eae9fac5e871a6524df1538 Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 11 Apr 2014 16:13:01 +0400 Subject: [PATCH] Update tree branch individually --- src/Event/Event_Message.h | 2 +- src/Model/Model_Events.h | 8 ++--- src/XGUI/XGUI_DataTreeModel.h | 8 ++++- src/XGUI/XGUI_DocumentDataModel.cpp | 48 ++++++++++++++++++++++++----- src/XGUI/XGUI_PartDataModel.cpp | 44 +++++++++++++++++++++++--- src/XGUI/XGUI_PartDataModel.h | 8 +++++ src/XGUI/XGUI_SelectionMgr.cpp | 8 +++-- src/XGUI/XGUI_SelectionMgr.h | 4 +++ src/XGUI/XGUI_Workshop.cpp | 4 ++- 9 files changed, 112 insertions(+), 22 deletions(-) diff --git a/src/Event/Event_Message.h b/src/Event/Event_Message.h index e07621af7..0c2702072 100644 --- a/src/Event/Event_Message.h +++ b/src/Event/Event_Message.h @@ -51,7 +51,7 @@ public: const Event_ID& eventID() const {return myEventId;} //! Returns sender of the message or NULL if it is anonymous message - void* sender() {return mySender;} + void* sender() const {return mySender;} }; #endif diff --git a/src/Model/Model_Events.h b/src/Model/Model_Events.h index 0fc4f329c..c4af9f527 100644 --- a/src/Model/Model_Events.h +++ b/src/Model/Model_Events.h @@ -32,9 +32,9 @@ public: const Event_ID& theEvent); /// Returns the feature that has been updated - std::shared_ptr feature() {return myFeature;} + std::shared_ptr feature() const {return myFeature;} /// Returns the document that has been updated - std::shared_ptr document() {return myDoc;} + std::shared_ptr document() const {return myDoc;} }; /// Message that feature was deleted (used for Object Browser update) @@ -50,10 +50,10 @@ public: static const Event_ID messageId(); /// Returns the feature that has been updated - std::shared_ptr document() {return myDoc;} + std::shared_ptr document() const {return myDoc;} /// Returns the group where the feature was deleted - const std::string& group() {return myGroup;} + const std::string& group() const {return myGroup;} }; #endif diff --git a/src/XGUI/XGUI_DataTreeModel.h b/src/XGUI/XGUI_DataTreeModel.h index 3be26bd2a..8f0ffd8d3 100644 --- a/src/XGUI/XGUI_DataTreeModel.h +++ b/src/XGUI/XGUI_DataTreeModel.h @@ -22,6 +22,9 @@ public: //! Returns 0 if the given index is not index of a feature virtual FeaturePtr feature(const QModelIndex& theIndex) const = 0; + //! Returns parent index of the given feature + virtual QModelIndex findParent(const std::shared_ptr& theFeature) const = 0; + protected: std::shared_ptr myDocument; }; @@ -37,7 +40,10 @@ public: XGUI_PartModel(const std::shared_ptr& theDocument, QObject* theParent): XGUI_FeaturesModel(theDocument, theParent) {} - void setPartId(int theId) { myId = theId; } + void setPartId(int theId) { myId = theId; } + + //! Returns true if the given document is a sub-document of this tree + virtual bool hasDocument(const std::shared_ptr& theDoc) const = 0; protected: //! Id of the current part object in the document diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index 1e53953f7..a17b618ee 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -40,13 +40,47 @@ XGUI_DocumentDataModel::~XGUI_DocumentDataModel() void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage) { if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) { - // Add a new part - int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size(); - beginInsertRows(QModelIndex(), aStart, aStart + 1); - XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this); - aModel->setPartId(myPartModels.count()); - myPartModels.append(aModel); - endInsertRows(); + const ModelAPI_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); + std::shared_ptr aDoc = aUpdMsg->document(); + std::shared_ptr aFeature = aUpdMsg->feature(); + + if (aDoc == myDocument) { + if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { + // Add a new part + int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size(); + beginInsertRows(QModelIndex(), aStart, aStart + 1); + XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this); + aModel->setPartId(myPartModels.count()); + myPartModels.append(aModel); + endInsertRows(); + } else { + QModelIndex aIndex = myModel->findParent(aFeature); + int aStart = myModel->rowCount(aIndex); + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)); + beginInsertRows(aIndex, aStart-1, aStart); + endInsertRows(); + if (aStart == 1) // Update parent if this is a first child in order to update node decoration + emit dataChanged(aIndex, aIndex); + } + } else { + XGUI_PartModel* aPartModel = 0; + QList::const_iterator aIt; + for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) { + if ((*aIt)->hasDocument(aDoc)) { + aPartModel = (*aIt); + break; + } + } + if (aPartModel) { + QModelIndex aIndex = aPartModel->findParent(aFeature); + int aStart = aPartModel->rowCount(aIndex); + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)); + beginInsertRows(aIndex, aStart-1, aStart); + endInsertRows(); + if (aStart == 1) // Update parent if this is a first child in order to update node decoration + emit dataChanged(aIndex, aIndex); + } + } } else { // Reset whole tree beginResetModel(); diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp index f68b5fc4a..e58d06ef0 100644 --- a/src/XGUI/XGUI_PartDataModel.cpp +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -30,14 +30,16 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const case ParamObject: { std::shared_ptr aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row()); - return aFeature->data()->getName().c_str(); + if (aFeature) + return aFeature->data()->getName().c_str(); } case ConstructFolder: return tr("Constructions"); case ConstructObject: { std::shared_ptr aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); - return aFeature->data()->getName().c_str(); + if (aFeature) + return aFeature->data()->getName().c_str(); } } break; @@ -139,6 +141,18 @@ FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const } +QModelIndex XGUI_TopDataModel::findParent(const std::shared_ptr& theFeature) const +{ + QString aGroup(theFeature->getGroup().c_str()); + + if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0) + return createIndex(0, 0, (quintptr) ParamsFolder); + if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0) + return createIndex(1, 0, (quintptr) ConstructFolder); + return QModelIndex(); +} + + //****************************************************************** //****************************************************************** //****************************************************************** @@ -161,7 +175,8 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons case MyRoot: { std::shared_ptr aFeature = myDocument->feature(PARTS_GROUP, myId); - return aFeature->data()->getName().c_str(); + if (aFeature) + return aFeature->data()->getName().c_str(); } case ParamsFolder: return tr("Parameters"); @@ -171,13 +186,15 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons { std::shared_ptr aFeature = featureDocument()->feature(PARAMETERS_GROUP, theIndex.row()); - return aFeature->data()->getName().c_str(); + if (aFeature) + return aFeature->data()->getName().c_str(); } case ConstructObject: { std::shared_ptr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row()); - return aFeature->data()->getName().c_str(); + if (aFeature) + return aFeature->data()->getName().c_str(); } } break; @@ -294,3 +311,20 @@ FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const } return 0; } + +bool XGUI_PartDataModel::hasDocument(const std::shared_ptr& theDoc) const +{ + return (featureDocument() == theDoc); +} + + +QModelIndex XGUI_PartDataModel::findParent(const std::shared_ptr& theFeature) const +{ + QString aGroup(theFeature->getGroup().c_str()); + + if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0) + return createIndex(0, 0, (quintptr) ParamsFolder); + if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0) + return createIndex(1, 0, (quintptr) ConstructFolder); + return QModelIndex(); +} \ No newline at end of file diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h index 559f00732..1b3b069bf 100644 --- a/src/XGUI/XGUI_PartDataModel.h +++ b/src/XGUI/XGUI_PartDataModel.h @@ -35,6 +35,8 @@ public: //! Returns 0 if the given index is not index of a feature virtual FeaturePtr feature(const QModelIndex& theIndex) const; + virtual QModelIndex findParent(const std::shared_ptr& theFeature) const; + private: //! Types of QModelIndexes enum DataIds { @@ -78,6 +80,12 @@ public: //! Returns 0 if the given index is not index of a feature virtual FeaturePtr feature(const QModelIndex& theIndex) const; + //! Returns true if the given document is a sub-document of this tree + virtual bool hasDocument(const std::shared_ptr& theDoc) const; + + //! Returns parent index of the given feature + virtual QModelIndex findParent(const std::shared_ptr& theFeature) const; + private: std::shared_ptr featureDocument() const; diff --git a/src/XGUI/XGUI_SelectionMgr.cpp b/src/XGUI/XGUI_SelectionMgr.cpp index cbff63773..510fb1b40 100644 --- a/src/XGUI/XGUI_SelectionMgr.cpp +++ b/src/XGUI/XGUI_SelectionMgr.cpp @@ -13,11 +13,13 @@ XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent) : QObject(theParent), myWorkshop(theParent) { - XGUI_ObjectsBrowser* aObjBrowser = myWorkshop->mainWindow()->objectBrowser(); - - connect(aObjBrowser, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); } +void XGUI_SelectionMgr::connectObjectBrowser(XGUI_ObjectsBrowser* theOB) +{ + myObjectBrowser = theOB; + connect(myObjectBrowser, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); +} XGUI_SelectionMgr::~XGUI_SelectionMgr() { diff --git a/src/XGUI/XGUI_SelectionMgr.h b/src/XGUI/XGUI_SelectionMgr.h index 58af58ea0..23849befe 100644 --- a/src/XGUI/XGUI_SelectionMgr.h +++ b/src/XGUI/XGUI_SelectionMgr.h @@ -5,6 +5,7 @@ #include class XGUI_Workshop; +class XGUI_ObjectsBrowser; /**\class XGUI_SelectionMgr * \ingroup GUI @@ -21,6 +22,8 @@ public: //! Returns list of currently selected objects QFeatureList selectedData() const { return mySelectedData; } + void connectObjectBrowser(XGUI_ObjectsBrowser* theOB); + signals: //! Emited when selection in a one of viewers was changed void selectionChanged(); @@ -30,6 +33,7 @@ public slots: private: XGUI_Workshop* myWorkshop; + XGUI_ObjectsBrowser* myObjectBrowser; //! List of selected features QFeatureList mySelectedData; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 6ff419ecf..284332eed 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -252,8 +252,10 @@ void XGUI_Workshop::onExit() void XGUI_Workshop::onNew() { QApplication::setOverrideCursor(Qt::WaitCursor); - if (myMainWindow->objectBrowser() == 0) + if (myMainWindow->objectBrowser() == 0) { myMainWindow->createDockWidgets(); + mySelector->connectObjectBrowser(myMainWindow->objectBrowser()); + } myMainWindow->showObjectBrowser(); myMainWindow->showPythonConsole(); QMdiSubWindow* aWnd = myMainWindow->viewer()->createView(); -- 2.39.2