0031939: Coding - correction of spelling errors in comments [part 2]
[occt.git] / tools / TreeModel / TreeModel_ItemBase.cxx
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 #include <inspector/TreeModel_ItemBase.hxx>
17 #include <inspector/TreeModel_ItemProperties.hxx>
18 #include <inspector/TreeModel_ItemRole.hxx>
19 #include <inspector/TreeModel_ItemStream.hxx>
20
21 #include <Standard_Dump.hxx>
22 #include <Standard_WarningsDisable.hxx>
23 #include <QStringList>
24 #include <Standard_WarningsRestore.hxx>
25
26 // =======================================================================
27 // function : Constructor
28 // purpose :
29 // =======================================================================
30 TreeModel_ItemBase::TreeModel_ItemBase (TreeModel_ItemBasePtr theParent, const int theRow, const int theColumn)
31  : m_iStreamChildren (0), m_bInitialized (false)
32 {
33   m_pParent = theParent;
34   m_iRow = theRow;
35   m_iColumn = theColumn;
36 }
37
38 // =======================================================================
39 // function :  Reset
40 // purpose :
41 // =======================================================================
42 void TreeModel_ItemBase::Reset()
43 {
44   for (PositionToItemHash::const_iterator aChildrenIt = m_ChildItems.begin(); aChildrenIt != m_ChildItems.end(); aChildrenIt++)
45   {
46     TreeModel_ItemBasePtr anItem = aChildrenIt.value();
47     if (anItem)
48       anItem->Reset();
49   }
50   m_bInitialized = false;
51   if (!myProperties.IsNull())
52   {
53     myProperties->Reset();
54   }
55   myCachedValues.clear();
56   myStream.str ("");
57 }
58
59 // =======================================================================
60 // function :  Reset
61 // purpose :
62 // =======================================================================
63 void TreeModel_ItemBase::Reset (int theRole)
64 {
65   if (!myCachedValues.contains (theRole))
66     return;
67
68   myCachedValues.remove (theRole);
69 }
70
71 // =======================================================================
72 // function :  child
73 // purpose :
74 // =======================================================================
75 TreeModel_ItemBasePtr TreeModel_ItemBase::Child (int theRow, int theColumn, const bool isToCreate)
76 {
77   QPair<int, int> aPos = qMakePair (theRow, theColumn);
78
79   if (m_ChildItems.contains (aPos))
80     return m_ChildItems[aPos];
81
82   TreeModel_ItemBasePtr anItem;
83   if (isToCreate) {
84     if (theRow < m_iStreamChildren)
85       anItem = TreeModel_ItemStream::CreateItem (currentItem(), theRow, theColumn);
86     else
87       anItem = createChild (theRow - m_iStreamChildren, theColumn);
88
89     if (anItem)
90       m_ChildItems[aPos] = anItem;
91   }
92   return anItem;
93 }
94
95 // =======================================================================
96 // function :  Presentations
97 // purpose :
98 // =======================================================================
99 void TreeModel_ItemBase::Presentations (NCollection_List<Handle(Standard_Transient)>& thePresentations)
100 {
101   if (Column() != 0)
102     return;
103
104   const Handle(TreeModel_ItemProperties)& anItemProperties = Properties();
105   if (anItemProperties)
106   {
107     anItemProperties->Presentations (thePresentations);
108   }
109 }
110
111 // =======================================================================
112 // function :  currentItem
113 // purpose :
114 // =======================================================================
115 const TreeModel_ItemBasePtr TreeModel_ItemBase::currentItem()
116 {
117   return TreeModel_ItemBasePtr (this);
118 }
119
120 // =======================================================================
121 // function :  cachedValue
122 // purpose :
123 // =======================================================================
124 QVariant TreeModel_ItemBase::cachedValue (const int theItemRole) const
125 {
126   if (myCachedValues.contains (theItemRole))
127     return myCachedValues[theItemRole];
128
129   QVariant aValueToCache;
130   if (theItemRole == TreeModel_ItemRole_RowCountRole)
131     aValueToCache = initRowCount() + const_cast<TreeModel_ItemBase*>(this)->initStreamRowCount();
132   else
133     aValueToCache = initValue (theItemRole);
134
135   myCachedValues.insert (theItemRole, aValueToCache);
136   return myCachedValues.contains (theItemRole) ? myCachedValues[theItemRole] : QVariant();
137 }
138
139 // =======================================================================
140 // function : Init
141 // purpose :
142 // =======================================================================
143 void TreeModel_ItemBase::Init()
144 {
145   m_bInitialized = true;
146
147   initStream(myStream);
148   initStreamRowCount();
149 }
150
151 // =======================================================================
152 // function : Object
153 // purpose :
154 // =======================================================================
155 const Handle(Standard_Transient)& TreeModel_ItemBase::Object() const
156 {
157   static Handle(Standard_Transient) aNullObject;
158   return aNullObject;
159 }
160
161 // =======================================================================
162 // function : initStreamRowCount
163 // purpose :
164 // =======================================================================
165 int TreeModel_ItemBase::initStreamRowCount()
166 {
167   int aStreamChildrenCount = 0;
168   if (Column() == 0)
169   {
170     Standard_SStream aStream;
171     initStream (aStream);
172     if (!Standard_Dump::Text (aStream).IsEmpty())
173     {
174       if (!myProperties)
175       {
176         myProperties = new TreeModel_ItemProperties();
177         myProperties->SetItem (currentItem());
178       }
179       myProperties->Init();
180       aStreamChildrenCount = myProperties->Children().Extent();
181     }
182   }
183   m_iStreamChildren = aStreamChildrenCount;
184   return m_iStreamChildren;
185 }
186
187 // =======================================================================
188 // function : initValue
189 // purpose :
190 // =======================================================================
191 QVariant TreeModel_ItemBase::initValue (const int theItemRole) const
192 {
193   if (theItemRole != Qt::DisplayRole && theItemRole != Qt::ToolTipRole)
194     return QVariant();
195
196   switch (Column())
197   {
198     case 1: { return Row(); }
199   }
200
201   return QVariant();
202 }