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 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 );
90 enum SortType { NoSorting, SelectedFirst, SortByZLevel };
91 GraphicsView_ObjectList getObjects( SortType theSortType = NoSorting ) const;
93 QRectF objectsBoundingRect( bool theOnlyVisible = false ) const;
95 QImage dumpView( bool theWholeScene = false,
96 QSizeF theSize = QSizeF() );
100 void setSceneGap( double theSceneGap );
101 void setFitAllGap( double theFitAllGap );
104 int interactionFlags() const;
105 bool hasInteractionFlag( InteractionFlag theFlag );
106 void setInteractionFlag( InteractionFlag theFlag,
107 bool theIsEnabled = true );
108 void setInteractionFlags( InteractionFlags theFlags );
111 void setViewLabelPosition( ViewLabelPosition thePosition,
112 bool theIsForced = false );
113 void setViewLabelText( const QString& theText );
115 // displaying mouse position (currently, overlaps with view label feature)
116 void setMousePositionEnabled( bool theState );
118 // background / foreground
119 QColor backgroundColor() const;
120 void setBackgroundColor( const QColor& theColor );
122 bool isForegroundEnabled() const { return myIsForegroundEnabled; }
123 void setForegroundEnabled( bool theState );
125 QSizeF foregroundSize() const { return myForegroundSize; }
126 void setForegroundSize( const QSizeF& theRect );
128 double foregroundMargin() const { return myForegroundMargin; }
129 void setForegroundMargin( double theMargin );
131 QColor foregroundColor() const { return myForegroundColor; }
132 void setForegroundColor( const QColor& theColor );
134 QColor foregroundFrameColor() const { return myForegroundFrameColor; }
135 void setForegroundFrameColor( const QColor& theColor );
137 double foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
138 void setForegroundFrameLineWidth( double theLineWidth );
140 void updateForeground();
143 void setGridEnabled( bool theState );
144 void setGridCellSize( int theCellSize );
145 void setGridLineStyle( int theLineStyle );
146 void setGridLineColor( const QColor& theLineColor );
151 void pan( double theDX, double theDY );
152 void setCenter( double theX, double theY );
153 void zoom( double theX1, double theY1, double theX2, double theY2 );
154 void fitRect( const QRectF& theRect );
156 void fitAll( bool theKeepScale = false );
159 bool isTransforming() const { return myIsTransforming; }
161 void applyTransform();
164 BlockStatus currentBlock();
167 virtual void highlight( double theX, double theY );
168 void clearHighlighted();
170 GraphicsView_Object* getHighlightedObject() const { return myHighlightedObject; }
173 virtual int select( const QRectF& theRect, bool theIsAppend );
174 void clearSelected();
175 void setSelected( GraphicsView_Object* theObject );
177 int nbSelected() const;
181 GraphicsView_Object* selectedObject();
183 const GraphicsView_ObjectList& getSelectedObjects() const { return mySelectedObjects; }
185 // rectangle selection
186 void startSelectByRect( int x, int y );
187 void drawSelectByRect( int x, int y );
188 void finishSelectByRect();
189 bool isSelectByRect() const;
190 QRect selectionRect();
193 void prepareToSketch( bool theStatus );
194 bool isPrepareToSketch();
195 void startSketching( const QPointF& thePoint,
197 void drawSketching( const QPointF& thePoint );
198 void finishSketching( bool theStatus );
199 bool isSketching( bool* theIsPath = 0 ) const;
202 bool isDragging() { return myIsDragging; }
205 bool startPulling( const QPointF& );
206 void drawPulling( const QPointF& );
207 void finishPulling( bool theStatus );
208 bool isPulling() const { return myIsPulling; }
211 bool cancelCurrentOperation();
214 static void createCursors();
215 static void destroyCursors();
216 static QCursor* getDefaultCursor() { return defCursor; }
217 static QCursor* getHandCursor() { return handCursor; }
218 static QCursor* getPanCursor() { return panCursor; }
219 static QCursor* getPanglCursor() { return panglCursor; }
220 static QCursor* getZoomCursor() { return zoomCursor; }
221 static QCursor* getSketchCursor() { return sketchCursor; }
224 void onBoundingRectChanged();
227 void onKeyEvent( QKeyEvent* );
228 void onMouseEvent( QGraphicsSceneMouseEvent* );
229 void onWheelEvent( QGraphicsSceneWheelEvent* );
230 void onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
233 virtual void scrollContentsBy( int theDX, int theDY );
236 void vpKeyEvent( QKeyEvent* );
237 void vpMouseEvent( QGraphicsSceneMouseEvent* );
238 void vpWheelEvent( QGraphicsSceneWheelEvent* );
239 void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
241 void vpSketchingFinished( QPainterPath );
243 void vpObjectBeforeMoving();
244 void vpObjectAfterMoving( bool );
250 void dragObjects( QGraphicsSceneMouseEvent* );
254 static QCursor* defCursor;
255 static QCursor* handCursor;
256 static QCursor* panCursor;
257 static QCursor* panglCursor;
258 static QCursor* zoomCursor;
259 static QCursor* sketchCursor;
263 GraphicsView_Scene* myScene;
266 GraphicsView_ObjectList myObjects;
269 InteractionFlags myInteractionFlags;
272 ViewLabel* myViewLabel;
273 ViewLabelPosition myViewLabelPosition;
274 QGridLayout* myViewLabelLayout;
276 // displaying mouse position (currently, overlaps with view label feature)
277 bool myIsMousePositionEnabled;
280 bool myIsForegroundEnabled;
281 QSizeF myForegroundSize;
282 double myForegroundMargin;
283 QColor myForegroundColor;
284 QColor myForegroundFrameColor;
285 double myForegroundFrameLineWidth;
286 QGraphicsRectItem* myForegroundItem;
289 bool myIsGridEnabled;
292 QColor myGridLineColor;
293 QGraphicsPathItem* myGridItem;
296 bool myIsTransforming;
297 QTransform myCurrentTransform;
300 GraphicsView_Object* myHighlightedObject;
303 bool myIsHighlighting;
306 GraphicsView_ObjectList mySelectedObjects;
307 int mySelectionIterator;
309 // rectangle selection
310 QRubberBand* myRectBand;
311 QPoint myFirstSelectionPoint;
312 QPoint myLastSelectionPoint;
313 bool myAreSelectionPointsInitialized;
316 QGraphicsPathItem* mySketchingItem;
317 QPointF mySketchingPoint;
318 bool myIsPrepareToSketch;
320 bool myIsSketchingByPath;
324 QPointF myDragPosition;
325 bool myIsDragPositionInitialized;
329 GraphicsView_Object* myPullingObject;
332 QCursor myStoredCursor;
335 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )