0032308: Configuration - make Xlib dependency optional
[occt.git] / tools / TreeModel / TreeModel_ItemProperties.hxx
1 // Created on: 2020-01-25
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2020 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_ItemProperties_H
17 #define TreeModel_ItemProperties_H
18
19 #include <Standard.hxx>
20 #include <Standard_Dump.hxx>
21 #include <Standard_Handle.hxx>
22 #include <Standard_Macro.hxx>
23 #include <Standard_Type.hxx>
24 #include <Standard_Transient.hxx>
25
26 #include <NCollection_IndexedDataMap.hxx>
27 #include <NCollection_IndexedMap.hxx>
28 #include <NCollection_List.hxx>
29
30 #include <TCollection_AsciiString.hxx>
31
32 #include <inspector/TreeModel_ItemBase.hxx>
33 #include <inspector/ViewControl_EditType.hxx>
34
35 #include <Standard_WarningsDisable.hxx>
36 #include <QAbstractTableModel>
37 #include <QColor>
38 #include <QList>
39 #include <QModelIndexList>
40 #include <QVariant>
41 #include <Standard_WarningsRestore.hxx>
42
43 //! \class TreeModel_ItemProperties
44 //! Class to manipulate properties of tree item. The properties are organized in table structure
45 class TreeModel_ItemProperties : public Standard_Transient
46 {
47   //! enum defined the dimension type
48   enum TreeModel_DimType
49   {
50     TreeModel_DimType_Rows,   //!< defines number of rows
51     TreeModel_DimType_Columns //!< defines number of columns
52   };
53
54   //! container of values in a row of property table
55   struct TreeModel_RowValue
56   {
57     TreeModel_RowValue (const Standard_Integer theValueStartPosition, const QVariant& theKey, QVariant theValue)
58     : ValueStartPosition (theValueStartPosition), Key (theKey), Value (theValue) {}
59
60     Standard_Integer ValueStartPosition; //!< start position of the key
61     QVariant Key; //!< value in the first column
62     QVariant Value; //!< value in the second column
63     QMap<int, QVariant> CustomValues; //!< custom values, e.g. key is Background, value is a defined color
64   };
65
66 public:
67   //! Constructor
68   TreeModel_ItemProperties() {}
69
70   //! Destructor
71   ~TreeModel_ItemProperties() {}
72
73   //! Sets the current item
74   void SetItem (const TreeModel_ItemBasePtr& theItem) { myItem = theItem; }
75
76   //! Returns the current item
77   TreeModel_ItemBasePtr Item() const { return myItem; }
78
79   //! Fills internal containers by item stream values
80   Standard_EXPORT void Init();
81
82   //! If the item has internal values, they should be reset here.
83   Standard_EXPORT virtual void Reset();
84
85   //! Returns number of table rows
86   //! \return an integer value
87   Standard_EXPORT int RowCount() const;
88
89   //! Returns number of table columns
90   //! \return an integer value
91   int ColumnCount() const { return 2; }
92
93   //! Returns content of the model index for the given role, it is obtained from internal container of values
94   //! \param theRow a model index row
95   //! \param theColumn a model index column
96   //! \param theRole a view role
97   //! \return value interpreted depending on the given role
98   Standard_EXPORT QVariant Data (const int theRow, const int theColumn, int theRole = Qt::DisplayRole) const;
99
100   //! Returns type of edit control for the model index. By default, it is an empty control
101   //! \param theRow a model index row
102   //! \param theColumn a model index column
103   //! \return edit type
104   Standard_EXPORT ViewControl_EditType EditType (const int theRow, const int theColumn) const;
105
106   //! Sets content of the model index for the given role, it is applied to internal container of values
107   //! \param theRow a model index row
108   //! \param theColumn a model index column
109   //! \param theRole a view role
110   //! \return true if the value is changed
111   Standard_EXPORT virtual bool SetData (const int theRow, const int theColumn, const QVariant& theValue,
112                                         int theRole = Qt::DisplayRole);
113
114   //! Returns presentation of the attribute to be visualized in the view
115   //! \thePresentations [out] container of presentation handles to be visualized
116   Standard_EXPORT virtual void Presentations (NCollection_List<Handle(Standard_Transient)>& thePresentations);
117
118   //! Returns flags for the item: ItemIsEnabled | Qt::ItemIsSelectable.
119   //! Additional flag for the column 1 is Qt::ItemIsEditable.
120   //! \param theIndex a model index
121   //! \return flags
122   Standard_EXPORT virtual Qt::ItemFlags TableFlags (const int theRow, const int theColumn) const;
123
124   //! Returns stream value of the item to fulfill property panel.
125   //! \return stream value or dummy
126   Standard_EXPORT void ChildStream (const int theRowId,
127                                     TCollection_AsciiString& theKey,
128                                     Standard_DumpValue& theValue) const;
129
130   //! Returns data object of the item.
131   //! \return object key
132   const TCollection_AsciiString& Key() const { return myKey; }
133
134   //! Returns stream value of the item.
135   //! \return value
136   const TCollection_AsciiString& StreamValue() const { return myStreamValue.myValue; }
137
138   //! Returns children stream values
139   //const NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue>& Values() const { initItem(); return myValues; }
140
141   //! Returns children stream values
142   const NCollection_IndexedDataMap<Standard_Integer, TreeModel_RowValue>& RowValues() const { initItem(); return myRowValues; }
143
144   //! Returns children stream values
145   const NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue>& Children() const { initItem(); return myChildren; }
146
147   DEFINE_STANDARD_RTTIEXT (TreeModel_ItemProperties, Standard_Transient)
148
149 protected:
150   //! Initializes the current item. It creates a backup of the specific item information
151   Standard_EXPORT void initItem() const;
152
153 private:
154   TreeModel_ItemBasePtr myItem; //!< current item
155
156   TCollection_AsciiString myKey; //!< the item key
157   Standard_DumpValue myStreamValue; //!< the stream value
158   NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue> myChildren; //!< the children
159   NCollection_IndexedDataMap<Standard_Integer, TreeModel_RowValue> myRowValues; //!< the values
160 };
161
162 #endif