1 /***************************************************************************
2                          qgslayoutmeasurementconverter.h
3                          -------------------------------
4     begin                : June 2017
5     copyright            : (C) 2017 by Nyall Dawson
6     email                : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef QGSLAYOUTMEASUREMENTCONVERTER_H
19 #define QGSLAYOUTMEASUREMENTCONVERTER_H
20 
21 #include "qgis_core.h"
22 #include "qgsunittypes.h"
23 #include "qgslayoutmeasurement.h"
24 #include "qgslayoutsize.h"
25 #include "qgslayoutpoint.h"
26 #include <QSizeF>
27 #include <QPointF>
28 
29 
30 /**
31  * \ingroup core
32  * \class QgsLayoutMeasurementConverter
33  * \brief This class provides a method of converting QgsLayoutMeasurements from
34  * one unit to another. Conversion to or from pixel units utilizes a specified
35  * dots per inch (DPI) property for the converter. Converters default to using
36  * 300 DPI.
37  * \see QgsLayoutMeasurement
38  * \since QGIS 3.0
39  */
40 class CORE_EXPORT QgsLayoutMeasurementConverter
41 {
42   public:
43 
44     /**
45      * Constructor for QgsLayoutMeasurementConverter.
46      */
47     QgsLayoutMeasurementConverter() = default;
48 
49     /**
50      * Sets the dots per inch (\a dpi) for the measurement converter. This is used
51      * when converting measurements to and from pixels.
52      * \see dpi()
53     */
setDpi(const double dpi)54     void setDpi( const double dpi ) { mDpi = dpi; }
55 
56     /**
57      * Returns the Dots per inch (DPI) of the measurement converter. This is used
58      * when converting measurements to and from pixels.
59      * \see setDpi()
60     */
dpi()61     double dpi() const { return mDpi; }
62 
63     /**
64      * Converts a measurement from one unit to another.
65      * \param measurement measurement to convert
66      * \param targetUnits units to convert measurement into
67      * \returns measurement converted to target units
68     */
69     QgsLayoutMeasurement convert( QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits ) const;
70 
71     /**
72      * Converts a layout size from one unit to another.
73      * \param size layout size to convert
74      * \param targetUnits units to convert size into
75      * \returns size converted to target units
76     */
77     QgsLayoutSize convert( const QgsLayoutSize &size, QgsUnitTypes::LayoutUnit targetUnits ) const;
78 
79     /**
80      * Converts a layout point from one unit to another.
81      * \param point layout point to convert
82      * \param targetUnits units to convert point into
83      * \returns point converted to target units
84     */
85     QgsLayoutPoint convert( const QgsLayoutPoint &point, QgsUnitTypes::LayoutUnit targetUnits ) const;
86 
87   private:
88 
89     double mDpi = 300.0;
90 
91     double convertToMillimeters( QgsLayoutMeasurement measurement ) const;
92     double convertToCentimeters( QgsLayoutMeasurement measurement ) const;
93     double convertToMeters( QgsLayoutMeasurement measurement ) const;
94     double convertToInches( QgsLayoutMeasurement measurement ) const;
95     double convertToFeet( QgsLayoutMeasurement measurement ) const;
96     double convertToPoints( QgsLayoutMeasurement measurement ) const;
97     double convertToPicas( QgsLayoutMeasurement measurement ) const;
98     double convertToPixels( QgsLayoutMeasurement measurement ) const;
99 
100 };
101 
102 #endif // QGSLAYOUTMEASUREMENTCONVERTER_H
103