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