QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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"
18
19#include "qgis.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
26//
27// QgsPageSizeRegistry
28//
29
31{
32 add( QgsPageSize( u"A6"_s, QgsLayoutSize( 105, 148 ), QObject::tr( "A6" ) ) );
33 add( QgsPageSize( u"A5"_s, QgsLayoutSize( 148, 210 ), QObject::tr( "A5" ) ) );
34 add( QgsPageSize( u"A4"_s, QgsLayoutSize( 210, 297 ), QObject::tr( "A4" ) ) );
35 add( QgsPageSize( u"A3"_s, QgsLayoutSize( 297, 420 ), QObject::tr( "A3" ) ) );
36 add( QgsPageSize( u"A2"_s, QgsLayoutSize( 420, 594 ), QObject::tr( "A2" ) ) );
37 add( QgsPageSize( u"A1"_s, QgsLayoutSize( 594, 841 ), QObject::tr( "A1" ) ) );
38 add( QgsPageSize( u"A0"_s, QgsLayoutSize( 841, 1189 ), QObject::tr( "A0" ) ) );
39 add( QgsPageSize( u"B6"_s, QgsLayoutSize( 125, 176 ), QObject::tr( "B6" ) ) );
40 add( QgsPageSize( u"B5"_s, QgsLayoutSize( 176, 250 ), QObject::tr( "B5" ) ) );
41 add( QgsPageSize( u"B4"_s, QgsLayoutSize( 250, 353 ), QObject::tr( "B4" ) ) );
42 add( QgsPageSize( u"B3"_s, QgsLayoutSize( 353, 500 ), QObject::tr( "B3" ) ) );
43 add( QgsPageSize( u"B2"_s, QgsLayoutSize( 500, 707 ), QObject::tr( "B2" ) ) );
44 add( QgsPageSize( u"B1"_s, QgsLayoutSize( 707, 1000 ), QObject::tr( "B1" ) ) );
45 add( QgsPageSize( u"B0"_s, QgsLayoutSize( 1000, 1414 ), QObject::tr( "B0" ) ) );
46 add( QgsPageSize( u"Legal"_s, QgsLayoutSize( 215.9, 355.6 ), QObject::tr( "Legal" ) ) );
47 add( QgsPageSize( u"Letter"_s, QgsLayoutSize( 215.9, 279.4 ), QObject::tr( "Letter" ) ) );
48 add( QgsPageSize( u"ANSI A"_s, QgsLayoutSize( 215.9, 279.4 ), QObject::tr( "ANSI A" ) ) );
49 add( QgsPageSize( u"ANSI B"_s, QgsLayoutSize( 279.4, 431.8 ), QObject::tr( "ANSI B" ) ) );
50 add( QgsPageSize( u"ANSI C"_s, QgsLayoutSize( 431.8, 558.8 ), QObject::tr( "ANSI C" ) ) );
51 add( QgsPageSize( u"ANSI D"_s, QgsLayoutSize( 558.8, 863.6 ), QObject::tr( "ANSI D" ) ) );
52 add( QgsPageSize( u"ANSI E"_s, QgsLayoutSize( 863.6, 1117.6 ), QObject::tr( "ANSI E" ) ) );
53 add( QgsPageSize( u"Arch A"_s, QgsLayoutSize( 228.6, 304.8 ), QObject::tr( "Arch A" ) ) );
54 add( QgsPageSize( u"Arch B"_s, QgsLayoutSize( 304.8, 457.2 ), QObject::tr( "Arch B" ) ) );
55 add( QgsPageSize( u"Arch C"_s, QgsLayoutSize( 457.2, 609.6 ), QObject::tr( "Arch C" ) ) );
56 add( QgsPageSize( u"Arch D"_s, QgsLayoutSize( 609.6, 914.4 ), QObject::tr( "Arch D" ) ) );
57 add( QgsPageSize( u"Arch E"_s, QgsLayoutSize( 914.4, 1219.2 ), QObject::tr( "Arch E" ) ) );
58 add( QgsPageSize( u"Arch E1"_s, QgsLayoutSize( 762, 1066.8 ), QObject::tr( "Arch E1" ) ) );
59 add( QgsPageSize( u"Arch E2"_s, QgsLayoutSize( 660, 965 ), QObject::tr( "Arch E2" ) ) );
60 add( QgsPageSize( u"Arch E3"_s, QgsLayoutSize( 686, 991 ), QObject::tr( "Arch E3" ) ) );
61 add( QgsPageSize( u"1920x1080"_s, QgsLayoutSize( 1080, 1920, Qgis::LayoutUnit::Pixels ), QObject::tr( "1920×1080 (16:9)" ) ) );
62 add( QgsPageSize( u"1280x800"_s, QgsLayoutSize( 800, 1280, Qgis::LayoutUnit::Pixels ), QObject::tr( "1280×800 (16:10)" ) ) );
63 add( QgsPageSize( u"1024x768"_s, QgsLayoutSize( 768, 1024, Qgis::LayoutUnit::Pixels ), QObject::tr( "1024×768 (4:3)" ) ) );
64}
65
67{
68 mPageSizes.append( size );
69}
70
71QList<QgsPageSize> QgsPageSizeRegistry::entries() const
72{
73 QList< QgsPageSize > result;
74 QList< QgsPageSize >::const_iterator it = mPageSizes.constBegin();
75 for ( ; it != mPageSizes.constEnd(); ++it )
76 {
77 result.push_back( *it );
78 }
79 return result;
80}
81
82QList<QgsPageSize> QgsPageSizeRegistry::find( const QString &name ) const
83{
84 QList< QgsPageSize > result;
85 QList< QgsPageSize >::const_iterator it = mPageSizes.constBegin();
86 for ( ; it != mPageSizes.constEnd(); ++it )
87 {
88 if ( ( *it ).name.compare( name, Qt::CaseInsensitive ) == 0 )
89 {
90 result.push_back( *it );
91 }
92 }
93 return result;
94}
95
96QString QgsPageSizeRegistry::find( const QgsLayoutSize &size ) const
97{
98 //try to match to existing page size
99 const QgsLayoutMeasurementConverter converter;
100 const auto constMPageSizes = mPageSizes;
101 for ( const QgsPageSize &pageSize : constMPageSizes )
102 {
103 // convert passed size to same units
104 const QgsLayoutSize xSize = converter.convert( size, pageSize.size.units() );
105
106 //consider width and height values may be exchanged
107 if ( ( qgsDoubleNear( xSize.width(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.height(), pageSize.size.height(), 0.01 ) )
108 || ( qgsDoubleNear( xSize.height(), pageSize.size.width(), 0.01 ) && qgsDoubleNear( xSize.width(), pageSize.size.height(), 0.01 ) ) )
109 {
110 return pageSize.name;
111 }
112 }
113 return QString();
114}
115
116bool QgsPageSizeRegistry::decodePageSize( const QString &pageSizeName, QgsPageSize &pageSize ) const
117{
118 const QList< QgsPageSize > matches = find( pageSizeName.trimmed() );
119 if ( matches.length() > 0 )
120 {
121 pageSize = matches.at( 0 );
122 return true;
123 }
124 return false;
125}
126
127//
128// QgsPageSize
129//
130
132 : size( QgsLayoutSize( 0.0, 0.0 ) )
133{
134}
135
136QgsPageSize::QgsPageSize( const QString &pageName, const QgsLayoutSize &pageSize, const QString &displayName )
137 : name( pageName )
138 , size( pageSize )
140{
141}
142
144 : size( pageSize )
145{
146
147}
148
149bool QgsPageSize::operator==( const QgsPageSize &other ) const
150{
151 return ( name == other.name && size == other.size );
152}
153
154bool QgsPageSize::operator!=( const QgsPageSize &other ) const
155{
156 return ( ! operator==( other ) );
157}
@ Pixels
Pixels.
Definition qgis.h:5318
Provides a method of converting QgsLayoutMeasurements from one unit to another.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, Qgis::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
double height() const
Returns the height of the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
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) const
Decodes a string representing a preset page size.
QgsPageSizeRegistry()
Creates a registry and populates it with known sizes.
void add(const QgsPageSize &size)
Adds a page size to the registry.
A named page size for layouts.
bool operator==(const QgsPageSize &other) const
QString displayName
Translated page name.
bool operator!=(const QgsPageSize &other) const
QString name
Name of page size.
QgsLayoutSize size
Page 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:6935