Changeset View
Changeset View
Standalone View
Standalone View
src/part/KgvPageLayout.cpp
Show All 19 Lines | 19 | /* This file was part of the KDE project | |||
---|---|---|---|---|---|
20 | Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> | 20 | Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> | ||
21 | 21 | | |||
22 | This program is free software; you can redistribute it and/or | 22 | This program is free software; you can redistribute it and/or | ||
23 | modify it under the terms of the GNU Library General Public | 23 | modify it under the terms of the GNU Library General Public | ||
24 | License as published by the Free Software Foundation; either | 24 | License as published by the Free Software Foundation; either | ||
25 | version 2 of the License, or (at your option) any later version. | 25 | version 2 of the License, or (at your option) any later version. | ||
26 | */ | 26 | */ | ||
27 | 27 | | |||
28 | #include "kgraphviewerlib_debug.h" | | |||
29 | #include "KgvPageLayout.h" | 28 | #include "KgvPageLayout.h" | ||
29 | #include "kgraphviewerlib_debug.h" | ||||
30 | 30 | | |||
31 | #include <KSharedConfig> | ||||
31 | #include <KgvUnit.h> | 32 | #include <KgvUnit.h> | ||
32 | #include <QDebug> | 33 | #include <QDebug> | ||
33 | #include <KSharedConfig> | | |||
34 | #include <QPageSize> | 34 | #include <QPageSize> | ||
35 | #include <QPrinterInfo> | 35 | #include <QPrinterInfo> | ||
36 | #include <qdom.h> | | |||
37 | #include <klocalizedstring.h> | 36 | #include <klocalizedstring.h> | ||
37 | #include <qdom.h> | ||||
38 | 38 | | |||
39 | KgvPageLayout KgvPageLayout::standardLayout() | 39 | KgvPageLayout KgvPageLayout::standardLayout() | ||
40 | { | 40 | { | ||
41 | KgvPageLayout layout; | 41 | KgvPageLayout layout; | ||
42 | layout.format = KgvPageFormat::defaultFormat(); | 42 | layout.format = KgvPageFormat::defaultFormat(); | ||
43 | layout.orientation = PG_PORTRAIT; | 43 | layout.orientation = PG_PORTRAIT; | ||
44 | layout.ptWidth = MM_TO_POINT( KgvPageFormat::width( layout.format, layout.orientation ) ); | 44 | layout.ptWidth = MM_TO_POINT(KgvPageFormat::width(layout.format, layout.orientation)); | ||
45 | layout.ptHeight = MM_TO_POINT( KgvPageFormat::height( layout.format, layout.orientation ) ); | 45 | layout.ptHeight = MM_TO_POINT(KgvPageFormat::height(layout.format, layout.orientation)); | ||
46 | layout.ptLeft = MM_TO_POINT( 20.0 ); | 46 | layout.ptLeft = MM_TO_POINT(20.0); | ||
47 | layout.ptRight = MM_TO_POINT( 20.0 ); | 47 | layout.ptRight = MM_TO_POINT(20.0); | ||
48 | layout.ptTop = MM_TO_POINT( 20.0 ); | 48 | layout.ptTop = MM_TO_POINT(20.0); | ||
49 | layout.ptBottom = MM_TO_POINT( 20.0 ); | 49 | layout.ptBottom = MM_TO_POINT(20.0); | ||
50 | layout.ptPageEdge = -1; | 50 | layout.ptPageEdge = -1; | ||
51 | layout.ptBindingSide = -1; | 51 | layout.ptBindingSide = -1; | ||
52 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Returning standardLayout"; | 52 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Returning standardLayout"; | ||
53 | return layout; | 53 | return layout; | ||
54 | } | 54 | } | ||
55 | 55 | | |||
56 | struct PageFormatInfo | 56 | struct PageFormatInfo { | ||
57 | { | | |||
58 | KgvFormat format; | 57 | KgvFormat format; | ||
59 | QPageSize::PageSizeId pageSize; | 58 | QPageSize::PageSizeId pageSize; | ||
60 | const char* shortName; // Short name | 59 | const char *shortName; // Short name | ||
61 | const char* descriptiveName; // Full name, which will be translated; nullptr to use QPageSize | 60 | const char *descriptiveName; // Full name, which will be translated; nullptr to use QPageSize | ||
62 | }; | 61 | }; | ||
63 | 62 | | |||
64 | const PageFormatInfo pageFormatInfo[]= | 63 | const PageFormatInfo pageFormatInfo[] = {{PG_DIN_A3, QPageSize::A3, "A3", nullptr}, | ||
65 | { | | |||
66 | { PG_DIN_A3, QPageSize::A3, "A3", nullptr }, | | |||
67 | { PG_DIN_A4, QPageSize::A4, "A4", nullptr }, | 64 | {PG_DIN_A4, QPageSize::A4, "A4", nullptr}, | ||
68 | { PG_DIN_A5, QPageSize::A5, "A5", nullptr }, | 65 | {PG_DIN_A5, QPageSize::A5, "A5", nullptr}, | ||
69 | { PG_US_LETTER, QPageSize::Letter, "Letter", nullptr }, | 66 | {PG_US_LETTER, QPageSize::Letter, "Letter", nullptr}, | ||
70 | { PG_US_LEGAL, QPageSize::Legal, "Legal", nullptr }, | 67 | {PG_US_LEGAL, QPageSize::Legal, "Legal", nullptr}, | ||
71 | { PG_SCREEN, QPageSize::A4, "Screen", I18N_NOOP2("Page size", "Screen") }, // Custom, so fall back to A4 | 68 | {PG_SCREEN, QPageSize::A4, "Screen", I18N_NOOP2("Page size", "Screen")}, // Custom, so fall back to A4 | ||
72 | { PG_CUSTOM, QPageSize::A4, "Custom", I18N_NOOP2("Page size", "Custom") }, // Custom, so fall back to A4 | 69 | {PG_CUSTOM, QPageSize::A4, "Custom", I18N_NOOP2("Page size", "Custom")}, // Custom, so fall back to A4 | ||
73 | { PG_DIN_B5, QPageSize::B5, "B5", nullptr }, | 70 | {PG_DIN_B5, QPageSize::B5, "B5", nullptr}, | ||
74 | { PG_US_EXECUTIVE, QPageSize::Executive, "Executive", nullptr }, | 71 | {PG_US_EXECUTIVE, QPageSize::Executive, "Executive", nullptr}, | ||
75 | { PG_DIN_A0, QPageSize::A0, "A0", nullptr }, | 72 | {PG_DIN_A0, QPageSize::A0, "A0", nullptr}, | ||
76 | { PG_DIN_A1, QPageSize::A1, "A1", nullptr }, | 73 | {PG_DIN_A1, QPageSize::A1, "A1", nullptr}, | ||
77 | { PG_DIN_A2, QPageSize::A2, "A2", nullptr }, | 74 | {PG_DIN_A2, QPageSize::A2, "A2", nullptr}, | ||
78 | { PG_DIN_A6, QPageSize::A6, "A6", nullptr }, | 75 | {PG_DIN_A6, QPageSize::A6, "A6", nullptr}, | ||
79 | { PG_DIN_A7, QPageSize::A7, "A7", nullptr }, | 76 | {PG_DIN_A7, QPageSize::A7, "A7", nullptr}, | ||
80 | { PG_DIN_A8, QPageSize::A8, "A8", nullptr }, | 77 | {PG_DIN_A8, QPageSize::A8, "A8", nullptr}, | ||
81 | { PG_DIN_A9, QPageSize::A9, "A9", nullptr }, | 78 | {PG_DIN_A9, QPageSize::A9, "A9", nullptr}, | ||
82 | { PG_DIN_B0, QPageSize::B0, "B0", nullptr }, | 79 | {PG_DIN_B0, QPageSize::B0, "B0", nullptr}, | ||
83 | { PG_DIN_B1, QPageSize::B1, "B1", nullptr }, | 80 | {PG_DIN_B1, QPageSize::B1, "B1", nullptr}, | ||
84 | { PG_DIN_B10, QPageSize::B10, "B10", nullptr }, | 81 | {PG_DIN_B10, QPageSize::B10, "B10", nullptr}, | ||
85 | { PG_DIN_B2, QPageSize::B2, "B2", nullptr }, | 82 | {PG_DIN_B2, QPageSize::B2, "B2", nullptr}, | ||
86 | { PG_DIN_B3, QPageSize::B3, "B3", nullptr }, | 83 | {PG_DIN_B3, QPageSize::B3, "B3", nullptr}, | ||
87 | { PG_DIN_B4, QPageSize::B4, "B4", nullptr }, | 84 | {PG_DIN_B4, QPageSize::B4, "B4", nullptr}, | ||
88 | { PG_DIN_B6, QPageSize::B6, "B6", nullptr }, | 85 | {PG_DIN_B6, QPageSize::B6, "B6", nullptr}, | ||
89 | { PG_ISO_C5, QPageSize::C5E, "C5", nullptr }, | 86 | {PG_ISO_C5, QPageSize::C5E, "C5", nullptr}, | ||
90 | { PG_US_COMM10, QPageSize::Comm10E, "Comm10", nullptr }, | 87 | {PG_US_COMM10, QPageSize::Comm10E, "Comm10", nullptr}, | ||
91 | { PG_ISO_DL, QPageSize::DLE, "DL", nullptr }, | 88 | {PG_ISO_DL, QPageSize::DLE, "DL", nullptr}, | ||
92 | { PG_US_FOLIO, QPageSize::Folio, "Folio", nullptr }, | 89 | {PG_US_FOLIO, QPageSize::Folio, "Folio", nullptr}, | ||
93 | { PG_US_LEDGER, QPageSize::Ledger, "Ledger", nullptr }, | 90 | {PG_US_LEDGER, QPageSize::Ledger, "Ledger", nullptr}, | ||
94 | { PG_US_TABLOID, QPageSize::Tabloid, "Tabloid", nullptr } | 91 | {PG_US_TABLOID, QPageSize::Tabloid, "Tabloid", nullptr}}; | ||
95 | }; | | |||
96 | 92 | | |||
97 | int KgvPageFormat::printerPageSize( KgvFormat format ) | 93 | int KgvPageFormat::printerPageSize(KgvFormat format) | ||
98 | { | 94 | { | ||
99 | if ( format == PG_SCREEN ) | 95 | if (format == PG_SCREEN) { | ||
100 | { | | |||
101 | qCWarning(KGRAPHVIEWERLIB_LOG) << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE."; | 96 | qCWarning(KGRAPHVIEWERLIB_LOG) << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE."; | ||
102 | return QPageSize::A4; | 97 | return QPageSize::A4; | ||
103 | } | 98 | } else if (format == PG_CUSTOM) { | ||
104 | else if ( format == PG_CUSTOM ) | | |||
105 | { | | |||
106 | qCWarning(KGRAPHVIEWERLIB_LOG) << "The used page layout (CUSTOM) is not supported by QPrinter. Printing in A4."; | 99 | qCWarning(KGRAPHVIEWERLIB_LOG) << "The used page layout (CUSTOM) is not supported by QPrinter. Printing in A4."; | ||
107 | return QPageSize::A4; | 100 | return QPageSize::A4; | ||
108 | } | 101 | } else if (format <= PG_LAST_FORMAT) | ||
109 | else if ( format <= PG_LAST_FORMAT ) | | |||
110 | return pageFormatInfo[ format ].pageSize; | 102 | return pageFormatInfo[format].pageSize; | ||
111 | else | 103 | else | ||
112 | return QPageSize::A4; | 104 | return QPageSize::A4; | ||
113 | } | 105 | } | ||
114 | 106 | | |||
115 | double KgvPageFormat::width( KgvFormat format, KgvOrientation orientation ) | 107 | double KgvPageFormat::width(KgvFormat format, KgvOrientation orientation) | ||
116 | { | 108 | { | ||
117 | if ( orientation == PG_LANDSCAPE ) | 109 | if (orientation == PG_LANDSCAPE) | ||
118 | return height( format, PG_PORTRAIT ); | 110 | return height(format, PG_PORTRAIT); | ||
119 | if ( format <= PG_LAST_FORMAT ) | 111 | if (format <= PG_LAST_FORMAT) | ||
120 | return QPageSize::size( pageFormatInfo[ format ].pageSize, QPageSize::Millimeter ).width(); | 112 | return QPageSize::size(pageFormatInfo[format].pageSize, QPageSize::Millimeter).width(); | ||
121 | return QPageSize::size( QPageSize::A4, QPageSize::Millimeter ).width(); // should never happen | 113 | return QPageSize::size(QPageSize::A4, QPageSize::Millimeter).width(); // should never happen | ||
122 | } | 114 | } | ||
123 | 115 | | |||
124 | double KgvPageFormat::height( KgvFormat format, KgvOrientation orientation ) | 116 | double KgvPageFormat::height(KgvFormat format, KgvOrientation orientation) | ||
125 | { | 117 | { | ||
126 | if ( orientation == PG_LANDSCAPE ) | 118 | if (orientation == PG_LANDSCAPE) | ||
127 | return width( format, PG_PORTRAIT ); | 119 | return width(format, PG_PORTRAIT); | ||
128 | if ( format <= PG_LAST_FORMAT ) | 120 | if (format <= PG_LAST_FORMAT) | ||
129 | return QPageSize::size( pageFormatInfo[ format ].pageSize, QPageSize::Millimeter ).height(); | 121 | return QPageSize::size(pageFormatInfo[format].pageSize, QPageSize::Millimeter).height(); | ||
130 | return QPageSize::size( QPageSize::A4, QPageSize::Millimeter ).height(); | 122 | return QPageSize::size(QPageSize::A4, QPageSize::Millimeter).height(); | ||
131 | } | 123 | } | ||
132 | 124 | | |||
133 | KgvFormat KgvPageFormat::guessFormat( double width, double height ) | 125 | KgvFormat KgvPageFormat::guessFormat(double width, double height) | ||
134 | { | 126 | { | ||
135 | for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) | 127 | for (int i = 0; i <= PG_LAST_FORMAT; ++i) { | ||
136 | { | | |||
137 | const QSizeF ps = QPageSize::size( pageFormatInfo[i].pageSize, QPageSize::Millimeter ); | 128 | const QSizeF ps = QPageSize::size(pageFormatInfo[i].pageSize, QPageSize::Millimeter); | ||
138 | // We have some tolerance. 1pt is a third of a mm, this is | 129 | // We have some tolerance. 1pt is a third of a mm, this is | ||
139 | // barely noticeable for a page size. | 130 | // barely noticeable for a page size. | ||
140 | if ( i != PG_CUSTOM | 131 | if (i != PG_CUSTOM && qAbs(width - ps.width()) < 1.0 && qAbs(height - ps.height()) < 1.0) | ||
141 | && qAbs( width - ps.width() ) < 1.0 | | |||
142 | && qAbs( height - ps.height() ) < 1.0 ) | | |||
143 | return static_cast<KgvFormat>(i); | 132 | return static_cast<KgvFormat>(i); | ||
144 | } | 133 | } | ||
145 | return PG_CUSTOM; | 134 | return PG_CUSTOM; | ||
146 | } | 135 | } | ||
147 | 136 | | |||
148 | QString KgvPageFormat::formatString( KgvFormat format ) | 137 | QString KgvPageFormat::formatString(KgvFormat format) | ||
149 | { | 138 | { | ||
150 | if ( format <= PG_LAST_FORMAT ) | 139 | if (format <= PG_LAST_FORMAT) | ||
151 | return QString::fromLatin1( pageFormatInfo[ format ].shortName ); | 140 | return QString::fromLatin1(pageFormatInfo[format].shortName); | ||
152 | return QString::fromLatin1( "A4" ); | 141 | return QString::fromLatin1("A4"); | ||
153 | } | 142 | } | ||
154 | 143 | | |||
155 | KgvFormat KgvPageFormat::formatFromString( const QString & string ) | 144 | KgvFormat KgvPageFormat::formatFromString(const QString &string) | ||
156 | { | 145 | { | ||
157 | for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) | 146 | for (int i = 0; i <= PG_LAST_FORMAT; ++i) { | ||
158 | { | | |||
159 | if (string == QString::fromLatin1( pageFormatInfo[ i ].shortName )) | 147 | if (string == QString::fromLatin1(pageFormatInfo[i].shortName)) | ||
160 | return pageFormatInfo[ i ].format; | 148 | return pageFormatInfo[i].format; | ||
161 | } | 149 | } | ||
162 | // We do not know the format name, so we have a custom format | 150 | // We do not know the format name, so we have a custom format | ||
163 | return PG_CUSTOM; | 151 | return PG_CUSTOM; | ||
164 | } | 152 | } | ||
165 | 153 | | |||
166 | KgvFormat KgvPageFormat::defaultFormat() | 154 | KgvFormat KgvPageFormat::defaultFormat() | ||
167 | { | 155 | { | ||
168 | const QPageSize::PageSizeId pageSize = QPrinterInfo::defaultPrinter().defaultPageSize().id(); | 156 | const QPageSize::PageSizeId pageSize = QPrinterInfo::defaultPrinter().defaultPageSize().id(); | ||
169 | for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) | 157 | for (int i = 0; i <= PG_LAST_FORMAT; ++i) { | ||
170 | { | | |||
171 | if ( pageFormatInfo[ i ].pageSize == pageSize ) | 158 | if (pageFormatInfo[i].pageSize == pageSize) | ||
172 | return static_cast<KgvFormat>(i); | 159 | return static_cast<KgvFormat>(i); | ||
173 | } | 160 | } | ||
174 | return PG_DIN_A4; | 161 | return PG_DIN_A4; | ||
175 | } | 162 | } | ||
176 | 163 | | |||
177 | QString KgvPageFormat::name( KgvFormat format ) | 164 | QString KgvPageFormat::name(KgvFormat format) | ||
178 | { | 165 | { | ||
179 | if ( format <= PG_LAST_FORMAT ) | 166 | if (format <= PG_LAST_FORMAT) { | ||
180 | { | | |||
181 | if ( pageFormatInfo[ format ].descriptiveName ) | 167 | if (pageFormatInfo[format].descriptiveName) | ||
182 | return i18nc( "Page size", pageFormatInfo[ format ].descriptiveName ); | 168 | return i18nc("Page size", pageFormatInfo[format].descriptiveName); | ||
183 | else | 169 | else | ||
184 | return QPageSize::name( pageFormatInfo[ format ].pageSize ); | 170 | return QPageSize::name(pageFormatInfo[format].pageSize); | ||
185 | } | 171 | } | ||
186 | return QPageSize::name( pageFormatInfo[ PG_DIN_A4 ].pageSize ); | 172 | return QPageSize::name(pageFormatInfo[PG_DIN_A4].pageSize); | ||
187 | } | 173 | } | ||
188 | 174 | | |||
189 | QStringList KgvPageFormat::allFormats() | 175 | QStringList KgvPageFormat::allFormats() | ||
190 | { | 176 | { | ||
191 | QStringList lst; | 177 | QStringList lst; | ||
192 | for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) | 178 | for (int i = 0; i <= PG_LAST_FORMAT; ++i) { | ||
193 | { | | |||
194 | if ( pageFormatInfo[ i ].descriptiveName ) | 179 | if (pageFormatInfo[i].descriptiveName) | ||
195 | lst << i18nc( "Page size", pageFormatInfo[ i ].descriptiveName ); | 180 | lst << i18nc("Page size", pageFormatInfo[i].descriptiveName); | ||
196 | else | 181 | else | ||
197 | lst << QPageSize::name( pageFormatInfo[ i ].pageSize ); | 182 | lst << QPageSize::name(pageFormatInfo[i].pageSize); | ||
198 | } | 183 | } | ||
199 | return lst; | 184 | return lst; | ||
200 | } | 185 | } |