c2d49ee32524cb1a34255b72fc3c42dcb6bd8f43
[occt.git] / tools / VInspector / VInspector_ItemContext.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_ItemContext.hxx>
17
18 #include <AIS_ListOfInteractive.hxx>
19 #include <SelectMgr_EntityOwner.hxx>
20 #include <inspector/VInspector_ItemPresentableObject.hxx>
21 #include <inspector/VInspector_Tools.hxx>
22
23 //#define DEBUG_FREE_OWNERS
24
25 // =======================================================================
26 // function : initRowCount
27 // purpose :
28 // =======================================================================
29 int VInspector_ItemContext::initRowCount() const
30 {
31   if (Column() != 0)
32     return 0;
33
34   int aNbPresentations = 0;
35   if (!GetContext().IsNull())
36   {
37     AIS_ListOfInteractive aListOfIO;
38     GetContext()->DisplayedObjects (aListOfIO);
39     GetContext()->ErasedObjects(aListOfIO);
40     aNbPresentations = aListOfIO.Extent();
41   }
42   // owners without Presentation
43 #ifdef DEBUG_FREE_OWNERS
44   int aRows = 0;
45   // only local context is processed: TODO for global context
46   Handle(AIS_InteractiveContext) aContext = GetContext();
47   if (!aContext.IsNull()) {
48     NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
49     aContext->MainSelector()->ActiveOwners(anActiveOwners);
50
51     Handle(SelectMgr_EntityOwner) anOwner;
52     for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt(anActiveOwners);
53       anOwnersIt.More(); anOwnersIt.Next())
54     {
55       anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
56       if (anOwner.IsNull())
57         continue;
58       Handle(AIS_InteractiveObject) anAISObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
59       if (anAISObj.IsNull())
60         aRows++;
61     }
62   }
63
64   // owners in Global Context
65   if (!aContext.IsNull())
66   {
67     NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
68     aContext->MainSelector()->ActiveOwners(anActiveOwners);
69   }
70   if (aRows > 0)
71     aNbPresentations += aRows;
72 #endif
73 #ifdef DEBUG_FREE_OWNERS
74   NCollection_List<Handle(SelectBasics_EntityOwner)> anEmptySelectableOwners;
75   NCollection_List<Handle(SelectBasics_EntityOwner)> anOwners =
76     VInspector_Tools::ActiveOwners (GetContext(), anEmptySelectableOwners);
77   if (anEmptySelectableOwners.Size() > 0)
78     aNbPresentations += 1;
79 #endif
80   return aNbPresentations;
81 }
82
83 // =======================================================================
84 // function : initValue
85 // purpose :
86 // =======================================================================
87 QVariant VInspector_ItemContext::initValue (const int theItemRole) const
88 {
89   if (theItemRole != Qt::DisplayRole && theItemRole != Qt::EditRole && theItemRole != Qt::ToolTipRole)
90     return QVariant();
91
92   switch (Column())
93   {
94     case 0: return GetContext()->DynamicType()->Name();
95     case 1: return rowCount();
96     case 4:
97     {
98       if (theItemRole == Qt::ToolTipRole)
99         return "Selected Owners";
100       else
101       {
102         Handle(AIS_InteractiveObject) anEmptyIO;
103         int aSelectedCount = VInspector_Tools::SelectedOwners (GetContext(), anEmptyIO, false);
104         return aSelectedCount > 0 ? QString::number (aSelectedCount) : "";
105       }
106     }
107     case 7:
108     {
109       if (theItemRole == Qt::ToolTipRole)
110         return QString ("All Owners/Active Owners");
111       else
112       {
113         NCollection_List<Handle(SelectBasics_EntityOwner)> anEmptySelectableOwners;
114         NCollection_List<Handle(SelectBasics_EntityOwner)> anOwners =
115                  VInspector_Tools::ActiveOwners (GetContext(), anEmptySelectableOwners);
116         int anActiveOwners = anOwners.Size();
117         anOwners = VInspector_Tools::ContextOwners (GetContext());
118         return QString ("%1 / %2").arg (anOwners.Size()).arg (anActiveOwners);
119       }
120     }
121     case 8:
122     {
123       if (theItemRole == Qt::ToolTipRole)
124         return QString ("DeviationCoefficient");
125       else
126         return GetContext()->DeviationCoefficient();
127     }
128     default:
129       break;
130   }
131   return QVariant();
132 }
133
134 // =======================================================================
135 // function : createChild
136 // purpose :
137 // =======================================================================
138 TreeModel_ItemBasePtr VInspector_ItemContext::createChild (int theRow, int theColumn)
139 {
140   return VInspector_ItemPresentableObject::CreateItem (currentItem(), theRow, theColumn);
141 }