QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 class QgsComposition;
21 class QGraphicsLineItem;
22 
26 class GUI_EXPORT QgsComposerRuler: public QWidget
27 {
28  Q_OBJECT
29 
30  public:
31  enum Direction
32  {
33  Horizontal = 0,
34  Vertical
35  };
36 
39 
40  QSize minimumSizeHint() const override;
41 
42  void setSceneTransform( const QTransform& transform );
43  void updateMarker( QPointF pos ) { mMarkerPos = pos; repaint(); }
44 
45  void setComposition( QgsComposition* c ) { mComposition = c; }
46  QgsComposition* composition() { return mComposition; }
47 
48  int rulerSize() { return mRulerMinSize; }
49 
50  protected:
51  void paintEvent( QPaintEvent* event ) override;
52  void mouseMoveEvent( QMouseEvent* event ) override;
53  void mouseReleaseEvent( QMouseEvent* event ) override;
54  void mousePressEvent( QMouseEvent* event ) override;
55 
56  private:
57  static const int validScaleMultiples[];
58  static const int validScaleMagnitudes[];
59 
60  Direction mDirection;
61  QTransform mTransform;
62  QPointF mMarkerPos;
63  QgsComposition* mComposition; //reference to composition for paper size, nPages
64  QGraphicsLineItem* mLineSnapItem;
65  //items snapped to the current snap line
66  QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > > mSnappedItems;
67 
68  QFont * mRulerFont;
69  QFontMetrics * mRulerFontMetrics;
70  double mScaleMinPixelsWidth;
71  int mRulerMinSize;
72  int mMinPixelsPerDivision;
73  int mPixelsBetweenLineAndText;
74  int mTextBaseline;
75  int mMinSpacingVerticalLabels;
76 
77  void setSnapLinePosition( QPointF pos );
78 
79  //calculate optimum labeled units for ruler so that labels are a good distance apart
80  int optimumScale( double minPixelDiff, int &magnitude, int &multiple );
81  //calculate number of small divisions for each ruler unit, ensuring that they
82  //are sufficiently spaced
83  int optimumNumberDivisions( double rulerScale, int scaleMultiple );
84 
85  //draws vertical text on a painter
86  void drawRotatedText( QPainter *painter, QPointF pos, const QString &text );
87 
88  /* Draws small ruler divisions
89  * Starting at startPos in mm, for numDivisions divisions, with major division spacing of rulerScale (in mm)
90  * Stop drawing if position exceeds maxPos
91  */
92  void drawSmallDivisions( QPainter *painter, double startPos, int numDivisions, double rulerScale, double maxPos = 0 );
93 
94  //draw current marker pos on ruler
95  void drawMarkerPos( QPainter *painter );
96 
97  signals:
99  void cursorPosChanged( QPointF );
100 
101 };
102 
103 #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()