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