Salome HOME
This file is given from DESCARTES project
[modules/gui.git] / src / GLViewer / GLViewer_ViewPort2d.h
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_ViewPort2d.h
23 // Created:   November, 2004
24
25 #ifndef GLVIEWER_VIEWPORT2D_H
26 #define GLVIEWER_VIEWPORT2D_H
27
28 #ifdef WNT
29 #include <windows.h>
30 #endif
31
32 #include <GL/gl.h>
33 #include <GL/glu.h>
34
35 #include "GLViewer_ViewPort.h"
36 #include "GLViewer_Widget.h"
37 #include "GLViewer_Geom.h"
38
39 #include <qgl.h>
40 #include <qcolor.h>
41 #include <qwidget.h>
42 #include <qpaintdevice.h>
43
44 #ifdef WNT
45 #pragma warning( disable:4251 )
46 #endif
47
48 class GLViewer_Compass;
49 class GLViewer_Grid;
50 class GLViewer_Object;
51 class GLViewer_ViewFrame;
52
53 class QtxToolTip;
54 /*! 
55  * Class GLViewer_ViewPort
56  * 2D visualisation canvas of GLViewer
57  */
58 class GLViewer_ViewPort2d: public GLViewer_ViewPort
59 {
60   Q_OBJECT
61
62   //! Dragging states
63   enum vpDragState{ noDrag, initDrag, inDrag };
64
65 public:
66   GLViewer_ViewPort2d( QWidget* parent, GLViewer_ViewFrame* theViewFrame = NULL );
67   ~GLViewer_ViewPort2d();
68
69   //! On/off rectangular grid
70   void                   turnGrid( GLboolean on );
71   //! Returns rectangular grid
72   GLViewer_Grid*         getGrid() const { return myGrid; }
73   //! Returns grid color
74   void                   setGridColor( const QColor gridColor, const QColor axisColor );
75
76   //! Returns parent window
77   GLViewer_ViewFrame*    getViewFrame() const { return myViewFrame; }
78   //! Returns painted widget
79   GLViewer_Widget*       getGLWidget() const { return myGLWidget; }
80   virtual QPaintDevice*  getPaintDevice() { return myGLWidget; }
81
82   //! Sets background color
83   void                   setBackgroundColor( const QColor& color);
84   //! Returns background color
85   QColor                 backgroundColor() const;
86
87   //! Sets borders of scene
88   void                   setBorder( GLViewer_Rect* border ) { myBorder = border; }
89   //! Get current borders of scene
90   GLViewer_Rect*         getBorder() const { return myBorder; }
91   
92   //! Sets margin of borders
93   void                   setMargin( GLfloat margin ) { myMargin = margin; }
94   //! Returns margin of borders
95   GLfloat                getMargin() const { return myMargin; }
96
97   //! Returns width of view
98   int                    getWidth() const { return myWidth; }
99   //! Returns height of view
100   int                    getHeight() const { return myHeight; }
101
102   
103   //! Returns scale factors
104   void                   getScale( GLfloat& xSc, GLfloat& ySc ) const { xSc = myXScale; ySc = myYScale; }
105   //! returns offsets
106   void                   getPan( GLfloat& xPan, GLfloat& yPan ) const { xPan = myXPan; yPan = myYPan; }
107
108   //! Resize view
109   void                   initResize( int width, int height );
110   
111   //! Begins rotation
112   void                   startRotation( int, int );
113   //! Process rotation
114   void                   rotate( int, int );
115   //! Completes rotation
116   void                   endRotation();
117   
118   //! Checks of dragging process state
119   bool                   isDragProcess(){ return myIsDragProcess; }
120   
121   //! On/off compass
122   void                   turnCompass( GLboolean on );
123   //! Draws compass
124   void                   drawCompass();
125   
126   //! Returns unique ID of ViewPort
127   int                    getViewPortId(){ return myViewPortId; }
128   
129   //! Redefined method
130   virtual BlockStatus    currentBlock();
131   
132   //! Initializes before selecting by rect
133   void                   startSelectByRect( int x, int y );
134   //! Draw selecting rectandle
135   void                   drawSelectByRect( int x, int y );
136   //! Pass rect into selector and update
137   void                   finishSelectByRect();
138   
139   //! \warnign It is for ouv
140   bool                   startPulling( GLViewer_Pnt );
141   //! \warnign It is for ouv
142   void                   drawPulling( GLViewer_Pnt );
143   //! \warnign It is for ouv
144   void                   finishPulling();
145   //! \warnign It is for ouv
146   bool                   isPulling() const { return myIsPulling; }
147
148   //! Returns selection by rect
149   QRect                  selectionRect();
150   
151   //! Transforms window rect to global rect 
152   GLViewer_Rect          win2GLV( const QRect& ) const;
153   //! Transforms global rect to window rect
154   QRect                  GLV2win( const GLViewer_Rect& ) const;
155
156 signals:
157   //! Emits after any transformation
158   void                   vpUpdateValues();
159
160   void                   objectMoved();
161
162 protected:
163   void                   onDragObject( QMouseEvent* );
164     
165   virtual void           mouseMoveEvent( QMouseEvent *);
166   virtual void           mousePressEvent( QMouseEvent *);
167   virtual void           mouseReleaseEvent( QMouseEvent *);
168   virtual void           mouseDoubleClickEvent( QMouseEvent *);
169   
170   virtual void           paintEvent( QPaintEvent* );
171   virtual void           resizeEvent( QResizeEvent* );
172
173   //! Returns view to begin state
174   virtual void           reset();
175   //! Sets offset to view
176   virtual void           pan( int dx, int dy );
177   //! Sets view center in global coords
178   virtual void           setCenter( int x, int y );
179   //! Process zoming transformation with mouse tracking from ( x0, y0 ) to ( x1, y1 )
180   virtual void           zoom( int x0, int y0, int x1, int y1 );
181   //! Transforms view by certangle
182   virtual void           fitRect( const QRect& );
183   //! Transforms view by selection
184   virtual void           fitSelect();
185   //! Transform view by view borders ( if \param keepScale = true, zoom does not change )
186   virtual void           fitAll( bool keepScale = false, bool withZ = true );
187
188 protected slots:
189   //! Initializes drag process
190   void                   onStartDragObject();
191   //! Pastes object from clipboard
192   void                   onPasteObject();
193   //! Cuts object to clipboard
194   void                   onCutObject();
195   //! Copies object to clipboard
196   void                   onCopyObject();
197
198   //! Sets tool tip with \param text to \param theTextReg and on \param theViewReg whan mouse is on \param thePoint
199   void                   onMaybeTip( QPoint thePoint, QString& text, QFont& font, QRect& theTextReg, QRect& theViewReg );
200   
201 protected:
202   GLViewer_ViewFrame*    myViewFrame;
203   GLViewer_Widget*       myGLWidget;
204   GLViewer_Rect*         myBorder;
205   QColor                 myBackgroundColor;
206   
207   GLfloat                myMargin;
208   int                    myHeight;
209   int                    myWidth;
210   
211   GLfloat                myXScale;
212   GLfloat                myYScale;
213   GLfloat                myXOldScale;
214   GLfloat                myYOldScale;
215   GLfloat                myXPan;
216   GLfloat                myYPan;
217
218   GLViewer_Grid*         myGrid; 
219   GLViewer_Compass*      myCompass;
220
221   //dragging
222   int                    myIsDragProcess;
223   float*                 myCurDragPosX;
224   float*                 myCurDragPosY;
225   
226   //selection by rect
227   QPoint*                mypFirstPoint;
228   QPoint*                mypLastPoint;
229
230   //pulling
231   bool                   myIsPulling;
232   GLViewer_Object*       myPullingObject;
233     
234   int                    myViewPortId;
235
236   //GLViewer_ObjectTip*    myObjectTip;
237   QtxToolTip*            myObjectTip;
238   //! flag to block mouse release event just after mouse double-click
239   bool                   myIsMouseReleaseBlock;
240 };
241
242 #ifdef WNT
243 #pragma warning ( default:4251 )
244 #endif
245
246 #endif