0032308: Configuration - make Xlib dependency optional
[occt.git] / tools / TreeModel / TreeModel_ItemBase.cxx
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
0cb512c0 16#include <inspector/TreeModel_ItemBase.hxx>
7e1c1e48 17#include <inspector/TreeModel_ItemProperties.hxx>
0cb512c0 18#include <inspector/TreeModel_ItemRole.hxx>
7e1c1e48 19#include <inspector/TreeModel_ItemStream.hxx>
14bbbdcb 20
7e1c1e48 21#include <Standard_Dump.hxx>
130eb114 22#include <Standard_WarningsDisable.hxx>
14bbbdcb 23#include <QStringList>
130eb114 24#include <Standard_WarningsRestore.hxx>
14bbbdcb 25
26// =======================================================================
27// function : Constructor
28// purpose :
29// =======================================================================
30TreeModel_ItemBase::TreeModel_ItemBase (TreeModel_ItemBasePtr theParent, const int theRow, const int theColumn)
7e1c1e48 31 : m_iStreamChildren (0), m_bInitialized (false)
14bbbdcb 32{
33 m_pParent = theParent;
34 m_iRow = theRow;
35 m_iColumn = theColumn;
36}
37
38// =======================================================================
7e1c1e48 39// function : Reset
14bbbdcb 40// purpose :
41// =======================================================================
42void 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;
7e1c1e48 51 if (!myProperties.IsNull())
52 {
53 myProperties->Reset();
54 }
6822a3be 55 myCachedValues.clear();
7e1c1e48 56 myStream.str ("");
6822a3be 57}
58
59// =======================================================================
60// function : Reset
61// purpose :
62// =======================================================================
63void TreeModel_ItemBase::Reset (int theRole)
64{
7e1c1e48 65 if (!myCachedValues.contains (theRole))
6822a3be 66 return;
14bbbdcb 67
6822a3be 68 myCachedValues.remove (theRole);
14bbbdcb 69}
70
71// =======================================================================
72// function : child
73// purpose :
74// =======================================================================
75TreeModel_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) {
7e1c1e48 84 if (theRow < m_iStreamChildren)
85 anItem = TreeModel_ItemStream::CreateItem (currentItem(), theRow, theColumn);
86 else
87 anItem = createChild (theRow - m_iStreamChildren, theColumn);
88
14bbbdcb 89 if (anItem)
90 m_ChildItems[aPos] = anItem;
91 }
92 return anItem;
93}
94
6b63dc83 95// =======================================================================
96// function : Presentations
97// purpose :
98// =======================================================================
99void 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
14bbbdcb 111// =======================================================================
112// function : currentItem
113// purpose :
114// =======================================================================
115const TreeModel_ItemBasePtr TreeModel_ItemBase::currentItem()
116{
117 return TreeModel_ItemBasePtr (this);
118}
119
120// =======================================================================
121// function : cachedValue
122// purpose :
123// =======================================================================
124QVariant TreeModel_ItemBase::cachedValue (const int theItemRole) const
125{
6822a3be 126 if (myCachedValues.contains (theItemRole))
127 return myCachedValues[theItemRole];
14bbbdcb 128
7e1c1e48 129 QVariant aValueToCache;
130 if (theItemRole == TreeModel_ItemRole_RowCountRole)
131 aValueToCache = initRowCount() + const_cast<TreeModel_ItemBase*>(this)->initStreamRowCount();
132 else
133 aValueToCache = initValue (theItemRole);
14bbbdcb 134
7e1c1e48 135 myCachedValues.insert (theItemRole, aValueToCache);
6822a3be 136 return myCachedValues.contains (theItemRole) ? myCachedValues[theItemRole] : QVariant();
14bbbdcb 137}
7e1c1e48 138
139// =======================================================================
140// function : Init
141// purpose :
142// =======================================================================
143void TreeModel_ItemBase::Init()
144{
145 m_bInitialized = true;
146
147 initStream(myStream);
148 initStreamRowCount();
149}
150
151// =======================================================================
152// function : Object
153// purpose :
154// =======================================================================
155const 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// =======================================================================
165int 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// =======================================================================
191QVariant 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}