QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsgraphicsviewmousehandles.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgraphicsviewmousehandles.h
3  -----------------------
4  begin : March 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSGRAPHICSVIEWMOUSEHANDLES_H
18 #define QGSGRAPHICSVIEWMOUSEHANDLES_H
19 
20 // We don't want to expose this in the public API
21 #define SIP_NO_FILE
22 
23 #include <QGraphicsRectItem>
24 #include <QObject>
25 #include <QPointer>
26 #include <memory>
27 
28 #include "qgis_gui.h"
29 
30 class QGraphicsView;
31 class QInputEvent;
32 
34 
45 class GUI_EXPORT QgsGraphicsViewMouseHandles: public QObject, public QGraphicsRectItem
46 {
47  Q_OBJECT
48  public:
49 
51  enum MouseAction
52  {
53  MoveItem,
54  ResizeUp,
55  ResizeDown,
56  ResizeLeft,
57  ResizeRight,
58  ResizeLeftUp,
59  ResizeRightUp,
60  ResizeLeftDown,
61  ResizeRightDown,
62  SelectItem,
63  NoAction
64  };
65 
66  enum ItemPositionMode
67  {
68  UpperLeft,
69  UpperMiddle,
70  UpperRight,
71  MiddleLeft,
72  Middle,
73  MiddleRight,
74  LowerLeft,
75  LowerMiddle,
76  LowerRight
77  };
78 
79  enum SnapGuideMode
80  {
81  Item,
82  Point
83  };
84 
85  QgsGraphicsViewMouseHandles( QGraphicsView *view );
86 
88  QgsGraphicsViewMouseHandles::MouseAction mouseActionForScenePos( QPointF sceneCoordPos );
89 
91  bool isDragging() const { return mIsDragging; }
92 
94  bool isResizing() const { return mIsResizing; }
95 
96  bool shouldBlockEvent( QInputEvent *event ) const;
97 
98  public slots:
99 
101  void selectedItemSizeChanged();
102 
104  void selectedItemRotationChanged();
105 
106  protected:
107 
108  void paintInternal( QPainter *painter, bool showHandles, bool showStaticBoundingBoxes,
109  bool showTemporaryBoundingBoxes, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr );
110 
112  virtual void setViewportCursor( Qt::CursorShape cursor ) = 0;
113 
114  virtual QList<QGraphicsItem *> sceneItemsAtPoint( QPointF scenePoint ) = 0;
115  virtual QList<QGraphicsItem *> selectedSceneItems( bool includeLockedItems = true ) const = 0;
116  virtual bool itemIsLocked( QGraphicsItem *item ) { Q_UNUSED( item ); return false; }
117  virtual bool itemIsGroupMember( QGraphicsItem *item ) { Q_UNUSED( item ); return false; }
118  virtual QRectF itemRect( QGraphicsItem *item ) const = 0;
119  virtual QRectF storedItemRect( QGraphicsItem *item ) const;
120  virtual void moveItem( QGraphicsItem *item, double deltaX, double deltaY ) = 0;
121  virtual void previewItemMove( QGraphicsItem *item, double deltaX, double deltaY );
122  virtual void setItemRect( QGraphicsItem *item, QRectF rect ) = 0;
123 
132  virtual QRectF previewSetItemRect( QGraphicsItem *item, QRectF rect );
133 
134  virtual void startMacroCommand( const QString &text );
135  virtual void endMacroCommand();
136  virtual void createItemCommand( QGraphicsItem *item );
137  virtual void endItemCommand( QGraphicsItem *item );
138  virtual void showStatusMessage( const QString &message ) { Q_UNUSED( message ) }
139  virtual void hideAlignItems() {}
141  virtual QPointF snapPoint( QPointF originalPoint, SnapGuideMode mode, bool snapHorizontal = true, bool snapVertical = true );
142 
144  virtual void expandItemList( const QList< QGraphicsItem * > &items, QList< QGraphicsItem * > &collected ) const;
145 
146  void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
147  void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
148  void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
149  void mousePressEvent( QGraphicsSceneMouseEvent *event ) override;
150  void mouseMoveEvent( QGraphicsSceneMouseEvent *event ) override;
151  void mouseReleaseEvent( QGraphicsSceneMouseEvent *event ) override;
152 
153  //resets the layout designer status bar to the default message
154  void resetStatusBar();
155 
157  bool selectionRotation( double &rotation ) const;
158 
160  void updateHandles();
161 
163  void dragMouseMove( QPointF currentPosition, bool lockMovement, bool preventSnap );
164 
166  void resizeMouseMove( QPointF currentPosition, bool lockAspect, bool fromCenter );
167 
168  void setHandleSize( double size );
169 
171  MouseAction mouseActionForPosition( QPointF itemCoordPos );
172 
174  QSizeF calcCursorEdgeOffset( QPointF cursorPos );
175 
177  QRectF selectionBounds() const;
178 
187  static void relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter );
188 
198  static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
199 
200  private:
201 
202  QGraphicsView *mView = nullptr;
203 
204  double mHandleSize = 10;
205 
206  QSizeF mCursorOffset;
207  double mResizeMoveX = 0;
208  double mResizeMoveY = 0;
209 
211  double mBeginHandleWidth = 0;
212  double mBeginHandleHeight = 0;
213 
214  QRectF mResizeRect;
215 
217  QPointF mMouseMoveStartPos;
218 
220  QPointF mLastMouseEventPos;
221 
222  MouseAction mCurrentMouseMoveAction = NoAction;
223 
225  bool mIsDragging = false;
227  bool mIsResizing = false;
228 
230  QPointF mBeginMouseEventPos;
231 
233  QPointF mBeginHandlePos;
234 
236  void drawHandles( QPainter *painter, double rectHandlerSize );
237 
239  void drawSelectedItemBounds( QPainter *painter );
240 
244  double rectHandlerBorderTolerance();
245 
247  Qt::CursorShape cursorForPosition( QPointF itemCoordPos );
248 
249 
250 
251 };
252 
254 
255 #endif // QGSLAYOUTMOUSEHANDLES_H