0029025: TInspector include files are not installed to inc directory by CMake
[occt.git] / tools / DFBrowser / DFBrowser_Item.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/DFBrowser_Item.hxx>
17
18 #include <inspector/DFBrowser_ItemRole.hxx>
19 #include <inspector/DFBrowser_Module.hxx>
20 #include <inspector/DFBrowser_Tools.hxx>
21
22 #include <inspector/DFBrowserPane_AttributePane.hxx>
23 #include <inspector/DFBrowserPane_ItemRole.hxx>
24 #include <inspector/DFBrowserPane_Tools.hxx>
25
26 #include <TDF_AttributeIterator.hxx>
27 #include <TDF_ChildIterator.hxx>
28
29 #include <QIcon>
30 #include <QObject>
31
32 const int INFO_LENGHT = 60;
33
34 // =======================================================================
35 // function : hasAttribute
36 // purpose :
37 // =======================================================================
38 bool DFBrowser_Item::HasAttribute() const
39 {
40   initItem();
41   return myAttributeGUID != Standard_GUID();
42 }
43
44 // =======================================================================
45 // function : getAttribute
46 // purpose :
47 // =======================================================================
48 Handle(TDF_Attribute) DFBrowser_Item::GetAttribute() const
49 {
50   initItem();
51   Handle(TDF_Attribute) anAttribute;
52   GetLabel().FindAttribute (myAttributeGUID, anAttribute);
53   return anAttribute;
54 }
55
56 // =======================================================================
57 // function : Init
58 // purpose :
59 // =======================================================================
60 void DFBrowser_Item::Init()
61 {
62   DFBrowser_ItemBasePtr aParentItem = itemDynamicCast<DFBrowser_ItemBase> (Parent());
63   if (!aParentItem)
64     return;
65   TDF_Label aParentLabel = aParentItem->GetLabel();
66   // items can exist only by items with not empty label
67   if (aParentLabel.IsNull())
68     return;
69
70   int aNbAttributes = aParentLabel.NbAttributes();
71   int aRowId = Row();
72   if (aRowId < aNbAttributes)
73   {
74     Handle(TDF_Attribute) anAttribute;
75     int anAttributeId = 0;
76     for (TDF_AttributeIterator anAttrIt (aParentLabel); anAttrIt.More(); anAttrIt.Next(), anAttributeId++)
77     {
78       if (anAttributeId == aRowId)
79         anAttribute = anAttrIt.Value();
80     }
81     SetAttribute (anAttribute);
82   }
83   else {
84     int aCurrentId = aRowId - aNbAttributes;
85     TDF_ChildIterator aLabelsIt (aParentLabel);
86     TDF_Label aLabel;
87     for (int aLabelId = 0; aLabelsIt.More(); aLabelsIt.Next(), aLabelId++)
88     {
89       if (aLabelId < aCurrentId)
90         continue;
91       aLabel = aLabelsIt.Value();
92       break;
93     }
94     if (!aLabel.IsNull())
95       setLabel (aLabel);
96   }
97   TreeModel_ItemBase::Init();
98 }
99
100 // =======================================================================
101 // function : reset
102 // purpose :
103 // =======================================================================
104 void DFBrowser_Item::Reset()
105 {
106   SetAttribute (Handle(TDF_Attribute)());
107
108   DFBrowser_ItemBase::Reset();
109 }
110
111 // =======================================================================
112 // function : initRowCount
113 // purpose :
114 // =======================================================================
115 int DFBrowser_Item::initRowCount() const
116 {
117   return HasAttribute() ? 0 : DFBrowser_ItemBase::initRowCount();
118 }
119
120 // =======================================================================
121 // function : initValue
122 // purpose :
123 // =======================================================================
124 QVariant DFBrowser_Item::initValue (const int theItemRole) const
125 {
126   if (!HasAttribute())
127     return DFBrowser_ItemBase::initValue (theItemRole);
128
129   if (theItemRole == DFBrowserPane_ItemRole_DisplayExtended || theItemRole == DFBrowserPane_ItemRole_ToolTipExtended)
130   {
131     int aRole = theItemRole == DFBrowserPane_ItemRole_DisplayExtended ? Qt::DisplayRole : Qt::ToolTipRole;
132     QVariant aValue = DFBrowser_Module::GetAttributeInfo (GetAttribute(), GetModule(), aRole, Column());
133     QString anAdditionalInfo = DFBrowser_Module::GetAttributeInfo (GetAttribute(), GetModule(),
134                                                                     DFBrowser_ItemRole_AdditionalInfo, Column()).toString();
135     if (!anAdditionalInfo.isEmpty())
136     {
137       if (theItemRole == DFBrowserPane_ItemRole_DisplayExtended && anAdditionalInfo.length() > INFO_LENGHT)
138         anAdditionalInfo = anAdditionalInfo.mid (0, INFO_LENGHT - 3) + "...";
139       if (!anAdditionalInfo.isEmpty())
140         aValue = QVariant (aValue.toString() + QString (" [%1]").arg (anAdditionalInfo));
141       //if (aRole == Qt::ToolTipRole)
142       //  aValue = wrapTextByWords(aValue.toString().toStdString(), INFO_LENGHT).c_str();
143     }
144     return aValue;
145   }
146
147   return DFBrowser_Module::GetAttributeInfo (GetAttribute(), GetModule(), theItemRole, Column());
148 }
149
150 // =======================================================================
151 // function : GetAttributeInfo
152 // purpose :
153 // =======================================================================
154 QVariant DFBrowser_Item::GetAttributeInfo (int theRole) const
155 {
156   initItem();
157   return cachedValue (theRole);
158 }
159
160 // =======================================================================
161 // function : initItem
162 // purpose :
163 // =======================================================================
164 void DFBrowser_Item::initItem() const
165 {
166   if (IsInitialized())
167     return;
168   
169   const_cast<DFBrowser_Item*>(this)->Init();
170 }
171
172 // =======================================================================
173 // function : SetAttribute
174 // purpose :
175 // =======================================================================
176 void DFBrowser_Item::SetAttribute (Handle(TDF_Attribute) theAttribute)
177 {
178   if (!theAttribute.IsNull())
179   {
180     setLabel (theAttribute->Label());
181     myAttributeGUID = theAttribute->ID();
182   }
183   else
184   {
185     setLabel (TDF_Label());
186     myAttributeGUID = Standard_GUID();
187   }
188 }