Salome HOME
5efbb827f16f330703ea23e120f6da86531f013e
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModuleBase_IDocumentDataModel.cpp
4 // Created:     28 Apr 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "XGUI_DataModel.h"
8
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Document.h>
11
12 #include <QIcon>
13
14 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
15 {
16   myXMLReader.readAll();
17 }
18
19 //******************************************************
20 void XGUI_DataModel::clear()
21 {
22
23 }
24
25 //******************************************************
26 void XGUI_DataModel::rebuildDataTree()
27 {
28
29 }
30
31 //******************************************************
32 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
33 {
34   return ObjectPtr();
35 }
36
37 //******************************************************
38 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
39 {
40   return QModelIndex();
41 }
42
43 //******************************************************
44 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
45 {
46   SessionPtr aSession = ModelAPI_Session::get();
47   DocumentPtr aRootDoc = aSession->moduleDocument();
48   int aNbFolders = myXMLReader.rootFoldersNumber();
49   int theIndexRow = theIndex.row();
50
51   if ((theIndex.column() == 1) ) {
52     if (theIndexRow >= aNbFolders) {
53       if (theRole == Qt::DecorationRole) {
54         return QIcon(":pictures/arrow.png");
55       }
56     }
57     return QVariant();
58   }
59
60   int aParentRow = theIndex.internalId();
61   if (aParentRow == -1) { // First level of items
62     if (theIndexRow < aNbFolders) { // A folder
63       switch (theRole) {
64         case Qt::DisplayRole:
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());
69       }
70     } else {
71       ObjectPtr aObj = aRootDoc->object(myXMLReader.rootType(), theIndexRow - aNbFolders);
72       switch (theRole) {
73         case Qt::DisplayRole:
74           return aObj->data()->name().c_str();
75         //case Qt::DecorationRole:
76         //  return featureIcon(aFeature);
77       }
78     }
79   } else { // Content of folders
80     if (aParentRow < aNbFolders) {
81       std::string aType = myXMLReader.rootFolderType(aParentRow);
82       ObjectPtr aObj = aRootDoc->object(aType, theIndexRow);
83       switch (theRole) {
84         case Qt::DisplayRole:
85           return aObj->data()->name().c_str();
86       }
87     }
88   }
89   return QVariant();
90 }
91
92 //******************************************************
93 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
94 {
95   return QVariant();
96 }
97
98 //******************************************************
99 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
100 {
101   SessionPtr aSession = ModelAPI_Session::get();
102   if (!aSession->hasModuleDocument())
103     return 0;
104   DocumentPtr aRootDoc = aSession->moduleDocument();
105   int aNbFolders = myXMLReader.rootFoldersNumber();
106
107   int aNbItems = 0;
108   std::string aType = myXMLReader.rootType();
109   if (!aType.empty())
110     aNbItems = aRootDoc->size(aType);
111
112   if (!theParent.isValid())
113     return aNbFolders + aNbItems;
114
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);
120   }
121   return 0;
122 }
123
124 //******************************************************
125 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
126 {
127   return 2;
128 }
129
130 //******************************************************
131 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
132 {
133   if (!theParent.isValid())
134     return createIndex(theRow, theColumn, -1);
135
136   return createIndex(theRow, theColumn, theParent.row());
137 }
138
139 //******************************************************
140 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
141 {
142   if (theIndex.isValid() && (theIndex.internalId() != -1)) {
143     return createIndex(theIndex.internalId(), theIndex.column(), -1);
144   }
145   return QModelIndex();
146 }
147
148 //******************************************************
149 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
150 {
151   int aNbFolders = myXMLReader.rootFoldersNumber();
152   if (!theParent.isValid() && aNbFolders)
153     return true;
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;
161       }
162     }
163   }
164   return false;
165 }
166