Salome HOME
5fffa5971227ac9b54b24d9ddfeddbf2ff682c0f
[modules/gui.git] / src / TreeData / TreeModel.hxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Author: Guillaume Boulant (EDF/R&D)
21
22
23 #ifndef TREEMODEL_H
24 #define TREEMODEL_H
25
26 #include "TreeData.hxx"
27
28 #include <QAbstractItemModel>
29 #include <QModelIndex>
30 #include <QVariant>
31 #include <QStringList>
32
33 #include "DataObject.hxx"
34
35 class TreeItem;
36 class TreeView;
37
38 class TREEDATA_EXPORT TreeModel : public QAbstractItemModel
39 {
40   Q_OBJECT
41
42   // IMPORTANT NOTE:
43   // In this implementation of QAbstractItemModel, a tree item is
44   // associated to the tree model it belongs to (it can request its
45   // model throw a pointer to this model). Then we declare the
46   // TreeItem as a friend class so that it can request the protected
47   // methods (for example beginInsertRows and endInsertRows, required
48   // to manage correctly the addition of an item in the model. An
49   // item can append a child to itself, so it needs to inform the
50   // model when it begins and when it ends).
51   friend class TreeItem;
52   friend class TreeView;
53
54 public:
55   TreeModel(const QStringList &headers, QObject *parent = 0);
56   ~TreeModel();
57
58   //
59   // =================================================================
60   // This part of the specification is the standard interface required
61   // for providing an operational TreeModel. These methods are used by
62   // the TreeView for display purpose. We just have to implement how
63   // we want the items to be displayed in the view.
64   // =================================================================
65   //
66   // MEM: note that these methods are not intended to be used
67   // directly. That's the job of the viewer to know how to use
68   // them. We just have to give the implementation for customizing the
69   // appearance of the tree. The implementation generally requests the
70   // items'data to set the appearance features.
71   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
72   QModelIndex parent(const QModelIndex &index) const;
73   int rowCount(const QModelIndex &parent = QModelIndex()) const;
74   int columnCount(const QModelIndex &parent = QModelIndex()) const;
75   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
76   bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
77   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
78   bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
79   Qt::ItemFlags flags(const QModelIndex &index) const;
80
81   //
82   // =================================================================
83   // This part is a specific behavior to get a TreeModel that can
84   // organize itself the tree hierarchy using data provided in a
85   // filesystem-like format:
86   //
87   // data="a/b/c" ==> creation/filling of the hierarchy a->b->c
88   // The "folder" categories are unique whereas the leaves may exists
89   // in multiple instances.
90   // =================================================================
91   //
92   bool addData(DataObject * dataObject);
93   bool addData(DataObject * dataObject, const QStringList &path);
94
95   // TODO: We should implement the delete and the update fucntions
96   bool removeData(DataObject * dataObject);
97
98   // This part contains helper functions for general purposes
99   TreeItem * getRootItem();
100
101 private:
102   TreeItem *getItem(const QModelIndex &index = QModelIndex()) const;
103   TreeItem * _rootItem;
104 };
105
106 #endif // TREEMODEL_H