1 // Copyright (C) 2013-2016 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 Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag )
65 BS_Selection = 0x0001, // and highlighting
66 BS_Dragging = 0x0002 // currently unused
69 enum ViewLabelPosition
79 GraphicsView_ViewPort( QWidget* theParent );
80 virtual ~GraphicsView_ViewPort();
83 GraphicsView_Scene* getScene() { return myScene; }
84 void addItem( QGraphicsItem* theItem );
85 bool isItemAdded( QGraphicsItem* theItem );
86 void removeItem( QGraphicsItem* theItem );
89 enum SortType { NoSorting, SelectedFirst, SortByZLevel };
90 GraphicsView_ObjectList getObjects( SortType theSortType = NoSorting ) const;
92 QRectF objectsBoundingRect( bool theOnlyVisible = false ) const;
94 QImage dumpView( bool theWholeScene = false,
95 QSizeF theSize = QSizeF() );
97 bool dumpViewToPSFormat(const QString& fileName);
101 void setSceneGap( double theSceneGap );
102 void setFitAllGap( double theFitAllGap );
105 int interactionFlags() const;
106 bool hasInteractionFlag( InteractionFlag theFlag );
107 void setInteractionFlag( InteractionFlag theFlag,
108 bool theIsEnabled = true );
109 void setInteractionFlags( InteractionFlags theFlags );
112 void setViewLabelPosition( ViewLabelPosition thePosition,
113 bool theIsForced = false );
114 void setViewLabelText( const QString& theText );
116 // displaying mouse position (currently, overlaps with view label feature)
117 void setMousePositionEnabled( bool theState );
119 // background / foreground
120 QColor backgroundColor() const;
121 void setBackgroundColor( const QColor& theColor );
123 bool isForegroundEnabled() const { return myIsForegroundEnabled; }
124 void setForegroundEnabled( bool theState );
126 QSizeF foregroundSize() const { return myForegroundSize; }
127 void setForegroundSize( const QSizeF& theRect );
129 double foregroundMargin() const { return myForegroundMargin; }
130 void setForegroundMargin( double theMargin );
132 QColor foregroundColor() const { return myForegroundColor; }
133 void setForegroundColor( const QColor& theColor );
135 QColor foregroundFrameColor() const { return myForegroundFrameColor; }
136 void setForegroundFrameColor( const QColor& theColor );
138 double foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
139 void setForegroundFrameLineWidth( double theLineWidth );
141 void updateForeground();
144 void setGridEnabled( bool theState );
145 void setGridCellSize( int theCellSize );
146 void setGridLineStyle( int theLineStyle );
147 void setGridLineColor( const QColor& theLineColor );
152 void pan( double theDX, double theDY );
153 void setCenter( double theX, double theY );
154 void zoom( double theX1, double theY1, double theX2, double theY2 );
155 void fitRect( const QRectF& theRect );
157 void fitAll( bool theKeepScale = false );
160 bool isTransforming() const { return myIsTransforming; }
162 void applyTransform();
165 BlockStatus currentBlock();
168 virtual void highlight( double theX, double theY );
169 void clearHighlighted();
171 GraphicsView_Object* getHighlightedObject() const { return myHighlightedObject; }
174 virtual int select( const QRectF& theRect, bool theIsAppend );
175 void clearSelected();
176 void setSelected( GraphicsView_Object* theObject );
178 int nbSelected() const;
182 GraphicsView_Object* selectedObject();
184 const GraphicsView_ObjectList& getSelectedObjects() const { return mySelectedObjects; }
186 // rectangle selection
187 void startSelectByRect( int x, int y );
188 void drawSelectByRect( int x, int y );
189 void finishSelectByRect();
190 bool isSelectByRect() const;
191 QRect selectionRect();
194 void prepareToSketch( bool theStatus );
195 bool isPrepareToSketch();
196 void startSketching( const QPointF& thePoint,
198 void drawSketching( const QPointF& thePoint );
199 void finishSketching( bool theStatus );
200 bool isSketching( bool* theIsPath = 0 ) const;
203 bool isDragging() { return myIsDragging; }
206 bool startPulling( const QPointF& );
207 void drawPulling( const QPointF& );
208 void finishPulling( bool theStatus );
209 bool isPulling() const { return myIsPulling; }
212 bool cancelCurrentOperation();
215 static void createCursors();
216 static void destroyCursors();
217 static QCursor* getDefaultCursor() { return defCursor; }
218 static QCursor* getHandCursor() { return handCursor; }
219 static QCursor* getPanCursor() { return panCursor; }
220 static QCursor* getPanglCursor() { return panglCursor; }
221 static QCursor* getZoomCursor() { return zoomCursor; }
222 static QCursor* getSketchCursor() { return sketchCursor; }
225 void onBoundingRectChanged();
228 void onKeyEvent( QKeyEvent* );
229 void onMouseEvent( QGraphicsSceneMouseEvent* );
230 void onWheelEvent( QGraphicsSceneWheelEvent* );
231 void onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
234 virtual void scrollContentsBy( int theDX, int theDY );
237 void vpKeyEvent( QKeyEvent* );
238 void vpMouseEvent( QGraphicsSceneMouseEvent* );
239 void vpWheelEvent( QGraphicsSceneWheelEvent* );
240 void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
242 void vpSketchingFinished( QPainterPath );
244 void vpObjectBeforeMoving();
245 void vpObjectAfterMoving( bool );
251 void dragObjects( QGraphicsSceneMouseEvent* );
255 static QCursor* defCursor;
256 static QCursor* handCursor;
257 static QCursor* panCursor;
258 static QCursor* panglCursor;
259 static QCursor* zoomCursor;
260 static QCursor* sketchCursor;
264 GraphicsView_Scene* myScene;
267 GraphicsView_ObjectList myObjects;
270 InteractionFlags myInteractionFlags;
273 ViewLabel* myViewLabel;
274 ViewLabelPosition myViewLabelPosition;
275 QGridLayout* myViewLabelLayout;
277 // displaying mouse position (currently, overlaps with view label feature)
278 bool myIsMousePositionEnabled;
281 bool myIsForegroundEnabled;
282 QSizeF myForegroundSize;
283 double myForegroundMargin;
284 QColor myForegroundColor;
285 QColor myForegroundFrameColor;
286 double myForegroundFrameLineWidth;
287 QGraphicsRectItem* myForegroundItem;
290 bool myIsGridEnabled;
293 QColor myGridLineColor;
294 QGraphicsPathItem* myGridItem;
297 bool myIsTransforming;
298 QTransform myCurrentTransform;
301 GraphicsView_Object* myHighlightedObject;
304 bool myIsHighlighting;
307 GraphicsView_ObjectList mySelectedObjects;
308 int mySelectionIterator;
310 // rectangle selection
311 QRubberBand* myRectBand;
312 QPoint myFirstSelectionPoint;
313 QPoint myLastSelectionPoint;
314 bool myAreSelectionPointsInitialized;
317 QGraphicsPathItem* mySketchingItem;
318 QPointF mySketchingPoint;
319 bool myIsPrepareToSketch;
321 bool myIsSketchingByPath;
325 QPointF myDragPosition;
326 bool myIsDragPositionInitialized;
330 GraphicsView_Object* myPullingObject;
333 QCursor myStoredCursor;
336 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )