1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/VInspector_ViewModel.hxx>
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>
24 #include <Standard_WarningsDisable.hxx>
25 #include <QItemSelectionModel>
26 #include <QStringList>
27 #include <Standard_WarningsRestore.hxx>
29 // =======================================================================
30 // function : Constructor
32 // =======================================================================
33 VInspector_ViewModel::VInspector_ViewModel (QObject* theParent)
34 : TreeModel_ModelBase (theParent)
36 for (int aColumnId = 0, aNbColumns = columnCount(); aColumnId < aNbColumns; aColumnId++)
37 myRootItems.insert (aColumnId, VInspector_ItemContext::CreateItem (TreeModel_ItemBasePtr(), 0, aColumnId));
39 m_pRootItem = myRootItems[0];
42 // =======================================================================
43 // function : GetContext
45 // =======================================================================
46 const Handle(AIS_InteractiveContext)& VInspector_ViewModel::GetContext() const
48 return itemDynamicCast<VInspector_ItemContext>(RootItem (0))->GetContext();
51 // =======================================================================
52 // function : SetContext
54 // =======================================================================
55 void VInspector_ViewModel::SetContext (const Handle(AIS_InteractiveContext)& theContext)
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);
63 // =======================================================================
64 // function : FindPointers
66 // =======================================================================
67 QModelIndexList VInspector_ViewModel::FindPointers (const QStringList& thePointers)
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++)
74 QModelIndex anIndex = index (aRowId, 0, aParentIndex);
75 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
76 VInspector_ItemPresentableObjectPtr anItemPrs = itemDynamicCast<VInspector_ItemPresentableObject>(anItemBase);
79 if (thePointers.contains (anItemPrs->PointerInfo()))
80 anIndices.append (anIndex);
85 // =======================================================================
86 // function : FindIndex
88 // =======================================================================
89 QModelIndex VInspector_ViewModel::FindIndex (const Handle(AIS_InteractiveObject)& thePresentation) const
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++)
95 QModelIndex anIndex = index (aRowId, 0, aParentIndex);
96 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
97 VInspector_ItemPresentableObjectPtr anItemPrs = itemDynamicCast<VInspector_ItemPresentableObject>(anItemBase);
100 if (anItemPrs->GetInteractiveObject() == thePresentation)
103 return QModelIndex();
106 // =======================================================================
107 // function : headerData
109 // =======================================================================
110 QVariant VInspector_ViewModel::headerData (int theSection, Qt::Orientation theOrientation, int theRole) const
112 if (theOrientation != Qt::Horizontal || theRole != Qt::DisplayRole)
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";
132 // =======================================================================
133 // function : GetSelectedOwners
135 // =======================================================================
136 void VInspector_ViewModel::GetSelectedOwners (QItemSelectionModel* theSelectionModel,
137 NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
139 if (!theSelectionModel)
141 QList<TreeModel_ItemBasePtr> anItems;
143 QModelIndexList anIndices = theSelectionModel->selectedIndexes();
144 for (QModelIndexList::const_iterator anIndicesIt = anIndices.begin(); anIndicesIt != anIndices.end(); anIndicesIt++)
146 TreeModel_ItemBasePtr anItem = TreeModel_ModelBase::GetItemByIndex (*anIndicesIt);
147 if (!anItem || anItems.contains (anItem))
149 anItems.append (anItem);
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++)
155 TreeModel_ItemBasePtr anItem = *anItemIt;
156 Handle(SelectBasics_EntityOwner) anEntityOwner;
157 if (VInspector_ItemEntityOwnerPtr anOwnerItem = itemDynamicCast<VInspector_ItemEntityOwner>(anItem))
159 anEntityOwner = anOwnerItem->EntityOwner();
161 else if (VInspector_ItemSensitiveEntityPtr aSensItem = itemDynamicCast<VInspector_ItemSensitiveEntity>(anItem))
163 anEntityOwner = aSensItem->GetSensitiveEntity()->BaseSensitive()->OwnerId();
165 if (anEntityOwner.IsNull())
167 if (aSelectedIds.contains ((size_t)anEntityOwner.operator->()))
169 aSelectedIds.append ((size_t)anEntityOwner.operator->());
170 if (!anEntityOwner.IsNull())
171 theOwners.Append (anEntityOwner);