]> SALOME platform Git repositories - modules/gui.git/blob - src/OCCViewer/OCCViewer_AISSelector.cxx
Salome HOME
44b55e471281af7c3102f216b12c5a3e0094b1f6
[modules/gui.git] / src / OCCViewer / OCCViewer_AISSelector.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "OCCViewer_AISSelector.h"
23
24 /*!
25   Constructor
26 */
27 OCCViewer_AISSelector::OCCViewer_AISSelector( QObject* parent, 
28                                              const Handle (AIS_InteractiveContext)& aisContext) :
29   QObject( parent ), 
30   myNumSelected( 0 ), 
31   myEnableSelection( true ),
32   myEnableMultipleSelection( true )
33 {
34   myHilightColor = Quantity_NOC_CYAN1;
35   mySelectColor  = Quantity_NOC_GRAY80;
36
37   setAISContext( aisContext );
38 }
39
40 /*!
41   Destructor
42 */
43 OCCViewer_AISSelector::~OCCViewer_AISSelector()
44 {
45 }
46
47 /*!
48   Enables/disables selection
49 */
50 void OCCViewer_AISSelector::enableSelection( bool bEnable )
51 {
52   myEnableSelection = bEnable;
53 }
54
55 /*!
56   Enables/disables multiple selection i.e
57   selection of several objects at the same time.
58 */
59 void OCCViewer_AISSelector::enableMultipleSelection( bool bEnable )
60 {
61   myEnableMultipleSelection = bEnable;
62   if ( bEnable ) myEnableSelection = bEnable;
63 }
64
65 /*!
66   Sets the color to hilight the detected objects
67 */
68 void OCCViewer_AISSelector::setHilightColor ( Quantity_NameOfColor color )
69 {
70   myHilightColor = color;
71   if ( !myAISContext.IsNull() )
72     myAISContext->SetHilightColor( myHilightColor );
73 }
74
75 /*!
76   Sets the color to display the selected objects
77 */
78 void OCCViewer_AISSelector::setSelectColor ( Quantity_NameOfColor color )
79 {
80   mySelectColor = color;
81   if ( !myAISContext.IsNull() )
82     myAISContext->SelectionColor( mySelectColor );
83 }
84
85 /*!
86   Sets the interactive context for this selector
87 */
88 void OCCViewer_AISSelector::setAISContext ( const Handle (AIS_InteractiveContext)& aisContext )
89 {
90   myAISContext = aisContext;
91   if ( ! myAISContext.IsNull() ) { 
92     myAISContext->SetHilightColor( myHilightColor );
93     myAISContext->SelectionColor( mySelectColor );
94     myAISContext->SetSubIntensityColor( Quantity_NOC_CYAN1 );
95   }
96 }
97
98 /*!
99   Checks the status of pick and emits 'selSelectionDone' or
100   'selSelectionCancel'.
101   Returns 'true' if no error, 'false' otherwise.
102 */
103 bool OCCViewer_AISSelector::checkSelection ( AIS_StatusOfPick status, 
104                                              bool hadSelection, 
105                                              bool addTo )
106 {
107   if ( myAISContext.IsNull() )
108     return false;
109
110   myNumSelected = myAISContext->NbCurrents(); /* update after the last selection */
111   
112   if ( status == AIS_SOP_NothingSelected && !hadSelection ) {
113     emit selSelectionCancel( addTo );
114   }
115   else if ( status == AIS_SOP_NothingSelected && hadSelection ) {
116     emit selSelectionCancel( addTo ); /* unselected now */
117   }
118   else if ( status == AIS_SOP_OneSelected || status == AIS_SOP_SeveralSelected )
119   {
120     emit selSelectionDone( addTo ); /* selected ( the same object, may be ) */
121   }
122   return ( status != AIS_SOP_Error && status != AIS_SOP_NothingSelected );
123 }
124
125 /*!
126   Detects the interactive objects at position (x,y).
127   Returns 'true' if no error, 'false' otherwise.
128 */
129 bool OCCViewer_AISSelector::moveTo ( int x, int y, const Handle (V3d_View)& view )
130 {
131   if ( myAISContext.IsNull() )
132     return false;
133
134   if ( !myEnableSelection )
135     return false;
136   
137   AIS_StatusOfDetection status = AIS_SOD_Error;
138   status = myAISContext->MoveTo (x, y, view);
139   
140   return ( status != AIS_SOD_Error && status != AIS_SOD_AllBad );
141 }
142
143 /*!
144   Selects the detected interactive objects.
145   Calls checkSelection() for checking the status.
146 */
147 bool OCCViewer_AISSelector::select ()
148 {
149   if ( myAISContext.IsNull() )
150     return false;
151
152   if ( !myEnableSelection )
153     return false;
154   
155   bool hadSelection = ( myNumSelected > 0 );
156   
157   /* select and send notifications */
158   return checkSelection ( myAISContext->Select(), hadSelection, false );
159 }
160
161 /*!
162   Selects the objects covered by the rectangle.
163   Multiple selection must be enabled to get use of this function.
164   Calls checkSelection() for checking the status.
165 */
166 bool OCCViewer_AISSelector::select ( int left, int top, int right, int bottom,
167                                      const Handle (V3d_View)& view )
168 {
169   if ( myAISContext.IsNull() )
170     return false;
171
172   if ( !myEnableSelection || !myEnableMultipleSelection )
173     return false;  /* selection with rectangle is considered as multiple selection */
174   
175   bool hadSelection = ( myNumSelected > 0 );
176   
177   /* select and send notifications */
178   return checkSelection ( myAISContext->Select(left, top, right, bottom, view),
179                           hadSelection, false );
180 }
181
182 /*!
183   Adds new selected objects to the objects previously selected.
184   Multiple selection must be enabled to get use of this function.
185   Calls checkSelection() for checking the status.
186 */
187 bool OCCViewer_AISSelector::shiftSelect ()
188 {
189   if ( myAISContext.IsNull() )
190     return false;
191
192   if ( !myEnableSelection )
193     return false;
194   
195   bool hadSelection = ( myNumSelected > 0 ); /* something was selected */
196   if ( hadSelection && !myEnableMultipleSelection)
197     return false;
198   
199   /* select and send notifications */
200   return checkSelection ( myAISContext->ShiftSelect(), hadSelection, true );
201 }
202
203 /*!
204   Adds new selected objects covered by the rectangle to the objects
205   previously selected.
206   Multiple selection must be enabled to get use of this function.
207   Calls checkSelection() for checking the status.
208 */
209 bool OCCViewer_AISSelector::shiftSelect ( int left, int top, int right, int bottom,
210                                          const Handle (V3d_View)& view )
211                                          
212 {
213   if ( myAISContext.IsNull() )
214     return false;
215
216   if ( !myEnableSelection || !myEnableMultipleSelection )
217     return false;  /* selection with rectangle is considered as multiple selection */
218   
219   bool hadSelection = ( myNumSelected > 0 );      /* something was selected */
220   if ( hadSelection && !myEnableMultipleSelection)
221     return false;
222   
223   /* select and send notifications */
224   return checkSelection ( myAISContext->ShiftSelect(left,top,right,bottom, view),
225     hadSelection, true );
226 }