1 // Copyright (C) 2013-2023 CEA, EDF, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef GRAPHICSVIEW_VIEWPORT_H
21 #define GRAPHICSVIEW_VIEWPORT_H
23 #include "GraphicsView.h"
25 #include "GraphicsView_Defs.h"
27 #include <QGraphicsView>
28 #include <QPainterPath>
33 class GraphicsView_Object;
34 class GraphicsView_Scene;
37 Class : GraphicsView_ViewPort
38 Description : View port of the graphics view
40 class GRAPHICSVIEW_API GraphicsView_ViewPort : public QGraphicsView
52 WheelScaling = 0x0004,
53 EditFlags = Dragging | Pulling | WheelScaling,
55 TraceBoundingRect = 0x0008,
56 DraggingByMiddleButton = 0x0010,
57 ImmediateContextMenu = 0x0020,
58 ImmediateSelection = 0x0040,
61 GlobalWheelScaling = 0x0100
63 Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag )
68 BS_Selection = 0x0001, // and highlighting
69 BS_Dragging = 0x0002 // currently unused
72 enum ViewLabelPosition
82 GraphicsView_ViewPort( QWidget* theParent );
83 ~GraphicsView_ViewPort();
86 GraphicsView_Scene* getScene() { return myScene; }
87 void addItem( QGraphicsItem* theItem );
88 bool isItemAdded( QGraphicsItem* theItem );
89 void removeItem( QGraphicsItem* theItem );
92 enum SortType { NoSorting, SelectedFirst, SortByZLevel };
93 GraphicsView_ObjectList getObjects( SortType theSortType = NoSorting ) const;
95 QRectF objectsBoundingRect( bool theOnlyVisible = false ) const;
97 QImage dumpView( bool theWholeScene = false,
98 QSizeF theSize = QSizeF() );
100 bool dumpViewToFormat(const QString& fileName, const QString& format);
104 void setSceneGap( double theSceneGap );
105 void setFitAllGap( double theFitAllGap );
108 int interactionFlags() const;
109 bool hasInteractionFlag( InteractionFlag theFlag );
110 void setInteractionFlag( InteractionFlag theFlag,
111 bool theIsEnabled = true );
112 void setInteractionFlags( InteractionFlags theFlags );
115 void setViewLabelPosition( ViewLabelPosition thePosition,
116 bool theIsForced = false );
117 void setViewLabelText( const QString& theText );
119 // displaying mouse position (currently, overlaps with view label feature)
120 void setMousePositionEnabled( bool theState );
122 // background / foreground
123 QColor backgroundColor() const;
124 void setBackgroundColor( const QColor& theColor );
126 bool isForegroundEnabled() const { return myIsForegroundEnabled; }
127 void setForegroundEnabled( bool theState );
129 QSizeF foregroundSize() const { return myForegroundSize; }
130 void setForegroundSize( const QSizeF& theRect );
132 double foregroundMargin() const { return myForegroundMargin; }
133 void setForegroundMargin( double theMargin );
135 QColor foregroundColor() const { return myForegroundColor; }
136 void setForegroundColor( const QColor& theColor );
138 QColor foregroundFrameColor() const { return myForegroundFrameColor; }
139 void setForegroundFrameColor( const QColor& theColor );
141 double foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
142 void setForegroundFrameLineWidth( double theLineWidth );
144 void updateForeground();
147 void setGridEnabled( bool theState );
148 void setGridCellSize( int theCellSize );
149 void setGridLineStyle( int theLineStyle );
150 void setGridLineColor( const QColor& theLineColor );
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 );
160 void fitAll( bool theKeepScale = false );
163 bool isTransforming() const { return myIsTransforming; }
165 void applyTransform();
167 int zoomCoeff() const { return myZoomCoeff; }
168 void setZoomCoeff( const int& theZoomCoeff );
170 bool isUnlimitedPanning() const { return myUnlimitedPanning; }
171 void setUnlimitedPanning( const bool& theValue );
174 BlockStatus currentBlock();
177 virtual void highlight( double theX, double theY );
178 void clearHighlighted();
180 GraphicsView_Object* getHighlightedObject() const { return myHighlightedObject; }
183 virtual int select( const QRectF& theRect, bool theIsAppend );
184 void clearSelected();
185 void setSelected( GraphicsView_Object* theObject );
187 int nbSelected() const;
191 GraphicsView_Object* selectedObject();
193 const GraphicsView_ObjectList& getSelectedObjects() const { return mySelectedObjects; }
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();
203 void prepareToSketch( bool theStatus );
204 bool isPrepareToSketch();
205 void startSketching( const QPointF& thePoint,
207 void drawSketching( const QPointF& thePoint );
208 void finishSketching( bool theStatus );
209 bool isSketching( bool* theIsPath = 0 ) const;
212 bool isDragging() { return myIsDragging; }
214 bool isDraggingSelectedByLeftButton() const { return myDraggingSelectedByLeftButton; }
215 void setDraggingSelectedByLeftButton( const bool& theValue );
218 bool startPulling( const QPointF& );
219 void drawPulling( const QPointF& );
220 void finishPulling( bool theStatus );
221 bool isPulling() const { return myIsPulling; }
224 bool cancelCurrentOperation();
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; }
237 void onBoundingRectChanged();
240 void onKeyEvent( QKeyEvent* );
241 void onMouseEvent( QGraphicsSceneMouseEvent* );
242 void onWheelEvent( QGraphicsSceneWheelEvent* );
243 void onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
246 virtual void scrollContentsBy( int theDX, int theDY );
249 void vpKeyEvent( QKeyEvent* );
250 void vpMouseEvent( QGraphicsSceneMouseEvent* );
251 void vpWheelEvent( QGraphicsSceneWheelEvent* );
252 void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
254 void vpSketchingFinished( QPainterPath );
256 void vpObjectBeforeMoving();
257 void vpObjectAfterMoving( bool );
263 void dragObjects( QGraphicsSceneMouseEvent* );
267 static QCursor* defCursor;
268 static QCursor* handCursor;
269 static QCursor* panCursor;
270 static QCursor* panglCursor;
271 static QCursor* zoomCursor;
272 static QCursor* sketchCursor;
276 GraphicsView_Scene* myScene;
279 GraphicsView_ObjectList myObjects;
282 InteractionFlags myInteractionFlags;
285 ViewLabel* myViewLabel;
286 ViewLabelPosition myViewLabelPosition;
287 QGridLayout* myViewLabelLayout;
289 // displaying mouse position (currently, overlaps with view label feature)
290 bool myIsMousePositionEnabled;
293 bool myIsForegroundEnabled;
294 QSizeF myForegroundSize;
295 double myForegroundMargin;
296 QColor myForegroundColor;
297 QColor myForegroundFrameColor;
298 double myForegroundFrameLineWidth;
299 QGraphicsRectItem* myForegroundItem;
302 bool myIsGridEnabled;
305 QColor myGridLineColor;
306 QGraphicsPathItem* myGridItem;
309 bool myIsTransforming;
310 QTransform myCurrentTransform;
312 bool myUnlimitedPanning;
313 Qt::ScrollBarPolicy myHBarPolicy;
314 Qt::ScrollBarPolicy myVBarPolicy;
317 GraphicsView_Object* myHighlightedObject;
320 bool myIsHighlighting;
323 GraphicsView_ObjectList mySelectedObjects;
324 int mySelectionIterator;
326 // rectangle selection
327 QRubberBand* myRectBand;
328 QPoint myFirstSelectionPoint;
329 QPoint myLastSelectionPoint;
330 bool myAreSelectionPointsInitialized;
333 QGraphicsPathItem* mySketchingItem;
334 QPointF mySketchingPoint;
335 bool myIsPrepareToSketch;
337 bool myIsSketchingByPath;
341 QPointF myDragPosition;
342 bool myIsDragPositionInitialized;
343 bool myDraggingSelectedByLeftButton;
347 GraphicsView_Object* myPullingObject;
350 QCursor myStoredCursor;
352 // zoom diagonal coefficient
356 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )