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