From fb06cbad27a12fb9921ebe20fff9e84d9e343799 Mon Sep 17 00:00:00 2001 From: vsv Date: Thu, 5 Jun 2014 18:18:15 +0400 Subject: [PATCH] Update document in Data tree model on loading a new document --- src/XGUI/XGUI_DataTreeModel.h | 9 ++--- src/XGUI/XGUI_DocumentDataModel.cpp | 61 ++++++++++++++++------------ src/XGUI/XGUI_DocumentDataModel.h | 5 +-- src/XGUI/XGUI_ObjectsBrowser.cpp | 7 ++++ src/XGUI/XGUI_ObjectsBrowser.h | 2 + src/XGUI/XGUI_PartDataModel.cpp | 62 +++++++++++++++++++---------- src/XGUI/XGUI_PartDataModel.h | 4 +- src/XGUI/XGUI_Viewer.cpp | 1 - src/XGUI/XGUI_Workshop.cpp | 3 +- 9 files changed, 95 insertions(+), 59 deletions(-) diff --git a/src/XGUI/XGUI_DataTreeModel.h b/src/XGUI/XGUI_DataTreeModel.h index 05c5f7e12..5b9f5a839 100644 --- a/src/XGUI/XGUI_DataTreeModel.h +++ b/src/XGUI/XGUI_DataTreeModel.h @@ -18,8 +18,8 @@ class XGUI_EXPORT XGUI_FeaturesModel : public QAbstractItemModel { public: - XGUI_FeaturesModel(const DocumentPtr& theDocument, QObject* theParent): - QAbstractItemModel(theParent), myDocument(theDocument), myItemsColor(Qt::black) {} + XGUI_FeaturesModel(QObject* theParent): + QAbstractItemModel(theParent), myItemsColor(Qt::black) {} //! Returns Feature object by the given Model index. //! Returns 0 if the given index is not index of a feature @@ -40,7 +40,6 @@ public: QColor itemsColor() const { return myItemsColor; } protected: - boost::shared_ptr myDocument; QColor myItemsColor; }; @@ -52,8 +51,8 @@ protected: class XGUI_PartModel : public XGUI_FeaturesModel { public: - XGUI_PartModel(const DocumentPtr& theDocument, QObject* theParent): - XGUI_FeaturesModel(theDocument, theParent) {} + XGUI_PartModel(QObject* theParent): + XGUI_FeaturesModel(theParent) {} void setPartId(int theId) { myId = theId; } diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index 066ebdcae..7c2529bd5 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -25,17 +25,13 @@ XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent) : QAbstractItemModel(theParent), myActivePart(0) { - // Find Document object - PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); - myDocument = aMgr->currentDocument(); - // Register in event loop Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED)); Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED)); Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED)); // Create a top part of data tree model - myModel = new XGUI_TopDataModel(myDocument, this); + myModel = new XGUI_TopDataModel(this); myModel->setItemsColor(ACTIVE_COLOR); } @@ -48,6 +44,8 @@ XGUI_DocumentDataModel::~XGUI_DocumentDataModel() void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + // Created object event ******************* if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) { const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); @@ -57,11 +55,11 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) { FeaturePtr aFeature = (*aIt); DocumentPtr aDoc = aFeature->document(); - if (aDoc == myDocument) { // If root objects + if (aDoc == aRootDoc) { // If root objects if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group // Add a new part int aStart = myPartModels.size(); - XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this); + XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this); aModel->setPartId(myPartModels.count()); myPartModels.append(aModel); insertRow(aStart, partFolderNode()); @@ -97,7 +95,7 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) std::set::const_iterator aIt; for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) { std::string aGroup = (*aIt); - if (aDoc == myDocument) { // If root objects + if (aDoc == aRootDoc) { // If root objects if (aGroup.compare(PARTS_GROUP) == 0) { // Updsate only Parts group int aStart = myPartModels.size() - 1; removeSubModel(aStart); @@ -142,22 +140,30 @@ void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage) // Reset whole tree ************************** } else { - beginResetModel(); - int aNbParts = myDocument->size(PARTS_GROUP); - if (myPartModels.size() != aNbParts) { // resize internal models - while (myPartModels.size() > aNbParts) { - delete myPartModels.last(); - myPartModels.removeLast(); - } - while (myPartModels.size() < aNbParts) { - myPartModels.append(new XGUI_PartDataModel(myDocument, this)); - } - for (int i = 0; i < myPartModels.size(); i++) - myPartModels.at(i)->setPartId(i); + rebuildDataTree(); + } +} + +void XGUI_DocumentDataModel::rebuildDataTree() +{ + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + + beginResetModel(); + clearModelIndexes(); + + int aNbParts = aRootDoc->size(PARTS_GROUP); + if (myPartModels.size() != aNbParts) { // resize internal models + while (myPartModels.size() > aNbParts) { + delete myPartModels.last(); + myPartModels.removeLast(); + } + while (myPartModels.size() < aNbParts) { + myPartModels.append(new XGUI_PartDataModel(this)); } - clearModelIndexes(); - endResetModel(); + for (int i = 0; i < myPartModels.size(); i++) + myPartModels.at(i)->setPartId(i); } + endResetModel(); } QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const @@ -185,7 +191,8 @@ QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) case HistoryNode: { int aOffset = historyOffset(); - FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + FeaturePtr aFeature = aRootDoc->feature(FEATURES_GROUP, theIndex.row() - aOffset); if (!aFeature) return QVariant(); switch (theRole) { @@ -225,10 +232,11 @@ QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theO int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const { if (!theParent.isValid()) { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); // Size of external models int aVal = historyOffset(); // Plus history size - aVal += myDocument->size(FEATURES_GROUP); + aVal += aRootDoc->size(FEATURES_GROUP); return aVal; } if (theParent.internalId() == PartsFolder) { @@ -354,8 +362,9 @@ FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const if (theIndex.internalId() == PartsFolder) return FeaturePtr(); if (theIndex.internalId() == HistoryNode) { - int aOffset = historyOffset(); - return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + int aOffset = historyOffset(); + return aRootDoc->feature(FEATURES_GROUP, theIndex.row() - aOffset); } QModelIndex* aIndex = toSourceModelIndex(theIndex); if (!isSubModel(aIndex->model())) diff --git a/src/XGUI/XGUI_DocumentDataModel.h b/src/XGUI/XGUI_DocumentDataModel.h index c43b99d19..6683db4c9 100644 --- a/src/XGUI/XGUI_DocumentDataModel.h +++ b/src/XGUI/XGUI_DocumentDataModel.h @@ -71,6 +71,8 @@ public: //! Deactivates a Part void deactivatePart(); + void rebuildDataTree(); + private: enum {PartsFolder, HistoryNode}; @@ -103,9 +105,6 @@ private: int historyOffset() const; - //! Document - boost::shared_ptr myDocument; - //! Data model of top part of data tree (not parts object) XGUI_TopDataModel* myModel; diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 818e72210..f77f005e8 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -322,4 +322,11 @@ void XGUI_ObjectsBrowser::onSelectionChanged() { myFeaturesList = myTreeView->selectedFeatures(); emit selectionChanged(); +} + +//*************************************************** +void XGUI_ObjectsBrowser::rebuildDataTree() +{ + myDocModel->rebuildDataTree(); + update(); } \ No newline at end of file diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index 8e4842df5..f9ae1fed3 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -75,6 +75,8 @@ public: //! Activates currently selected part. Signal activePartChanged will not be sent void activatePart(const FeaturePtr& thePart); + void rebuildDataTree(); + signals: //! Emited when selection is changed void selectionChanged(); diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp index dc09185cc..ea8c1a51a 100644 --- a/src/XGUI/XGUI_PartDataModel.cpp +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -21,8 +21,8 @@ //} -XGUI_TopDataModel::XGUI_TopDataModel(const DocumentPtr& theDocument, QObject* theParent) - : XGUI_FeaturesModel(theDocument, theParent) +XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent) + : XGUI_FeaturesModel(theParent) { } @@ -41,7 +41,8 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex)); case ParamObject: { - FeaturePtr aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row()); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + FeaturePtr aFeature = aRootDoc->feature(PARAMETERS_GROUP, theIndex.row()); if (aFeature) return boost::dynamic_pointer_cast(aFeature)->getName().c_str(); } @@ -49,7 +50,8 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex)); case ConstructObject: { - FeaturePtr aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + FeaturePtr aFeature = aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row()); if (aFeature) return boost::dynamic_pointer_cast(aFeature)->getName().c_str(); } @@ -65,7 +67,8 @@ QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const return QIcon(":pictures/constr_folder.png"); case ConstructObject: { - FeaturePtr aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + FeaturePtr aFeature = aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row()); if (aFeature) return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind())); } @@ -92,11 +95,12 @@ int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const if (!theParent.isValid()) return 2; + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); if (theParent.internalId() == ParamsFolder) - return myDocument->size(PARAMETERS_GROUP); + return aRootDoc->size(PARAMETERS_GROUP); if (theParent.internalId() == ConstructFolder) - return myDocument->size(CONSTRUCTIONS_GROUP); + return aRootDoc->size(CONSTRUCTIONS_GROUP); return 0; } @@ -152,9 +156,15 @@ FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const case ConstructFolder: return FeaturePtr(); case ParamObject: - return myDocument->feature(PARAMETERS_GROUP, theIndex.row()); + { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + return aRootDoc->feature(PARAMETERS_GROUP, theIndex.row()); + } case ConstructObject: - return myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + return aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + } } return FeaturePtr(); } @@ -178,11 +188,12 @@ QModelIndex XGUI_TopDataModel::featureIndex(const FeaturePtr& theFeature) const { QModelIndex aIndex; if (theFeature) { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); std::string aGroup = theFeature->getGroup(); - int aNb = myDocument->size(aGroup); + int aNb = aRootDoc->size(aGroup); int aRow = -1; for (int i = 0; i < aNb; i++) { - if (myDocument->feature(aGroup, i) == theFeature) { + if (aRootDoc->feature(aGroup, i) == theFeature) { aRow = i; break; } @@ -202,8 +213,8 @@ QModelIndex XGUI_TopDataModel::featureIndex(const FeaturePtr& theFeature) const //****************************************************************** //****************************************************************** //****************************************************************** -XGUI_PartDataModel::XGUI_PartDataModel(const DocumentPtr& theDocument, QObject* theParent) - : XGUI_PartModel(theDocument, theParent) +XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent) + : XGUI_PartModel(theParent) { } @@ -220,7 +231,8 @@ QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) cons switch (theIndex.internalId()) { case MyRoot: { - FeaturePtr aFeature = myDocument->feature(PARTS_GROUP, myId); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + FeaturePtr aFeature = aRootDoc->feature(PARTS_GROUP, myId); if (aFeature) return boost::dynamic_pointer_cast(aFeature)->getName().c_str(); } @@ -291,11 +303,13 @@ QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const { - if (!parent.isValid()) - if (myDocument->feature(PARTS_GROUP, myId)) + if (!parent.isValid()) { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + if (aRootDoc->feature(PARTS_GROUP, myId)) return 1; else return 0; + } switch (parent.internalId()) { case MyRoot: return 3 + featureDocument()->size(FEATURES_GROUP); @@ -368,7 +382,8 @@ bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const DocumentPtr XGUI_PartDataModel::featureDocument() const { - FeaturePtr aFeature = myDocument->feature(PARTS_GROUP, myId, true); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + FeaturePtr aFeature = aRootDoc->feature(PARTS_GROUP, myId, true); return aFeature->data()->docRef("PartDocument")->value(); } @@ -376,7 +391,10 @@ FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const { switch (theIndex.internalId()) { case MyRoot: - return myDocument->feature(PARTS_GROUP, myId); + { + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + return aRootDoc->feature(PARTS_GROUP, myId); + } case ParamsFolder: case ConstructFolder: case BodiesFolder: @@ -415,7 +433,8 @@ QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const FeaturePtr XGUI_PartDataModel::part() const { - return myDocument->feature(PARTS_GROUP, myId, true); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + return aRootDoc->feature(PARTS_GROUP, myId, true); } QModelIndex XGUI_PartDataModel::featureIndex(const FeaturePtr& theFeature) const @@ -426,10 +445,11 @@ QModelIndex XGUI_PartDataModel::featureIndex(const FeaturePtr& theFeature) const return aIndex; std::string aGroup = theFeature->getGroup(); - int aNb = myDocument->size(aGroup); + DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument(); + int aNb = aRootDoc->size(aGroup); int aRow = -1; for (int i = 0; i < aNb; i++) { - if (myDocument->feature(aGroup, i) == theFeature) { + if (aRootDoc->feature(aGroup, i) == theFeature) { aRow = i; break; } diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h index e1533ee0b..8d43fe46e 100644 --- a/src/XGUI/XGUI_PartDataModel.h +++ b/src/XGUI/XGUI_PartDataModel.h @@ -14,7 +14,7 @@ class XGUI_EXPORT XGUI_TopDataModel : public XGUI_FeaturesModel { Q_OBJECT public: - XGUI_TopDataModel(const DocumentPtr& theDocument, QObject* theParent); + XGUI_TopDataModel(QObject* theParent); virtual ~XGUI_TopDataModel(); // Reimpl from QAbstractItemModel @@ -67,7 +67,7 @@ class XGUI_PartDataModel : public XGUI_PartModel { Q_OBJECT public: - XGUI_PartDataModel(const DocumentPtr& theDocument, QObject* theParent); + XGUI_PartDataModel(QObject* theParent); virtual ~XGUI_PartDataModel(); // Reimpl from QAbstractItemModel diff --git a/src/XGUI/XGUI_Viewer.cpp b/src/XGUI/XGUI_Viewer.cpp index 3d5af347a..db719599f 100644 --- a/src/XGUI/XGUI_Viewer.cpp +++ b/src/XGUI/XGUI_Viewer.cpp @@ -464,7 +464,6 @@ void XGUI_Viewer::addView(QMdiSubWindow* theView) void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view) { if (view && (view != myActiveView) && (!view->isMinimized())) { - qDebug("onWindowActivated"); myActiveView = view; ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated(); QList::iterator aIt; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index cdf0c42a5..8ac845aaa 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -472,8 +472,9 @@ void XGUI_Workshop::onOpen() } QApplication::setOverrideCursor(Qt::WaitCursor); aDoc->load(myCurrentDir.toLatin1().constData()); - QApplication::restoreOverrideCursor(); updateCommandStatus(); + myObjectBrowser->rebuildDataTree(); + QApplication::restoreOverrideCursor(); } //****************************************************** -- 2.39.2