QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
20 //
21 // QgsPageSizeRegistry
22 //
23 
25 {
26  add( QgsPageSize( QStringLiteral( "A6" ), QgsLayoutSize( 105, 148 ), QObject::tr( "A6" ) ) );
27  add( QgsPageSize( QStringLiteral( "A5" ), QgsLayoutSize( 148, 210 ), QObject::tr( "A5" ) ) );
28  add( QgsPageSize( QStringLiteral( "A4" ), QgsLayoutSize( 210, 297 ), QObject::tr( "A4" ) ) );
29  add( QgsPageSize( QStringLiteral( "A3" ), QgsLayoutSize( 297, 420 ), QObject::tr( "A3" ) ) );
30  add( QgsPageSize( QStringLiteral( "A2" ), QgsLayoutSize( 420, 594 ), QObject::tr( "A2" ) ) );
31  add( QgsPageSize( QStringLiteral( "A1" ), QgsLayoutSize( 594, 841 ), QObject::tr( "A1" ) ) );
32  add( QgsPageSize( QStringLiteral( "A0" ), QgsLayoutSize( 841, 1189 ), QObject::tr( "A0" ) ) );
33  add( QgsPageSize( QStringLiteral( "B6" ), QgsLayoutSize( 125, 176 ), QObject::tr( "B6" ) ) );
34  add( QgsPageSize( QStringLiteral( "B5" ), QgsLayoutSize( 176, 250 ), QObject::tr( "B5" ) ) );
35  add( QgsPageSize( QStringLiteral( "B4" ), QgsLayoutSize( 250, 353 ), QObject::tr( "B4" ) ) );
36  add( QgsPageSize( QStringLiteral( "B3" ), QgsLayoutSize( 353, 500 ), QObject::tr( "B3" ) ) );
37  add( QgsPageSize( QStringLiteral( "B2" ), QgsLayoutSize( 500, 707 ), QObject::tr( "B2" ) ) );
38  add( QgsPageSize( QStringLiteral( "B1" ), QgsLayoutSize( 707, 1000 ), QObject::tr( "B1" ) ) );
39  add( QgsPageSize( QStringLiteral( "B0" ), QgsLayoutSize( 1000, 1414 ), QObject::tr( "B0" ) ) );
40  add( QgsPageSize( QStringLiteral( "Legal" ), QgsLayoutSize( 215.9, 355.6 ), QObject::tr( "Legal" ) ) );
41  add( QgsPageSize( QStringLiteral( "Letter" ), QgsLayoutSize( 215.9, 279.4 ), QObject::tr( "Letter" ) ) );
42  add( QgsPageSize( QStringLiteral( "ANSI A" ), QgsLayoutSize( 215.9, 279.4 ), QObject::tr( "ANSI A" ) ) );
43  add( QgsPageSize( QStringLiteral( "ANSI B" ), QgsLayoutSize( 279.4, 431.8 ), QObject::tr( "ANSI B" ) ) );
44  add( QgsPageSize( QStringLiteral( "ANSI C" ), QgsLayoutSize( 431.8, 558.8 ), QObject::tr( "ANSI C" ) ) );
45  add( QgsPageSize( QStringLiteral( "ANSI D" ), QgsLayoutSize( 558.8, 863.6 ), QObject::tr( "ANSI D" ) ) );
46  add( QgsPageSize( QStringLiteral( "ANSI E" ), QgsLayoutSize( 863.6, 1117.6 ), QObject::tr( "ANSI E" ) ) );
47  add( QgsPageSize( QStringLiteral( "Arch A" ), QgsLayoutSize( 228.6, 304.8 ), QObject::tr( "Arch A" ) ) );
48  add( QgsPageSize( QStringLiteral( "Arch B" ), QgsLayoutSize( 304.8, 457.2 ), QObject::tr( "Arch B" ) ) );
49  add( QgsPageSize( QStringLiteral( "Arch C" ), QgsLayoutSize( 457.2, 609.6 ), QObject::tr( "Arch C" ) ) );
50  add( QgsPageSize( QStringLiteral( "Arch D" ), QgsLayoutSize( 609.6, 914.4 ), QObject::tr( "Arch D" ) ) );
51  add( QgsPageSize( QStringLiteral( "Arch E" ), QgsLayoutSize( 914.4, 1219.2 ), QObject::tr( "Arch E" ) ) );
52  add( QgsPageSize( QStringLiteral( "Arch E1" ), QgsLayoutSize( 762, 1066.8 ), QObject::tr( "Arch E1" ) ) );
53  add( QgsPageSize( QStringLiteral( "Arch E2" ), QgsLayoutSize( 660, 965 ), QObject::tr( "Arch E2" ) ) );
54  add( QgsPageSize( QStringLiteral( "Arch E3" ), QgsLayoutSize( 686, 991 ), QObject::tr( "Arch E3" ) ) );
55  add( QgsPageSize( QStringLiteral( "1920x1080" ), QgsLayoutSize( 1080, 1920, QgsUnitTypes::LayoutPixels ), QObject::tr( "1920×1080" ) ) );
56  add( QgsPageSize( QStringLiteral( "1280x800" ), QgsLayoutSize( 800, 1280, QgsUnitTypes::LayoutPixels ), QObject::tr( "1280×800" ) ) );
57  add( QgsPageSize( QStringLiteral( "1024x768" ), QgsLayoutSize( 768, 1024, QgsUnitTypes::LayoutPixels ), QObject::tr( "1024×768" ) ) );
58 }
59 
61 {
62  mPageSizes.append( size );
63 }
64 
65 QList<QgsPageSize> QgsPageSizeRegistry::entries() const
66 {
67  QList< QgsPageSize > result;
68  QList< QgsPageSize >::const_iterator it = mPageSizes.constBegin();
69  for ( ; it != mPageSizes.constEnd(); ++it )
70  {
71  result.push_back( *it );
72  }
73  return result;
74 }
75 
76 QList<QgsPageSize> QgsPageSizeRegistry::find( const QString &name ) const
77 {
78  QList< QgsPageSize > result;
79  QList< QgsPageSize >::const_iterator it = mPageSizes.constBegin();
80  for ( ; it != mPageSizes.constEnd(); ++it )
81  {
82  if ( ( *it ).name.compare( name, Qt::CaseInsensitive ) == 0 )
83  {
84  result.push_back( *it );
85  }
86  }
87  return result;
88 }
89 
90 QString QgsPageSizeRegistry::find( const QgsLayoutSize &size ) const
91 {
92  //try to match to existing page size
94  Q_FOREACH ( const QgsPageSize &pageSize, mPageSizes )
95  {
96  // convert passed size to same units
97  QgsLayoutSize xSize = converter.convert( size, pageSize.size.units() );
98 
99  //consider width and height values may be exchanged
100  if ( ( qgsDoubleNear( xSize.width(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.height(), pageSize.size.height(), 0.01 ) )
101  || ( qgsDoubleNear( xSize.height(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.width(), pageSize.size.height(), 0.01 ) ) )
102  {
103  return pageSize.name;
104  }
105  }
106  return QString();
107 }
108 
109 bool QgsPageSizeRegistry::decodePageSize( const QString &pageSizeName, QgsPageSize &pageSize )
110 {
111  QList< QgsPageSize > matches = find( pageSizeName.trimmed() );
112  if ( matches.length() > 0 )
113  {
114  pageSize = matches.at( 0 );
115  return true;
116  }
117  return false;
118 }
119 
120 //
121 // QgsPageSize
122 //
123 
125  : size( QgsLayoutSize( 0.0, 0.0 ) )
126 {
127 }
128 
129 QgsPageSize::QgsPageSize( const QString &pageName, const QgsLayoutSize &pageSize, const QString &displayName )
130  : name( pageName )
131  , size( pageSize )
132  , displayName( displayName )
133 {
134 }
135 
137  : size( pageSize )
138 {
139 
140 }
141 
142 bool QgsPageSize::operator==( const QgsPageSize &other ) const
143 {
144  return ( name == other.name && size == other.size );
145 }
146 
147 bool QgsPageSize::operator!=( const QgsPageSize &other ) const
148 {
149  return ( ! operator==( other ) );
150 }
QList< QgsPageSize > find(const QString &name) const
Finds matching page sizes from the registry, using a case insensitive match on the page size name...
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:278
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
bool operator!=(const QgsPageSize &other) const
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
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.
QList< QgsPageSize > entries() const
Returns a list of page sizes in the registry.
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.