QGIS API Documentation  3.6.0-Noosa (5873452)
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 ), QgsUnitTypes::LayoutMillimeters );
33  return QgsLayoutMeasurement( convertToCentimeters( measurement ), QgsUnitTypes::LayoutCentimeters );
35  return QgsLayoutMeasurement( convertToMeters( measurement ), QgsUnitTypes::LayoutMeters );
37  return QgsLayoutMeasurement( convertToInches( measurement ), QgsUnitTypes::LayoutInches );
39  return QgsLayoutMeasurement( convertToFeet( measurement ), QgsUnitTypes::LayoutFeet );
41  return QgsLayoutMeasurement( convertToPoints( measurement ), QgsUnitTypes::LayoutPoints );
43  return QgsLayoutMeasurement( convertToPicas( measurement ), QgsUnitTypes::LayoutPicas );
45  return QgsLayoutMeasurement( convertToPixels( measurement ), QgsUnitTypes::LayoutPixels );
46  }
47 
48  //will never be reached, but required to prevent warnings
49  return QgsLayoutMeasurement( convertToMillimeters( measurement ), QgsUnitTypes::LayoutMillimeters );
50 }
51 
53 {
54  if ( size.units() == targetUnits )
55  {
56  return size;
57  }
58 
59  QgsLayoutSize result( size );
60  result.setUnits( targetUnits );
61  QgsLayoutMeasurement width = QgsLayoutMeasurement( size.width(), size.units() );
62  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  QgsLayoutMeasurement x = QgsLayoutMeasurement( point.x(), point.units() );
103  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 
134 double 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 
160 double QgsLayoutMeasurementConverter::convertToCentimeters( const QgsLayoutMeasurement measurement ) const
161 {
162  return convertToMillimeters( measurement ) / 10.0;
163 }
164 
165 double QgsLayoutMeasurementConverter::convertToMeters( const QgsLayoutMeasurement measurement ) const
166 {
167  return convertToMillimeters( measurement ) / 1000.0;
168 }
169 
170 double QgsLayoutMeasurementConverter::convertToInches( const QgsLayoutMeasurement measurement ) const
171 {
172  return convertToMillimeters( measurement ) / 25.4;
173 }
174 
175 double QgsLayoutMeasurementConverter::convertToFeet( const QgsLayoutMeasurement measurement ) const
176 {
177  return convertToMillimeters( measurement ) / 304.8;
178 }
179 
180 double QgsLayoutMeasurementConverter::convertToPoints( const QgsLayoutMeasurement measurement ) const
181 {
182  return convertToMillimeters( measurement ) * 2.83464567;
183 }
184 
185 double QgsLayoutMeasurementConverter::convertToPicas( const QgsLayoutMeasurement measurement ) const
186 {
187  return convertToMillimeters( measurement ) * 0.236220472;
188 }
189 
190 double QgsLayoutMeasurementConverter::convertToPixels( const QgsLayoutMeasurement measurement ) const
191 {
192  return convertToMillimeters( measurement ) * mDpi / 25.4;
193 }
QgsUnitTypes::LayoutUnit units() const
Returns the units for the measurement.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
void setPoint(const double x, const double y)
Sets new x and y coordinates for the point.
Typographic points.
Definition: qgsunittypes.h:132
This class provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
double y() const
Returns y coordinate of point.
double x() const
Returns x coordinate of point.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
double length() const
Returns the length of the measurement.
void setUnits(const QgsUnitTypes::LayoutUnit units)
Sets the units for the point.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:125
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
void setSize(const double width, const double height)
Sets new width and height for the size.
Definition: qgslayoutsize.h:69
Typographic picas.
Definition: qgsunittypes.h:133
void setUnits(const QgsUnitTypes::LayoutUnit units)
Sets the units for the size.
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76