Salome HOME
This file is given from DESCARTES project
[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 /****************************************************************************
26 **  Class:   GLViewer_Selector2d 
27 **  Descr:   OpenGL Selector 2D
28 **  Module:  GLViewer
29 **  Created: UI team, 20.09.02
30 *****************************************************************************/
31
32 //#include <GLViewerAfx.h>
33 #include "GLViewer_Selector2d.h"
34 #include "GLViewer_Viewer2d.h"
35 #include "GLViewer_Context.h"
36 #include "GLViewer_ViewPort2d.h"
37
38 GLViewer_Selector2d::GLViewer_Selector2d( GLViewer_Viewer2d* v2d, GLViewer_Context* glc ) :
39 GLViewer_Selector( v2d ),
40 myGLContext( glc )
41 {   
42 //  myGLContext->SetHighlightColor( Quantity_NOC_CYAN1 );
43 //  myGLContext->SetSelectionColor( Quantity_NOC_RED );
44 }
45
46 GLViewer_Selector2d::~GLViewer_Selector2d()
47 {
48 }
49
50 void GLViewer_Selector2d::setHilightColor( Quantity_NameOfColor color )
51 {
52   myGLContext->SetHighlightColor( color );
53 }
54
55 void GLViewer_Selector2d::setSelectColor( Quantity_NameOfColor color )
56 {
57   myGLContext->SetSelectionColor( color );
58 }
59
60 void GLViewer_Selector2d::detect( int x, int y )
61 {
62   //cout << "GLViewer_Selector2d    : detect ( " << x << " , " << y << " )" << endl;
63   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() || 
64        myViewer->getSelectionMode() == GLViewer_Viewer::NoSelection )
65     return;
66
67   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
68   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
69     return;
70
71   myGLContext->MoveTo( x, y );
72 }
73
74 void GLViewer_Selector2d::undetectAll()
75 {
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->clearHighlighted( true );
85 }
86
87 void GLViewer_Selector2d::select( bool append )
88 {
89   //cout << "GLViewer_Selector2d    : select ( " << (int)append << " )" << endl;
90   GLViewer_Viewer::SelectionMode selMode = myViewer->getSelectionMode();
91   if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() ||
92        selMode == GLViewer_Viewer::NoSelection ) 
93     return;
94
95   int selBefore = numSelected();
96   if ( selBefore && append && selMode != GLViewer_Viewer::Multiple )
97     return;    
98
99   GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
100   if( !vp->inherits( "GLViewer_ViewPort2d" ) )
101       return;
102
103   int status = myGLContext->Select( append );
104   checkSelection( selBefore, append, status );
105 }
106
107 void GLViewer_Selector2d::select( const QRect& selRect, bool append )
108 {
109     GLViewer_Viewer::SelectionMode selMode = myViewer->getSelectionMode();
110     if ( myLocked || !myGLContext || !myViewer || !myViewer->getActiveView() ||
111        selMode == GLViewer_Viewer::NoSelection ) 
112     return;
113
114     int selBefore = numSelected();
115     if ( selBefore && append && selMode != GLViewer_Viewer::Multiple )
116         return;    
117
118     GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
119     if( !vp->inherits( "GLViewer_ViewPort2d" ) )
120         return;
121
122     int aStatus = myGLContext->SelectByRect( selRect, append );
123     checkSelection( selBefore, append, aStatus );
124 }
125
126 void GLViewer_Selector2d::unselectAll()
127 {
128   if ( myLocked || !myViewer ) 
129     return;
130
131 //  bool updateViewer = true;
132   bool hadSelection = ( numSelected() > 0 );
133      
134 //     bool lcOpen = ( myAISContext->IndexOfCurrentLocal() != -1 );
135 //     lcOpen ? myAISContext->ClearSelected( updateViewer ) :    
136 //              myAISContext->ClearCurrent( updateViewer );     
137   if ( hadSelection ) emit selSelectionCancel();
138 }
139
140 /*  Checks selection state and emits  'selSelectionDone' or 'selSelectionCancel'     
141     Should be called by after non-interactive selection. */
142 void GLViewer_Selector2d::checkSelection( int selBefore, bool append, int aStatus )
143 {
144     int selAfter = numSelected();
145     if ( selBefore > 0 && selAfter < 1 )     
146         emit selSelectionCancel();
147     else if ( selAfter > 0 )
148     {
149         switch( aStatus )
150         {
151         case SS_LocalChanged:
152             emit selSelectionDone( selAfter > 1, SCS_Local );
153             break;
154         case SS_GlobalChanged:
155             emit selSelectionDone( selAfter > 1, SCS_Global );
156             break;
157         }
158     }
159 }
160
161 int GLViewer_Selector2d::numSelected() const
162 {
163   return myGLContext->NbSelected();
164 }
165