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