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 bool isItemAdded( QGraphicsItem* theItem );
86 void removeItem( QGraphicsItem* theItem );
88 enum SortType { NoSorting, SelectedFirst, SortByZLevel };
89 GraphicsView_ObjectList getObjects( SortType theSortType = NoSorting ) const;
91 QRectF objectsBoundingRect( bool theOnlyVisible = false ) const;
93 QImage dumpView( bool theWholeScene = false,
94 QSizeF theSize = QSizeF() );
98 void setSceneGap( double theSceneGap );
99 void setFitAllGap( double theFitAllGap );
102 int interactionFlags() const;
103 bool hasInteractionFlag( InteractionFlag theFlag );
104 void setInteractionFlag( InteractionFlag theFlag,
105 bool theIsEnabled = true );
106 void setInteractionFlags( InteractionFlags theFlags );
109 void setViewLabelPosition( ViewLabelPosition thePosition,
110 bool theIsForced = false );
111 void setViewLabelText( const QString& theText );
113 // displaying mouse position (currently, overlaps with view label feature)
114 void setMousePositionEnabled( bool theState );
116 // background / foreground
117 QColor backgroundColor() const;
118 void setBackgroundColor( const QColor& theColor );
120 bool isForegroundEnabled() const { return myIsForegroundEnabled; }
121 void setForegroundEnabled( bool theState );
123 QSizeF foregroundSize() const { return myForegroundSize; }
124 void setForegroundSize( const QSizeF& theRect );
126 double foregroundMargin() const { return myForegroundMargin; }
127 void setForegroundMargin( double theMargin );
129 QColor foregroundColor() const { return myForegroundColor; }
130 void setForegroundColor( const QColor& theColor );
132 QColor foregroundFrameColor() const { return myForegroundFrameColor; }
133 void setForegroundFrameColor( const QColor& theColor );
135 double foregroundFrameLineWidth() const { return myForegroundFrameLineWidth; }
136 void setForegroundFrameLineWidth( double theLineWidth );
138 void updateForeground();
141 void setGridEnabled( bool theState );
142 void setGridCellSize( int theCellSize );
143 void setGridLineStyle( int theLineStyle );
144 void setGridLineColor( const QColor& theLineColor );
149 void pan( double theDX, double theDY );
150 void setCenter( double theX, double theY );
151 void zoom( double theX1, double theY1, double theX2, double theY2 );
152 void fitRect( const QRectF& theRect );
154 void fitAll( bool theKeepScale = false );
157 bool isTransforming() const { return myIsTransforming; }
159 void applyTransform();
162 BlockStatus currentBlock();
165 virtual void highlight( double theX, double theY );
166 void clearHighlighted();
168 GraphicsView_Object* getHighlightedObject() const { return myHighlightedObject; }
171 virtual int select( const QRectF& theRect, bool theIsAppend );
172 void clearSelected();
173 void setSelected( GraphicsView_Object* theObject );
175 int nbSelected() const;
179 GraphicsView_Object* selectedObject();
181 const GraphicsView_ObjectList& getSelectedObjects() const { return mySelectedObjects; }
183 // rectangle selection
184 void startSelectByRect( int x, int y );
185 void drawSelectByRect( int x, int y );
186 void finishSelectByRect();
187 bool isSelectByRect() const;
188 QRect selectionRect();
191 void prepareToSketch( bool theStatus );
192 bool isPrepareToSketch();
193 void startSketching( const QPointF& thePoint,
195 void drawSketching( const QPointF& thePoint );
196 void finishSketching( bool theStatus );
197 bool isSketching( bool* theIsPath = 0 ) const;
200 bool isDragging() { return myIsDragging; }
203 bool startPulling( const QPointF& );
204 void drawPulling( const QPointF& );
205 void finishPulling( bool theStatus );
206 bool isPulling() const { return myIsPulling; }
209 bool cancelCurrentOperation();
212 static void createCursors();
213 static void destroyCursors();
214 static QCursor* getDefaultCursor() { return defCursor; }
215 static QCursor* getHandCursor() { return handCursor; }
216 static QCursor* getPanCursor() { return panCursor; }
217 static QCursor* getPanglCursor() { return panglCursor; }
218 static QCursor* getZoomCursor() { return zoomCursor; }
219 static QCursor* getSketchCursor() { return sketchCursor; }
222 void onBoundingRectChanged();
225 void onKeyEvent( QKeyEvent* );
226 void onMouseEvent( QGraphicsSceneMouseEvent* );
227 void onWheelEvent( QGraphicsSceneWheelEvent* );
228 void onContextMenuEvent( QGraphicsSceneContextMenuEvent* );
231 virtual void scrollContentsBy( int theDX, int theDY );
234 void vpKeyEvent( QKeyEvent* );
235 void vpMouseEvent( QGraphicsSceneMouseEvent* );
236 void vpWheelEvent( QGraphicsSceneWheelEvent* );
237 void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* );
239 void vpSketchingFinished( QPainterPath );
241 void vpObjectBeforeMoving();
242 void vpObjectAfterMoving( bool );
248 void dragObjects( QGraphicsSceneMouseEvent* );
252 static QCursor* defCursor;
253 static QCursor* handCursor;
254 static QCursor* panCursor;
255 static QCursor* panglCursor;
256 static QCursor* zoomCursor;
257 static QCursor* sketchCursor;
261 GraphicsView_Scene* myScene;
264 GraphicsView_ObjectList myObjects;
267 InteractionFlags myInteractionFlags;
270 ViewLabel* myViewLabel;
271 ViewLabelPosition myViewLabelPosition;
272 QGridLayout* myViewLabelLayout;
274 // displaying mouse position (currently, overlaps with view label feature)
275 bool myIsMousePositionEnabled;
278 bool myIsForegroundEnabled;
279 QSizeF myForegroundSize;
280 double myForegroundMargin;
281 QColor myForegroundColor;
282 QColor myForegroundFrameColor;
283 double myForegroundFrameLineWidth;
284 QGraphicsRectItem* myForegroundItem;
287 bool myIsGridEnabled;
290 QColor myGridLineColor;
291 QGraphicsPathItem* myGridItem;
294 bool myIsTransforming;
295 QTransform myCurrentTransform;
298 GraphicsView_Object* myHighlightedObject;
301 bool myIsHighlighting;
304 GraphicsView_ObjectList mySelectedObjects;
305 int mySelectionIterator;
307 // rectangle selection
308 QRubberBand* myRectBand;
309 QPoint myFirstSelectionPoint;
310 QPoint myLastSelectionPoint;
311 bool myAreSelectionPointsInitialized;
314 QGraphicsPathItem* mySketchingItem;
315 QPointF mySketchingPoint;
316 bool myIsPrepareToSketch;
318 bool myIsSketchingByPath;
322 QPointF myDragPosition;
323 bool myIsDragPositionInitialized;
327 GraphicsView_Object* myPullingObject;
330 QCursor myStoredCursor;
333 Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags )