Salome HOME
PR: mergefrom_PAL_OCC_21Oct04
[modules/kernel.git] / src / OCCViewer / OCCViewer_AISSelector.cxx
1 //  SALOME OCCViewer : build OCC Viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : OCCViewer_AISSelector.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 /*!
31   \class OCCViewer_AISSelector OCCViewer_AISSelector.h
32   \brief Selector for AIS interactive context.
33
34   Used only by Viewer Open CASCADE.
35 */
36
37 #include "OCCViewer_AISSelector.h"
38
39 /*!
40     Constructor
41 */
42 OCCViewer_AISSelector::OCCViewer_AISSelector( const Handle (AIS_InteractiveContext)& aisContext) :
43        myAISContext ( aisContext ),
44        myEnableSelection( true ),
45        myEnableMultipleSelection( true )
46 {
47   initialize();
48 }
49
50 /*!
51     Destructor
52 */
53 OCCViewer_AISSelector::~OCCViewer_AISSelector()
54 {
55   cleanup();
56 }
57
58 /*!
59     Initialization ( internal )
60 */
61 void OCCViewer_AISSelector::initialize()
62 {
63   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
64
65   myHilightColor = Quantity_NOC_CYAN1;
66   mySelectColor = Quantity_NOC_WHITE;
67
68   myAISContext->SetHilightColor( myHilightColor );
69   myAISContext->SelectionColor( mySelectColor );
70   myAISContext->SetSubIntensityColor( Quantity_NOC_CYAN1 );
71 }
72
73 /*!
74     Cleanup ( internal )
75 */
76 void OCCViewer_AISSelector::cleanup()
77 {
78 }
79
80 /*!
81     Sets the color to hilight the detected objects
82 */
83 void OCCViewer_AISSelector::setHilightColor ( Quantity_NameOfColor color )
84 {
85   myHilightColor = color;
86 }
87
88 /*!
89     Sets the color to display the selected objects
90 */
91 void OCCViewer_AISSelector::setSelectColor ( Quantity_NameOfColor color )
92 {
93   mySelectColor = color;
94 }
95
96 /*!
97   Sets the interactive context for this selector
98 */
99 void OCCViewer_AISSelector::setContext ( const Handle (AIS_InteractiveContext)& aisContext )
100 {
101   QAD_ASSERT_DEBUG_ONLY ( !aisContext.IsNull() );
102   myAISContext = aisContext;
103 }
104
105 /*!
106     Checks the status of pick and emits 'selSelectionDone' or
107     'selSelectionCancel'.
108     Returns 'true' if no error, 'false' otherwise.
109 */
110 void OCCViewer_AISSelector::checkSelection ( int numSelBefore )
111 {
112   int numSelAfter = numSelected();
113   if ( numSelAfter < 1 && numSelBefore > 0 )
114     emit selSelectionCancel( false );
115   else if ( numSelAfter > 0 )
116     emit selSelectionDone( numSelAfter > 1 );
117 }
118
119
120 /*!
121     Detects the interactive objects at position (x,y).
122     Returns 'true' if no error, 'false' otherwise.
123 */
124 void OCCViewer_AISSelector::moveTo ( int x, int y, const Handle (V3d_View)& view )
125 {
126   if ( !myEnableSelection )
127     return;
128
129   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
130   myAISContext->MoveTo (x, y, view);
131 }
132
133 /*!
134     Selects the detected interactive objects.
135     Calls checkSelection() for checking the status.
136 */
137 void OCCViewer_AISSelector::select ()
138 {
139   if ( !myEnableSelection )
140     return;
141
142   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
143   int numBefore = numSelected();
144
145   myAISContext->Select();
146
147   /* send notifications */
148   checkSelection ( numBefore );
149 }
150
151 /*!
152     Selects the objects covered by the rectangle.
153     Multiple selection must be enabled to get use of this function.
154     Calls checkSelection() for checking the status.
155 */
156 void OCCViewer_AISSelector::select ( int left, int top, int right, int bottom,
157                                const Handle (V3d_View)& view )
158 {
159   if ( !myEnableSelection || !myEnableMultipleSelection )
160     return;     /* selection with rectangle is considered as multiple selection */
161
162   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
163   int numBefore = numSelected();
164
165   myAISContext->Select(left, top, right, bottom, view);
166
167   /* send notifications */
168   checkSelection ( numBefore );
169 }
170
171 /*!
172     Adds new selected objects to the objects previously selected.
173     Multiple selection must be enabled to get use of this function.
174     Calls checkSelection() for checking the status.
175 */
176 void OCCViewer_AISSelector::shiftSelect ()
177 {
178   if ( !myEnableSelection )
179     return;
180
181   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
182   int numBefore = numSelected();
183
184   if ( numBefore && !myEnableMultipleSelection)
185     myAISContext->Select();
186   else
187     myAISContext->ShiftSelect();
188
189   /* send notifications */
190   checkSelection ( numBefore );
191 }
192
193 /*!
194     Adds new selected objects covered by the rectangle to the objects
195     previously selected.
196     Multiple selection must be enabled to get use of this function.
197     Calls checkSelection() for checking the status.
198 */
199 void OCCViewer_AISSelector::shiftSelect ( int left, int top, int right, int bottom,
200                                     const Handle (V3d_View)& view )
201
202 {
203   if ( !myEnableSelection || !myEnableMultipleSelection )
204     return;     /* selection with rectangle is considered as multiple selection */
205
206   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
207   int numBefore = numSelected();
208
209   myAISContext->ShiftSelect(left,top,right,bottom, view);
210
211   /* send notifications */
212   checkSelection ( numBefore );
213 }
214
215 /*!
216     Enables/disables selection
217 */
218 void OCCViewer_AISSelector::enableSelection( bool bEnable )
219 {
220   myEnableSelection = bEnable;
221 }
222
223 /*!
224     Enables/disables multiple selection i.e
225     selection of several objects at the same time.
226     If enabled, non-multiple selection is enabled as well.
227 */
228 void OCCViewer_AISSelector::enableMultipleSelection( bool bEnable )
229 {
230   myEnableMultipleSelection = bEnable;
231   if ( bEnable ) myEnableSelection = bEnable;
232 }
233
234 /*!
235     Returns the number of selected objects.
236 */
237 int OCCViewer_AISSelector::numSelected() const
238 {
239   if ( myAISContext.IsNull() )
240     return 0;
241
242   if ( myAISContext->HasOpenedContext() )
243     return myAISContext->NbSelected();
244   return myAISContext->NbCurrents();
245 }