QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgspagesizeregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspagesizeregistry.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  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgspagesizeregistry.h"
19 #include "qgis.h"
20 
21 //
22 // QgsPageSizeRegistry
23 //
24 
26 {
27  add( QgsPageSize( QStringLiteral( "A6" ), QgsLayoutSize( 105, 148 ), QObject::tr( "A6" ) ) );
28  add( QgsPageSize( QStringLiteral( "A5" ), QgsLayoutSize( 148, 210 ), QObject::tr( "A5" ) ) );
29  add( QgsPageSize( QStringLiteral( "A4" ), QgsLayoutSize( 210, 297 ), QObject::tr( "A4" ) ) );
30  add( QgsPageSize( QStringLiteral( "A3" ), QgsLayoutSize( 297, 420 ), QObject::tr( "A3" ) ) );
31  add( QgsPageSize( QStringLiteral( "A2" ), QgsLayoutSize( 420, 594 ), QObject::tr( "A2" ) ) );
32  add( QgsPageSize( QStringLiteral( "A1" ), QgsLayoutSize( 594, 841 ), QObject::tr( "A1" ) ) );
33  add( QgsPageSize( QStringLiteral( "A0" ), QgsLayoutSize( 841, 1189 ), QObject::tr( "A0" ) ) );
34  add( QgsPageSize( QStringLiteral( "B6" ), QgsLayoutSize( 125, 176 ), QObject::tr( "B6" ) ) );
35  add( QgsPageSize( QStringLiteral( "B5" ), QgsLayoutSize( 176, 250 ), QObject::tr( "B5" ) ) );
36  add( QgsPageSize( QStringLiteral( "B4" ), QgsLayoutSize( 250, 353 ), QObject::tr( "B4" ) ) );
37  add( QgsPageSize( QStringLiteral( "B3" ), QgsLayoutSize( 353, 500 ), QObject::tr( "B3" ) ) );
38  add( QgsPageSize( QStringLiteral( "B2" ), QgsLayoutSize( 500, 707 ), QObject::tr( "B2" ) ) );
39  add( QgsPageSize( QStringLiteral( "B1" ), QgsLayoutSize( 707, 1000 ), QObject::tr( "B1" ) ) );
40  add( QgsPageSize( QStringLiteral( "B0" ), QgsLayoutSize( 1000, 1414 ), QObject::tr( "B0" ) ) );
41  add( QgsPageSize( QStringLiteral( "Legal" ), QgsLayoutSize( 215.9, 355.6 ), QObject::tr( "Legal" ) ) );
42  add( QgsPageSize( QStringLiteral( "Letter" ), QgsLayoutSize( 215.9, 279.4 ), QObject::tr( "Letter" ) ) );
43  add( QgsPageSize( QStringLiteral( "ANSI A" ), QgsLayoutSize( 215.9, 279.4 ), QObject::tr( "ANSI A" ) ) );
44  add( QgsPageSize( QStringLiteral( "ANSI B" ), QgsLayoutSize( 279.4, 431.8 ), QObject::tr( "ANSI B" ) ) );
45  add( QgsPageSize( QStringLiteral( "ANSI C" ), QgsLayoutSize( 431.8, 558.8 ), QObject::tr( "ANSI C" ) ) );
46  add( QgsPageSize( QStringLiteral( "ANSI D" ), QgsLayoutSize( 558.8, 863.6 ), QObject::tr( "ANSI D" ) ) );
47  add( QgsPageSize( QStringLiteral( "ANSI E" ), QgsLayoutSize( 863.6, 1117.6 ), QObject::tr( "ANSI E" ) ) );
48  add( QgsPageSize( QStringLiteral( "Arch A" ), QgsLayoutSize( 228.6, 304.8 ), QObject::tr( "Arch A" ) ) );
49  add( QgsPageSize( QStringLiteral( "Arch B" ), QgsLayoutSize( 304.8, 457.2 ), QObject::tr( "Arch B" ) ) );
50  add( QgsPageSize( QStringLiteral( "Arch C" ), QgsLayoutSize( 457.2, 609.6 ), QObject::tr( "Arch C" ) ) );
51  add( QgsPageSize( QStringLiteral( "Arch D" ), QgsLayoutSize( 609.6, 914.4 ), QObject::tr( "Arch D" ) ) );
52  add( QgsPageSize( QStringLiteral( "Arch E" ), QgsLayoutSize( 914.4, 1219.2 ), QObject::tr( "Arch E" ) ) );
53  add( QgsPageSize( QStringLiteral( "Arch E1" ), QgsLayoutSize( 762, 1066.8 ), QObject::tr( "Arch E1" ) ) );
54  add( QgsPageSize( QStringLiteral( "Arch E2" ), QgsLayoutSize( 660, 965 ), QObject::tr( "Arch E2" ) ) );
55  add( QgsPageSize( QStringLiteral( "Arch E3" ), QgsLayoutSize( 686, 991 ), QObject::tr( "Arch E3" ) ) );
56  add( QgsPageSize( QStringLiteral( "1920x1080" ), QgsLayoutSize( 1080, 1920, QgsUnitTypes::LayoutPixels ), QObject::tr( "1920×1080" ) ) );
57  add( QgsPageSize( QStringLiteral( "1280x800" ), QgsLayoutSize( 800, 1280, QgsUnitTypes::LayoutPixels ), QObject::tr( "1280×800" ) ) );
58  add( QgsPageSize( QStringLiteral( "1024x768" ), QgsLayoutSize( 768, 1024, QgsUnitTypes::LayoutPixels ), QObject::tr( "1024×768" ) ) );
59 }
60 
62 {
63  mPageSizes.append( size );
64 }
65 
66 QList<QgsPageSize> QgsPageSizeRegistry::entries() const
67 {
68  QList< QgsPageSize > result;
69  QList< QgsPageSize >::const_iterator it = mPageSizes.constBegin();
70  for ( ; it != mPageSizes.constEnd(); ++it )
71  {
72  result.push_back( *it );
73  }
74  return result;
75 }
76 
77 QList<QgsPageSize> QgsPageSizeRegistry::find( const QString &name ) const
78 {
79  QList< QgsPageSize > result;
80  QList< QgsPageSize >::const_iterator it = mPageSizes.constBegin();
81  for ( ; it != mPageSizes.constEnd(); ++it )
82  {
83  if ( ( *it ).name.compare( name, Qt::CaseInsensitive ) == 0 )
84  {
85  result.push_back( *it );
86  }
87  }
88  return result;
89 }
90 
91 QString QgsPageSizeRegistry::find( const QgsLayoutSize &size ) const
92 {
93  //try to match to existing page size
95  const auto constMPageSizes = mPageSizes;
96  for ( const QgsPageSize &pageSize : constMPageSizes )
97  {
98  // convert passed size to same units
99  QgsLayoutSize xSize = converter.convert( size, pageSize.size.units() );
100 
101  //consider width and height values may be exchanged
102  if ( ( qgsDoubleNear( xSize.width(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.height(), pageSize.size.height(), 0.01 ) )
103  || ( qgsDoubleNear( xSize.height(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.width(), pageSize.size.height(), 0.01 ) ) )
104  {
105  return pageSize.name;
106  }
107  }
108  return QString();
109 }
110 
111 bool QgsPageSizeRegistry::decodePageSize( const QString &pageSizeName, QgsPageSize &pageSize )
112 {
113  QList< QgsPageSize > matches = find( pageSizeName.trimmed() );
114  if ( matches.length() > 0 )
115  {
116  pageSize = matches.at( 0 );
117  return true;
118  }
119  return false;
120 }
121 
122 //
123 // QgsPageSize
124 //
125 
127  : size( QgsLayoutSize( 0.0, 0.0 ) )
128 {
129 }
130 
131 QgsPageSize::QgsPageSize( const QString &pageName, const QgsLayoutSize &pageSize, const QString &displayName )
132  : name( pageName )
133  , size( pageSize )
134  , displayName( displayName )
135 {
136 }
137 
139  : size( pageSize )
140 {
141 
142 }
143 
144 bool QgsPageSize::operator==( const QgsPageSize &other ) const
145 {
146  return ( name == other.name && size == other.size );
147 }
148 
149 bool QgsPageSize::operator!=( const QgsPageSize &other ) const
150 {
151  return ( ! operator==( other ) );
152 }
QgsLayoutSize::width
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76
QgsLayoutSize::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
Definition: qgslayoutsize.h:103
QgsPageSizeRegistry::decodePageSize
bool decodePageSize(const QString &string, QgsPageSize &size)
Decodes a string representing a preset page size.
Definition: qgspagesizeregistry.cpp:111
QgsUnitTypes::LayoutPixels
@ LayoutPixels
Pixels.
Definition: qgsunittypes.h:189
QgsPageSizeRegistry::entries
QList< QgsPageSize > entries() const
Returns a list of page sizes in the registry.
Definition: qgspagesizeregistry.cpp:66
QgsPageSizeRegistry::QgsPageSizeRegistry
QgsPageSizeRegistry()
Creates a registry and populates it with known sizes.
Definition: qgspagesizeregistry.cpp:25
qgis.h
QgsLayoutMeasurementConverter::convert
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
Definition: qgslayoutmeasurementconverter.cpp:21
QgsPageSizeRegistry::find
QList< QgsPageSize > find(const QString &name) const
Finds matching page sizes from the registry, using a case insensitive match on the page size name.
Definition: qgspagesizeregistry.cpp:77
qgslayoutmeasurementconverter.h
QgsPageSize
A named page size for layouts.
Definition: qgspagesizeregistry.h:32
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsPageSize::QgsPageSize
QgsPageSize()
Definition: qgspagesizeregistry.cpp:126
QgsPageSize::operator!=
bool operator!=(const QgsPageSize &other) const
Definition: qgspagesizeregistry.cpp:149
qgspagesizeregistry.h
QgsLayoutMeasurementConverter
This class provides a method of converting QgsLayoutMeasurements from one unit to another....
Definition: qgslayoutmeasurementconverter.h:40
QgsLayoutSize::height
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
QgsPageSize::name
QString name
Name of page size.
Definition: qgspagesizeregistry.h:51
QgsPageSize::operator==
bool operator==(const QgsPageSize &other) const
Definition: qgspagesizeregistry.cpp:144
QgsLayoutSize
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
QgsPageSize::size
QgsLayoutSize size
Page size.
Definition: qgspagesizeregistry.h:54
QgsPageSizeRegistry::add
void add(const QgsPageSize &size)
Adds a page size to the registry.
Definition: qgspagesizeregistry.cpp:61