0027398: Integrate Qt Browser Widget to Open CASCADE Technology
[occt.git] / tools / TreeModel / TreeModel_ItemBase.hxx
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_ItemBase_H
17 #define TreeModel_ItemBase_H
18
19 #include <Standard.hxx>
20 #include <Standard_Macro.hxx>
21 #include <TreeModel_ItemRole.hxx>
22
23 #ifdef _MSC_VER
24 #pragma warning(disable : 4127) // conditional expression is constant
25 #endif
26 #include <QExplicitlySharedDataPointer>
27 #include <QHash>
28 #include <QMap>
29 #include <QModelIndex>
30 #include <QObject>
31 #include <QPair>
32 #include <QSharedData>
33 #include <QVariant>
34
35 class TreeModel_ItemBase;
36
37 typedef QExplicitlySharedDataPointer<TreeModel_ItemBase> TreeModel_ItemBasePtr;
38
39 //! \class TreeModel_ItemBase
40 //! \brief Declaration of an abstract interface of model item.
41 //!
42 //! The TreeModel_ItemBase class defines the standard interface that model items must use
43 //! to be able to provide the model information in the tree view architecture.
44 //! It is not supposed to be instantiated directly. Instead, you should subclass it to
45 //! create new items.
46 //!
47 //! The goal of the item is to be an data container of a custom model, based on the
48 //! QAbstractItemModel. It provides the items architecture in order to realize the model
49 //! functionality to find a parent model index by a child index and vise versa.
50 //!
51 //! The item should be created by the model and is saved in the internal pointer of the
52 //! QModelIndex. Only model knows when the index is removed/created. By this cause,
53 //! the item is wrapped in the QExplicitlySharedDataPointer. It is a counter on the pointer
54 //! and if there is no index that refers to the item, it is removed automatically. So,
55 //! there is no necessity to remove the item manually.
56 //!
57 //! The item knows a pointer to the parent item and its position into.
58 //! Some methods of the item should be realized to fill the item content.
59 //! These are: the children count, a child creation and a child data.
60 //!
61 //! The best way of the item using is to request the content of the item from some
62 //! information object without the caching it. But it can be very expensive realisation,
63 //! because method data, for example, is called by the viewer repaint, in other words,
64 //! constantly.
65 //!
66 //! It is possible to cache some information in the item. Do not give it throught the item
67 //! constructor. Realize method Init() to save the values in the item internal fields.
68 //! If the information model is changed, call Reset() for this item, or the item's parent.
69 //! It leads the item to non initialized state and by the next get of the item content,
70 //! call Init() method to fulfill the item content again.
71 class TreeModel_ItemBase : public QSharedData
72 {
73 public:
74
75   //! Destructor
76   virtual ~TreeModel_ItemBase() {}
77
78   //! Gets whether the item is already initialized.The initialized state is thrown down
79   //! by the reset method and get back after the method Init().
80   //!  \return if the item is initialized
81   bool IsInitialized() const { return m_bInitialized; }
82
83   //! Sets the item internal initialized state to the true. If the item has internal values,
84   //! there should be initialized here.
85   virtual void Init() { m_bInitialized = true; }
86
87   //! Resets the item and the child items content. Sets the initialized state to false.
88   //! If the item has internal values, there should be reseted here.
89   Standard_EXPORT virtual void Reset();
90
91   //! Gets the parent of the item, or TreeModel_ItemBasePtr() if it has no parent.
92   //! \return pointer to the item
93   TreeModel_ItemBasePtr Parent() const { return m_pParent; };
94
95   //! Gets the row of the item in the parent
96   //! \return the row position
97   int Row() const { return m_iRow; }
98
99   //! Gets the column of the item in the parent
100   //! \return the column position
101   int Column() const { return m_iColumn; }
102
103   //! Gets a child tree item in the given position. Find an item in the children hash.
104   //! Creates a new child item, if there is no a cached item in the given position and
105   //! if the flag about the creation is true.
106   //! \param theRow the row of the child item
107   //! \param theColumn the column of the child item
108   //! \param isToCreate the flag whether the item should be created if it is not created yet
109   //! \return the child item or TreeModel_ItemBasePtr() if it does not exist
110   Standard_EXPORT TreeModel_ItemBasePtr Child (int theRow, int theColumn, const bool isToCreate = true);
111
112   //! Returns the data stored under the given role for the current item
113   //! \param theIndex the item model index
114   //! \param theRole the item model role
115   virtual QVariant data (const QModelIndex& theIndex, int theRole = Qt::DisplayRole) const
116   { (void)theIndex; return cachedValue(theRole); }
117
118   //! Returns number of rows where the children are
119   //! \return the row count
120   int rowCount() const { return cachedValue(TreeModel_ItemRole_RowCountRole).toInt(); }
121
122 protected:
123
124   //! \param theParent the parent item
125   //! \param theRow the item row positition in the parent item
126   //! \param theColumn the item column positition in the parent item
127   Standard_EXPORT TreeModel_ItemBase (TreeModel_ItemBasePtr theParent, const int theRow, const int theColumn);
128
129   //! Creates a child item in the given position.
130   //! \param theRow the child row position
131   //! \param theColumn the child column position
132   //! \return the created item
133   virtual TreeModel_ItemBasePtr createChild (int theRow, int theColumn) = 0;
134
135   //! Wraps the currrent item by shared pointer
136   //! \return the shared pointer to the current item
137   Standard_EXPORT const TreeModel_ItemBasePtr currentItem();
138
139   //! Returns the cached value for the role. Init the value if it is requested the first time
140   //! By default, it calls initRowCount(TreeModel_ItemRole_RowCountRole) or initValue for the item role
141   //! \param theItemRole a value role
142   //! \return the value
143   Standard_EXPORT QVariant cachedValue (const int theItemRole) const;
144
145   //! \return number of children. It should be reimplemented in child
146   virtual int initRowCount() const = 0;
147
148   //! Return data value for the role. It should be reimplemented in child
149   //! \param theItemRole a value role
150   //! \return the value
151   virtual QVariant initValue (const int theItemRole) const = 0;
152
153 private:
154
155   typedef QHash< QPair<int, int>, TreeModel_ItemBasePtr > PositionToItemHash;
156   PositionToItemHash m_ChildItems; //!< the hash of item children
157
158   QMap<int, QVariant> mycachedValues; //!< cached values, should be cleared by reset
159   TreeModel_ItemBasePtr m_pParent; //!< the parent item
160   int m_iRow;          //!< the item row position in the parent item
161   int m_iColumn;       //!< the item column position in the parent item
162   bool m_bInitialized; //!< the state whether the item content is already initialized
163 };
164
165 //! Returns an explicitly shared pointer to the pointer held by other, using a
166 //! dynamic cast to type X to obtain an internal pointer of the appropriate type.
167 //! If the dynamic_cast fails, the object returned will be null.
168 //! Example of using: 
169 //! TreeModel_ItemBase* aParent;
170 //! TreeModel_CustomItemPtr aParentItem = itemDynamicCast<TreeModel_CustomItem>(aParent);
171 //! \param theItem a source item
172 //! \return a converted item
173 template <class X, class T> QExplicitlySharedDataPointer<X> itemDynamicCast (const QExplicitlySharedDataPointer<T>& theItem)
174 {
175   X* ptr = dynamic_cast<X*> (theItem.data());
176
177   QExplicitlySharedDataPointer<X> result;
178   result = ptr;
179
180   return result;
181 }
182
183 #endif