QGIS API Documentation  2.99.0-Master (9fdd060)
qgscomposerruler.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerruler.h
3  ---------------------
4  begin : January 2013
5  copyright : (C) 2013 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 QGSCOMPOSERRULER_H
16 #define QGSCOMPOSERRULER_H
17 
18 #include "qgscomposeritem.h"
19 #include <QWidget>
20 #include "qgis_gui.h"
21 class QgsComposition;
22 class QGraphicsLineItem;
23 
28 class GUI_EXPORT QgsComposerRuler: public QWidget
29 {
30  Q_OBJECT
31 
32  public:
33  enum Direction
34  {
35  Horizontal = 0,
36  Vertical
37  };
38 
41 
42  QSize minimumSizeHint() const override;
43 
44  void setSceneTransform( const QTransform &transform );
45  void updateMarker( QPointF pos ) { mMarkerPos = pos; repaint(); }
46 
47  void setComposition( QgsComposition *c ) { mComposition = c; }
48  QgsComposition *composition() { return mComposition; }
49 
50  int rulerSize() { return mRulerMinSize; }
51 
52  protected:
53  void paintEvent( QPaintEvent *event ) override;
54  void mouseMoveEvent( QMouseEvent *event ) override;
55  void mouseReleaseEvent( QMouseEvent *event ) override;
56  void mousePressEvent( QMouseEvent *event ) override;
57 
58  private:
59  static const int VALID_SCALE_MULTIPLES[];
60  static const int VALID_SCALE_MAGNITUDES[];
61 
62  Direction mDirection;
63  QTransform mTransform;
64  QPointF mMarkerPos;
65  QgsComposition *mComposition = nullptr; //reference to composition for paper size, nPages
66  QGraphicsLineItem *mLineSnapItem = nullptr;
67  //items snapped to the current snap line
68  QList< QPair< QgsComposerItem *, QgsComposerItem::ItemPositionMode > > mSnappedItems;
69 
70  QFont *mRulerFont = nullptr;
71  QFontMetrics *mRulerFontMetrics = nullptr;
72  double mScaleMinPixelsWidth;
73  int mRulerMinSize;
74  int mMinPixelsPerDivision;
75  int mPixelsBetweenLineAndText;
76  int mTextBaseline;
77  int mMinSpacingVerticalLabels;
78 
79  void setSnapLinePosition( QPointF pos );
80 
81  //calculate optimum labeled units for ruler so that labels are a good distance apart
82  int optimumScale( double minPixelDiff, int &magnitude, int &multiple );
83  //calculate number of small divisions for each ruler unit, ensuring that they
84  //are sufficiently spaced
85  int optimumNumberDivisions( double rulerScale, int scaleMultiple );
86 
87  //draws vertical text on a painter
88  void drawRotatedText( QPainter *painter, QPointF pos, const QString &text );
89 
90  /* Draws small ruler divisions
91  * Starting at startPos in mm, for numDivisions divisions, with major division spacing of rulerScale (in mm)
92  * Stop drawing if position exceeds maxPos
93  */
94  void drawSmallDivisions( QPainter *painter, double startPos, int numDivisions, double rulerScale, double maxPos = 0 );
95 
96  //draw current marker pos on ruler
97  void drawMarkerPos( QPainter *painter );
98 
99  signals:
101  void cursorPosChanged( QPointF );
102 
103 };
104 
105 #endif // QGSCOMPOSERRULER_H
void setComposition(QgsComposition *c)
Graphics scene for map printing.
A class to show paper scale and the current cursor position.
void updateMarker(QPointF pos)
QgsComposition * composition()