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