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