QGIS API Documentation  3.6.0-Noosa (5873452)
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  Q_FOREACH ( const QgsPageSize &pageSize, mPageSizes )
96  {
97  // convert passed size to same units
98  QgsLayoutSize xSize = converter.convert( size, pageSize.size.units() );
99 
100  //consider width and height values may be exchanged
101  if ( ( qgsDoubleNear( xSize.width(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.height(), pageSize.size.height(), 0.01 ) )
102  || ( qgsDoubleNear( xSize.height(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.width(), pageSize.size.height(), 0.01 ) ) )
103  {
104  return pageSize.name;
105  }
106  }
107  return QString();
108 }
109 
110 bool QgsPageSizeRegistry::decodePageSize( const QString &pageSizeName, QgsPageSize &pageSize )
111 {
112  QList< QgsPageSize > matches = find( pageSizeName.trimmed() );
113  if ( matches.length() > 0 )
114  {
115  pageSize = matches.at( 0 );
116  return true;
117  }
118  return false;
119 }
120 
121 //
122 // QgsPageSize
123 //
124 
126  : size( QgsLayoutSize( 0.0, 0.0 ) )
127 {
128 }
129 
130 QgsPageSize::QgsPageSize( const QString &pageName, const QgsLayoutSize &pageSize, const QString &displayName )
131  : name( pageName )
132  , size( pageSize )
133  , displayName( displayName )
134 {
135 }
136 
138  : size( pageSize )
139 {
140 
141 }
142 
143 bool QgsPageSize::operator==( const QgsPageSize &other ) const
144 {
145  return ( name == other.name && size == other.size );
146 }
147 
148 bool QgsPageSize::operator!=( const QgsPageSize &other ) const
149 {
150  return ( ! operator==( other ) );
151 }
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:265
QList< QgsPageSize > entries() const
Returns a list of page sizes in the registry.
QList< QgsPageSize > find(const QString &name) const
Finds matching page sizes from the registry, using a case insensitive match on the page size name...
bool decodePageSize(const QString &string, QgsPageSize &size)
Decodes a string representing a preset page size.
QgsPageSizeRegistry()
Creates a registry and populates it with known sizes.
QString name
Name of page size.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
bool operator!=(const QgsPageSize &other) const
A named page size for layouts.
void add(const QgsPageSize &size)
Adds a page size to the registry.
This class provides a method of converting QgsLayoutMeasurements from one unit to another...
bool operator==(const QgsPageSize &other) const
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
QgsLayoutSize size
Page size.
QString displayName
Translated page name.
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76