1 // Copyright (C) 2013-2020 CEA/DEN, EDF R&D, 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>
32 class GraphicsView_Object;
33 class GraphicsView_Scene;
36 Class : GraphicsView_ViewPort
37 Description : View port of the graphics view
39 class GRAPHICSVIEW_API GraphicsView_ViewPort : public QGraphicsView
51 WheelScaling = 0x0004,
52 EditFlags = Dragging | Pulling | WheelScaling,
54 TraceBoundingRect = 0x0008,
55 DraggingByMiddleButton = 0x0010,
56 ImmediateContextMenu = 0x0020,
57 ImmediateSelection = 0x0040,
60 GlobalWheelScaling = 0x0100
62 Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag )
67 BS_Selection = 0x0001, // and highlighting
68 BS_Dragging = 0x0002 // currently unused
71 enum ViewLabelPosition
81 GraphicsView_ViewPort( QWidget* theParent );
82 ~GraphicsView_ViewPort();
85 GraphicsView_Scene* getScene() { return myScene; }
86 void addItem( QGraphicsItem* theItem );
87 bool isItemAdded( QGraphicsItem* theItem );
88 void removeItem( QGraphicsItem* theItem );
91 enum SortType { NoSorting, SelectedFirst, SortByZLevel };
92 GraphicsView_ObjectList getObjects( SortType theSortType = NoSorting ) const;
94 QRectF objectsBoundingRect( bool theOnlyVisible = false ) const;
96 QImage dumpView( bool theWholeScene = false,
97 QSizeF theSize = QSizeF() );
99 bool dumpViewToFormat(const QString& fileName, const QString& format);
103 void setSceneGap( double theSceneGap );
104 void setFitAllGap( double theFitAllGap );
107 int interactionFlags() const;
108 bool hasInteractionFlag( InteractionFlag theFlag );
109 void setInteractionFlag( InteractionFlag theFlag,
110 bool theIsEnabled = true );
111 void setInteractionFlags( InteractionFlags theFlags );
114 void setViewLabelPosition( ViewLabelPosition thePosition,
115 bool theIsForced = false );
116 void setViewLabelText( const QString& theText );
118 // displaying mouse position (currently, overlaps with view label feature)
119 void setMousePositionEnabled( bool theState );
121 // background / foreground
122 QColor backgroundColor() const;
123 void setBackgroundColor( const QColor& theColor );
125 bool isForegroundEnabled() const { return myIsForegroundEnabled; }
126 void setForegroundEnabled( bool theState );
128 QSizeF foregroundSize() const { return myForegroundSize; }
129 void setForegroundSize( const QSizeF& theRect );
131 double foregroundMargin() const { return myForegroundMargin; }
132 void setForegroundMargin( double theMargin );
134 QColor foregroundColor() const { return myForegroundColor; }
135 void setForegroundColor( const QColor& theColor );
137 QColor foregroundFrameColor() const { return myForegroundFrameColor; }
138 void setForegroundFrameColor( const QColor& theColor );
140 double foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
141 void setForegroundFrameLineWidth( double theLineWidth );
143 void updateForeground();
146 void setGridEnabled( bool theState );
147 void setGridCellSize( int theCellSize );
148 void setGridLineStyle( int theLineStyle );
149 void setGridLineColor( const QColor& theLineColor );
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 );
159 void fitAll( bool theKeepScale = false );
162 bool isTransforming() const { return myIsTransforming; }
164 void applyTransform();
166 int zoomCoeff() const { return myZoomCoeff; }
167 void setZoomCoeff( const int& theZoomCoeff );
169 bool isUnlimitedPanning() const { return myUnlimitedPanning; }
170 void setUnlimitedPanning( const bool& theValue );
173 BlockStatus currentBlock();
176 virtual void highlight( double theX, double theY );
177 void clearHighlighted();
179 GraphicsView_Object* getHighlightedObject() const { return myHighlightedObject; }
182 virtual int select( const QRectF& theRect, bool theIsAppend );
183 void clearSelected();
184 void setSelected( GraphicsView_Object* theObject );
186 int nbSelected() const;
190 GraphicsView_Object* selectedObject();
192 const GraphicsView_ObjectList& getSelectedObjects() const { return mySelectedObjects; }
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();
202 void prepareToSketch( bool theStatus );
203 bool isPrepareToSketch();
204 void startSketching( const QPointF& thePoint,
206 void drawSketching( const QPointF& thePoint );
207 void finishSketching( bool theStatus );
208 bool isSketching( bool* theIsPath = 0 ) const;
211 bool isDragging() { return myIsDragging; }
213 bool isDraggingSelectedByLeftButton() const { return myDraggingSelectedByLeftButton; }
214 void setDraggingSelectedByLeftButton( const bool& theValue );
217 bool startPulling( const QPointF& );
218 void drawPulling( const QPointF& );
219 void finishPulling( bool theStatus );
220 bool isPulling() const { return myIsPulling; }
223 bool cancelCurrentOperation();
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; }
236 void onBoundingRectChanged();
239 void onKeyEvent( QKeyEvent* );
240 void onMouseEvent( QGraphicsSceneMouseEvent* );
241 void onWheelEvent( QGraphicsSceneWheelEvent* );
242 void onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
245 virtual void scrollContentsBy( int theDX, int theDY );
248 void vpKeyEvent( QKeyEvent* );
249 void vpMouseEvent( QGraphicsSceneMouseEvent* );
250 void vpWheelEvent( QGraphicsSceneWheelEvent* );
251 void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
253 void vpSketchingFinished( QPainterPath );
255 void vpObjectBeforeMoving();
256 void vpObjectAfterMoving( bool );
262 void dragObjects( QGraphicsSceneMouseEvent* );
266 static QCursor* defCursor;
267 static QCursor* handCursor;
268 static QCursor* panCursor;
269 static QCursor* panglCursor;
270 static QCursor* zoomCursor;
271 static QCursor* sketchCursor;
275 GraphicsView_Scene* myScene;
278 GraphicsView_ObjectList myObjects;
281 InteractionFlags myInteractionFlags;
284 ViewLabel* myViewLabel;
285 ViewLabelPosition myViewLabelPosition;
286 QGridLayout* myViewLabelLayout;
288 // displaying mouse position (currently, overlaps with view label feature)
289 bool myIsMousePositionEnabled;
292 bool myIsForegroundEnabled;
293 QSizeF myForegroundSize;
294 double myForegroundMargin;
295 QColor myForegroundColor;
296 QColor myForegroundFrameColor;
297 double myForegroundFrameLineWidth;
298 QGraphicsRectItem* myForegroundItem;
301 bool myIsGridEnabled;
304 QColor myGridLineColor;
305 QGraphicsPathItem* myGridItem;
308 bool myIsTransforming;
309 QTransform myCurrentTransform;
311 bool myUnlimitedPanning;
312 Qt::ScrollBarPolicy myHBarPolicy;
313 Qt::ScrollBarPolicy myVBarPolicy;
316 GraphicsView_Object* myHighlightedObject;
319 bool myIsHighlighting;
322 GraphicsView_ObjectList mySelectedObjects;
323 int mySelectionIterator;
325 // rectangle selection
326 QRubberBand* myRectBand;
327 QPoint myFirstSelectionPoint;
328 QPoint myLastSelectionPoint;
329 bool myAreSelectionPointsInitialized;
332 QGraphicsPathItem* mySketchingItem;
333 QPointF mySketchingPoint;
334 bool myIsPrepareToSketch;
336 bool myIsSketchingByPath;
340 QPointF myDragPosition;
341 bool myIsDragPositionInitialized;
342 bool myDraggingSelectedByLeftButton;
346 GraphicsView_Object* myPullingObject;
349 QCursor myStoredCursor;
351 // zoom diagonal coefficient
355 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )