QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgslayoutruler.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutruler.h
3  ----------------
4  Date : July 2017
5  Copyright : (C) 2017 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSLAYOUTRULER_H
16 #define QGSLAYOUTRULER_H
17 
18 #include "qgis_gui.h"
19 #include "qgis_sip.h"
20 #include <QWidget>
21 #include <QPointer>
22 #include <QMenu>
23 #include <memory>
24 
25 class QgsLayout;
26 class QGraphicsLineItem;
27 class QgsLayoutView;
28 class QgsLayoutGuide;
29 
38 class GUI_EXPORT QgsLayoutRuler: public QWidget
39 {
40  Q_OBJECT
41 
42  public:
43 
47  explicit QgsLayoutRuler( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::Orientation orientation = Qt::Horizontal );
48 
49  QSize minimumSizeHint() const override;
50 
56  void setSceneTransform( const QTransform &transform );
57 
62  QgsLayoutView *layoutView() { return mView; }
63 
68  void setLayoutView( QgsLayoutView *view );
69 
74  int rulerSize() const { return mRulerMinSize; }
75 
80  void setContextMenu( QMenu *menu );
81 
82  public slots:
83 
89  void setCursorPosition( QPointF position );
90 
91  signals:
93  void cursorPosChanged( QPointF );
94 
95  protected:
96  void paintEvent( QPaintEvent *event ) override;
97  void mouseMoveEvent( QMouseEvent *event ) override;
98  void mousePressEvent( QMouseEvent *event ) override;
99  void mouseReleaseEvent( QMouseEvent *event ) override;
100 
101  private:
102  static const int VALID_SCALE_MULTIPLES[];
103  static const int VALID_SCALE_MAGNITUDES[];
104 
105  Qt::Orientation mOrientation = Qt::Horizontal;
106  QgsLayoutView *mView = nullptr;
107 
108  QTransform mTransform;
109  QPoint mMarkerPos;
110 
111  QFont mRulerFont;
112  std::unique_ptr< QFontMetrics > mRulerFontMetrics;
113 
114  double mScaleMinPixelsWidth = 0.0;
115  int mRulerMinSize;
116  int mMinPixelsPerDivision;
117  int mPixelsBetweenLineAndText;
118  int mTextBaseline;
119  int mMinSpacingVerticalLabels;
120 
121  int mDragGuideTolerance = 0;
122  QgsLayoutGuide *mDraggingGuide = nullptr;
123  QgsLayoutGuide *mHoverGuide = nullptr;
124 
125  bool mCreatingGuide = false;
126  QGraphicsLineItem *mGuideItem = nullptr;
127 
129  QPolygonF mGuideMarker;
130 
131  QPointer< QMenu > mMenu;
132 
134  int optimumScale( double minPixelDiff, int &magnitude, int &multiple );
135 
140  int optimumNumberDivisions( double rulerScale, int scaleMultiple );
141 
143  void drawRotatedText( QPainter *painter, QPointF pos, const QString &text );
144 
150  void drawSmallDivisions( QPainter *painter, double startPos, int numDivisions, double rulerScale, double maxPos = 0 );
151 
153  void drawMarkerPos( QPainter *painter );
154 
155  void drawGuideMarkers( QPainter *painter, QgsLayout *layout );
156 
158  void drawGuideAtPos( QPainter *painter, QPoint pos );
159 
160  void createTemporaryGuideItem();
161 
162  QPointF convertLocalPointToLayout( QPoint localPoint ) const;
163 
164  QPoint convertLayoutPointToLocal( QPointF layoutPoint ) const;
165 
170  QgsLayoutGuide *guideAtPoint( QPoint localPoint ) const;
171 
172 };
173 
174 #endif // QGSLAYOUTRULER_H
A custom ruler widget for use with QgsLayoutView, displaying the current zoom and position of the vis...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:49
Contains the configuration for a single snap guide used by a layout.
int rulerSize() const
Returns the ruler size (either the height of a horizontal ruler or the width of a vertical rule)...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QgsLayoutView * layoutView()
Returns the current layout view associated with the ruler.