|
| 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 | + |
| 18 | +#include "qgslayoutmeasurementconverter.h" |
| 19 | + |
| 20 | + |
| 21 | +QgsLayoutMeasurementConverter::QgsLayoutMeasurementConverter() |
| 22 | + : mDpi( 300.0 ) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +QgsLayoutMeasurementConverter::~QgsLayoutMeasurementConverter() |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +QgsLayoutMeasurement QgsLayoutMeasurementConverter::convert( const QgsLayoutMeasurement &measurement, const QgsUnitTypes::LayoutUnit targetUnits ) const |
| 31 | +{ |
| 32 | + if ( measurement.units() == targetUnits ) |
| 33 | + { |
| 34 | + return measurement; |
| 35 | + } |
| 36 | + |
| 37 | + switch ( targetUnits ) |
| 38 | + { |
| 39 | + case QgsUnitTypes::LayoutMillimeters: |
| 40 | + return QgsLayoutMeasurement( convertToMillimeters( measurement ), QgsUnitTypes::LayoutMillimeters ); |
| 41 | + case QgsUnitTypes::LayoutCentimeters: |
| 42 | + return QgsLayoutMeasurement( convertToCentimeters( measurement ), QgsUnitTypes::LayoutCentimeters ); |
| 43 | + case QgsUnitTypes::LayoutMeters: |
| 44 | + return QgsLayoutMeasurement( convertToMeters( measurement ), QgsUnitTypes::LayoutMeters ); |
| 45 | + case QgsUnitTypes::LayoutInches: |
| 46 | + return QgsLayoutMeasurement( convertToInches( measurement ), QgsUnitTypes::LayoutInches ); |
| 47 | + case QgsUnitTypes::LayoutFeet: |
| 48 | + return QgsLayoutMeasurement( convertToFeet( measurement ), QgsUnitTypes::LayoutFeet ); |
| 49 | + case QgsUnitTypes::LayoutPoints: |
| 50 | + return QgsLayoutMeasurement( convertToPoints( measurement ), QgsUnitTypes::LayoutPoints ); |
| 51 | + case QgsUnitTypes::LayoutPicas: |
| 52 | + return QgsLayoutMeasurement( convertToPicas( measurement ), QgsUnitTypes::LayoutPicas ); |
| 53 | + case QgsUnitTypes::LayoutPixels: |
| 54 | + return QgsLayoutMeasurement( convertToPixels( measurement ), QgsUnitTypes::LayoutPixels ); |
| 55 | + } |
| 56 | + |
| 57 | + //will never be reached, but required to prevent warnings |
| 58 | + return QgsLayoutMeasurement( convertToMillimeters( measurement ), QgsUnitTypes::LayoutMillimeters ); |
| 59 | +} |
| 60 | + |
| 61 | +QgsLayoutSize QgsLayoutMeasurementConverter::convert( const QgsLayoutSize &size, const QgsUnitTypes::LayoutUnit targetUnits ) const |
| 62 | +{ |
| 63 | + if ( size.units() == targetUnits ) |
| 64 | + { |
| 65 | + return size; |
| 66 | + } |
| 67 | + |
| 68 | + QgsLayoutSize result( size ); |
| 69 | + result.setUnits( targetUnits ); |
| 70 | + QgsLayoutMeasurement width = QgsLayoutMeasurement( size.width(), size.units() ); |
| 71 | + QgsLayoutMeasurement height = QgsLayoutMeasurement( size.height(), size.units() ); |
| 72 | + switch ( targetUnits ) |
| 73 | + { |
| 74 | + case QgsUnitTypes::LayoutMillimeters: |
| 75 | + result.setSize( convertToMillimeters( width ), convertToMillimeters( height ) ); |
| 76 | + break; |
| 77 | + case QgsUnitTypes::LayoutCentimeters: |
| 78 | + result.setSize( convertToCentimeters( width ), convertToCentimeters( height ) ); |
| 79 | + break; |
| 80 | + case QgsUnitTypes::LayoutMeters: |
| 81 | + result.setSize( convertToMeters( width ), convertToMeters( height ) ); |
| 82 | + break; |
| 83 | + case QgsUnitTypes::LayoutInches: |
| 84 | + result.setSize( convertToInches( width ), convertToInches( height ) ); |
| 85 | + break; |
| 86 | + case QgsUnitTypes::LayoutFeet: |
| 87 | + result.setSize( convertToFeet( width ), convertToFeet( height ) ); |
| 88 | + break; |
| 89 | + case QgsUnitTypes::LayoutPoints: |
| 90 | + result.setSize( convertToPoints( width ), convertToPoints( height ) ); |
| 91 | + break; |
| 92 | + case QgsUnitTypes::LayoutPicas: |
| 93 | + result.setSize( convertToPicas( width ), convertToPicas( height ) ); |
| 94 | + break; |
| 95 | + case QgsUnitTypes::LayoutPixels: |
| 96 | + result.setSize( convertToPixels( width ), convertToPixels( height ) ); |
| 97 | + break; |
| 98 | + } |
| 99 | + return result; |
| 100 | +} |
| 101 | + |
| 102 | +QgsLayoutPoint QgsLayoutMeasurementConverter::convert( const QgsLayoutPoint &point, const QgsUnitTypes::LayoutUnit targetUnits ) const |
| 103 | +{ |
| 104 | + if ( point.units() == targetUnits ) |
| 105 | + { |
| 106 | + return point; |
| 107 | + } |
| 108 | + |
| 109 | + QgsLayoutPoint result( point ); |
| 110 | + result.setUnits( targetUnits ); |
| 111 | + QgsLayoutMeasurement x = QgsLayoutMeasurement( point.x(), point.units() ); |
| 112 | + QgsLayoutMeasurement y = QgsLayoutMeasurement( point.y(), point.units() ); |
| 113 | + switch ( targetUnits ) |
| 114 | + { |
| 115 | + case QgsUnitTypes::LayoutMillimeters: |
| 116 | + result.setPoint( convertToMillimeters( x ), convertToMillimeters( y ) ); |
| 117 | + break; |
| 118 | + case QgsUnitTypes::LayoutCentimeters: |
| 119 | + result.setPoint( convertToCentimeters( x ), convertToCentimeters( y ) ); |
| 120 | + break; |
| 121 | + case QgsUnitTypes::LayoutMeters: |
| 122 | + result.setPoint( convertToMeters( x ), convertToMeters( y ) ); |
| 123 | + break; |
| 124 | + case QgsUnitTypes::LayoutInches: |
| 125 | + result.setPoint( convertToInches( x ), convertToInches( y ) ); |
| 126 | + break; |
| 127 | + case QgsUnitTypes::LayoutFeet: |
| 128 | + result.setPoint( convertToFeet( x ), convertToFeet( y ) ); |
| 129 | + break; |
| 130 | + case QgsUnitTypes::LayoutPoints: |
| 131 | + result.setPoint( convertToPoints( x ), convertToPoints( y ) ); |
| 132 | + break; |
| 133 | + case QgsUnitTypes::LayoutPicas: |
| 134 | + result.setPoint( convertToPicas( x ), convertToPicas( y ) ); |
| 135 | + break; |
| 136 | + case QgsUnitTypes::LayoutPixels: |
| 137 | + result.setPoint( convertToPixels( x ), convertToPixels( y ) ); |
| 138 | + break; |
| 139 | + } |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +double QgsLayoutMeasurementConverter::convertToMillimeters( const QgsLayoutMeasurement &measurement ) const |
| 144 | +{ |
| 145 | + switch ( measurement.units() ) |
| 146 | + { |
| 147 | + case QgsUnitTypes::LayoutMillimeters: |
| 148 | + return measurement.length(); |
| 149 | + case QgsUnitTypes::LayoutCentimeters: |
| 150 | + return measurement.length() * 10.0; |
| 151 | + case QgsUnitTypes::LayoutMeters: |
| 152 | + return measurement.length() * 1000.0; |
| 153 | + case QgsUnitTypes::LayoutInches: |
| 154 | + return measurement.length() * 25.4; |
| 155 | + case QgsUnitTypes::LayoutFeet: |
| 156 | + return measurement.length() * 304.8; |
| 157 | + case QgsUnitTypes::LayoutPoints: |
| 158 | + return measurement.length() * 0.352777778; |
| 159 | + case QgsUnitTypes::LayoutPicas: |
| 160 | + return measurement.length() * 4.23333333; |
| 161 | + case QgsUnitTypes::LayoutPixels: |
| 162 | + return measurement.length() * 25.4 / mDpi; |
| 163 | + } |
| 164 | + |
| 165 | + //will never be reached, but required to prevent warnings |
| 166 | + return measurement.length(); |
| 167 | +} |
| 168 | + |
| 169 | +double QgsLayoutMeasurementConverter::convertToCentimeters( const QgsLayoutMeasurement &measurement ) const |
| 170 | +{ |
| 171 | + return convertToMillimeters( measurement ) / 10.0; |
| 172 | +} |
| 173 | + |
| 174 | +double QgsLayoutMeasurementConverter::convertToMeters( const QgsLayoutMeasurement &measurement ) const |
| 175 | +{ |
| 176 | + return convertToMillimeters( measurement ) / 1000.0; |
| 177 | +} |
| 178 | + |
| 179 | +double QgsLayoutMeasurementConverter::convertToInches( const QgsLayoutMeasurement &measurement ) const |
| 180 | +{ |
| 181 | + return convertToMillimeters( measurement ) / 25.4; |
| 182 | +} |
| 183 | + |
| 184 | +double QgsLayoutMeasurementConverter::convertToFeet( const QgsLayoutMeasurement &measurement ) const |
| 185 | +{ |
| 186 | + return convertToMillimeters( measurement ) / 304.8; |
| 187 | +} |
| 188 | + |
| 189 | +double QgsLayoutMeasurementConverter::convertToPoints( const QgsLayoutMeasurement &measurement ) const |
| 190 | +{ |
| 191 | + return convertToMillimeters( measurement ) * 2.83464567; |
| 192 | +} |
| 193 | + |
| 194 | +double QgsLayoutMeasurementConverter::convertToPicas( const QgsLayoutMeasurement &measurement ) const |
| 195 | +{ |
| 196 | + return convertToMillimeters( measurement ) * 0.236220472; |
| 197 | +} |
| 198 | + |
| 199 | +double QgsLayoutMeasurementConverter::convertToPixels( const QgsLayoutMeasurement &measurement ) const |
| 200 | +{ |
| 201 | + return convertToMillimeters( measurement ) * mDpi / 25.4; |
| 202 | +} |
0 commit comments