1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef TreeModel_ModelBase_H
17 #define TreeModel_ModelBase_H
19 #include <Standard.hxx>
20 #include <inspector/TreeModel_ItemBase.hxx>
21 #include <inspector/TreeModel_HeaderSection.hxx>
23 #include <NCollection_List.hxx>
25 #include <Standard_WarningsDisable.hxx>
26 #include <QAbstractItemModel>
27 #include <QExplicitlySharedDataPointer>
29 #include <QModelIndex>
32 #include <Standard_WarningsRestore.hxx>
34 class TreeModel_VisibilityState;
36 //! \class TreeModel_ModelBase
37 //! \brief Implementation of the tree item based model of QAbstractItemModel.
38 //! The TreeModel_ModelBase class defines the abstract model realization throught the base item architecture.
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
47 //! \param theParent the parent object
48 Standard_EXPORT TreeModel_ModelBase (QObject* theParent = 0);
51 virtual ~TreeModel_ModelBase() {}
53 //! Creates model columns and root items.
54 //! Default columns are: [0] - Name, [1] - Visibility, [2] - Row
55 Standard_EXPORT virtual void InitColumns();
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);
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();
66 //! Returns the model root item.
67 //! It is realized for OCAFBrowser
68 TreeModel_ItemBasePtr RootItem (const int theColumn) const { return myRootItems[theColumn]; }
70 //! Emits the layoutChanged signal from outside of this class
71 Standard_EXPORT void EmitLayoutChanged();
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);
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; }
81 //! Returns state whether visibility column (0) is used in the model
82 //! \param theState state
83 bool IsUseVisibilityColumn() const { return m_pUseVisibilityColumn; }
85 //! Fills visibility state checker
86 //! \param theController the checker interface
87 void SetVisibilityState (TreeModel_VisibilityState* theController) { myVisibilityState = theController; }
89 //! Returns visibility state checker
90 //! \return the checker interface
91 TreeModel_VisibilityState* GetVisibilityState () const { return myVisibilityState; }
93 //! Returns true if the tree view model contains highlighted items. This highlight is set manually.
94 bool HasHighlighted() { return !myHighlightedIndices.isEmpty(); }
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; }
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;
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;
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;
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;
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,
133 int theRole = Qt::DisplayRole) const Standard_OVERRIDE;
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;
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]; }
146 //! Sets header properties item.
147 //! \param theColumnId a column index
148 //! \param theSection a section value
149 Standard_EXPORT void SetHeaderItem (const int theColumnId, const TreeModel_HeaderSection& theSection);
151 //! Returns count of columns in the model
152 //! \param theParent an index of the parent item
153 //! \return integer value
154 virtual int columnCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE
155 { (void)theParent; return myHeaderValues.size(); }
157 //! Returns default value of the visibility column
158 //! \return integer value
159 static int ColumnVisibilityWidth() { return 20; }
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);
169 //! Returns single selected item in the cell of given orientation. If the orientation is Horizontal,
170 //! in the cell id colum, one row should be selected.
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);
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);
184 //! Creates root item
185 //! \param theColumnId index of a column
186 virtual TreeModel_ItemBasePtr createRootItem (const int theColumnId) = 0;
188 //! Converts the item shared pointer to void* type
190 //! \return an item pointer
191 Standard_EXPORT static void* getIndexValue (const TreeModel_ItemBasePtr& theItem);
194 //! Creates root item
195 //! \param theColumnId index of a column
196 Standard_EXPORT void createRoot (const int theColumnId);
200 QMap<int, TreeModel_ItemBasePtr> myRootItems; //!< container of root items, for each column own root item
201 QMap<int, TreeModel_HeaderSection> myHeaderValues; //!< header values
202 //!< model subclass. The model is fulfilled by this item content
204 bool m_pUseVisibilityColumn; //!< the state whether column=0 is reserved for Visibility state
205 TreeModel_VisibilityState* myVisibilityState; //!< the interface of item visibility
206 QIcon myVisibleIcon; //!< icon of visible state
207 QIcon myInvisibleIcon; //!< icon of invisible state
209 QModelIndexList myHighlightedIndices; //!< tree model indices that should be visualized as highlighted