QGIS API Documentation  3.6.0-Noosa (5873452)
qgslayoutsnapper.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutsnapper.h
3  -------------------
4  begin : July 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSLAYOUTSNAPPER_H
17 #define QGSLAYOUTSNAPPER_H
18 
19 #include "qgis_core.h"
20 #include "qgslayoutmeasurement.h"
21 #include "qgslayoutpoint.h"
24 #include <QPen>
25 
26 class QgsLayout;
28 
37 {
38 
39  public:
40 
44  QgsLayoutSnapper( QgsLayout *layout );
45 
46  QString stringType() const override { return QStringLiteral( "LayoutSnapper" ); }
47  QgsLayout *layout() override;
48 
53  void setSnapTolerance( int snapTolerance );
54 
59  int snapTolerance() const { return mTolerance; }
60 
65  bool snapToGrid() const { return mSnapToGrid; }
66 
71  void setSnapToGrid( bool enabled );
72 
77  bool snapToGuides() const { return mSnapToGuides; }
78 
83  void setSnapToGuides( bool enabled );
84 
89  bool snapToItems() const { return mSnapToItems; }
90 
95  void setSnapToItems( bool enabled );
96 
113  QPointF snapPoint( QPointF point, double scaleFactor, bool &snapped SIP_OUT, QGraphicsLineItem *horizontalSnapLine = nullptr,
114  QGraphicsLineItem *verticalSnapLine = nullptr,
115  const QList< QgsLayoutItem * > *ignoreItems = nullptr ) const;
116 
136  QRectF snapRect( const QRectF &rect, double scaleFactor, bool &snapped SIP_OUT, QGraphicsLineItem *horizontalSnapLine = nullptr,
137  QGraphicsLineItem *verticalSnapLine = nullptr,
138  const QList< QgsLayoutItem * > *ignoreItems = nullptr ) const;
139 
154  QPointF snapPointToGrid( QPointF point, double scaleFactor, bool &snappedX SIP_OUT, bool &snappedY SIP_OUT ) const;
155 
171  QPointF snapPointsToGrid( const QList< QPointF > &points, double scaleFactor, bool &snappedX SIP_OUT, bool &snappedY SIP_OUT ) const;
172 
186  double snapPointToGuides( double original, Qt::Orientation orientation, double scaleFactor, bool &snapped SIP_OUT ) const;
187 
203  double snapPointsToGuides( const QList< double > &points, Qt::Orientation orientation, double scaleFactor, bool &snapped SIP_OUT ) const;
204 
223  double snapPointToItems( double original, Qt::Orientation orientation, double scaleFactor, const QList< QgsLayoutItem * > &ignoreItems, bool &snapped SIP_OUT,
224  QGraphicsLineItem *snapLine = nullptr ) const;
225 
241  double snapPointsToItems( const QList< double > &points, Qt::Orientation orientation, double scaleFactor, const QList< QgsLayoutItem * > &ignoreItems, bool &snapped SIP_OUT,
242  QGraphicsLineItem *snapLine = nullptr ) const;
243 
248  bool writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override;
249 
254  bool readXml( const QDomElement &gridElement, const QDomDocument &document, const QgsReadWriteContext &context ) override;
255 
256  private:
257 
258  // Used for 'collapsing' undo commands
259  enum UndoCommand
260  {
261  UndoTolerance = 1,
262  UndoSnapToGrid,
263  UndoSnapToGuides,
264  };
265 
266  QgsLayout *mLayout = nullptr;
267 
268  int mTolerance = 5;
269  bool mSnapToGrid = false;
270  bool mSnapToGuides = true;
271  bool mSnapToItems = true;
272 
273  friend class QgsLayoutSnapperUndoCommand;
274 
275 };
276 
277 #endif //QGSLAYOUTSNAPPER_H
The class is used as a container of context for various read/write operations on other objects...
virtual bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const =0
Stores the objects&#39;s state in a DOM element.
bool snapToGrid() const
Returns true if snapping to grid is enabled.
QString stringType() const override
Returns the object type as a string.
bool snapToItems() const
Returns true if snapping to items is enabled.
virtual QgsLayout * layout()=0
Returns the layout the object belongs to.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
bool snapToGuides() const
Returns true if snapping to guides is enabled.
#define SIP_OUT
Definition: qgis_sip.h:51
An interface for layout objects which can be stored and read from DOM elements.
int snapTolerance() const
Returns the snap tolerance (in pixels) to use when snapping.
virtual bool readXml(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)=0
Sets the objects&#39;s state from a DOM element.
Manages snapping grids and preset snap lines in a layout, and handles snapping points to the nearest ...