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