1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ModuleBase_IDocumentDataModel.cpp
4 // Created: 28 Apr 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "XGUI_DataModel.h"
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Document.h>
14 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
16 myXMLReader.readAll();
19 //******************************************************
20 void XGUI_DataModel::clear()
25 //******************************************************
26 void XGUI_DataModel::rebuildDataTree()
31 //******************************************************
32 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
37 //******************************************************
38 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
43 //******************************************************
44 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
46 SessionPtr aSession = ModelAPI_Session::get();
47 DocumentPtr aRootDoc = aSession->moduleDocument();
48 int aNbFolders = myXMLReader.rootFoldersNumber();
49 int theIndexRow = theIndex.row();
51 if ((theIndex.column() == 1) ) {
52 if (theIndexRow >= aNbFolders) {
53 if (theRole == Qt::DecorationRole) {
54 return QIcon(":pictures/arrow.png");
60 int aParentRow = theIndex.internalId();
61 if (aParentRow == -1) { // First level of items
62 if (theIndexRow < aNbFolders) { // A folder
65 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
66 QString(" (%1)").arg(rowCount(theIndex));
67 case Qt::DecorationRole:
68 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
71 ObjectPtr aObj = aRootDoc->object(myXMLReader.rootType(), theIndexRow - aNbFolders);
74 return aObj->data()->name().c_str();
75 //case Qt::DecorationRole:
76 // return featureIcon(aFeature);
79 } else { // Content of folders
80 if (aParentRow < aNbFolders) {
81 std::string aType = myXMLReader.rootFolderType(aParentRow);
82 ObjectPtr aObj = aRootDoc->object(aType, theIndexRow);
85 return aObj->data()->name().c_str();
92 //******************************************************
93 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
98 //******************************************************
99 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
101 SessionPtr aSession = ModelAPI_Session::get();
102 if (!aSession->hasModuleDocument())
104 DocumentPtr aRootDoc = aSession->moduleDocument();
105 int aNbFolders = myXMLReader.rootFoldersNumber();
108 std::string aType = myXMLReader.rootType();
110 aNbItems = aRootDoc->size(aType);
112 if (!theParent.isValid())
113 return aNbFolders + aNbItems;
115 int aParentPos = theParent.row();
116 if (aParentPos < aNbFolders) {
117 // This is number of items under folder
118 aType = myXMLReader.rootFolderType(aParentPos);
119 return aRootDoc->size(aType);
124 //******************************************************
125 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
130 //******************************************************
131 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
133 if (!theParent.isValid())
134 return createIndex(theRow, theColumn, -1);
136 return createIndex(theRow, theColumn, theParent.row());
139 //******************************************************
140 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
142 if (theIndex.isValid() && (theIndex.internalId() != -1)) {
143 return createIndex(theIndex.internalId(), theIndex.column(), -1);
145 return QModelIndex();
148 //******************************************************
149 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
151 int aNbFolders = myXMLReader.rootFoldersNumber();
152 if (!theParent.isValid() && aNbFolders)
154 if (theParent.internalId() == -1) {
155 if (theParent.row() < aNbFolders) {
156 std::string aType = myXMLReader.rootFolderType(theParent.row());
157 if (!aType.empty()) {
158 SessionPtr aSession = ModelAPI_Session::get();
159 DocumentPtr aRootDoc = aSession->moduleDocument();
160 return aRootDoc->size(aType) > 0;