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