]> SALOME platform Git repositories - modules/gui.git/blob - src/GLViewer/GLViewer_Selector2d.cxx
Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / GLViewer / GLViewer_Selector2d.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  Author : OPEN CASCADE
23 // File:      GLViewer_Selector2d.cxx
24 // Created:   November, 2004
25 //#include <GLViewerAfx.h>
26 //
27 #include "GLViewer_Selector2d.h"
28 #include "GLViewer_Viewer2d.h"
29 #include "GLViewer_Context.h"
30 #include "GLViewer_ViewPort.h"
31 #include "GLViewer_ViewFrame.h"
32
33 /*!
34   Constructor
35 */
36 GLViewer_Selector2d::GLViewer_Selector2d( GLViewer_Viewer2d* v2d, GLViewer_Context* glc ) :
37 GLViewer_Selector( v2d ),
38 myGLContext( glc )
39 {   
40 //  myGLContext->SetHighlightColor( Quantity_NOC_CYAN1 );
41 //  myGLContext->SetSelectionColor( Quantity_NOC_RED );
42 }
43
44 /*!
45   Destructor
46 */
47 GLViewer_Selector2d::~GLViewer_Selector2d()
48 {
49 }
50
51 /*!
52   Changes hilight color of context
53   \param color - new hilight color
54 */
55 void GLViewer_Selector2d::setHilightColor( Quantity_NameOfColor color )
56 {
57   myGLContext->SetHighlightColor( color );
58 }
59
60 /*!
61   Changes selection color of context
62   \param color - new selection color
63 */
64 void GLViewer_Selector2d::setSelectColor( Quantity_NameOfColor color )
65 {
66   myGLContext->SetSelectionColor( color );
67 }
68
69 /*!
70   Detects object at point
71   \param x, y - point co-ordinates
72 */
73 void GLViewer_Selector2d::detect( int x, int y )
74 {
75   //cout << "GLViewer_Selector2d    : detect ( " << x << " , " << y << " )" << endl;
76   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() || 
77        myViewer->getSelectionMode() == GLViewer_Viewer::NoSelection )
78     return;
79
80   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
81   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
82     return;
83
84   myGLContext->MoveTo( x, y );
85 }
86
87 /*!
88   Undetects all objects
89 */
90 void GLViewer_Selector2d::undetectAll()
91 {
92   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() || 
93        myViewer->getSelectionMode() == GLViewer_Viewer::NoSelection )
94     return;
95
96   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
97   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
98     return;
99
100   myGLContext->clearHighlighted( true );
101 }
102
103 /*!
104   Selects previously hilighted objects
105   \param append - append objects to selection
106 */
107 void GLViewer_Selector2d::select( bool append )
108 {
109   //cout << "GLViewer_Selector2d    : select ( " << (int)append << " )" << endl;
110   GLViewer_Viewer::SelectionMode selMode = myViewer->getSelectionMode();
111   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() ||
112        selMode == GLViewer_Viewer::NoSelection ) 
113     return;
114
115   int selBefore = numSelected();
116   if ( selBefore && append && selMode != GLViewer_Viewer::Multiple )
117     return;    
118
119   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
120   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
121       return;
122
123   int status = myGLContext->Select( append );
124   checkSelection( selBefore, append, status );
125 }
126
127 /*!
128   Selects objects in rectangle
129   \param selRect - selection rectangle
130   \param append - append objects to selection
131 */
132 void GLViewer_Selector2d::select( const QRect& selRect, bool append )
133 {
134     GLViewer_Viewer::SelectionMode selMode = myViewer->getSelectionMode();
135     if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() ||
136        selMode == GLViewer_Viewer::NoSelection ) 
137     return;
138
139     int selBefore = numSelected();
140     if ( selBefore && append && selMode != GLViewer_Viewer::Multiple )
141         return;    
142
143     GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
144     if( !vp->inherits( "GLViewer_ViewPort2d" ) )
145         return;
146
147     int aStatus = myGLContext->SelectByRect( selRect, append );
148     checkSelection( selBefore, append, aStatus );
149 }
150
151 /*!
152   Unselects all objects
153 */
154 void GLViewer_Selector2d::unselectAll()
155 {
156   if ( myLocked || !myViewer ) 
157     return;
158
159 //  bool updateViewer = true;
160   bool hadSelection = ( numSelected() > 0 );
161      
162 //     bool lcOpen = ( myAISContext->IndexOfCurrentLocal() != -1 );
163 //     lcOpen ? myAISContext->ClearSelected( updateViewer ) :    
164 //              myAISContext->ClearCurrent( updateViewer );     
165   if ( hadSelection ) emit selSelectionCancel();
166 }
167
168 /*!
169   Checks selection state and emits  'selSelectionDone' or 'selSelectionCancel'     
170   Should be called by after non-interactive selection.
171 */
172 void GLViewer_Selector2d::checkSelection( int selBefore, bool append, int aStatus )
173 {
174     int selAfter = numSelected();
175     if ( selBefore > 0 && selAfter < 1 )     
176         emit selSelectionCancel();
177     else if ( selAfter > 0 )
178     {
179         switch( aStatus )
180         {
181         case SS_LocalChanged:
182             emit selSelectionDone( selAfter > 1, SCS_Local );
183             break;
184         case SS_GlobalChanged:
185             emit selSelectionDone( selAfter > 1, SCS_Global );
186             break;
187         }
188     }
189 }
190
191 /*!
192   \return number of selected objects
193 */
194 int GLViewer_Selector2d::numSelected() const
195 {
196   return myGLContext->NbSelected();
197 }
198