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