0030687: Visualization - remove redundant interfaces SelectBasics_EntityOwner and...
[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_Shape.hxx>
19 #include <AIS_ListOfInteractive.hxx>
20 #include <AIS_ListIteratorOfListOfInteractive.hxx>
21 #include <inspector/VInspector_ItemContext.hxx>
22 #include <inspector/VInspector_ItemEntityOwner.hxx>
23 #include <inspector/VInspector_ItemSelection.hxx>
24 #include <inspector/VInspector_Tools.hxx>
25 #include <inspector/VInspector_ViewModel.hxx>
26
27 #include <NCollection_List.hxx>
28 #include <Prs3d.hxx>
29 #include <Prs3d_Drawer.hxx>
30 #include <SelectMgr_EntityOwner.hxx>
31 #include <StdSelect_BRepOwner.hxx>
32 #include <Standard_Version.hxx>
33
34 #include <Standard_WarningsDisable.hxx>
35 #include <QColor>
36 #include <QItemSelectionModel>
37 #include <Standard_WarningsRestore.hxx>
38
39 // =======================================================================
40 // function : initValue
41 // purpose :
42 // =======================================================================
43 QVariant VInspector_ItemPresentableObject::initValue (int theItemRole) const
44 {
45   if (Column() == 20 && theItemRole == Qt::BackgroundRole) {
46     Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
47     if (!anIO.IsNull() && anIO->HasColor())
48     {
49       Quantity_Color aColor;
50       anIO->Color(aColor);
51       return QColor ((int)(aColor.Red()*255.), (int)(aColor.Green()*255.), (int)(aColor.Blue()*255.));
52     }
53   }
54
55   if (theItemRole == Qt::DisplayRole || theItemRole == Qt::ToolTipRole)
56   {
57     Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
58     bool aNullIO = anIO.IsNull();
59     switch (Column())
60     {
61       case 0:
62       {
63         if (aNullIO)
64           return theItemRole == Qt::ToolTipRole ? QVariant ("Owners where Selectable is empty")
65                                                 : QVariant ("Free Owners");
66         else
67           return theItemRole == Qt::ToolTipRole ? QVariant ("")
68                                                 : QVariant (anIO->DynamicType()->Name());
69       }
70       case 1:
71         return rowCount();
72       case 2:
73       {
74         if (!aNullIO)
75           return VInspector_Tools::GetPointerInfo (anIO, true).ToCString();
76         break;
77       }
78       case 3:
79       {
80         Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (anIO);
81         if (!aShapeIO.IsNull())
82         {
83           const TopoDS_Shape& aShape = aShapeIO->Shape();
84           if (!aShape.IsNull())
85             return VInspector_Tools::GetShapeTypeInfo (aShape.ShapeType()).ToCString();
86         }
87         break;
88       }
89       case 4:
90       {
91         int aNbSelected = VInspector_Tools::SelectedOwners (GetContext(), anIO, false);
92         return aNbSelected > 0 ? QString::number (aNbSelected) : "";
93       }
94       case 5:
95       {
96         TColStd_ListOfInteger aModes;
97         Handle(AIS_InteractiveContext) aContext = GetContext();
98         aContext->ActivatedModes(anIO, aModes);
99         TCollection_AsciiString aModesInfo;
100         for (TColStd_ListIteratorOfListOfInteger itr (aModes); itr.More(); itr.Next())
101         {
102           if (!aModesInfo.IsEmpty())
103             aModesInfo += ", ";
104           aModesInfo += VInspector_Tools::GetShapeTypeInfo (AIS_Shape::SelectionType(itr.Value()));
105         }
106         return aModesInfo.ToCString();
107       }
108       break;
109       case 6:
110       {
111         double aDeviationCoefficient = 0;
112         Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast (anIO);
113         if (!anAISShape.IsNull())
114         {
115           Standard_Real aPreviousCoefficient;
116           anAISShape->OwnDeviationCoefficient(aDeviationCoefficient, aPreviousCoefficient);
117         }
118         return QString::number(aDeviationCoefficient);
119       }
120       case 7:
121       {
122         double aShapeDeflection = 0;
123         Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (anIO);
124         if (!aShapeIO.IsNull())
125         {
126           const TopoDS_Shape& aShape = aShapeIO->Shape();
127           if (!aShape.IsNull())
128             aShapeDeflection = Prs3d::GetDeflection(aShape, anIO->Attributes());
129         }
130         return QString::number (aShapeDeflection);
131       }
132       case 8:
133       {
134         double aDeviationCoefficient = 0;
135         Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast (anIO);
136         if (!anAISShape.IsNull())
137         {
138           Standard_Real aPreviousCoefficient;
139           anAISShape->OwnDeviationCoefficient(aDeviationCoefficient, aPreviousCoefficient);
140         }
141         Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (anIO);
142         bool anIsAutoTriangulation = aNullIO ? false : anIO->Attributes()->IsAutoTriangulation();
143         return anIsAutoTriangulation ? QString ("true") : QString ("false");
144       }
145       case 17:
146       case 18:
147       case 19:
148         {
149         Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (anIO);
150         if (aShapeIO.IsNull())
151           return QVariant();
152         const TopoDS_Shape& aShape = aShapeIO->Shape();
153         if (aShape.IsNull())
154           return QVariant();
155
156         return Column() == 17 ? VInspector_Tools::GetPointerInfo (aShape.TShape(), true).ToCString()
157               : Column() == 18 ? VInspector_Tools::OrientationToName (aShape.Orientation()).ToCString()
158               :           /*19*/ VInspector_Tools::LocationToName (aShape.Location()).ToCString();
159       }
160       default: break;
161     }
162   }
163   if (theItemRole == Qt::BackgroundRole || theItemRole == Qt::ForegroundRole)
164   {
165     Handle(AIS_InteractiveContext) aContext = GetContext();
166     if (Column() == 2 && VInspector_Tools::SelectedOwners(aContext, GetInteractiveObject(), false) > 0)
167     {
168       return (theItemRole == Qt::BackgroundRole) ? QColor(Qt::darkBlue) : QColor(Qt::white);
169     }
170     else if (theItemRole == Qt::ForegroundRole)
171     {
172       Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
173       if (anIO.IsNull())
174         return QVariant();
175
176       AIS_ListOfInteractive aListOfIO;
177       GetContext()->ErasedObjects(aListOfIO);
178       for (AIS_ListIteratorOfListOfInteractive anIOIt(aListOfIO); anIOIt.More(); anIOIt.Next())
179       {
180         if (anIO == anIOIt.Value())
181           return QColor(Qt::darkGray);
182       }
183       return QColor(Qt::black);
184     }
185   }
186   return QVariant();
187 }
188
189 // =======================================================================
190 // function : initRowCount
191 // purpose :
192 // =======================================================================
193 int VInspector_ItemPresentableObject::initRowCount() const
194 {
195   Handle(AIS_InteractiveObject) anIO = GetInteractiveObject();
196 #if OCC_VERSION_HEX < 0x070201
197   int aRows = 0;
198   if (anIO.IsNull())
199     return aRows;
200   // iteration through sensitive privitives
201   for (anIO->Init(); anIO->More(); anIO->Next())
202     aRows++;
203   return aRows;
204 #else
205   return !anIO.IsNull()
206         ? anIO->Selections().Size()
207         : 0;
208 #endif
209 }
210
211 // =======================================================================
212 // function : createChild
213 // purpose :
214 // =======================================================================
215 TreeModel_ItemBasePtr VInspector_ItemPresentableObject::createChild (int theRow, int theColumn)
216 {
217   return VInspector_ItemSelection::CreateItem(currentItem(), theRow, theColumn);
218 }
219
220 // =======================================================================
221 // function : Init
222 // purpose :
223 // =======================================================================
224 void VInspector_ItemPresentableObject::Init()
225 {
226   VInspector_ItemContextPtr aParentItem = itemDynamicCast<VInspector_ItemContext>(Parent());
227   Handle(AIS_InteractiveContext) aContext = aParentItem->GetContext();
228   SetContext (aContext);
229
230   Handle(AIS_InteractiveObject) anIO;
231   if (!GetContext().IsNull())
232   {
233     int aRowId = Row();
234     AIS_ListOfInteractive aListOfIO;
235     GetContext()->DisplayedObjects (aListOfIO); // the presentation is in displayed objects of Context
236     GetContext()->ErasedObjects (aListOfIO); // the presentation is in erased objects of Context
237     int aDeltaIndex = 1; // properties item
238     int aCurrentIndex = 0;
239     for (AIS_ListIteratorOfListOfInteractive anIOIt (aListOfIO); anIOIt.More(); anIOIt.Next(), aCurrentIndex++)
240     {
241       if (aCurrentIndex != aRowId - aDeltaIndex)
242         continue;
243       anIO = anIOIt.Value();
244       break;
245     }
246   }
247
248   setInteractiveObject (anIO);
249   TreeModel_ItemBase::Init(); // to use getIO() without circling initialization
250 }
251
252 // =======================================================================
253 // function : Reset
254 // purpose :
255 // =======================================================================
256 void VInspector_ItemPresentableObject::Reset()
257 {
258   VInspector_ItemBase::Reset();
259
260   SetContext (NULL);
261   setInteractiveObject (NULL);
262 }
263
264 // =======================================================================
265 // function : initItem
266 // purpose :
267 // =======================================================================
268 void VInspector_ItemPresentableObject::initItem() const
269 {
270   if (IsInitialized())
271     return;
272   const_cast<VInspector_ItemPresentableObject*>(this)->Init();
273 }
274
275 // =======================================================================
276 // function : GetInteractiveObject
277 // purpose :
278 // =======================================================================
279 Handle(AIS_InteractiveObject) VInspector_ItemPresentableObject::GetInteractiveObject() const
280 {
281   initItem();
282   return myIO;
283 }
284
285 // =======================================================================
286 // function : PointerInfo
287 // purpose :
288 // =======================================================================
289 QString VInspector_ItemPresentableObject::PointerInfo() const
290 {
291   return VInspector_Tools::GetPointerInfo (GetInteractiveObject(), true).ToCString();
292 }
293
294 // =======================================================================
295 // function : GetSelectedPresentations
296 // purpose :
297 // =======================================================================
298 NCollection_List<Handle(AIS_InteractiveObject)> VInspector_ItemPresentableObject::GetSelectedPresentations
299                                                                   (QItemSelectionModel* theSelectionModel)
300 {
301   NCollection_List<Handle(AIS_InteractiveObject)> aResultList;
302   if (!theSelectionModel)
303     return aResultList;
304   
305   QList<TreeModel_ItemBasePtr> anItems;
306   
307   QModelIndexList anIndices = theSelectionModel->selectedIndexes();
308   for (QModelIndexList::const_iterator anIndicesIt = anIndices.begin(); anIndicesIt != anIndices.end(); anIndicesIt++)
309   {
310     TreeModel_ItemBasePtr anItem = TreeModel_ModelBase::GetItemByIndex (*anIndicesIt);
311     if (!anItem || anItems.contains (anItem))
312       continue;
313     anItems.append (anItem);
314   }
315
316   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
317   for (QList<TreeModel_ItemBasePtr>::const_iterator anItemIt = anItems.begin(); anItemIt != anItems.end(); anItemIt++)
318   {
319     TreeModel_ItemBasePtr anItem = *anItemIt;
320     VInspector_ItemPresentableObjectPtr aPrsItem = itemDynamicCast<VInspector_ItemPresentableObject>(anItem);
321     if (!aPrsItem)
322       continue;
323     Handle(AIS_InteractiveObject) aPresentation = aPrsItem->GetInteractiveObject();
324     if (aSelectedIds.contains ((size_t)aPresentation.operator->()))
325       continue;
326     aSelectedIds.append ((size_t)aPresentation.operator->());
327     if (!aPresentation.IsNull())
328       aResultList.Append (aPresentation);
329   }
330   return aResultList;
331 }