]> SALOME platform Git repositories - modules/gui.git/blob - src/OCCViewer/OCCViewer_AISSelector.cxx
Salome HOME
RNV: fix compilation with the dev version of OCCT.
[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     const Handle(Graphic3d_HighlightStyle)& hStyle = myAISContext->HighlightStyle();
77     hStyle->SetColor( myHilightColor );
78 #else
79   myAISContext->SetHilightColor( myHilightColor );
80 #endif  
81   }
82 }
83
84 /*!
85   Sets the color to display the selected objects
86 */
87 void OCCViewer_AISSelector::setSelectColor ( Quantity_NameOfColor color )
88 {
89   mySelectColor = color;
90   if ( !myAISContext.IsNull() ) {
91 #if OCC_VERSION_LARGE > 0x07000000
92     const Handle(Graphic3d_HighlightStyle)& sStyle = myAISContext->SelectionStyle();
93     sStyle->SetColor( mySelectColor );
94 #else    
95     myAISContext->SelectionColor( mySelectColor );
96 #endif
97   }
98 }
99
100 /*!
101   Sets the interactive context for this selector
102 */
103 void OCCViewer_AISSelector::setAISContext ( const Handle (AIS_InteractiveContext)& aisContext )
104 {
105   myAISContext = aisContext;
106   if ( ! myAISContext.IsNull() ) { 
107 #if OCC_VERSION_LARGE > 0x07000000
108     const Handle(Graphic3d_HighlightStyle)& hStyle = myAISContext->HighlightStyle();
109     const Handle(Graphic3d_HighlightStyle)& sStyle = myAISContext->SelectionStyle();
110     hStyle->SetColor( myHilightColor );
111     sStyle->SetColor( mySelectColor );
112 #else
113     myAISContext->SetHilightColor( myHilightColor );
114     myAISContext->SelectionColor( mySelectColor );
115 #endif    
116     myAISContext->SetSubIntensityColor( Quantity_NOC_CYAN1 );
117   }
118 }
119
120 /*!
121   Checks the status of pick and emits 'selSelectionDone' or
122   'selSelectionCancel'.
123   Returns 'true' if no error, 'false' otherwise.
124 */
125 bool OCCViewer_AISSelector::checkSelection ( AIS_StatusOfPick status, 
126                                              bool hadSelection, 
127                                              bool addTo )
128 {
129   if ( myAISContext.IsNull() )
130     return false;
131
132   myNumSelected = myAISContext->NbCurrents(); /* update after the last selection */
133   
134   if ( status == AIS_SOP_NothingSelected && !hadSelection ) {
135     emit selSelectionCancel( addTo );
136   }
137   else if ( status == AIS_SOP_NothingSelected && hadSelection ) {
138     emit selSelectionCancel( addTo ); /* unselected now */
139   }
140   else if ( status == AIS_SOP_OneSelected || status == AIS_SOP_SeveralSelected )
141   {
142     emit selSelectionDone( addTo ); /* selected ( the same object, may be ) */
143   }
144   return ( status != AIS_SOP_Error && status != AIS_SOP_NothingSelected );
145 }
146
147 /*!
148   Detects the interactive objects at position (x,y).
149   Returns 'true' if no error, 'false' otherwise.
150 */
151 bool OCCViewer_AISSelector::moveTo ( int x, int y, const Handle (V3d_View)& view )
152 {
153   if ( myAISContext.IsNull() )
154     return false;
155
156   if ( !myEnableSelection )
157     return false;
158   
159   AIS_StatusOfDetection status = AIS_SOD_Error;
160   status = myAISContext->MoveTo (x, y, view);
161   
162   return ( status != AIS_SOD_Error && status != AIS_SOD_AllBad );
163 }
164
165 /*!
166   Selects the detected interactive objects.
167   Calls checkSelection() for checking the status.
168 */
169 bool OCCViewer_AISSelector::select ()
170 {
171   if ( myAISContext.IsNull() )
172     return false;
173
174   if ( !myEnableSelection )
175     return false;
176   
177   bool hadSelection = ( myNumSelected > 0 );
178   
179   /* select and send notifications */
180   return checkSelection ( myAISContext->Select(), hadSelection, false );
181 }
182
183 /*!
184   Selects the objects covered by the rectangle.
185   Multiple selection must be enabled to get use of this function.
186   Calls checkSelection() for checking the status.
187 */
188 bool OCCViewer_AISSelector::select ( int left, int top, int right, int bottom,
189                                      const Handle (V3d_View)& view )
190 {
191   if ( myAISContext.IsNull() )
192     return false;
193
194   if ( !myEnableSelection || !myEnableMultipleSelection )
195     return false;  /* selection with rectangle is considered as multiple selection */
196   
197   bool hadSelection = ( myNumSelected > 0 );
198   
199   /* select and send notifications */
200   return checkSelection ( myAISContext->Select(left, top, right, bottom, view),
201                           hadSelection, false );
202 }
203
204 /*!
205   Adds new selected objects to the objects 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 ()
210 {
211   if ( myAISContext.IsNull() )
212     return false;
213
214   if ( !myEnableSelection )
215     return false;
216   
217   bool hadSelection = ( myNumSelected > 0 ); /* something was selected */
218   if ( hadSelection && !myEnableMultipleSelection)
219     return false;
220   
221   /* select and send notifications */
222   return checkSelection ( myAISContext->ShiftSelect(), hadSelection, true );
223 }
224
225 /*!
226   Adds new selected objects covered by the rectangle to the objects
227   previously selected.
228   Multiple selection must be enabled to get use of this function.
229   Calls checkSelection() for checking the status.
230 */
231 bool OCCViewer_AISSelector::shiftSelect ( int left, int top, int right, int bottom,
232                                          const Handle (V3d_View)& view )
233                                          
234 {
235   if ( myAISContext.IsNull() )
236     return false;
237
238   if ( !myEnableSelection || !myEnableMultipleSelection )
239     return false;  /* selection with rectangle is considered as multiple selection */
240   
241   bool hadSelection = ( myNumSelected > 0 );      /* something was selected */
242   if ( hadSelection && !myEnableMultipleSelection)
243     return false;
244   
245   /* select and send notifications */
246   return checkSelection ( myAISContext->ShiftSelect(left,top,right,bottom, view),
247     hadSelection, true );
248 }