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