cb3c281a79483302823abc949c7b33dca015ab1f
[occt.git] / tools / VInspector / VInspector_ViewModel.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_ViewModel.hxx>
17
18 #include <inspector/VInspector_ItemContext.hxx>
19 #include <inspector/VInspector_ItemEntityOwner.hxx>
20 #include <inspector/VInspector_ItemPresentableObject.hxx>
21 #include <inspector/VInspector_ItemSensitiveEntity.hxx>
22 #include <SelectBasics_EntityOwner.hxx>
23
24 #include <Standard_WarningsDisable.hxx>
25 #include <QItemSelectionModel>
26 #include <QStringList>
27 #include <Standard_WarningsRestore.hxx>
28
29 // =======================================================================
30 // function : Constructor
31 // purpose :
32 // =======================================================================
33 VInspector_ViewModel::VInspector_ViewModel (QObject* theParent)
34   : TreeModel_ModelBase (theParent)
35 {
36   for (int aColumnId = 0, aNbColumns = columnCount(); aColumnId < aNbColumns; aColumnId++)
37     myRootItems.insert (aColumnId, VInspector_ItemContext::CreateItem (TreeModel_ItemBasePtr(), 0, aColumnId));
38
39   m_pRootItem = myRootItems[0];
40 }
41
42 // =======================================================================
43 // function : GetContext
44 // purpose :
45 // =======================================================================
46 const Handle(AIS_InteractiveContext)& VInspector_ViewModel::GetContext() const
47 {
48   return itemDynamicCast<VInspector_ItemContext>(RootItem (0))->GetContext();
49 }
50
51 // =======================================================================
52 // function : SetContext
53 // purpose :
54 // =======================================================================
55 void VInspector_ViewModel::SetContext (const Handle(AIS_InteractiveContext)& theContext)
56 {
57   // fill root item by the application
58   for (int aColId = 0, aNbColumns = columnCount(); aColId < aNbColumns; aColId++)
59     itemDynamicCast<VInspector_ItemContext>(myRootItems[aColId])->SetContext (theContext);
60   EmitLayoutChanged();
61 }
62
63 // =======================================================================
64 // function : FindPointers
65 // purpose :
66 // =======================================================================
67 QModelIndexList VInspector_ViewModel::FindPointers (const QStringList& thePointers)
68 {
69   QModelIndexList anIndices;
70   QModelIndex aParentIndex = index (0, 0);
71   TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex); // context item
72   for (int aRowId = 0, aCount = aParentItem->rowCount(); aRowId < aCount; aRowId++)
73   {
74     QModelIndex anIndex = index (aRowId, 0, aParentIndex);
75     TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
76     VInspector_ItemPresentableObjectPtr anItemPrs = itemDynamicCast<VInspector_ItemPresentableObject>(anItemBase);
77     if (!anItemPrs)
78       continue;
79     if (thePointers.contains (anItemPrs->PointerInfo()))
80       anIndices.append (anIndex);
81   }
82   return anIndices;
83 }
84
85 // =======================================================================
86 // function : FindIndex
87 // purpose :
88 // =======================================================================
89 QModelIndex VInspector_ViewModel::FindIndex (const Handle(AIS_InteractiveObject)& thePresentation) const
90 {
91   QModelIndex aParentIndex = index (0, 0);
92   TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex); // context item
93   for (int aRowId = 0, aCount = aParentItem->rowCount(); aRowId < aCount; aRowId++)
94   {
95     QModelIndex anIndex = index (aRowId, 0, aParentIndex);
96     TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
97     VInspector_ItemPresentableObjectPtr anItemPrs = itemDynamicCast<VInspector_ItemPresentableObject>(anItemBase);
98     if (!anItemPrs)
99       continue;
100     if (anItemPrs->GetInteractiveObject() == thePresentation)
101       return anIndex;
102   }
103   return QModelIndex();
104 }
105
106 // =======================================================================
107 // function : headerData
108 // purpose :
109 // =======================================================================
110 QVariant VInspector_ViewModel::headerData (int theSection, Qt::Orientation theOrientation, int theRole) const
111 {
112   if (theOrientation != Qt::Horizontal || theRole != Qt::DisplayRole)
113     return QVariant();
114
115   switch (theSection)
116   {
117     case 0: return "Name";
118     case 1: return "Size";
119     case 2: return "Pointer";
120     case 3: return "Shape type";
121     case 4: return "Selection";
122     case 5: return "Base Sensitive";
123     case 6: return "Sensitivity";
124     case 7: return "SubElements"; 
125     case 8: return "Deviation/Deflectiton/Update/Priority";
126       //Auto Triangulation
127     default: break;
128   }
129   return QVariant();
130 }
131
132 // =======================================================================
133 // function : GetSelectedOwners
134 // purpose :
135 // =======================================================================
136 void VInspector_ViewModel::GetSelectedOwners (QItemSelectionModel* theSelectionModel,
137                                               NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
138 {
139   if (!theSelectionModel)
140     return;
141   QList<TreeModel_ItemBasePtr> anItems;
142   
143   QModelIndexList anIndices = theSelectionModel->selectedIndexes();
144   for (QModelIndexList::const_iterator anIndicesIt = anIndices.begin(); anIndicesIt != anIndices.end(); anIndicesIt++)
145   {
146     TreeModel_ItemBasePtr anItem = TreeModel_ModelBase::GetItemByIndex (*anIndicesIt);
147     if (!anItem || anItems.contains (anItem))
148       continue;
149     anItems.append (anItem);
150   }
151
152   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
153   for (QList<TreeModel_ItemBasePtr>::const_iterator anItemIt = anItems.begin(); anItemIt != anItems.end(); anItemIt++)
154   {
155     TreeModel_ItemBasePtr anItem = *anItemIt;
156     Handle(SelectBasics_EntityOwner) anEntityOwner;
157     if (VInspector_ItemEntityOwnerPtr anOwnerItem = itemDynamicCast<VInspector_ItemEntityOwner>(anItem))
158     {
159       anEntityOwner = anOwnerItem->EntityOwner();
160     }
161     else if (VInspector_ItemSensitiveEntityPtr aSensItem = itemDynamicCast<VInspector_ItemSensitiveEntity>(anItem))
162     {
163       anEntityOwner = aSensItem->GetSensitiveEntity()->BaseSensitive()->OwnerId();
164     }
165     if (anEntityOwner.IsNull())
166       continue;
167     if (aSelectedIds.contains ((size_t)anEntityOwner.operator->()))
168       continue;
169     aSelectedIds.append ((size_t)anEntityOwner.operator->());
170     if (!anEntityOwner.IsNull())
171       theOwners.Append (anEntityOwner);
172   }
173 }