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