QGIS API Documentation  2.99.0-Master (9caa722)
qgscomposermousehandles.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposermousehandles.h
3  -------------------
4  begin : September 2013
5  copyright : (C) 2013 by Nyall Dawson, Radim Blazek
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 QGSCOMPOSERMOUSEHANDLES_H
18 #define QGSCOMPOSERMOUSEHANDLES_H
19 
20 #define SIP_NO_FILE
21 
22 #include <QGraphicsRectItem>
23 #include <QObject>
24 #include <QPointer>
25 
26 #include "qgis_core.h"
27 
28 class QgsComposition;
29 class QgsComposerItem;
30 class QGraphicsView;
31 
37 class CORE_EXPORT QgsComposerMouseHandles: public QObject, public QGraphicsRectItem
38 {
39  Q_OBJECT
40  public:
41 
44  {
55  NoAction
56  };
57 
59  {
68  LowerRight
69  };
70 
72  {
74  Point
75  };
76 
78 
79  void setComposition( QgsComposition *c ) { mComposition = c; }
80  QgsComposition *composition() { return mComposition; }
81 
82  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
83 
85  QgsComposerMouseHandles::MouseAction mouseActionForScenePos( QPointF sceneCoordPos );
86 
88  bool isDragging() { return mIsDragging; }
89 
91  bool isResizing() { return mIsResizing; }
92 
93  protected:
94 
95  void mouseMoveEvent( QGraphicsSceneMouseEvent *event ) override;
96  void mouseReleaseEvent( QGraphicsSceneMouseEvent *event ) override;
97  void mousePressEvent( QGraphicsSceneMouseEvent *event ) override;
98  void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
99  void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
100  void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
101 
102  public slots:
103 
105  void selectionChanged();
106 
108  void selectedItemSizeChanged();
109 
111  void selectedItemRotationChanged();
112 
113  private:
114 
115  QgsComposition *mComposition; //reference to composition
116  QPointer< QGraphicsView > mGraphicsView; //reference to QGraphicsView
117 
118  QgsComposerMouseHandles::MouseAction mCurrentMouseMoveAction;
120  QPointF mMouseMoveStartPos;
122  QPointF mLastMouseEventPos;
124  QPointF mBeginMouseEventPos;
126  QPointF mBeginHandlePos;
128  double mBeginHandleWidth;
129  double mBeginHandleHeight;
130 
131  QRectF mResizeRect;
132  double mResizeMoveX;
133  double mResizeMoveY;
134 
136  bool mIsDragging;
138  bool mIsResizing;
139 
141  QGraphicsLineItem *mHAlignSnapItem = nullptr;
142  QGraphicsLineItem *mVAlignSnapItem = nullptr;
143 
144  QSizeF mCursorOffset;
145 
147  QRectF selectionBounds() const;
148 
150  bool selectionRotation( double &rotation ) const;
151 
153  void updateHandles();
155  void drawHandles( QPainter *painter, double rectHandlerSize );
157  void drawSelectedItemBounds( QPainter *painter );
158 
161  double rectHandlerBorderTolerance();
162 
164  Qt::CursorShape cursorForPosition( QPointF itemCoordPos );
165 
167  QgsComposerMouseHandles::MouseAction mouseActionForPosition( QPointF itemCoordPos );
168 
170  void dragMouseMove( QPointF currentPosition, bool lockMovement, bool preventSnap );
171 
173  QSizeF calcCursorEdgeOffset( QPointF cursorPos );
174 
176  void resizeMouseMove( QPointF currentPosition, bool lockAspect, bool fromCenter );
177 
179  QGraphicsLineItem *hAlignSnapItem();
180  void deleteHAlignSnapItem();
182  QGraphicsLineItem *vAlignSnapItem();
183  void deleteVAlignSnapItem();
184  void deleteAlignItems();
185 
187  QPointF snapPoint( QPointF originalPoint, QgsComposerMouseHandles::SnapGuideMode mode );
189  QPointF alignItem( double &alignX, double &alignY, double unalignedX, double unalignedY );
191  QPointF alignPos( QPointF pos, double &alignX, double &alignY );
192 
193  //helper functions for item align
194  void collectAlignCoordinates( QMap< double, const QgsComposerItem * > &alignCoordsX, QMap< double, const QgsComposerItem * > &alignCoordsY );
195  bool nearestItem( const QMap< double, const QgsComposerItem * > &coords, double value, double &nearestValue ) const;
196  void checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem * > &alignCoords, double &smallestDiff, double itemCoordOffset, double &itemCoord, double &alignCoord );
197 
198  //tries to return the current QGraphicsView attached to the composition
199  QGraphicsView *graphicsView();
200 
201  //sets the mouse cursor for the QGraphicsView attached to the composition (workaround qt bug #3732)
202  void setViewportCursor( Qt::CursorShape cursor );
203 
204  //resets the composer window status bar to the default message
205  void resetStatusBar();
206 };
207 
208 #endif // QGSCOMPOSERMOUSEHANDLES_H
A item that forms part of a map composition.
bool isDragging()
Returns true is user is currently dragging the handles.
MouseAction
Describes the action (move or resize in different directon) to be done during mouse move...
bool isResizing()
Returns true is user is currently resizing with the handles.
Graphics scene for map printing.
void setComposition(QgsComposition *c)
Handles drawing of selection outlines and mouse handles.