QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgslayoutmeasurementconverter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutmeasurementconverter.cpp
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
19
20
22{
23 if ( measurement.units() == targetUnits )
24 {
25 return measurement;
26 }
27
28 switch ( targetUnits )
29 {
30 case Qgis::LayoutUnit::Millimeters:
31 return QgsLayoutMeasurement( convertToMillimeters( measurement ), Qgis::LayoutUnit::Millimeters );
32 case Qgis::LayoutUnit::Centimeters:
33 return QgsLayoutMeasurement( convertToCentimeters( measurement ), Qgis::LayoutUnit::Centimeters );
34 case Qgis::LayoutUnit::Meters:
35 return QgsLayoutMeasurement( convertToMeters( measurement ), Qgis::LayoutUnit::Meters );
36 case Qgis::LayoutUnit::Inches:
37 return QgsLayoutMeasurement( convertToInches( measurement ), Qgis::LayoutUnit::Inches );
38 case Qgis::LayoutUnit::Feet:
39 return QgsLayoutMeasurement( convertToFeet( measurement ), Qgis::LayoutUnit::Feet );
40 case Qgis::LayoutUnit::Points:
41 return QgsLayoutMeasurement( convertToPoints( measurement ), Qgis::LayoutUnit::Points );
42 case Qgis::LayoutUnit::Picas:
43 return QgsLayoutMeasurement( convertToPicas( measurement ), Qgis::LayoutUnit::Picas );
44 case Qgis::LayoutUnit::Pixels:
45 return QgsLayoutMeasurement( convertToPixels( measurement ), Qgis::LayoutUnit::Pixels );
46 }
47
48 //will never be reached, but required to prevent warnings
49 return QgsLayoutMeasurement( convertToMillimeters( measurement ), Qgis::LayoutUnit::Millimeters );
50}
51
53{
54 if ( size.units() == targetUnits )
55 {
56 return size;
57 }
58
59 QgsLayoutSize result( size );
60 result.setUnits( targetUnits );
61 const QgsLayoutMeasurement width = QgsLayoutMeasurement( size.width(), size.units() );
62 const QgsLayoutMeasurement height = QgsLayoutMeasurement( size.height(), size.units() );
63 switch ( targetUnits )
64 {
65 case Qgis::LayoutUnit::Millimeters:
66 result.setSize( convertToMillimeters( width ), convertToMillimeters( height ) );
67 break;
68 case Qgis::LayoutUnit::Centimeters:
69 result.setSize( convertToCentimeters( width ), convertToCentimeters( height ) );
70 break;
71 case Qgis::LayoutUnit::Meters:
72 result.setSize( convertToMeters( width ), convertToMeters( height ) );
73 break;
74 case Qgis::LayoutUnit::Inches:
75 result.setSize( convertToInches( width ), convertToInches( height ) );
76 break;
77 case Qgis::LayoutUnit::Feet:
78 result.setSize( convertToFeet( width ), convertToFeet( height ) );
79 break;
80 case Qgis::LayoutUnit::Points:
81 result.setSize( convertToPoints( width ), convertToPoints( height ) );
82 break;
83 case Qgis::LayoutUnit::Picas:
84 result.setSize( convertToPicas( width ), convertToPicas( height ) );
85 break;
86 case Qgis::LayoutUnit::Pixels:
87 result.setSize( convertToPixels( width ), convertToPixels( height ) );
88 break;
89 }
90 return result;
91}
92
94{
95 if ( point.units() == targetUnits )
96 {
97 return point;
98 }
99
100 QgsLayoutPoint result( point );
101 result.setUnits( targetUnits );
102 const QgsLayoutMeasurement x = QgsLayoutMeasurement( point.x(), point.units() );
103 const QgsLayoutMeasurement y = QgsLayoutMeasurement( point.y(), point.units() );
104 switch ( targetUnits )
105 {
106 case Qgis::LayoutUnit::Millimeters:
107 result.setPoint( convertToMillimeters( x ), convertToMillimeters( y ) );
108 break;
109 case Qgis::LayoutUnit::Centimeters:
110 result.setPoint( convertToCentimeters( x ), convertToCentimeters( y ) );
111 break;
112 case Qgis::LayoutUnit::Meters:
113 result.setPoint( convertToMeters( x ), convertToMeters( y ) );
114 break;
115 case Qgis::LayoutUnit::Inches:
116 result.setPoint( convertToInches( x ), convertToInches( y ) );
117 break;
118 case Qgis::LayoutUnit::Feet:
119 result.setPoint( convertToFeet( x ), convertToFeet( y ) );
120 break;
121 case Qgis::LayoutUnit::Points:
122 result.setPoint( convertToPoints( x ), convertToPoints( y ) );
123 break;
124 case Qgis::LayoutUnit::Picas:
125 result.setPoint( convertToPicas( x ), convertToPicas( y ) );
126 break;
127 case Qgis::LayoutUnit::Pixels:
128 result.setPoint( convertToPixels( x ), convertToPixels( y ) );
129 break;
130 }
131 return result;
132}
133
134double QgsLayoutMeasurementConverter::convertToMillimeters( const QgsLayoutMeasurement measurement ) const
135{
136 switch ( measurement.units() )
137 {
138 case Qgis::LayoutUnit::Millimeters:
139 return measurement.length();
140 case Qgis::LayoutUnit::Centimeters:
141 return measurement.length() * 10.0;
142 case Qgis::LayoutUnit::Meters:
143 return measurement.length() * 1000.0;
144 case Qgis::LayoutUnit::Inches:
145 return measurement.length() * 25.4;
146 case Qgis::LayoutUnit::Feet:
147 return measurement.length() * 304.8;
148 case Qgis::LayoutUnit::Points:
149 return measurement.length() * 0.352777778;
150 case Qgis::LayoutUnit::Picas:
151 return measurement.length() * 4.23333333;
152 case Qgis::LayoutUnit::Pixels:
153 return measurement.length() * 25.4 / mDpi;
154 }
155
156 //will never be reached, but required to prevent warnings
157 return measurement.length();
158}
159
160double QgsLayoutMeasurementConverter::convertToCentimeters( const QgsLayoutMeasurement measurement ) const
161{
162 return convertToMillimeters( measurement ) / 10.0;
163}
164
165double QgsLayoutMeasurementConverter::convertToMeters( const QgsLayoutMeasurement measurement ) const
166{
167 return convertToMillimeters( measurement ) / 1000.0;
168}
169
170double QgsLayoutMeasurementConverter::convertToInches( const QgsLayoutMeasurement measurement ) const
171{
172 return convertToMillimeters( measurement ) / 25.4;
173}
174
175double QgsLayoutMeasurementConverter::convertToFeet( const QgsLayoutMeasurement measurement ) const
176{
177 return convertToMillimeters( measurement ) / 304.8;
178}
179
180double QgsLayoutMeasurementConverter::convertToPoints( const QgsLayoutMeasurement measurement ) const
181{
182 return convertToMillimeters( measurement ) * 2.83464567;
183}
184
185double QgsLayoutMeasurementConverter::convertToPicas( const QgsLayoutMeasurement measurement ) const
186{
187 return convertToMillimeters( measurement ) * 0.236220472;
188}
189
190double QgsLayoutMeasurementConverter::convertToPixels( const QgsLayoutMeasurement measurement ) const
191{
192 return convertToMillimeters( measurement ) * mDpi / 25.4;
193}
LayoutUnit
Layout measurement units.
Definition: qgis.h:3196
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, Qgis::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Qgis::LayoutUnit units() const
Returns the units for the measurement.
double length() const
Returns the length of the measurement.
This class provides a method of storing points, consisting of an x and y coordinate,...
double x() const
Returns x coordinate of point.
void setUnits(const Qgis::LayoutUnit units)
Sets the units for the point.
double y() const
Returns y coordinate of point.
Qgis::LayoutUnit units() const
Returns the units for the point.
void setPoint(const double x, const double y)
Sets new x and y coordinates for the point.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:41
void setUnits(const Qgis::LayoutUnit units)
Sets the units for the size.
void setSize(const double width, const double height)
Sets new width and height for the size.
Definition: qgslayoutsize.h:69
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76