Salome HOME
pre-design of XML based tree model
authorvsv <vitaly.smetannikov@opencascade.com>
Wed, 22 Jul 2015 14:34:10 +0000 (17:34 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Wed, 22 Jul 2015 14:34:22 +0000 (17:34 +0300)
src/Config/Config_DataModelReader.cpp
src/Config/Config_DataModelReader.h
src/XGUI/XGUI_DataModel.cpp
src/XGUI/XGUI_DataModel.h

index 5479a98e16fb9a65cccd39a6b871f51c2b65cc7d..f20f9195c2271f59d21af97dd16234978f2201e2 100644 (file)
@@ -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<std::string>::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
index caba2b6b848cbc2b5a66101bdeda2a1d9595feb2..67455d9a8cd982a1a7e1ddff83f7b6c9b1498aee 100644 (file)
@@ -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);
index 5efbb827f16f330703ea23e120f6da86531f013e..935b74ec40a6094d91a30da94a27e69cfdb86cbd 100644 (file)
@@ -8,12 +8,59 @@
 
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
+#include <ModelAPI_Events.h>
+
+#include <Config_FeatureMessage.h>
+
+#include <Events_Loop.h>
 
 #include <QIcon>
 
 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<Events_Message>& 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<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
+        std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+    std::set<ObjectPtr> aObjects = aUpdMsg->objects();
+
+    std::set<ObjectPtr>::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
index e1157ccb74cc74a68eca189a1cb873f273620132..6d98b9d8ed46e0f4ac343c9f5bc8ba969444b7bb 100644 (file)
 #include <ModelAPI_Object.h>
 #include <Config_DataModelReader.h>
 #include <QAbstractItemModel>
+#include <Events_Listener.h>
 
-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<Events_Message>& 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;