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