From: vsv Date: Wed, 22 Jul 2015 14:34:10 +0000 (+0300) Subject: pre-design of XML based tree model X-Git-Tag: V_1.4.0_beta4~453 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8678933843f3bc0dd10ffa6ae6953e94d9e7a44c;p=modules%2Fshaper.git pre-design of XML based tree model --- diff --git a/src/Config/Config_DataModelReader.cpp b/src/Config/Config_DataModelReader.cpp index 5479a98e1..f20f9195c 100644 --- a/src/Config/Config_DataModelReader.cpp +++ b/src/Config/Config_DataModelReader.cpp @@ -38,3 +38,14 @@ void Config_DataModelReader::processNode(xmlNodePtr theNode) myRootTypes = getProperty(theNode, GROUP_TYPE); } } + +int Config_DataModelReader::rootFolderId(std::string theType) const +{ + std::vector::const_iterator aIt; + int aId; + for (aIt = myRootFolderTypes.cbegin(), aId = 0; aIt != myRootFolderTypes.cend(); ++aIt, ++aId) { + if ((*aIt) == theType) + return aId; + } + return -1; +} \ No newline at end of file diff --git a/src/Config/Config_DataModelReader.h b/src/Config/Config_DataModelReader.h index caba2b6b8..67455d9a8 100644 --- a/src/Config/Config_DataModelReader.h +++ b/src/Config/Config_DataModelReader.h @@ -50,6 +50,10 @@ class Config_DataModelReader : public Config_XMLReader /// \param theId id of the folder CONFIG_EXPORT std::string rootFolderIcon(int theId) const { return myRootFolderIcons[theId]; } + /// Returns id of a folder containing the given type + /// \param theType type of objects in folder + CONFIG_EXPORT int rootFolderId(std::string theType) const; + protected: /// Overloaded method. Defines how to process each node virtual void processNode(xmlNodePtr theNode); diff --git a/src/XGUI/XGUI_DataModel.cpp b/src/XGUI/XGUI_DataModel.cpp index 5efbb827f..935b74ec4 100644 --- a/src/XGUI/XGUI_DataModel.cpp +++ b/src/XGUI/XGUI_DataModel.cpp @@ -8,12 +8,59 @@ #include #include +#include + +#include + +#include #include XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent) { myXMLReader.readAll(); + + Events_Loop* aLoop = Events_Loop::loop(); + aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); + aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED)); + aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED)); + aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT())); +} + +//****************************************************** +void XGUI_DataModel::processEvent(const std::shared_ptr& theMessage) +{ + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + std::string aRootType = myXMLReader.rootType(); + int aNbFolders = myXMLReader.rootFoldersNumber(); + + // Created object event ******************* + if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) { + std::shared_ptr aUpdMsg = + std::dynamic_pointer_cast(theMessage); + std::set aObjects = aUpdMsg->objects(); + + std::set::const_iterator aIt; + std::string aObjType; + for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) { + ObjectPtr aObject = (*aIt); + aObjType = aObject->groupName(); + DocumentPtr aDoc = aObject->document(); + if (aDoc == aRootDoc) { + int aRow = aRootDoc->size(aObjType) - 1; + if (aRow != -1) { + if (aObjType == aRootType) { + insertRow(aRow + aNbFolders); + } else { + int aFolderId = myXMLReader.rootFolderId(aObjType); + if (aFolderId != -1) { + insertRow(aRow, createIndex(aFolderId, 0, -1)); + } + } + } + } + } + } } //****************************************************** @@ -37,7 +84,12 @@ ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const //****************************************************** QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const { - return QModelIndex(); + //std::string aType = theObject->groupName(); + //DocumentPtr aDoc = theObject->document(); + //int aRow = aDoc->index(theObject); + //if (aRow == -1) + return QModelIndex(); + //return createIndex(aRow, 0, theObject.get()); } //****************************************************** @@ -49,11 +101,11 @@ QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const int theIndexRow = theIndex.row(); if ((theIndex.column() == 1) ) { - if (theIndexRow >= aNbFolders) { - if (theRole == Qt::DecorationRole) { - return QIcon(":pictures/arrow.png"); - } - } + //if (theIndexRow >= aNbFolders) { + // if (theRole == Qt::DecorationRole) { + // return QIcon(":pictures/arrow.png"); + // } + //} return QVariant(); } @@ -164,3 +216,19 @@ bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const return false; } +//****************************************************** +bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent) +{ + beginInsertRows(theParent, theRow, theRow + theCount - 1); + endInsertRows(); + + return true; +} + +//****************************************************** +bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent) +{ + beginRemoveRows(theParent, theRow, theRow + theCount - 1); + endRemoveRows(); + return true; +} \ No newline at end of file diff --git a/src/XGUI/XGUI_DataModel.h b/src/XGUI/XGUI_DataModel.h index e1157ccb7..6d98b9d8e 100644 --- a/src/XGUI/XGUI_DataModel.h +++ b/src/XGUI/XGUI_DataModel.h @@ -12,13 +12,24 @@ #include #include #include +#include -class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel + +/**\class XGUI_DataModel + * \ingroup GUI + * \brief This is a data model for Object Browser (QTreeView). + * It uses XML file for definition of data tree. + */ +class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener { Q_OBJECT public: XGUI_DataModel(QObject* theParent); + /// Event Listener method + /// \param theMessage an event message + virtual void processEvent(const std::shared_ptr& theMessage); + //! Returns an object by the given Model index. //! Returns 0 if the given index is not index of an object virtual ObjectPtr object(const QModelIndex& theIndex) const; @@ -72,6 +83,19 @@ public: /// \param theParent a parent model index virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + /// Inserts count rows into the model before the given row. + /// Items in the new row will be children of the item represented by the parent model index. + /// \param theRow a start row + /// \param theCount a nember of rows to insert + /// \param theParent a parent model index + virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex()); + + /// Removes count rows starting with the given row under parent parent from the model. + /// \param theRow a start row + /// \param theCount a nember of rows to remove + /// \param theParent a parent model index + virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex()); + private: Config_DataModelReader myXMLReader;