QGIS API Documentation 3.37.0-Master (684a802617f)
Loading...
Searching...
No Matches
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 {
31 return QgsLayoutMeasurement( convertToMillimeters( measurement ), Qgis::LayoutUnit::Millimeters );
33 return QgsLayoutMeasurement( convertToCentimeters( measurement ), Qgis::LayoutUnit::Centimeters );
35 return QgsLayoutMeasurement( convertToMeters( measurement ), Qgis::LayoutUnit::Meters );
37 return QgsLayoutMeasurement( convertToInches( measurement ), Qgis::LayoutUnit::Inches );
39 return QgsLayoutMeasurement( convertToFeet( measurement ), Qgis::LayoutUnit::Feet );
41 return QgsLayoutMeasurement( convertToPoints( measurement ), Qgis::LayoutUnit::Points );
43 return QgsLayoutMeasurement( convertToPicas( measurement ), Qgis::LayoutUnit::Picas );
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 {
66 result.setSize( convertToMillimeters( width ), convertToMillimeters( height ) );
67 break;
69 result.setSize( convertToCentimeters( width ), convertToCentimeters( height ) );
70 break;
72 result.setSize( convertToMeters( width ), convertToMeters( height ) );
73 break;
75 result.setSize( convertToInches( width ), convertToInches( height ) );
76 break;
78 result.setSize( convertToFeet( width ), convertToFeet( height ) );
79 break;
81 result.setSize( convertToPoints( width ), convertToPoints( height ) );
82 break;
84 result.setSize( convertToPicas( width ), convertToPicas( height ) );
85 break;
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 {
107 result.setPoint( convertToMillimeters( x ), convertToMillimeters( y ) );
108 break;
110 result.setPoint( convertToCentimeters( x ), convertToCentimeters( y ) );
111 break;
113 result.setPoint( convertToMeters( x ), convertToMeters( y ) );
114 break;
116 result.setPoint( convertToInches( x ), convertToInches( y ) );
117 break;
119 result.setPoint( convertToFeet( x ), convertToFeet( y ) );
120 break;
122 result.setPoint( convertToPoints( x ), convertToPoints( y ) );
123 break;
125 result.setPoint( convertToPicas( x ), convertToPicas( y ) );
126 break;
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 {
139 return measurement.length();
141 return measurement.length() * 10.0;
143 return measurement.length() * 1000.0;
145 return measurement.length() * 25.4;
147 return measurement.length() * 304.8;
149 return measurement.length() * 0.352777778;
151 return measurement.length() * 4.23333333;
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:4509
@ Centimeters
Centimeters.
@ Millimeters
Millimeters.
@ Points
Typographic points.
@ Picas
Typographic picas.
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...
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.
double height() const
Returns the height of the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.