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