Salome HOME
refs #1468: patch to customize dragging of several selected objects with left mouse...
[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   // block status
168   BlockStatus                      currentBlock();
169
170   // highlighting
171   virtual void                     highlight( double theX, double theY );
172   void                             clearHighlighted();
173
174   GraphicsView_Object*             getHighlightedObject() const { return myHighlightedObject; }
175
176   // selection
177   virtual int                      select( const QRectF& theRect, bool theIsAppend );
178   void                             clearSelected();
179   void                             setSelected( GraphicsView_Object* theObject );
180
181   int                              nbSelected() const;
182   void                             initSelected();
183   bool                             moreSelected();
184   bool                             nextSelected();
185   GraphicsView_Object*             selectedObject();
186
187   const GraphicsView_ObjectList&   getSelectedObjects() const { return mySelectedObjects; }
188
189   // rectangle selection
190   void                             startSelectByRect( int x, int y );
191   void                             drawSelectByRect( int x, int y );
192   void                             finishSelectByRect();
193   bool                             isSelectByRect() const;
194   QRect                            selectionRect();
195
196   // sketching
197   void                             prepareToSketch( bool theStatus );
198   bool                             isPrepareToSketch();
199   void                             startSketching( const QPointF& thePoint,
200                                                    bool theIsPath );
201   void                             drawSketching( const QPointF& thePoint );
202   void                             finishSketching( bool theStatus );
203   bool                             isSketching( bool* theIsPath = 0 ) const;
204
205   // dragging
206   bool                             isDragging() { return myIsDragging; }
207
208   bool                             isDraggingSelectedByLeftButton() const { return myDraggingSelectedByLeftButton; }
209   void                             setDraggingSelectedByLeftButton( const bool& theValue );
210
211   // pulling
212   bool                             startPulling( const QPointF& );
213   void                             drawPulling( const QPointF& );
214   void                             finishPulling( bool theStatus );
215   bool                             isPulling() const { return myIsPulling; }
216
217   // other
218   bool                             cancelCurrentOperation();
219
220 public:
221   static void                      createCursors();
222   static void                      destroyCursors();
223   static QCursor*                  getDefaultCursor() { return defCursor; }
224   static QCursor*                  getHandCursor() { return handCursor; }
225   static QCursor*                  getPanCursor() { return panCursor; }
226   static QCursor*                  getPanglCursor() { return panglCursor; }
227   static QCursor*                  getZoomCursor() { return zoomCursor; }
228   static QCursor*                  getSketchCursor() { return sketchCursor; }
229
230 public slots:
231   void                             onBoundingRectChanged();
232
233 protected slots:
234   void                             onKeyEvent( QKeyEvent* );
235   void                             onMouseEvent( QGraphicsSceneMouseEvent* );
236   void                             onWheelEvent( QGraphicsSceneWheelEvent* );
237   void                             onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
238
239 protected:
240   virtual void                     scrollContentsBy( int theDX, int theDY );
241
242 signals:
243   void                             vpKeyEvent( QKeyEvent* );
244   void                             vpMouseEvent( QGraphicsSceneMouseEvent* );
245   void                             vpWheelEvent( QGraphicsSceneWheelEvent* );
246   void                             vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
247
248   void                             vpSketchingFinished( QPainterPath );
249
250   void                             vpObjectBeforeMoving();
251   void                             vpObjectAfterMoving( bool );
252
253 private:
254   void                             initialize();
255   void                             cleanup();
256
257   void                             dragObjects( QGraphicsSceneMouseEvent* );
258
259 private:
260   static int                       nCounter;
261   static QCursor*                  defCursor;
262   static QCursor*                  handCursor;
263   static QCursor*                  panCursor;
264   static QCursor*                  panglCursor;
265   static QCursor*                  zoomCursor;
266   static QCursor*                  sketchCursor;
267
268 private:
269   // scene
270   GraphicsView_Scene*              myScene;
271   double                           mySceneGap;
272   double                           myFitAllGap;
273   GraphicsView_ObjectList          myObjects;
274
275   // interaction flags
276   InteractionFlags                 myInteractionFlags;
277
278   // view label
279   ViewLabel*                       myViewLabel;
280   ViewLabelPosition                myViewLabelPosition;
281   QGridLayout*                     myViewLabelLayout;
282
283   // displaying mouse position (currently, overlaps with view label feature)
284   bool                             myIsMousePositionEnabled;
285
286   // foreground
287   bool                             myIsForegroundEnabled;
288   QSizeF                           myForegroundSize;
289   double                           myForegroundMargin;
290   QColor                           myForegroundColor;
291   QColor                           myForegroundFrameColor;
292   double                           myForegroundFrameLineWidth;
293   QGraphicsRectItem*               myForegroundItem;
294
295   // grid
296   bool                             myIsGridEnabled;
297   int                              myGridCellSize;
298   int                              myGridLineStyle;
299   QColor                           myGridLineColor;
300   QGraphicsPathItem*               myGridItem;
301
302   // transformation
303   bool                             myIsTransforming;
304   QTransform                       myCurrentTransform;
305
306   // highlighting
307   GraphicsView_Object*             myHighlightedObject;
308   double                           myHighlightX;
309   double                           myHighlightY;
310   bool                             myIsHighlighting;
311
312   // selection
313   GraphicsView_ObjectList          mySelectedObjects;
314   int                              mySelectionIterator;
315
316   // rectangle selection
317   QRubberBand*                     myRectBand;
318   QPoint                           myFirstSelectionPoint;
319   QPoint                           myLastSelectionPoint;
320   bool                             myAreSelectionPointsInitialized;
321
322   // sketching
323   QGraphicsPathItem*               mySketchingItem;
324   QPointF                          mySketchingPoint;
325   bool                             myIsPrepareToSketch;
326   bool                             myIsSketching;
327   bool                             myIsSketchingByPath;
328
329   // dragging
330   int                              myIsDragging;
331   QPointF                          myDragPosition;
332   bool                             myIsDragPositionInitialized;
333   bool                             myDraggingSelectedByLeftButton;
334
335   // pulling
336   bool                             myIsPulling;
337   GraphicsView_Object*             myPullingObject;
338
339   // cursor
340   QCursor                          myStoredCursor;
341
342   // zoom diagonal coefficient
343   int                              myZoomCoeff;
344 };
345
346 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )
347
348 #endif