0031939: Coding - correction of spelling errors in comments [part 2]
[occt.git] / tools / VInspector / VInspector_ItemPresentableObject.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/VInspector_ItemPresentableObject.hxx>
17
18 #include <AIS.hxx>
19 #include <AIS_Shape.hxx>
20 #include <AIS_ListOfInteractive.hxx>
21 #include <AIS_ListIteratorOfListOfInteractive.hxx>
22
23 #include <inspector/VInspector_ItemContext.hxx>
24 #include <inspector/VInspector_Tools.hxx>
25 #include <inspector/VInspector_ViewModel.hxx>
26
27 #include <inspector/ViewControl_Table.hxx>
28 #include <inspector/ViewControl_Tools.hxx>
29
30 #include <NCollection_List.hxx>
31 #include <Prs3d.hxx>
32 #include <Prs3d_Drawer.hxx>
33 #include <SelectBasics_EntityOwner.hxx>
34 #include <StdSelect_BRepOwner.hxx>
35
36 #include <Standard_WarningsDisable.hxx>
37 #include <QColor>
38 #include <QItemSelectionModel>
39 #include <Standard_WarningsRestore.hxx>
40
41 // =======================================================================
42 // function : initValue
43 // purpose :
44 // =======================================================================
45 QVariant VInspector_ItemPresentableObject::initValue (int theItemRole) const
46 {
47   QVariant aParentValue = VInspector_ItemBase::initValue (theItemRole);
48   if (aParentValue.isValid())
49     return aParentValue;
50
51   if (theItemRole == Qt::DisplayRole || theItemRole == Qt::ToolTipRole)
52   {
53     Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
54     bool aNullIO = anIO.IsNull();
55     switch (Column())
56     {
57       case 0:
58       {
59         if (aNullIO)
60           return theItemRole == Qt::ToolTipRole ? QVariant ("Owners where Selectable is empty")
61                                                 : QVariant ("Free Owners");
62         else
63           return theItemRole == Qt::ToolTipRole ? QVariant ("")
64                                                 : QVariant (anIO->DynamicType()->Name());
65       }
66       case 4:
67       {
68         int aNbSelected = VInspector_Tools::SelectedOwners (GetContext(), anIO, false);
69         return aNbSelected > 0 ? QString::number (aNbSelected) : "";
70       }
71       case 6:
72       {
73         double aDeviationCoefficient = 0;
74         Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast (anIO);
75         if (!anAISShape.IsNull())
76         {
77           Standard_Real aPreviousCoefficient;
78           anAISShape->OwnDeviationCoefficient(aDeviationCoefficient, aPreviousCoefficient);
79         }
80         return QString::number(aDeviationCoefficient);
81       }
82       case 8:
83       {
84         double aDeviationCoefficient = 0;
85         Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast (anIO);
86         if (!anAISShape.IsNull())
87         {
88           Standard_Real aPreviousCoefficient;
89           anAISShape->OwnDeviationCoefficient(aDeviationCoefficient, aPreviousCoefficient);
90         }
91         Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (anIO);
92         bool anIsAutoTriangulation = aNullIO ? false : anIO->Attributes()->IsAutoTriangulation();
93         return anIsAutoTriangulation ? QString ("true") : QString ("false");
94       }
95       default: break;
96     }
97   }
98   if (theItemRole == Qt::BackgroundRole || theItemRole == Qt::ForegroundRole)
99   {
100     Handle(AIS_InteractiveContext) aContext = GetContext();
101     if (Column() == 2 && VInspector_Tools::SelectedOwners(aContext, GetInteractiveObject(), false) > 0)
102     {
103       return (theItemRole == Qt::BackgroundRole) ? QColor(Qt::darkBlue) : QColor(Qt::white);
104     }
105     else if (theItemRole == Qt::ForegroundRole)
106     {
107       Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
108       if (anIO.IsNull())
109         return QVariant();
110
111       AIS_ListOfInteractive aListOfIO;
112       GetContext()->ErasedObjects(aListOfIO);
113       for (AIS_ListIteratorOfListOfInteractive anIOIt(aListOfIO); anIOIt.More(); anIOIt.Next())
114       {
115         if (anIO == anIOIt.Value())
116           return QColor(Qt::darkGray);
117       }
118       return QColor(Qt::black);
119     }
120   }
121   return QVariant();
122 }
123
124 // =======================================================================
125 // function : initRowCount
126 // purpose :
127 // =======================================================================
128 int VInspector_ItemPresentableObject::initRowCount() const
129 {
130   return 0;
131 }
132
133 // =======================================================================
134 // function : Init
135 // purpose :
136 // =======================================================================
137 void VInspector_ItemPresentableObject::Init()
138 {
139   VInspector_ItemContextPtr aParentItem = itemDynamicCast<VInspector_ItemContext>(Parent());
140   Handle(AIS_InteractiveContext) aContext = aParentItem->GetContext();
141   SetContext (aContext);
142
143   Handle(AIS_InteractiveObject) anIO;
144   if (!GetContext().IsNull())
145   {
146     int aRowId = Row();
147     AIS_ListOfInteractive aListOfIO;
148     GetContext()->DisplayedObjects (aListOfIO); // the presentation is in displayed objects of Context
149     GetContext()->ErasedObjects (aListOfIO); // the presentation is in erased objects of Context
150
151     std::vector<Handle(AIS_InteractiveObject)> aListOfIOSorted;
152     aListOfIOSorted.reserve (aListOfIO.Size());
153     for (AIS_ListIteratorOfListOfInteractive anIOIt (aListOfIO); anIOIt.More(); anIOIt.Next())
154     {
155       aListOfIOSorted.push_back (anIOIt.Value());
156     }
157     std::sort (aListOfIOSorted.begin(), aListOfIOSorted.end());
158
159     int aCurrentIndex = 1; /* Properties item of context*/
160     for (std::vector<Handle(AIS_InteractiveObject)>::const_iterator anIOIt = aListOfIOSorted.begin(); anIOIt != aListOfIOSorted.end(); anIOIt++, aCurrentIndex++)
161     {
162       if (aCurrentIndex != aRowId)
163         continue;
164       anIO = *anIOIt;
165       break;
166     }
167   }
168
169   setInteractiveObject (anIO);
170   myTransformPersistence = !anIO.IsNull() ? anIO->TransformPersistence() : NULL;
171   UpdatePresentationShape();
172   TreeModel_ItemBase::Init(); // to use getIO() without circling initialization
173 }
174
175 // =======================================================================
176 // function : Reset
177 // purpose :
178 // =======================================================================
179 void VInspector_ItemPresentableObject::Reset()
180 {
181   VInspector_ItemBase::Reset();
182
183   SetContext (NULL);
184   setInteractiveObject (NULL);
185   myTransformPersistence = NULL;
186 }
187
188 // =======================================================================
189 // function : initItem
190 // purpose :
191 // =======================================================================
192 void VInspector_ItemPresentableObject::initItem() const
193 {
194   if (IsInitialized())
195     return;
196   const_cast<VInspector_ItemPresentableObject*>(this)->Init();
197 }
198
199 // =======================================================================
200 // function : buildPresentationShape
201 // purpose :
202 // =======================================================================
203 TopoDS_Shape VInspector_ItemPresentableObject::buildPresentationShape()
204 {
205   Handle(AIS_InteractiveObject) aPrs = myIO;
206   if (aPrs.IsNull())
207     return TopoDS_Shape();
208
209   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast (aPrs);
210   if (!aShapePrs.IsNull())
211     return aShapePrs->Shape();
212
213   return TopoDS_Shape();
214 }
215
216 // =======================================================================
217 // function : PointerInfo
218 // purpose :
219 // =======================================================================
220 QString VInspector_ItemPresentableObject::PointerInfo() const
221 {
222   return Standard_Dump::GetPointerInfo (GetInteractiveObject(), true).ToCString();
223 }
224
225 // =======================================================================
226 // function : Presentations
227 // purpose :
228 // =======================================================================
229 void VInspector_ItemPresentableObject::Presentations (NCollection_List<Handle(Standard_Transient)>& thePresentations)
230 {
231   TreeModel_ItemBase::Presentations (thePresentations); 
232
233   if (Column() != 0)
234     return;
235   thePresentations.Append (GetInteractiveObject());
236 }
237
238 // =======================================================================
239 // function : initStream
240 // purpose :
241 // =======================================================================
242 void VInspector_ItemPresentableObject::initStream (Standard_OStream& theOStream) const
243 {
244   Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
245   if (anIO.IsNull())
246     return;
247
248   anIO->DumpJson (theOStream);
249 }
250