1 // Copyright (C) 2013-2014 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 ~GraphicsView_ViewPort();
83 GraphicsView_Scene* getScene() { return myScene; }
84 void addItem( QGraphicsItem* theItem );
85 void removeItem( QGraphicsItem* theItem );
87 enum SortType { NoSorting, SelectedFirst, SortByZLevel };
88 GraphicsView_ObjectList getObjects( SortType theSortType = NoSorting ) const;
90 QRectF objectsBoundingRect( bool theOnlyVisible = false ) const;
92 QImage dumpView( bool theWholeScene = false,
93 QSizeF theSize = QSizeF() );
97 void setSceneGap( double theSceneGap );
98 void setFitAllGap( double theFitAllGap );
101 int interactionFlags() const;
102 bool hasInteractionFlag( InteractionFlag theFlag );
103 void setInteractionFlag( InteractionFlag theFlag,
104 bool theIsEnabled = true );
105 void setInteractionFlags( InteractionFlags theFlags );
108 void setViewLabelPosition( ViewLabelPosition thePosition,
109 bool theIsForced = false );
110 void setViewLabelText( const QString& theText );
112 // displaying mouse position (currently, overlaps with view label feature)
113 void setMousePositionEnabled( bool theState );
115 // background / foreground
116 QColor backgroundColor() const;
117 void setBackgroundColor( const QColor& theColor );
119 bool isForegroundEnabled() const { return myIsForegroundEnabled; }
120 void setForegroundEnabled( bool theState );
122 QSizeF foregroundSize() const { return myForegroundSize; }
123 void setForegroundSize( const QSizeF& theRect );
125 double foregroundMargin() const { return myForegroundMargin; }
126 void setForegroundMargin( double theMargin );
128 QColor foregroundColor() const { return myForegroundColor; }
129 void setForegroundColor( const QColor& theColor );
131 QColor foregroundFrameColor() const { return myForegroundFrameColor; }
132 void setForegroundFrameColor( const QColor& theColor );
134 double foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
135 void setForegroundFrameLineWidth( double theLineWidth );
137 void updateForeground();
140 void setGridEnabled( bool theState );
141 void setGridCellSize( int theCellSize );
142 void setGridLineStyle( int theLineStyle );
143 void setGridLineColor( const QColor& theLineColor );
148 void pan( double theDX, double theDY );
149 void setCenter( double theX, double theY );
150 void zoom( double theX1, double theY1, double theX2, double theY2 );
151 void fitRect( const QRectF& theRect );
153 void fitAll( bool theKeepScale = false );
156 bool isTransforming() const { return myIsTransforming; }
158 void applyTransform();
161 BlockStatus currentBlock();
164 virtual void highlight( double theX, double theY );
165 void clearHighlighted();
167 GraphicsView_Object* getHighlightedObject() const { return myHighlightedObject; }
170 virtual int select( const QRectF& theRect, bool theIsAppend );
171 void clearSelected();
172 void setSelected( GraphicsView_Object* theObject );
174 int nbSelected() const;
178 GraphicsView_Object* selectedObject();
180 const GraphicsView_ObjectList& getSelectedObjects() const { return mySelectedObjects; }
182 // rectangle selection
183 void startSelectByRect( int x, int y );
184 void drawSelectByRect( int x, int y );
185 void finishSelectByRect();
186 bool isSelectByRect() const;
187 QRect selectionRect();
190 void prepareToSketch( bool theStatus );
191 bool isPrepareToSketch();
192 void startSketching( const QPointF& thePoint,
194 void drawSketching( const QPointF& thePoint );
195 void finishSketching( bool theStatus );
196 bool isSketching( bool* theIsPath = 0 ) const;
199 bool isDragging() { return myIsDragging; }
202 bool startPulling( const QPointF& );
203 void drawPulling( const QPointF& );
204 void finishPulling( bool theStatus );
205 bool isPulling() const { return myIsPulling; }
208 bool cancelCurrentOperation();
211 static void createCursors();
212 static void destroyCursors();
213 static QCursor* getDefaultCursor() { return defCursor; }
214 static QCursor* getHandCursor() { return handCursor; }
215 static QCursor* getPanCursor() { return panCursor; }
216 static QCursor* getPanglCursor() { return panglCursor; }
217 static QCursor* getZoomCursor() { return zoomCursor; }
218 static QCursor* getSketchCursor() { return sketchCursor; }
221 void onBoundingRectChanged();
224 void onKeyEvent( QKeyEvent* );
225 void onMouseEvent( QGraphicsSceneMouseEvent* );
226 void onWheelEvent( QGraphicsSceneWheelEvent* );
227 void onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
230 virtual void scrollContentsBy( int theDX, int theDY );
233 void vpKeyEvent( QKeyEvent* );
234 void vpMouseEvent( QGraphicsSceneMouseEvent* );
235 void vpWheelEvent( QGraphicsSceneWheelEvent* );
236 void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
238 void vpSketchingFinished( QPainterPath );
240 void vpObjectBeforeMoving();
241 void vpObjectAfterMoving( bool );
247 void dragObjects( QGraphicsSceneMouseEvent* );
251 static QCursor* defCursor;
252 static QCursor* handCursor;
253 static QCursor* panCursor;
254 static QCursor* panglCursor;
255 static QCursor* zoomCursor;
256 static QCursor* sketchCursor;
260 GraphicsView_Scene* myScene;
263 GraphicsView_ObjectList myObjects;
266 InteractionFlags myInteractionFlags;
269 ViewLabel* myViewLabel;
270 ViewLabelPosition myViewLabelPosition;
271 QGridLayout* myViewLabelLayout;
273 // displaying mouse position (currently, overlaps with view label feature)
274 bool myIsMousePositionEnabled;
277 bool myIsForegroundEnabled;
278 QSizeF myForegroundSize;
279 double myForegroundMargin;
280 QColor myForegroundColor;
281 QColor myForegroundFrameColor;
282 double myForegroundFrameLineWidth;
283 QGraphicsRectItem* myForegroundItem;
286 bool myIsGridEnabled;
289 QColor myGridLineColor;
290 QGraphicsPathItem* myGridItem;
293 bool myIsTransforming;
294 QTransform myCurrentTransform;
297 GraphicsView_Object* myHighlightedObject;
300 bool myIsHighlighting;
303 GraphicsView_ObjectList mySelectedObjects;
304 int mySelectionIterator;
306 // rectangle selection
307 QRubberBand* myRectBand;
308 QPoint myFirstSelectionPoint;
309 QPoint myLastSelectionPoint;
310 bool myAreSelectionPointsInitialized;
313 QGraphicsPathItem* mySketchingItem;
314 QPointF mySketchingPoint;
315 bool myIsPrepareToSketch;
317 bool myIsSketchingByPath;
321 QPointF myDragPosition;
322 bool myIsDragPositionInitialized;
326 GraphicsView_Object* myPullingObject;
329 QCursor myStoredCursor;
332 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )