0029674: Improvements in Inspector tool
[occt.git] / tools / TreeModel / TreeModel_ModelBase.hxx
CommitLineData
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
130eb114 23#include <Standard_WarningsDisable.hxx>
14bbbdcb 24#include <QAbstractItemModel>
25#include <QExplicitlySharedDataPointer>
6822a3be 26#include <QMap>
14bbbdcb 27#include <QModelIndex>
28#include <QVariant>
6822a3be 29#include <QVector>
130eb114 30#include <Standard_WarningsRestore.hxx>
14bbbdcb 31
6822a3be 32class TreeModel_VisibilityState;
33
14bbbdcb 34//! \class TreeModel_ModelBase
35//! \brief Implementation of the tree item based model of QAbstractItemModel.
36//! The TreeModel_ModelBase class defines the abstract model realization throught the base item architecture.
37//! By the model index creation, a base item is created and attached to the index.
38//! Each item contains an iformation about the item parent, position in the parent and
39//! the item's children. So, it is possible to get the model index relation from the item.
40class TreeModel_ModelBase : public QAbstractItemModel
41{
42public:
43
44 //! Constructor
45 //! \param theParent the parent object
46 Standard_EXPORT TreeModel_ModelBase (QObject* theParent = 0);
47
48 //! Destructor
49 virtual ~TreeModel_ModelBase() {}
50
51 //! Returns the item shared pointer by the model index
52 //! if it is in the index internal pointer
53 //! @param theIndex a model index
54 Standard_EXPORT static TreeModel_ItemBasePtr GetItemByIndex (const QModelIndex& theIndex);
55
56 //! Resets the model items content. Calls the same method of the root item.
57 //! It leads to reset of all child/sub child items.
58 Standard_EXPORT virtual void Reset();
59
60 //! Returns the model root item.
61 //! It is realized for OCAFBrowser
62 virtual TreeModel_ItemBasePtr RootItem (const int theColumn) const { (void)theColumn; return m_pRootItem; }
63
64 //! Emits the layoutChanged signal from outside of this class
65 Standard_EXPORT void EmitLayoutChanged();
66
6822a3be 67 //! Emits the dataChanged signal from outside of this class
68 Standard_EXPORT void EmitDataChanged (const QModelIndex& theTopLeft, const QModelIndex& theBottomRight,
69 const QVector<int>& theRoles = QVector<int>(), const bool isResetItem = true);
70
71 //! Sets state whether visibility column (0) is used in the model
72 //! \param theState state
73 void SetUseVisibilityColumn (const bool theState) { m_pUseVisibilityColumn = theState; }
74
75 //! Returns state whether visibility column (0) is used in the model
76 //! \param theState state
77 bool IsUseVisibilityColumn() const { return m_pUseVisibilityColumn; }
78
79 //!< Fills visibility state checker
80 //!< \param theController the checker interface
81 void SetVisibilityState (TreeModel_VisibilityState* theController) { myVisibilityState = theController; }
82
83 //!< Returns visibility state checker
84 //!< \return the checker interface
85 TreeModel_VisibilityState* GetVisibilityState () const { return myVisibilityState; }
86
14bbbdcb 87 //! Returns the index of the item in the model specified by the given row, column and parent index.
88 //! Saves an internal pointer at the createIndex. This pointer is a shared pointer to the class,
89 //! that realizes a base item interface. If the parent is invalid, a root item is used, otherwise a new item
90 //! is created by the pointer item saved the parent model index
91 //! \param theRow the index row position
92 //! \param theColummn the index column position
93 //! \param theParent the parent index
94 //! \return the model index
95 Standard_EXPORT virtual QModelIndex index (int theRow, int theColumn,
96 const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE;
97
98 //! Returns the data stored under the given role for the item referred to by the index.
99 //! \param theIndex a model index
100 //! \param theRole an enumeration value of role for data obtaining
101 Standard_EXPORT virtual QVariant data (const QModelIndex& theIndex, int theRole = Qt::DisplayRole) const Standard_OVERRIDE;
102
103 //! Returns the parent index by the child index. Founds the item, saved in the index;
104 //! obtains the parent item by the item. Create a new index by the item and containing it.
105 //! \param theIndex a model index
106 Standard_EXPORT virtual QModelIndex parent (const QModelIndex& theIndex) const Standard_OVERRIDE;
107
108 //! Returns the item flags for the given index. The base class implementation returns a combination of flags that
109 //! enables the item (ItemIsEnabled) and allows it to be selected (ItemIsSelectable)
110 //! \param theIndex the model index
111 //! \return Qt flag combination
112 Standard_EXPORT virtual Qt::ItemFlags flags (const QModelIndex& theIndex) const Standard_OVERRIDE;
113
114 //! Returns the header data for the given role and section in the header with the specified orientation.
115 //! \param theSection the header section. For horizontal headers - column number, for vertical headers - row number.
116 //! \param theOrientation a header orientation
117 //! \param theRole a data role
118 //! \return the header data
119 Standard_EXPORT virtual QVariant headerData (int theSection, Qt::Orientation theOrientation,
6822a3be 120 int theRole = Qt::DisplayRole) const Standard_OVERRIDE;
14bbbdcb 121
122 //! Returns the number of rows under the given parent. When the parent is valid it means that rowCount is returning
123 //! the number of children of parent.
124 //! \param theParent a parent model index
125 //! \return the number of rows
126 Standard_EXPORT virtual int rowCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE;
127
6822a3be 128 //! Returns whether the column is hidden by default
129 //! \param theColumnId a column index
130 //! \return header section values container
131 TreeModel_HeaderSection GetHeaderItem (const int theColumnId) const { return myHeaderValues[theColumnId]; }
132
133 //! Set header properties item.
134 //! \param theColumnId a column index
135 //! \param theSection a section value
136 void SetHeaderItem (const int theColumnId, const TreeModel_HeaderSection& theSection)
137 { myHeaderValues[theColumnId] = theSection; createRootItem (theColumnId); }
138
139 //! Returns count of columns in the model
140 //! \param theParent an index of the parent item
141 //! \return integer value
14bbbdcb 142 virtual int columnCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE
6822a3be 143 { (void)theParent; return myHeaderValues.size(); }
144
145 //! Returns default value of the visibility column
146 //! \return integer value
147 static int ColumnVisibilityWidth() { return 20; }
148
149 //! Returns single selected item in the cell of given orientation. If the orientation is Horizontal,
150 //! in the cell id colum, one row should be selected.
151 //! \param theIndices a container of selected indices
152 //! \param theCellId column index if orientation is horizontal, row index otherwise
153 //! \param theOrientation an orientation to apply the cell index
154 //! \return model index from the list
155 Standard_EXPORT static QModelIndex SingleSelected (const QModelIndexList& theIndices, const int theCellId,
156 const Qt::Orientation theOrientation = Qt::Horizontal);
14bbbdcb 157
158protected:
6822a3be 159 //! Creates root item
160 //! \param theColumnId index of a column
161 virtual void createRootItem (const int theColumnId) = 0;
14bbbdcb 162
163 //! Converts the item shared pointer to void* type
164 //! \param theItem
165 //! \return an item pointer
166 Standard_EXPORT static void* getIndexValue (const TreeModel_ItemBasePtr& theItem);
167
168protected:
169
170 TreeModel_ItemBasePtr m_pRootItem; //!< the model root item. It should be created in the
6822a3be 171 QMap<int, TreeModel_HeaderSection> myHeaderValues; //!< header values
14bbbdcb 172 //!< model subclass. The model is fulfilled by this item content
6822a3be 173
174 bool m_pUseVisibilityColumn; //!< the state whether column=0 is reserved for Visibility state
175 TreeModel_VisibilityState* myVisibilityState; //!< the interface of item visibility
14bbbdcb 176};
177
178#endif