File indexing completed on 2024-05-19 15:27:50
0001 /* This file is part of KGraphViewer. 0002 Copyright (C) 2005-2006 Gaël de Chalendar <kleag@free.fr> 0003 0004 KGraphViewer is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU General Public 0006 License as published by the Free Software Foundation, version 2. 0007 0008 This program is distributed in the hope that it will be useful, 0009 but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 General Public License for more details. 0012 0013 You should have received a copy of the GNU General Public License 0014 along with this program; if not, write to the Free Software 0015 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0016 02110-1301, USA 0017 */ 0018 0019 /* This file was part of the KDE project 0020 Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> 0021 0022 This program is free software; you can redistribute it and/or 0023 modify it under the terms of the GNU Library General Public 0024 License as published by the Free Software Foundation; either 0025 version 2 of the License, or (at your option) any later version. 0026 */ 0027 0028 #include "KgvPageLayout.h" 0029 #include "kgraphviewerlib_debug.h" 0030 0031 #include <KSharedConfig> 0032 #include <KgvUnit.h> 0033 #include <QDebug> 0034 #include <QPageSize> 0035 #include <QPrinterInfo> 0036 #include <klocalizedstring.h> 0037 #include <qdom.h> 0038 0039 KgvPageLayout KgvPageLayout::standardLayout() 0040 { 0041 KgvPageLayout layout; 0042 layout.format = KgvPageFormat::defaultFormat(); 0043 layout.orientation = PG_PORTRAIT; 0044 layout.ptWidth = MM_TO_POINT(KgvPageFormat::width(layout.format, layout.orientation)); 0045 layout.ptHeight = MM_TO_POINT(KgvPageFormat::height(layout.format, layout.orientation)); 0046 layout.ptLeft = MM_TO_POINT(20.0); 0047 layout.ptRight = MM_TO_POINT(20.0); 0048 layout.ptTop = MM_TO_POINT(20.0); 0049 layout.ptBottom = MM_TO_POINT(20.0); 0050 layout.ptPageEdge = -1; 0051 layout.ptBindingSide = -1; 0052 qCDebug(KGRAPHVIEWERLIB_LOG) << "Returning standardLayout"; 0053 return layout; 0054 } 0055 0056 struct PageFormatInfo { 0057 KgvFormat format; 0058 QPageSize::PageSizeId pageSize; 0059 const char *shortName; // Short name 0060 const char *descriptiveName; // Full name, which will be translated; nullptr to use QPageSize 0061 }; 0062 0063 const PageFormatInfo pageFormatInfo[] = {{PG_DIN_A3, QPageSize::A3, "A3", nullptr}, 0064 {PG_DIN_A4, QPageSize::A4, "A4", nullptr}, 0065 {PG_DIN_A5, QPageSize::A5, "A5", nullptr}, 0066 {PG_US_LETTER, QPageSize::Letter, "Letter", nullptr}, 0067 {PG_US_LEGAL, QPageSize::Legal, "Legal", nullptr}, 0068 {PG_SCREEN, QPageSize::A4, "Screen", I18N_NOOP2("Page size", "Screen")}, // Custom, so fall back to A4 0069 {PG_CUSTOM, QPageSize::A4, "Custom", I18N_NOOP2("Page size", "Custom")}, // Custom, so fall back to A4 0070 {PG_DIN_B5, QPageSize::B5, "B5", nullptr}, 0071 {PG_US_EXECUTIVE, QPageSize::Executive, "Executive", nullptr}, 0072 {PG_DIN_A0, QPageSize::A0, "A0", nullptr}, 0073 {PG_DIN_A1, QPageSize::A1, "A1", nullptr}, 0074 {PG_DIN_A2, QPageSize::A2, "A2", nullptr}, 0075 {PG_DIN_A6, QPageSize::A6, "A6", nullptr}, 0076 {PG_DIN_A7, QPageSize::A7, "A7", nullptr}, 0077 {PG_DIN_A8, QPageSize::A8, "A8", nullptr}, 0078 {PG_DIN_A9, QPageSize::A9, "A9", nullptr}, 0079 {PG_DIN_B0, QPageSize::B0, "B0", nullptr}, 0080 {PG_DIN_B1, QPageSize::B1, "B1", nullptr}, 0081 {PG_DIN_B10, QPageSize::B10, "B10", nullptr}, 0082 {PG_DIN_B2, QPageSize::B2, "B2", nullptr}, 0083 {PG_DIN_B3, QPageSize::B3, "B3", nullptr}, 0084 {PG_DIN_B4, QPageSize::B4, "B4", nullptr}, 0085 {PG_DIN_B6, QPageSize::B6, "B6", nullptr}, 0086 {PG_ISO_C5, QPageSize::C5E, "C5", nullptr}, 0087 {PG_US_COMM10, QPageSize::Comm10E, "Comm10", nullptr}, 0088 {PG_ISO_DL, QPageSize::DLE, "DL", nullptr}, 0089 {PG_US_FOLIO, QPageSize::Folio, "Folio", nullptr}, 0090 {PG_US_LEDGER, QPageSize::Ledger, "Ledger", nullptr}, 0091 {PG_US_TABLOID, QPageSize::Tabloid, "Tabloid", nullptr}}; 0092 0093 int KgvPageFormat::printerPageSize(KgvFormat format) 0094 { 0095 if (format == PG_SCREEN) { 0096 qCWarning(KGRAPHVIEWERLIB_LOG) << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE."; 0097 return QPageSize::A4; 0098 } else if (format == PG_CUSTOM) { 0099 qCWarning(KGRAPHVIEWERLIB_LOG) << "The used page layout (CUSTOM) is not supported by QPrinter. Printing in A4."; 0100 return QPageSize::A4; 0101 } else if (format <= PG_LAST_FORMAT) 0102 return pageFormatInfo[format].pageSize; 0103 else 0104 return QPageSize::A4; 0105 } 0106 0107 double KgvPageFormat::width(KgvFormat format, KgvOrientation orientation) 0108 { 0109 if (orientation == PG_LANDSCAPE) 0110 return height(format, PG_PORTRAIT); 0111 if (format <= PG_LAST_FORMAT) 0112 return QPageSize::size(pageFormatInfo[format].pageSize, QPageSize::Millimeter).width(); 0113 return QPageSize::size(QPageSize::A4, QPageSize::Millimeter).width(); // should never happen 0114 } 0115 0116 double KgvPageFormat::height(KgvFormat format, KgvOrientation orientation) 0117 { 0118 if (orientation == PG_LANDSCAPE) 0119 return width(format, PG_PORTRAIT); 0120 if (format <= PG_LAST_FORMAT) 0121 return QPageSize::size(pageFormatInfo[format].pageSize, QPageSize::Millimeter).height(); 0122 return QPageSize::size(QPageSize::A4, QPageSize::Millimeter).height(); 0123 } 0124 0125 KgvFormat KgvPageFormat::guessFormat(double width, double height) 0126 { 0127 for (int i = 0; i <= PG_LAST_FORMAT; ++i) { 0128 const QSizeF ps = QPageSize::size(pageFormatInfo[i].pageSize, QPageSize::Millimeter); 0129 // We have some tolerance. 1pt is a third of a mm, this is 0130 // barely noticeable for a page size. 0131 if (i != PG_CUSTOM && qAbs(width - ps.width()) < 1.0 && qAbs(height - ps.height()) < 1.0) 0132 return static_cast<KgvFormat>(i); 0133 } 0134 return PG_CUSTOM; 0135 } 0136 0137 QString KgvPageFormat::formatString(KgvFormat format) 0138 { 0139 if (format <= PG_LAST_FORMAT) 0140 return QString::fromLatin1(pageFormatInfo[format].shortName); 0141 return QString::fromLatin1("A4"); 0142 } 0143 0144 KgvFormat KgvPageFormat::formatFromString(const QString &string) 0145 { 0146 for (int i = 0; i <= PG_LAST_FORMAT; ++i) { 0147 if (string == QString::fromLatin1(pageFormatInfo[i].shortName)) 0148 return pageFormatInfo[i].format; 0149 } 0150 // We do not know the format name, so we have a custom format 0151 return PG_CUSTOM; 0152 } 0153 0154 KgvFormat KgvPageFormat::defaultFormat() 0155 { 0156 const QPageSize::PageSizeId pageSize = QPrinterInfo::defaultPrinter().defaultPageSize().id(); 0157 for (int i = 0; i <= PG_LAST_FORMAT; ++i) { 0158 if (pageFormatInfo[i].pageSize == pageSize) 0159 return static_cast<KgvFormat>(i); 0160 } 0161 return PG_DIN_A4; 0162 } 0163 0164 QString KgvPageFormat::name(KgvFormat format) 0165 { 0166 if (format <= PG_LAST_FORMAT) { 0167 if (pageFormatInfo[format].descriptiveName) 0168 return i18nc("Page size", pageFormatInfo[format].descriptiveName); 0169 else 0170 return QPageSize::name(pageFormatInfo[format].pageSize); 0171 } 0172 return QPageSize::name(pageFormatInfo[PG_DIN_A4].pageSize); 0173 } 0174 0175 QStringList KgvPageFormat::allFormats() 0176 { 0177 QStringList lst; 0178 for (int i = 0; i <= PG_LAST_FORMAT; ++i) { 0179 if (pageFormatInfo[i].descriptiveName) 0180 lst << i18nc("Page size", pageFormatInfo[i].descriptiveName); 0181 else 0182 lst << QPageSize::name(pageFormatInfo[i].pageSize); 0183 } 0184 return lst; 0185 }