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