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 <QItemSelectionModel>
25 #include <QStringList>
27 // =======================================================================
28 // function : Constructor
30 // =======================================================================
31 VInspector_ViewModel::VInspector_ViewModel (QObject* theParent)
32 : TreeModel_ModelBase (theParent)
34 for (int aColumnId = 0, aNbColumns = columnCount(); aColumnId < aNbColumns; aColumnId++)
35 myRootItems.insert (aColumnId, VInspector_ItemContext::CreateItem (TreeModel_ItemBasePtr(), 0, aColumnId));
37 m_pRootItem = myRootItems[0];
40 // =======================================================================
41 // function : GetContext
43 // =======================================================================
44 const Handle(AIS_InteractiveContext)& VInspector_ViewModel::GetContext() const
46 return itemDynamicCast<VInspector_ItemContext>(RootItem (0))->GetContext();
49 // =======================================================================
50 // function : SetContext
52 // =======================================================================
53 void VInspector_ViewModel::SetContext (const Handle(AIS_InteractiveContext)& theContext)
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);
61 // =======================================================================
62 // function : FindPointers
64 // =======================================================================
65 QModelIndexList VInspector_ViewModel::FindPointers (const QStringList& thePointers)
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++)
72 QModelIndex anIndex = index (aRowId, 0, aParentIndex);
73 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
74 VInspector_ItemPresentableObjectPtr anItemPrs = itemDynamicCast<VInspector_ItemPresentableObject>(anItemBase);
77 if (thePointers.contains (anItemPrs->PointerInfo()))
78 anIndices.append (anIndex);
83 // =======================================================================
84 // function : FindIndex
86 // =======================================================================
87 QModelIndex VInspector_ViewModel::FindIndex (const Handle(AIS_InteractiveObject)& thePresentation) const
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++)
93 QModelIndex anIndex = index (aRowId, 0, aParentIndex);
94 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
95 VInspector_ItemPresentableObjectPtr anItemPrs = itemDynamicCast<VInspector_ItemPresentableObject>(anItemBase);
98 if (anItemPrs->GetInteractiveObject() == thePresentation)
101 return QModelIndex();
104 // =======================================================================
105 // function : headerData
107 // =======================================================================
108 QVariant VInspector_ViewModel::headerData (int theSection, Qt::Orientation theOrientation, int theRole) const
110 if (theOrientation != Qt::Horizontal || theRole != Qt::DisplayRole)
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";
130 // =======================================================================
131 // function : GetSelectedOwners
133 // =======================================================================
134 void VInspector_ViewModel::GetSelectedOwners (QItemSelectionModel* theSelectionModel,
135 NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
137 if (!theSelectionModel)
139 QList<TreeModel_ItemBasePtr> anItems;
141 QModelIndexList anIndices = theSelectionModel->selectedIndexes();
142 for (QModelIndexList::const_iterator anIndicesIt = anIndices.begin(); anIndicesIt != anIndices.end(); anIndicesIt++)
144 TreeModel_ItemBasePtr anItem = TreeModel_ModelBase::GetItemByIndex (*anIndicesIt);
145 if (!anItem || anItems.contains (anItem))
147 anItems.append (anItem);
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++)
153 TreeModel_ItemBasePtr anItem = *anItemIt;
154 Handle(SelectBasics_EntityOwner) anEntityOwner;
155 if (VInspector_ItemEntityOwnerPtr anOwnerItem = itemDynamicCast<VInspector_ItemEntityOwner>(anItem))
157 anEntityOwner = anOwnerItem->EntityOwner();
159 else if (VInspector_ItemSensitiveEntityPtr aSensItem = itemDynamicCast<VInspector_ItemSensitiveEntity>(anItem))
161 anEntityOwner = aSensItem->GetSensitiveEntity()->BaseSensitive()->OwnerId();
163 if (anEntityOwner.IsNull())
165 if (aSelectedIds.contains ((size_t)anEntityOwner.operator->()))
167 aSelectedIds.append ((size_t)anEntityOwner.operator->());
168 if (!anEntityOwner.IsNull())
169 theOwners.Append (anEntityOwner);