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