Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / GLViewer / GLViewer_Selector2d.cxx
1 //  Copyright (C) 2005 OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 // File:      GLViewer_Selector2d.cxx
23 // Created:   November, 2004
24
25 //#include <GLViewerAfx.h>
26 #include "GLViewer_Selector2d.h"
27 #include "GLViewer_Viewer2d.h"
28 #include "GLViewer_Context.h"
29 #include "GLViewer_ViewPort2d.h"
30
31 /*!
32   Constructor
33 */
34 GLViewer_Selector2d::GLViewer_Selector2d( GLViewer_Viewer2d* v2d, GLViewer_Context* glc ) :
35 GLViewer_Selector( v2d ),
36 myGLContext( glc )
37 {   
38 //  myGLContext->SetHighlightColor( Quantity_NOC_CYAN1 );
39 //  myGLContext->SetSelectionColor( Quantity_NOC_RED );
40 }
41
42 /*!
43   Destructor
44 */
45 GLViewer_Selector2d::~GLViewer_Selector2d()
46 {
47 }
48
49 /*!
50   Changes hilight color of context
51   \param color - new hilight color
52 */
53 void GLViewer_Selector2d::setHilightColor( Quantity_NameOfColor color )
54 {
55   myGLContext->SetHighlightColor( color );
56 }
57
58 /*!
59   Changes selection color of context
60   \param color - new selection color
61 */
62 void GLViewer_Selector2d::setSelectColor( Quantity_NameOfColor color )
63 {
64   myGLContext->SetSelectionColor( color );
65 }
66
67 /*!
68   Detects object at point
69   \param x, y - point co-ordinates
70 */
71 void GLViewer_Selector2d::detect( int x, int y )
72 {
73   //cout << "GLViewer_Selector2d    : detect ( " << x << " , " << y << " )" << endl;
74   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() || 
75        myViewer->getSelectionMode() == GLViewer_Viewer::NoSelection )
76     return;
77
78   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
79   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
80     return;
81
82   myGLContext->MoveTo( x, y );
83 }
84
85 /*!
86   Undetects all objects
87 */
88 void GLViewer_Selector2d::undetectAll()
89 {
90   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() || 
91        myViewer->getSelectionMode() == GLViewer_Viewer::NoSelection )
92     return;
93
94   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
95   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
96     return;
97
98   myGLContext->clearHighlighted( true );
99 }
100
101 /*!
102   Selects previously hilighted objects
103   \param append - append objects to selection
104 */
105 void GLViewer_Selector2d::select( bool append )
106 {
107   //cout << "GLViewer_Selector2d    : select ( " << (int)append << " )" << endl;
108   GLViewer_Viewer::SelectionMode selMode = myViewer->getSelectionMode();
109   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() ||
110        selMode == GLViewer_Viewer::NoSelection ) 
111     return;
112
113   int selBefore = numSelected();
114   if ( selBefore && append && selMode != GLViewer_Viewer::Multiple )
115     return;    
116
117   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
118   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
119       return;
120
121   int status = myGLContext->Select( append );
122   checkSelection( selBefore, append, status );
123 }
124
125 /*!
126   Selects objects in rectangle
127   \param selRect - selection rectangle
128   \param append - append objects to selection
129 */
130 void GLViewer_Selector2d::select( const QRect& selRect, bool append )
131 {
132     GLViewer_Viewer::SelectionMode selMode = myViewer->getSelectionMode();
133     if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() ||
134        selMode == GLViewer_Viewer::NoSelection ) 
135     return;
136
137     int selBefore = numSelected();
138     if ( selBefore && append && selMode != GLViewer_Viewer::Multiple )
139         return;    
140
141     GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
142     if( !vp->inherits( "GLViewer_ViewPort2d" ) )
143         return;
144
145     int aStatus = myGLContext->SelectByRect( selRect, append );
146     checkSelection( selBefore, append, aStatus );
147 }
148
149 /*!
150   Unselects all objects
151 */
152 void GLViewer_Selector2d::unselectAll()
153 {
154   if ( myLocked || !myViewer ) 
155     return;
156
157 //  bool updateViewer = true;
158   bool hadSelection = ( numSelected() > 0 );
159      
160 //     bool lcOpen = ( myAISContext->IndexOfCurrentLocal() != -1 );
161 //     lcOpen ? myAISContext->ClearSelected( updateViewer ) :    
162 //              myAISContext->ClearCurrent( updateViewer );     
163   if ( hadSelection ) emit selSelectionCancel();
164 }
165
166 /*!
167   Checks selection state and emits  'selSelectionDone' or 'selSelectionCancel'     
168   Should be called by after non-interactive selection.
169 */
170 void GLViewer_Selector2d::checkSelection( int selBefore, bool append, int aStatus )
171 {
172     int selAfter = numSelected();
173     if ( selBefore > 0 && selAfter < 1 )     
174         emit selSelectionCancel();
175     else if ( selAfter > 0 )
176     {
177         switch( aStatus )
178         {
179         case SS_LocalChanged:
180             emit selSelectionDone( selAfter > 1, SCS_Local );
181             break;
182         case SS_GlobalChanged:
183             emit selSelectionDone( selAfter > 1, SCS_Global );
184             break;
185         }
186     }
187 }
188
189 /*!
190   \return number of selected objects
191 */
192 int GLViewer_Selector2d::numSelected() const
193 {
194   return myGLContext->NbSelected();
195 }
196