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>
22 #include <Standard_WarningsDisable.hxx>
23 #include <QAbstractItemModel>
24 #include <QExplicitlySharedDataPointer>
25 #include <QModelIndex>
27 #include <Standard_WarningsRestore.hxx>
29 //! \class TreeModel_ModelBase
30 //! \brief Implementation of the tree item based model of QAbstractItemModel.
31 //! The TreeModel_ModelBase class defines the abstract model realization throught the base item architecture.
32 //! By the model index creation, a base item is created and attached to the index.
33 //! Each item contains an iformation about the item parent, position in the parent and
34 //! the item's children. So, it is possible to get the model index relation from the item.
35 class TreeModel_ModelBase : public QAbstractItemModel
40 //! \param theParent the parent object
41 Standard_EXPORT TreeModel_ModelBase (QObject* theParent = 0);
44 virtual ~TreeModel_ModelBase() {}
46 //! Returns the item shared pointer by the model index
47 //! if it is in the index internal pointer
48 //! @param theIndex a model index
49 Standard_EXPORT static TreeModel_ItemBasePtr GetItemByIndex (const QModelIndex& theIndex);
51 //! Resets the model items content. Calls the same method of the root item.
52 //! It leads to reset of all child/sub child items.
53 Standard_EXPORT virtual void Reset();
55 //! Returns the model root item.
56 //! It is realized for OCAFBrowser
57 virtual TreeModel_ItemBasePtr RootItem (const int theColumn) const { (void)theColumn; return m_pRootItem; }
59 //! Emits the layoutChanged signal from outside of this class
60 Standard_EXPORT void EmitLayoutChanged();
62 //! Returns the index of the item in the model specified by the given row, column and parent index.
63 //! Saves an internal pointer at the createIndex. This pointer is a shared pointer to the class,
64 //! that realizes a base item interface. If the parent is invalid, a root item is used, otherwise a new item
65 //! is created by the pointer item saved the parent model index
66 //! \param theRow the index row position
67 //! \param theColummn the index column position
68 //! \param theParent the parent index
69 //! \return the model index
70 Standard_EXPORT virtual QModelIndex index (int theRow, int theColumn,
71 const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE;
73 //! Returns the data stored under the given role for the item referred to by the index.
74 //! \param theIndex a model index
75 //! \param theRole an enumeration value of role for data obtaining
76 Standard_EXPORT virtual QVariant data (const QModelIndex& theIndex, int theRole = Qt::DisplayRole) const Standard_OVERRIDE;
78 //! Returns the parent index by the child index. Founds the item, saved in the index;
79 //! obtains the parent item by the item. Create a new index by the item and containing it.
80 //! \param theIndex a model index
81 Standard_EXPORT virtual QModelIndex parent (const QModelIndex& theIndex) const Standard_OVERRIDE;
83 //! Returns the item flags for the given index. The base class implementation returns a combination of flags that
84 //! enables the item (ItemIsEnabled) and allows it to be selected (ItemIsSelectable)
85 //! \param theIndex the model index
86 //! \return Qt flag combination
87 Standard_EXPORT virtual Qt::ItemFlags flags (const QModelIndex& theIndex) const Standard_OVERRIDE;
89 //! Returns the header data for the given role and section in the header with the specified orientation.
90 //! \param theSection the header section. For horizontal headers - column number, for vertical headers - row number.
91 //! \param theOrientation a header orientation
92 //! \param theRole a data role
93 //! \return the header data
94 Standard_EXPORT virtual QVariant headerData (int theSection, Qt::Orientation theOrientation,
95 int theRole = Qt::DisplayRole) const Standard_OVERRIDE
96 { (void)theSection, (void)theOrientation; (void)theRole; return QVariant(); }
98 //! Returns the number of rows under the given parent. When the parent is valid it means that rowCount is returning
99 //! the number of children of parent.
100 //! \param theParent a parent model index
101 //! \return the number of rows
102 Standard_EXPORT virtual int rowCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE;
104 //! Returns the number of columns for the children of the given parent.
105 //! \param theParent a parent model index
106 //! \return the number of columns
107 virtual int columnCount (const QModelIndex& theParent = QModelIndex()) const Standard_OVERRIDE
108 { (void)theParent; return 1; }
112 //! Converts the item shared pointer to void* type
114 //! \return an item pointer
115 Standard_EXPORT static void* getIndexValue (const TreeModel_ItemBasePtr& theItem);
119 TreeModel_ItemBasePtr m_pRootItem; //!< the model root item. It should be created in the
120 //!< model subclass. The model is fulfilled by this item content