Salome HOME
sources v1.2
[modules/kernel.git] / src / OCCViewer / OCCViewer_AISSelector.cxx
1 //  SALOME OCCViewer : build OCC Viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : OCCViewer_AISSelector.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 /*!
31   \class OCCViewer_AISSelector OCCViewer_AISSelector.h
32   \brief Selector for AIS interactive context.
33
34   Used only by Viewer Open CASCADE.
35 */
36
37 #include "OCCViewer_AISSelector.h"
38
39 /*!
40     Constructor
41 */
42 OCCViewer_AISSelector::OCCViewer_AISSelector( const Handle (AIS_InteractiveContext)& aisContext) :
43 myAISContext ( aisContext )
44 {
45   initialize();
46 }
47
48 /*!
49     Destructor
50 */
51 OCCViewer_AISSelector::~OCCViewer_AISSelector()
52 {
53   cleanup();
54 }
55
56 /*!
57     Initialization ( internal )
58 */
59 void OCCViewer_AISSelector::initialize()
60 {
61   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
62
63   myHilightColor = Quantity_NOC_CYAN1;
64   mySelectColor = Quantity_NOC_GRAY80;
65
66   myAISContext->SetHilightColor( myHilightColor );
67   myAISContext->SelectionColor( mySelectColor );
68   myAISContext->SetSubIntensityColor( Quantity_NOC_CYAN1 );
69 }
70
71 /*!
72     Cleanup ( internal )
73 */
74 void OCCViewer_AISSelector::cleanup()
75 {
76 }
77
78 /*!
79     Sets the color to hilight the detected objects
80 */
81 void OCCViewer_AISSelector::setHilightColor ( Quantity_NameOfColor color )
82 {
83   myHilightColor = color;
84 }
85
86 /*!
87     Sets the color to display the selected objects
88 */
89 void OCCViewer_AISSelector::setSelectColor ( Quantity_NameOfColor color )
90 {
91   mySelectColor = color;
92 }
93
94 /*!
95   Sets the interactive context for this selector
96 */
97 void OCCViewer_AISSelector::setContext ( const Handle (AIS_InteractiveContext)& aisContext )
98 {
99   QAD_ASSERT_DEBUG_ONLY ( !aisContext.IsNull() );
100   myAISContext = aisContext;
101 }
102
103 /*!
104     Checks the status of pick and emits 'selSelectionDone' or
105     'selSelectionCancel'.
106     Returns 'true' if no error, 'false' otherwise.
107 */
108 bool OCCViewer_AISSelector::checkSelection ( AIS_StatusOfPick status, bool hadSelection, bool addTo )
109 {
110   myNumSelected = myAISContext->NbCurrents(); /* update after the last selection */
111
112   if ( status == AIS_SOP_NothingSelected && !hadSelection ) {
113     emit selSelectionCancel( addTo );
114   }
115   else if ( status == AIS_SOP_NothingSelected && hadSelection ) {
116     emit selSelectionCancel( addTo ); /* unselected now */
117   }
118   else if ( status == AIS_SOP_OneSelected || status == AIS_SOP_SeveralSelected )
119     {
120       //      if ( !hadSelection || status != AIS_SOP_SeveralSelected )
121       //        addTo = false;
122       //      MESSAGE (" AIS_StatusOfPick : " << AIS_SOP_OneSelected )
123
124       emit selSelectionDone( addTo ); /* selected ( the same object, may be ) */
125     }
126   return ( status != AIS_SOP_Error && status != AIS_SOP_NothingSelected );
127 }
128
129
130 /*!
131     Detects the interactive objects at position (x,y).
132     Returns 'true' if no error, 'false' otherwise.
133 */
134 bool OCCViewer_AISSelector::moveTo ( int x, int y, const Handle (V3d_View)& view )
135 {
136   if ( !myEnableSelection )
137     return false;
138
139   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
140   AIS_StatusOfDetection status = AIS_SOD_Error;
141   status = myAISContext->MoveTo (x, y, view);
142
143   return ( status != AIS_SOD_Error && status != AIS_SOD_AllBad );
144 }
145
146 /*!
147     Selects the detected interactive objects.
148     Calls checkSelection() for checking the status.
149 */
150 bool OCCViewer_AISSelector::select ()
151 {
152   if ( !myEnableSelection )
153     return false;
154
155   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
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 ( !myEnableSelection || !myEnableMultipleSelection )
171     return false;       /* selection with rectangle is considered as multiple selection */
172
173   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
174   bool hadSelection = ( myNumSelected > 0 );
175
176   /* select and send notifications */
177   return checkSelection ( myAISContext->Select(left, top, right, bottom, view),
178                           hadSelection, false );
179 }
180
181 /*!
182     Adds new selected objects to the objects previously selected.
183     Multiple selection must be enabled to get use of this function.
184     Calls checkSelection() for checking the status.
185 */
186 bool OCCViewer_AISSelector::shiftSelect ()
187 {
188   if ( !myEnableSelection )
189     return false;
190
191   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
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 ( !myEnableSelection || !myEnableMultipleSelection )
211     return false;       /* selection with rectangle is considered as multiple selection */
212
213   QAD_ASSERT_DEBUG_ONLY ( !myAISContext.IsNull() );
214   bool hadSelection = ( myNumSelected > 0 );                    /* something was selected */
215   if ( hadSelection && !myEnableMultipleSelection)
216     return false;
217
218   /* select and send notifications */
219   return checkSelection ( myAISContext->ShiftSelect(left,top,right,bottom, view),
220                           hadSelection, true );
221 }