0026462: Visualization - selection does not adapt to line width change
[occt.git] / src / SelectMgr / SelectMgr_Selection.cxx
1 // Created on: 1995-02-16
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Standard_NullObject.hxx>
18
19 #include <SelectBasics_EntityOwner.hxx>
20 #include <SelectMgr_Selection.hxx>
21
22 //==================================================
23 // Function: SelectMgr_Selection
24 // Purpose :
25 //==================================================
26 SelectMgr_Selection::SelectMgr_Selection (const Standard_Integer theModeIdx)
27 : myMode (theModeIdx),
28   mySelectionState (SelectMgr_SOS_Unknown),
29   myBVHUpdateStatus (SelectMgr_TBU_None),
30   mySensFactor (2),
31   myIsCustomSens (Standard_False)
32 {}
33
34 SelectMgr_Selection::~SelectMgr_Selection()
35 {
36   Destroy();
37 }
38
39 //==================================================
40 // Function: Destroy
41 // Purpose :
42 //==================================================
43 void SelectMgr_Selection::Destroy()
44 {
45   for (Standard_Integer anEntityIdx = 0; anEntityIdx < myEntities.Size(); ++anEntityIdx)
46   {
47     Handle(SelectMgr_SensitiveEntity)& anEntity = myEntities.ChangeValue (anEntityIdx);
48     anEntity->BaseSensitive()->Set (NULL);
49   }
50   mySensFactor = 2;
51 }
52
53 //==================================================
54 // Function: ADD
55 // Purpose :
56 //==================================================
57 void SelectMgr_Selection::Add (const Handle(SelectBasics_SensitiveEntity)& theSensitive)
58 {
59   // if input is null:
60   // in debug mode raise exception
61   Standard_NullObject_Raise_if
62     (theSensitive.IsNull(), "Null sensitive entity is added to the selection");
63
64   // in release mode do not add
65   if (!theSensitive.IsNull())
66   {
67     Handle(SelectMgr_SensitiveEntity) anEntity = new SelectMgr_SensitiveEntity (theSensitive);
68     myEntities.Append (anEntity);
69     if (mySelectionState == SelectMgr_SOS_Activated &&
70         !anEntity->IsActiveForSelection())
71     {
72       anEntity->SetActiveForSelection();
73     }
74
75     if (myIsCustomSens)
76     {
77       anEntity->BaseSensitive()->SetSensitivityFactor (mySensFactor);
78     }
79     else
80     {
81       mySensFactor = Max (mySensFactor,
82                           anEntity->BaseSensitive()->SensitivityFactor());
83     }
84   }
85 }       
86
87 //==================================================
88 // Function: Clear
89 // Purpose :
90 //==================================================
91 void SelectMgr_Selection::Clear()
92 {
93   for (Standard_Integer anIdx = 0; anIdx < myEntities.Size(); ++anIdx)
94   {
95     Handle(SelectMgr_SensitiveEntity)& anEntity = myEntities.ChangeValue (anIdx);
96     anEntity->Clear();
97   }
98
99   myEntities.Clear();
100 }
101
102 //==================================================
103 // Function: IsEmpty 
104 // Purpose :
105 //==================================================
106 Standard_Boolean SelectMgr_Selection::IsEmpty() const
107 {
108   return myEntities.IsEmpty();
109 }
110
111 //==================================================
112 // function: GetEntityById
113 // purpose : Returns sensitive entity stored by
114 //           index theIdx in entites vector
115 //==================================================
116 Handle(SelectMgr_SensitiveEntity)& SelectMgr_Selection::GetEntityById (const Standard_Integer theIdx)
117 {
118   return myEntities.ChangeValue (theIdx);
119 }
120
121 //==================================================
122 // function: GetSelectionState
123 // purpose : Returns status of selection
124 //==================================================
125 const SelectMgr_StateOfSelection SelectMgr_Selection::GetSelectionState() const
126 {
127   return mySelectionState;
128 }
129
130 //==================================================
131 // function: SetSelectionState
132 // purpose : Sets status of selection
133 //==================================================
134 void SelectMgr_Selection::SetSelectionState (const SelectMgr_StateOfSelection theState) const
135 {
136   mySelectionState = theState;
137 }
138
139 //==================================================
140 // function: Sensitivity
141 // purpose : Returns sensitivity of the selection
142 //==================================================
143 Standard_Integer SelectMgr_Selection::Sensitivity() const
144 {
145   return mySensFactor;
146 }
147
148 //==================================================
149 // function: SetSensitivity
150 // purpose : Changes sensitivity of the selection and all its entities to the given value.
151 //           IMPORTANT: This method does not update any outer selection structures, so for
152 //           proper updates use SelectMgr_SelectionManager::SetSelectionSensitivity method.
153 //==================================================
154 void SelectMgr_Selection::SetSensitivity (const Standard_Integer theNewSens)
155 {
156   mySensFactor = theNewSens;
157   myIsCustomSens = Standard_True;
158   for (Standard_Integer anIdx = 0; anIdx < myEntities.Size(); ++anIdx)
159   {
160     Handle(SelectMgr_SensitiveEntity)& anEntity = myEntities.ChangeValue (anIdx);
161     anEntity->BaseSensitive()->SetSensitivityFactor (theNewSens);
162   }
163 }