Salome HOME
refs #1472: patch for unlimited panning in graphics view.
[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   Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag )
61
62   enum BlockStatus
63   {
64     BS_NoBlock   = 0x0000,
65     BS_Selection = 0x0001, // and highlighting
66     BS_Dragging  = 0x0002  // currently unused
67   };
68
69   enum ViewLabelPosition
70   {
71     VLP_None         = 0,
72     VLP_TopLeft      = 1,
73     VLP_TopRight     = 2,
74     VLP_BottomLeft   = 3,
75     VLP_BottomRight  = 4
76   };
77
78 public:
79   GraphicsView_ViewPort( QWidget* theParent );
80   virtual ~GraphicsView_ViewPort();
81
82 public:
83   GraphicsView_Scene*              getScene() { return myScene; }
84   void                             addItem( QGraphicsItem* theItem );
85   bool                             isItemAdded( QGraphicsItem* theItem );
86   void                             removeItem( QGraphicsItem* theItem );
87   void                             clearItems();
88
89   enum SortType { NoSorting, SelectedFirst, SortByZLevel };
90   GraphicsView_ObjectList          getObjects( SortType theSortType = NoSorting ) const;
91
92   QRectF                           objectsBoundingRect( bool theOnlyVisible = false ) const;
93
94   QImage                           dumpView( bool theWholeScene = false,
95                                              QSizeF theSize = QSizeF() );
96
97   bool                             dumpViewToPSFormat(const QString& fileName);
98
99 public:
100   // scene
101   void                             setSceneGap( double theSceneGap );
102   void                             setFitAllGap( double theFitAllGap );
103
104   // interaction flags
105   int                              interactionFlags() const;
106   bool                             hasInteractionFlag( InteractionFlag theFlag );
107   void                             setInteractionFlag( InteractionFlag theFlag,
108                                                        bool theIsEnabled = true );
109   void                             setInteractionFlags( InteractionFlags theFlags );
110
111   // view label
112   void                             setViewLabelPosition( ViewLabelPosition thePosition,
113                                                          bool theIsForced = false );
114   void                             setViewLabelText( const QString& theText );
115
116   // displaying mouse position (currently, overlaps with view label feature)
117   void                             setMousePositionEnabled( bool theState );
118
119   // background / foreground
120   QColor                           backgroundColor() const;
121   void                             setBackgroundColor( const QColor& theColor );
122
123   bool                             isForegroundEnabled() const { return myIsForegroundEnabled; }
124   void                             setForegroundEnabled( bool theState );
125
126   QSizeF                           foregroundSize() const { return myForegroundSize; }
127   void                             setForegroundSize( const QSizeF& theRect );
128
129   double                           foregroundMargin() const { return myForegroundMargin; }
130   void                             setForegroundMargin( double theMargin );
131
132   QColor                           foregroundColor() const { return myForegroundColor; }
133   void                             setForegroundColor( const QColor& theColor );
134
135   QColor                           foregroundFrameColor() const { return myForegroundFrameColor; }
136   void                             setForegroundFrameColor( const QColor& theColor );
137
138   double                           foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
139   void                             setForegroundFrameLineWidth( double theLineWidth );
140
141   void                             updateForeground();
142
143   // grid
144   void                             setGridEnabled( bool theState );
145   void                             setGridCellSize( int theCellSize );
146   void                             setGridLineStyle( int theLineStyle );
147   void                             setGridLineColor( const QColor& theLineColor );
148   void                             updateGrid();
149
150   // transformation
151   void                             reset();
152   void                             pan( double theDX, double theDY );
153   void                             setCenter( double theX, double theY );
154   void                             zoom( double theX1, double theY1, double theX2, double theY2 );
155   void                             fitRect( const QRectF& theRect );
156   void                             fitSelect();
157   void                             fitAll( bool theKeepScale = false );
158   void                             fitWidth();
159
160   bool                             isTransforming() const { return myIsTransforming; }
161
162   void                             applyTransform();
163
164   int                              zoomCoeff() const { return myZoomCoeff; }
165   void                             setZoomCoeff( const int& theZoomCoeff );
166
167   bool                             isUnlimitedPanning() const { return myUnlimitedPanning; }
168   void                             setUnlimitedPanning( const bool& theValue );
169
170   // block status
171   BlockStatus                      currentBlock();
172
173   // highlighting
174   virtual void                     highlight( double theX, double theY );
175   void                             clearHighlighted();
176
177   GraphicsView_Object*             getHighlightedObject() const { return myHighlightedObject; }
178
179   // selection
180   virtual int                      select( const QRectF& theRect, bool theIsAppend );
181   void                             clearSelected();
182   void                             setSelected( GraphicsView_Object* theObject );
183
184   int                              nbSelected() const;
185   void                             initSelected();
186   bool                             moreSelected();
187   bool                             nextSelected();
188   GraphicsView_Object*             selectedObject();
189
190   const GraphicsView_ObjectList&   getSelectedObjects() const { return mySelectedObjects; }
191
192   // rectangle selection
193   void                             startSelectByRect( int x, int y );
194   void                             drawSelectByRect( int x, int y );
195   void                             finishSelectByRect();
196   bool                             isSelectByRect() const;
197   QRect                            selectionRect();
198
199   // sketching
200   void                             prepareToSketch( bool theStatus );
201   bool                             isPrepareToSketch();
202   void                             startSketching( const QPointF& thePoint,
203                                                    bool theIsPath );
204   void                             drawSketching( const QPointF& thePoint );
205   void                             finishSketching( bool theStatus );
206   bool                             isSketching( bool* theIsPath = 0 ) const;
207
208   // dragging
209   bool                             isDragging() { return myIsDragging; }
210
211   bool                             isDraggingSelectedByLeftButton() const { return myDraggingSelectedByLeftButton; }
212   void                             setDraggingSelectedByLeftButton( const bool& theValue );
213
214   // pulling
215   bool                             startPulling( const QPointF& );
216   void                             drawPulling( const QPointF& );
217   void                             finishPulling( bool theStatus );
218   bool                             isPulling() const { return myIsPulling; }
219
220   // other
221   bool                             cancelCurrentOperation();
222
223 public:
224   static void                      createCursors();
225   static void                      destroyCursors();
226   static QCursor*                  getDefaultCursor() { return defCursor; }
227   static QCursor*                  getHandCursor() { return handCursor; }
228   static QCursor*                  getPanCursor() { return panCursor; }
229   static QCursor*                  getPanglCursor() { return panglCursor; }
230   static QCursor*                  getZoomCursor() { return zoomCursor; }
231   static QCursor*                  getSketchCursor() { return sketchCursor; }
232
233 public slots:
234   void                             onBoundingRectChanged();
235
236 protected slots:
237   void                             onKeyEvent( QKeyEvent* );
238   void                             onMouseEvent( QGraphicsSceneMouseEvent* );
239   void                             onWheelEvent( QGraphicsSceneWheelEvent* );
240   void                             onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
241
242 protected:
243   virtual void                     scrollContentsBy( int theDX, int theDY );
244
245 signals:
246   void                             vpKeyEvent( QKeyEvent* );
247   void                             vpMouseEvent( QGraphicsSceneMouseEvent* );
248   void                             vpWheelEvent( QGraphicsSceneWheelEvent* );
249   void                             vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
250
251   void                             vpSketchingFinished( QPainterPath );
252
253   void                             vpObjectBeforeMoving();
254   void                             vpObjectAfterMoving( bool );
255
256 private:
257   void                             initialize();
258   void                             cleanup();
259
260   void                             dragObjects( QGraphicsSceneMouseEvent* );
261
262 private:
263   static int                       nCounter;
264   static QCursor*                  defCursor;
265   static QCursor*                  handCursor;
266   static QCursor*                  panCursor;
267   static QCursor*                  panglCursor;
268   static QCursor*                  zoomCursor;
269   static QCursor*                  sketchCursor;
270
271 private:
272   // scene
273   GraphicsView_Scene*              myScene;
274   double                           mySceneGap;
275   double                           myFitAllGap;
276   GraphicsView_ObjectList          myObjects;
277
278   // interaction flags
279   InteractionFlags                 myInteractionFlags;
280
281   // view label
282   ViewLabel*                       myViewLabel;
283   ViewLabelPosition                myViewLabelPosition;
284   QGridLayout*                     myViewLabelLayout;
285
286   // displaying mouse position (currently, overlaps with view label feature)
287   bool                             myIsMousePositionEnabled;
288
289   // foreground
290   bool                             myIsForegroundEnabled;
291   QSizeF                           myForegroundSize;
292   double                           myForegroundMargin;
293   QColor                           myForegroundColor;
294   QColor                           myForegroundFrameColor;
295   double                           myForegroundFrameLineWidth;
296   QGraphicsRectItem*               myForegroundItem;
297
298   // grid
299   bool                             myIsGridEnabled;
300   int                              myGridCellSize;
301   int                              myGridLineStyle;
302   QColor                           myGridLineColor;
303   QGraphicsPathItem*               myGridItem;
304
305   // transformation
306   bool                             myIsTransforming;
307   QTransform                       myCurrentTransform;
308
309   bool                             myUnlimitedPanning;
310   Qt::ScrollBarPolicy              myHBarPolicy;
311   Qt::ScrollBarPolicy              myVBarPolicy;
312
313   // highlighting
314   GraphicsView_Object*             myHighlightedObject;
315   double                           myHighlightX;
316   double                           myHighlightY;
317   bool                             myIsHighlighting;
318
319   // selection
320   GraphicsView_ObjectList          mySelectedObjects;
321   int                              mySelectionIterator;
322
323   // rectangle selection
324   QRubberBand*                     myRectBand;
325   QPoint                           myFirstSelectionPoint;
326   QPoint                           myLastSelectionPoint;
327   bool                             myAreSelectionPointsInitialized;
328
329   // sketching
330   QGraphicsPathItem*               mySketchingItem;
331   QPointF                          mySketchingPoint;
332   bool                             myIsPrepareToSketch;
333   bool                             myIsSketching;
334   bool                             myIsSketchingByPath;
335
336   // dragging
337   int                              myIsDragging;
338   QPointF                          myDragPosition;
339   bool                             myIsDragPositionInitialized;
340   bool                             myDraggingSelectedByLeftButton;
341
342   // pulling
343   bool                             myIsPulling;
344   GraphicsView_Object*             myPullingObject;
345
346   // cursor
347   QCursor                          myStoredCursor;
348
349   // zoom diagonal coefficient
350   int                              myZoomCoeff;
351 };
352
353 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )
354
355 #endif