Salome HOME
refs #1322: first implementation for center zoom under the pressed mouse roll in...
[modules/gui.git] / src / GraphicsView / GraphicsView_ViewPort.h
1 // Copyright (C) 2013-2016  CEA/DEN, EDF R&D, 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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef GRAPHICSVIEW_VIEWPORT_H
21 #define GRAPHICSVIEW_VIEWPORT_H
22
23 #include "GraphicsView.h"
24
25 #include "GraphicsView_Defs.h"
26
27 #include <QGraphicsView>
28
29 class QGridLayout;
30 class QRubberBand;
31
32 class GraphicsView_Object;
33 class GraphicsView_Scene;
34
35 /*
36   Class       : GraphicsView_ViewPort
37   Description : View port of the graphics view
38 */
39 class GRAPHICSVIEW_API GraphicsView_ViewPort : public QGraphicsView
40 {
41   Q_OBJECT
42
43 public:
44   class ViewLabel;
45
46   enum InteractionFlag
47   {
48     // main flags
49     Dragging               = 0x0001,
50     Pulling                = 0x0002,
51     WheelScaling           = 0x0004,
52     EditFlags              = Dragging | Pulling | WheelScaling,
53     // advanced flags
54     TraceBoundingRect      = 0x0008,
55     DraggingByMiddleButton = 0x0010,
56     ImmediateContextMenu   = 0x0020,
57     ImmediateSelection     = 0x0040,
58     Sketching              = 0x0080,
59
60     GlobalWheelScaling     = 0x0100
61   };
62   Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag )
63
64   enum BlockStatus
65   {
66     BS_NoBlock   = 0x0000,
67     BS_Selection = 0x0001, // and highlighting
68     BS_Dragging  = 0x0002  // currently unused
69   };
70
71   enum ViewLabelPosition
72   {
73     VLP_None         = 0,
74     VLP_TopLeft      = 1,
75     VLP_TopRight     = 2,
76     VLP_BottomLeft   = 3,
77     VLP_BottomRight  = 4
78   };
79
80 public:
81   GraphicsView_ViewPort( QWidget* theParent );
82   ~GraphicsView_ViewPort();
83
84 public:
85   GraphicsView_Scene*              getScene() { return myScene; }
86   void                             addItem( QGraphicsItem* theItem );
87   bool                             isItemAdded( QGraphicsItem* theItem );
88   void                             removeItem( QGraphicsItem* theItem );
89
90   enum SortType { NoSorting, SelectedFirst, SortByZLevel };
91   GraphicsView_ObjectList          getObjects( SortType theSortType = NoSorting ) const;
92
93   QRectF                           objectsBoundingRect( bool theOnlyVisible = false ) const;
94
95   QImage                           dumpView( bool theWholeScene = false,
96                                              QSizeF theSize = QSizeF() );
97
98 public:
99   // scene
100   void                             setSceneGap( double theSceneGap );
101   void                             setFitAllGap( double theFitAllGap );
102
103   // interaction flags
104   int                              interactionFlags() const;
105   bool                             hasInteractionFlag( InteractionFlag theFlag );
106   void                             setInteractionFlag( InteractionFlag theFlag,
107                                                        bool theIsEnabled = true );
108   void                             setInteractionFlags( InteractionFlags theFlags );
109
110   // view label
111   void                             setViewLabelPosition( ViewLabelPosition thePosition,
112                                                          bool theIsForced = false );
113   void                             setViewLabelText( const QString& theText );
114
115   // displaying mouse position (currently, overlaps with view label feature)
116   void                             setMousePositionEnabled( bool theState );
117
118   // background / foreground
119   QColor                           backgroundColor() const;
120   void                             setBackgroundColor( const QColor& theColor );
121
122   bool                             isForegroundEnabled() const { return myIsForegroundEnabled; }
123   void                             setForegroundEnabled( bool theState );
124
125   QSizeF                           foregroundSize() const { return myForegroundSize; }
126   void                             setForegroundSize( const QSizeF& theRect );
127
128   double                           foregroundMargin() const { return myForegroundMargin; }
129   void                             setForegroundMargin( double theMargin );
130
131   QColor                           foregroundColor() const { return myForegroundColor; }
132   void                             setForegroundColor( const QColor& theColor );
133
134   QColor                           foregroundFrameColor() const { return myForegroundFrameColor; }
135   void                             setForegroundFrameColor( const QColor& theColor );
136
137   double                           foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
138   void                             setForegroundFrameLineWidth( double theLineWidth );
139
140   void                             updateForeground();
141
142   // grid
143   void                             setGridEnabled( bool theState );
144   void                             setGridCellSize( int theCellSize );
145   void                             setGridLineStyle( int theLineStyle );
146   void                             setGridLineColor( const QColor& theLineColor );
147   void                             updateGrid();
148
149   // transformation
150   void                             reset();
151   void                             pan( double theDX, double theDY );
152   void                             setCenter( double theX, double theY );
153   void                             zoom( double theX1, double theY1, double theX2, double theY2 );
154   void                             fitRect( const QRectF& theRect );
155   void                             fitSelect();
156   void                             fitAll( bool theKeepScale = false );
157   void                             fitWidth();
158
159   bool                             isTransforming() const { return myIsTransforming; }
160
161   void                             applyTransform();
162
163   // block status
164   BlockStatus                      currentBlock();
165
166   // highlighting
167   virtual void                     highlight( double theX, double theY );
168   void                             clearHighlighted();
169
170   GraphicsView_Object*             getHighlightedObject() const { return myHighlightedObject; }
171
172   // selection
173   virtual int                      select( const QRectF& theRect, bool theIsAppend );
174   void                             clearSelected();
175   void                             setSelected( GraphicsView_Object* theObject );
176
177   int                              nbSelected() const;
178   void                             initSelected();
179   bool                             moreSelected();
180   bool                             nextSelected();
181   GraphicsView_Object*             selectedObject();
182
183   const GraphicsView_ObjectList&   getSelectedObjects() const { return mySelectedObjects; }
184
185   // rectangle selection
186   void                             startSelectByRect( int x, int y );
187   void                             drawSelectByRect( int x, int y );
188   void                             finishSelectByRect();
189   bool                             isSelectByRect() const;
190   QRect                            selectionRect();
191
192   // sketching
193   void                             prepareToSketch( bool theStatus );
194   bool                             isPrepareToSketch();
195   void                             startSketching( const QPointF& thePoint,
196                                                    bool theIsPath );
197   void                             drawSketching( const QPointF& thePoint );
198   void                             finishSketching( bool theStatus );
199   bool                             isSketching( bool* theIsPath = 0 ) const;
200
201   // dragging
202   bool                             isDragging() { return myIsDragging; }
203
204   // pulling
205   bool                             startPulling( const QPointF& );
206   void                             drawPulling( const QPointF& );
207   void                             finishPulling( bool theStatus );
208   bool                             isPulling() const { return myIsPulling; }
209
210   // other
211   bool                             cancelCurrentOperation();
212
213 public:
214   static void                      createCursors();
215   static void                      destroyCursors();
216   static QCursor*                  getDefaultCursor() { return defCursor; }
217   static QCursor*                  getHandCursor() { return handCursor; }
218   static QCursor*                  getPanCursor() { return panCursor; }
219   static QCursor*                  getPanglCursor() { return panglCursor; }
220   static QCursor*                  getZoomCursor() { return zoomCursor; }
221   static QCursor*                  getSketchCursor() { return sketchCursor; }
222
223 public slots:
224   void                             onBoundingRectChanged();
225
226 protected slots:
227   void                             onKeyEvent( QKeyEvent* );
228   void                             onMouseEvent( QGraphicsSceneMouseEvent* );
229   void                             onWheelEvent( QGraphicsSceneWheelEvent* );
230   void                             onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
231
232 protected:
233   virtual void                     scrollContentsBy( int theDX, int theDY );
234
235 signals:
236   void                             vpKeyEvent( QKeyEvent* );
237   void                             vpMouseEvent( QGraphicsSceneMouseEvent* );
238   void                             vpWheelEvent( QGraphicsSceneWheelEvent* );
239   void                             vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
240
241   void                             vpSketchingFinished( QPainterPath );
242
243   void                             vpObjectBeforeMoving();
244   void                             vpObjectAfterMoving( bool );
245
246 private:
247   void                             initialize();
248   void                             cleanup();
249
250   void                             dragObjects( QGraphicsSceneMouseEvent* );
251
252 private:
253   static int                       nCounter;
254   static QCursor*                  defCursor;
255   static QCursor*                  handCursor;
256   static QCursor*                  panCursor;
257   static QCursor*                  panglCursor;
258   static QCursor*                  zoomCursor;
259   static QCursor*                  sketchCursor;
260
261 private:
262   // scene
263   GraphicsView_Scene*              myScene;
264   double                           mySceneGap;
265   double                           myFitAllGap;
266   GraphicsView_ObjectList          myObjects;
267
268   // interaction flags
269   InteractionFlags                 myInteractionFlags;
270
271   // view label
272   ViewLabel*                       myViewLabel;
273   ViewLabelPosition                myViewLabelPosition;
274   QGridLayout*                     myViewLabelLayout;
275
276   // displaying mouse position (currently, overlaps with view label feature)
277   bool                             myIsMousePositionEnabled;
278
279   // foreground
280   bool                             myIsForegroundEnabled;
281   QSizeF                           myForegroundSize;
282   double                           myForegroundMargin;
283   QColor                           myForegroundColor;
284   QColor                           myForegroundFrameColor;
285   double                           myForegroundFrameLineWidth;
286   QGraphicsRectItem*               myForegroundItem;
287
288   // grid
289   bool                             myIsGridEnabled;
290   int                              myGridCellSize;
291   int                              myGridLineStyle;
292   QColor                           myGridLineColor;
293   QGraphicsPathItem*               myGridItem;
294
295   // transformation
296   bool                             myIsTransforming;
297   QTransform                       myCurrentTransform;
298
299   // highlighting
300   GraphicsView_Object*             myHighlightedObject;
301   double                           myHighlightX;
302   double                           myHighlightY;
303   bool                             myIsHighlighting;
304
305   // selection
306   GraphicsView_ObjectList          mySelectedObjects;
307   int                              mySelectionIterator;
308
309   // rectangle selection
310   QRubberBand*                     myRectBand;
311   QPoint                           myFirstSelectionPoint;
312   QPoint                           myLastSelectionPoint;
313   bool                             myAreSelectionPointsInitialized;
314
315   // sketching
316   QGraphicsPathItem*               mySketchingItem;
317   QPointF                          mySketchingPoint;
318   bool                             myIsPrepareToSketch;
319   bool                             myIsSketching;
320   bool                             myIsSketchingByPath;
321
322   // dragging
323   int                              myIsDragging;
324   QPointF                          myDragPosition;
325   bool                             myIsDragPositionInitialized;
326
327   // pulling
328   bool                             myIsPulling;
329   GraphicsView_Object*             myPullingObject;
330
331   // cursor
332   QCursor                          myStoredCursor;
333 };
334
335 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )
336
337 #endif