14bbbdcb |
1 | // Created on: 2017-06-16 |
2 | // Created by: Natalia ERMOLAEVA |
3 | // Copyright (c) 2017 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #ifndef TreeModel_ModelBase_H |
17 | #define TreeModel_ModelBase_H |
18 | |
19 | #include <Standard.hxx> |
0cb512c0 |
20 | #include <inspector/TreeModel_ItemBase.hxx> |
6822a3be |
21 | #include <inspector/TreeModel_HeaderSection.hxx> |
14bbbdcb |
22 | |
7e1c1e48 |
23 | #include <NCollection_List.hxx> |
24 | |
130eb114 |
25 | #include <Standard_WarningsDisable.hxx> |
14bbbdcb |
26 | #include <QAbstractItemModel> |
27 | #include <QExplicitlySharedDataPointer> |
6822a3be |
28 | #include <QMap> |
14bbbdcb |
29 | #include <QModelIndex> |
30 | #include <QVariant> |
6822a3be |
31 | #include <QVector> |
130eb114 |
32 | #include <Standard_WarningsRestore.hxx> |
14bbbdcb |
33 | |
6822a3be |
34 | class TreeModel_VisibilityState; |
35 | |
14bbbdcb |
36 | //! \class TreeModel_ModelBase |
37 | //! \brief Implementation of the tree item based model of QAbstractItemModel. |
a110c4a3 |
38 | //! The TreeModel_ModelBase class defines the abstract model realization through the base item architecture. |
14bbbdcb |
39 | //! By the model index creation, a base item is created and attached to the index. |
40 | //! Each item contains an iformation about the item parent, position in the parent and |
41 | //! the item's children. So, it is possible to get the model index relation from the item. |
42 | class TreeModel_ModelBase : public QAbstractItemModel |
43 | { |
44 | public: |
45 | |
46 | //! Constructor |
47 | //! \param theParent the parent object |
48 | Standard_EXPORT TreeModel_ModelBase (QObject* theParent = 0); |
49 | |
50 | //! Destructor |
51 | virtual ~TreeModel_ModelBase() {} |
52 | |
7e1c1e48 |
53 | //! Creates model columns and root items. |
54 | //! Default columns are: [0] - Name, [1] - Visibility, [2] - Row |
55 | Standard_EXPORT virtual void InitColumns(); |
56 | |
14bbbdcb |
57 | //! Returns the item shared pointer by the model index |
58 | //! if it is in the index internal pointer |
59 | //! @param theIndex a model index |
60 | Standard_EXPORT static TreeModel_ItemBasePtr GetItemByIndex (const QModelIndex& theIndex); |
61 | |
62 | //! Resets the model items content. Calls the same method of the root item. |
63 | //! It leads to reset of all child/sub child items. |
64 | Standard_EXPORT virtual void Reset(); |
65 | |
66 | //! Returns the model root item. |
67 | //! It is realized for OCAFBrowser |
7e1c1e48 |
68 | TreeModel_ItemBasePtr RootItem (const int theColumn) const { return myRootItems[theColumn]; } |
14bbbdcb |
69 | |
70 | //! Emits the layoutChanged signal from outside of this class |
71 | Standard_EXPORT void EmitLayoutChanged(); |
72 | |
6822a3be |
73 | //! Emits the dataChanged signal from outside of this class |
74 | Standard_EXPORT void EmitDataChanged (const QModelIndex& theTopLeft, const QModelIndex& theBottomRight, |
75 | const QVector<int>& theRoles = QVector<int>(), const bool isResetItem = true); |
76 | |
77 | //! Sets state whether visibility column (0) is used in the model |
78 | //! \param theState state |
79 | void SetUseVisibilityColumn (const bool theState) { m_pUseVisibilityColumn = theState; } |
80 | |
81 | //! Returns state whether visibility column (0) is used in the model |
82 | //! \param theState state |
83 | bool IsUseVisibilityColumn() const { return m_pUseVisibilityColumn; } |
84 | |
7e1c1e48 |
85 | //! Fills visibility state checker |
86 | //! \param theController the checker interface |
6822a3be |
87 | void SetVisibilityState (TreeModel_VisibilityState* theController) { myVisibilityState = theController; } |
88 | |
7e1c1e48 |
89 | //! Returns visibility state checker |
90 | //! \return the checker interface |
6822a3be |
91 | TreeModel_VisibilityState* GetVisibilityState () const { return myVisibilityState; } |
92 | |
7e1c1e48 |
93 | //! Returns true if the tree view model contains highlighted items. This highlight is set manually. |
94 | bool HasHighlighted() { return !myHighlightedIndices.isEmpty(); } |
95 | |
96 | //! Sets items of the indices highlighted in the model. |
97 | //! \param theIndices a list of tree model indices |
98 | void SetHighlighted (const QModelIndexList& theIndices = QModelIndexList()) { myHighlightedIndices = theIndices; } |
99 | |
14bbbdcb |
100 | //! Returns the index of the item in the model specified by the given row, column and parent index. |
101 | //! Saves an internal pointer at the createIndex. This pointer is a shared pointer to the class, |
102 | //! that realizes a base item interface. If the parent is invalid, a root item is used, otherwise a new item |
103 | //! is created by the pointer item saved the parent model index |
104 | //! \param theRow the index row position |
105 | //! \param theColummn the index column position |
106 | //! \param theParent the parent index |
107 | //! \return the model index |
108 | Standard_EXPORT virtual QModelIndex index (int theRow, int theColumn, |
109 | const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE; |
110 | |
111 | //! Returns the data stored under the given role for the item referred to by the index. |
112 | //! \param theIndex a model index |
113 | //! \param theRole an enumeration value of role for data obtaining |
114 | Standard_EXPORT virtual QVariant data (const QModelIndex& theIndex, int theRole = Qt::DisplayRole) const Standard_OVERRIDE; |
115 | |
116 | //! Returns the parent index by the child index. Founds the item, saved in the index; |
117 | //! obtains the parent item by the item. Create a new index by the item and containing it. |
118 | //! \param theIndex a model index |
119 | Standard_EXPORT virtual QModelIndex parent (const QModelIndex& theIndex) const Standard_OVERRIDE; |
120 | |
121 | //! Returns the item flags for the given index. The base class implementation returns a combination of flags that |
122 | //! enables the item (ItemIsEnabled) and allows it to be selected (ItemIsSelectable) |
123 | //! \param theIndex the model index |
124 | //! \return Qt flag combination |
125 | Standard_EXPORT virtual Qt::ItemFlags flags (const QModelIndex& theIndex) const Standard_OVERRIDE; |
126 | |
127 | //! Returns the header data for the given role and section in the header with the specified orientation. |
128 | //! \param theSection the header section. For horizontal headers - column number, for vertical headers - row number. |
129 | //! \param theOrientation a header orientation |
130 | //! \param theRole a data role |
131 | //! \return the header data |
132 | Standard_EXPORT virtual QVariant headerData (int theSection, Qt::Orientation theOrientation, |
6822a3be |
133 | int theRole = Qt::DisplayRole) const Standard_OVERRIDE; |
14bbbdcb |
134 | |
135 | //! Returns the number of rows under the given parent. When the parent is valid it means that rowCount is returning |
136 | //! the number of children of parent. |
137 | //! \param theParent a parent model index |
138 | //! \return the number of rows |
139 | Standard_EXPORT virtual int rowCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE; |
140 | |
6822a3be |
141 | //! Returns whether the column is hidden by default |
142 | //! \param theColumnId a column index |
143 | //! \return header section values container |
144 | TreeModel_HeaderSection GetHeaderItem (const int theColumnId) const { return myHeaderValues[theColumnId]; } |
145 | |
7e1c1e48 |
146 | //! Sets header properties item. |
6822a3be |
147 | //! \param theColumnId a column index |
148 | //! \param theSection a section value |
7e1c1e48 |
149 | Standard_EXPORT void SetHeaderItem (const int theColumnId, const TreeModel_HeaderSection& theSection); |
6822a3be |
150 | |
151 | //! Returns count of columns in the model |
152 | //! \param theParent an index of the parent item |
153 | //! \return integer value |
14bbbdcb |
154 | virtual int columnCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE |
6822a3be |
155 | { (void)theParent; return myHeaderValues.size(); } |
156 | |
157 | //! Returns default value of the visibility column |
158 | //! \return integer value |
159 | static int ColumnVisibilityWidth() { return 20; } |
160 | |
7e1c1e48 |
161 | //! Returns selected items in the cell of given orientation. |
162 | //! \param theIndices a container of selected indices |
163 | //! \param theCellId column index if orientation is horizontal, row index otherwise |
164 | //! \param theOrientation an orientation to apply the cell index |
165 | //! \return model indices from the list |
166 | Standard_EXPORT static QModelIndexList Selected (const QModelIndexList& theIndices, const int theCellId, |
167 | const Qt::Orientation theOrientation = Qt::Horizontal); |
168 | |
6822a3be |
169 | //! Returns single selected item in the cell of given orientation. If the orientation is Horizontal, |
a110c4a3 |
170 | //! in the cell id column, one row should be selected. |
6822a3be |
171 | //! \param theIndices a container of selected indices |
172 | //! \param theCellId column index if orientation is horizontal, row index otherwise |
173 | //! \param theOrientation an orientation to apply the cell index |
174 | //! \return model index from the list |
175 | Standard_EXPORT static QModelIndex SingleSelected (const QModelIndexList& theIndices, const int theCellId, |
176 | const Qt::Orientation theOrientation = Qt::Horizontal); |
14bbbdcb |
177 | |
7e1c1e48 |
178 | //! Returns selected tree model items for indices. |
179 | //! \param theIndices a container of selected indices |
180 | //! \return model items from the list |
181 | Standard_EXPORT static QList<TreeModel_ItemBasePtr> SelectedItems (const QModelIndexList& theIndices); |
182 | |
14bbbdcb |
183 | protected: |
6822a3be |
184 | //! Creates root item |
185 | //! \param theColumnId index of a column |
7e1c1e48 |
186 | virtual TreeModel_ItemBasePtr createRootItem (const int theColumnId) = 0; |
14bbbdcb |
187 | |
188 | //! Converts the item shared pointer to void* type |
189 | //! \param theItem |
190 | //! \return an item pointer |
191 | Standard_EXPORT static void* getIndexValue (const TreeModel_ItemBasePtr& theItem); |
192 | |
7e1c1e48 |
193 | private: |
194 | //! Creates root item |
195 | //! \param theColumnId index of a column |
196 | Standard_EXPORT void createRoot (const int theColumnId); |
197 | |
14bbbdcb |
198 | protected: |
199 | |
7e1c1e48 |
200 | QMap<int, TreeModel_ItemBasePtr> myRootItems; //!< container of root items, for each column own root item |
6822a3be |
201 | QMap<int, TreeModel_HeaderSection> myHeaderValues; //!< header values |
14bbbdcb |
202 | //!< model subclass. The model is fulfilled by this item content |
6822a3be |
203 | |
204 | bool m_pUseVisibilityColumn; //!< the state whether column=0 is reserved for Visibility state |
205 | TreeModel_VisibilityState* myVisibilityState; //!< the interface of item visibility |
7e1c1e48 |
206 | QIcon myVisibleIcon; //!< icon of visible state |
207 | QIcon myInvisibleIcon; //!< icon of invisible state |
208 | |
209 | QModelIndexList myHighlightedIndices; //!< tree model indices that should be visualized as highlighted |
14bbbdcb |
210 | }; |
211 | |
212 | #endif |