diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,7 +30,7 @@ common/KReportPluginMetaData.cpp common/KReportData.cpp common/KReportUtils.cpp - common/KReportPageFormat.cpp + common/KReportPageSize.cpp common/KReportUnit.cpp common/KReportDesign.cpp common/KReportDesign_p.cpp @@ -246,7 +246,7 @@ ORIGINAL CAMELCASE RELATIVE common HEADER_NAMES - KReportPageFormat + KReportPageSize KReportData KReportItemBase KReportItemLine diff --git a/src/common/KReportDocument.cpp b/src/common/KReportDocument.cpp --- a/src/common/KReportDocument.cpp +++ b/src/common/KReportDocument.cpp @@ -19,10 +19,10 @@ #include "KReportUnit.h" #include "KReportDetailSectionData.h" #include "KReportItemBase.h" +#include "KReportDpi.h" #include #include -#include #include "kreport_debug.h" void KReportDocument::init() @@ -52,10 +52,9 @@ << elemSource.text(); return; } - QScreen *srn = QApplication::screens().at(0); - const qreal dpiX = srn->logicalDotsPerInchX(); - const qreal dpiY = srn->logicalDotsPerInchY(); + const qreal dpiX = KReportDpi::dpiX(); + const qreal dpiY = KReportDpi::dpiY(); QDomNodeList sections = elemSource.childNodes(); diff --git a/src/common/KReportPageFormat.h b/src/common/KReportPageFormat.h deleted file mode 100644 --- a/src/common/KReportPageFormat.h +++ /dev/null @@ -1,142 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 1998, 1999 Torben Weis - Copyright 2002, 2003 David Faure - Copyright 2003 Nicolas GOUTTE - Copyright 2007 Thomas Zander - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#ifndef KREPORTPAGEFORMAT_H -#define KREPORTPAGEFORMAT_H - -#include - -#include "kreport_export.h" - -/// The page formats KReport supports -namespace KReportPageFormat -{ -/** - * @brief Represents the paper format a document shall be printed on. - * - * For compatibility reasons, and because of screen and custom, - * this enum doesn't map to QPrinter::PageSize but KReportPageFormat::printerPageSize - * does the conversion. - */ -enum Format { - IsoA3Size, - IsoA4Size, - IsoA5Size, - UsLetterSize, - UsLegalSize, - ScreenSize, - CustomSize, - IsoB5Size, - UsExecutiveSize, - IsoA0Size, - IsoA1Size, - IsoA2Size, - IsoA6Size, - IsoA7Size, - IsoA8Size, - IsoA9Size, - IsoB0Size, - IsoB1Size, - IsoB10Size, - IsoB2Size, - IsoB3Size, - IsoB4Size, - IsoB6Size, - IsoC5Size, - UsComm10Size, - IsoDLSize, - UsFolioSize, - UsLedgerSize, - UsTabloidSize -}; - -/** - * Represents the orientation of a printed document. - */ -enum Orientation { - Portrait, - Landscape -}; - -/** - * @brief Convert a Format into a KPrinter::PageSize. - * - * If format is 'screen' it will use A4 landscape. - * If format is 'custom' it will use A4 portrait. - * (you may want to take care of those cases separately). - * Usually passed to KPrinter::setPageSize(). - */ -KREPORT_EXPORT QPrinter::PageSize printerPageSize(Format format); - -/** - * Returns the width (in mm) for a given page format and orientation - * 'Custom' isn't supported by this function, obviously. - */ -KREPORT_EXPORT qreal width(Format format, Orientation orientation = Landscape); - -/** - * Returns the height (in mm) for a given page format and orientation - * 'Custom' isn't supported by this function, obviously. - */ -KREPORT_EXPORT qreal height(Format format, Orientation orientation = Landscape); - -/** - * Returns the internal name of the given page format. - * Use for saving. - */ -KREPORT_EXPORT QString formatString(Format format); - -/** - * Convert a format string (internal name) to a page format value. - * Use for loading. - */ -KREPORT_EXPORT Format formatFromString(const QString &string); - -/** - * Returns the default format (based on the KControl settings) - */ -KREPORT_EXPORT Format defaultFormat(); - -/** - * Returns the translated name of the given page format. - * Use for showing the user. - */ -KREPORT_EXPORT QString name(Format format); - -/** - * Lists the translated names of all the available formats - */ -KREPORT_EXPORT QStringList localizedPageFormatNames(); - -/** - * Lists the non-translated names of all the available formats - */ -KREPORT_EXPORT QStringList pageFormatNames(); - -/** - * Try to find the paper format for the given width and height (in mm). - * Useful to some import filters. - */ -KREPORT_EXPORT Format guessFormat(qreal width, qreal height); -} - -#endif diff --git a/src/common/KReportPageFormat.cpp b/src/common/KReportPageFormat.cpp deleted file mode 100644 --- a/src/common/KReportPageFormat.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 1998, 1999 Torben Weis - Copyright 2002, 2003 David Faure - Copyright 2003 Nicolas GOUTTE - Copyright 2007 Thomas Zander - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "KReportPageFormat.h" - -#include "kreport_debug.h" -#include -#include -#include - -// paper formats ( mm ) -#define PG_A3_WIDTH 297.0 -#define PG_A3_HEIGHT 420.0 -#define PG_A4_WIDTH 210.0 -#define PG_A4_HEIGHT 297.0 -#define PG_A5_WIDTH 148.0 -#define PG_A5_HEIGHT 210.0 -#define PG_B5_WIDTH 182.0 -#define PG_B5_HEIGHT 257.0 -#define PG_US_LETTER_WIDTH 216.0 -#define PG_US_LETTER_HEIGHT 279.0 -#define PG_US_LEGAL_WIDTH 216.0 -#define PG_US_LEGAL_HEIGHT 356.0 -#define PG_US_EXECUTIVE_WIDTH 191.0 -#define PG_US_EXECUTIVE_HEIGHT 254.0 - -// To ignore the clang warning we get because we have a -// for (int i = 0; pageFormatInfo[i].format != -1 ;i++) -// construct and pageFormatInfo has (KReportPageFormat::Format) - 1 -#if defined(__clang__) -#pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" -#endif - -struct PageFormatInfo { - KReportPageFormat::Format format; - QPrinter::PageSize qprinter; - const char* shortName; // Short name - const char* descriptiveName; // Full name, which will be translated - qreal width; // in mm - qreal height; // in mm -}; - -// NOTES: -// - the width and height of non-ISO formats are rounded -// http://en.wikipedia.org/wiki/Paper_size can help -// - the comments "should be..." indicates the exact values if the inch sizes would be multiplied by 25.4 mm/inch - -const PageFormatInfo pageFormatInfo[] = { - { KReportPageFormat::IsoA3Size, QPrinter::A3, "A3", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A3"), 297.0, 420.0 }, - { KReportPageFormat::IsoA4Size, QPrinter::A4, "A4", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A4"), 210.0, 297.0 }, - { KReportPageFormat::IsoA5Size, QPrinter::A5, "A5", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A5"), 148.0, 210.0 }, - { KReportPageFormat::UsLetterSize, QPrinter::Letter, "Letter", QT_TRANSLATE_NOOP("KReportPageFormat", "US Letter"), 215.9, 279.4 }, - { KReportPageFormat::UsLegalSize, QPrinter::Legal, "Legal", QT_TRANSLATE_NOOP("KReportPageFormat", "US Legal"), 215.9, 355.6 }, - { KReportPageFormat::ScreenSize, QPrinter::A4, "Screen", QT_TRANSLATE_NOOP("KReportPageFormat", "Screen"), PG_A4_HEIGHT, PG_A4_WIDTH }, // Custom, so fall back to A4 - { KReportPageFormat::CustomSize, QPrinter::A4, "Custom", QT_TRANSLATE_NOOP("KReportPageFormat", "Custom"), PG_A4_WIDTH, PG_A4_HEIGHT }, // Custom, so fall back to A4 - { KReportPageFormat::IsoB5Size, QPrinter::B5, "B5", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B5"), 182.0, 257.0 }, - { KReportPageFormat::UsExecutiveSize, QPrinter::Executive, "Executive", QT_TRANSLATE_NOOP("KReportPageFormat", "US Executive"), 191.0, 254.0 }, // should be 190.5 mm x 254.0 mm - { KReportPageFormat::IsoA0Size, QPrinter::A0, "A0", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A0"), 841.0, 1189.0 }, - { KReportPageFormat::IsoA1Size, QPrinter::A1, "A1", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A1"), 594.0, 841.0 }, - { KReportPageFormat::IsoA2Size, QPrinter::A2, "A2", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A2"), 420.0, 594.0 }, - { KReportPageFormat::IsoA6Size, QPrinter::A6, "A6", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A6"), 105.0, 148.0 }, - { KReportPageFormat::IsoA7Size, QPrinter::A7, "A7", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A7"), 74.0, 105.0 }, - { KReportPageFormat::IsoA8Size, QPrinter::A8, "A8", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A8"), 52.0, 74.0 }, - { KReportPageFormat::IsoA9Size, QPrinter::A9, "A9", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO A9"), 37.0, 52.0 }, - { KReportPageFormat::IsoB0Size, QPrinter::B0, "B0", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B0"), 1030.0, 1456.0 }, - { KReportPageFormat::IsoB1Size, QPrinter::B1, "B1", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B1"), 728.0, 1030.0 }, - { KReportPageFormat::IsoB10Size, QPrinter::B10, "B10", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B10"), 32.0, 45.0 }, - { KReportPageFormat::IsoB2Size, QPrinter::B2, "B2", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B2"), 515.0, 728.0 }, - { KReportPageFormat::IsoB3Size, QPrinter::B3, "B3", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B3"), 364.0, 515.0 }, - { KReportPageFormat::IsoB4Size, QPrinter::B4, "B4", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B4"), 257.0, 364.0 }, - { KReportPageFormat::IsoB6Size, QPrinter::B6, "B6", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO B6"), 128.0, 182.0 }, - { KReportPageFormat::IsoC5Size, QPrinter::C5E, "C5", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO C5"), 163.0, 229.0 }, // Some sources tells: 162 mm x 228 mm - { KReportPageFormat::UsComm10Size, QPrinter::Comm10E, "Comm10", QT_TRANSLATE_NOOP("KReportPageFormat", "US Common 10"), 105.0, 241.0 }, // should be 104.775 mm x 241.3 mm - { KReportPageFormat::IsoDLSize, QPrinter::DLE, "DL", QT_TRANSLATE_NOOP("KReportPageFormat", "ISO DL"), 110.0, 220.0 }, - { KReportPageFormat::UsFolioSize, QPrinter::Folio, "Folio", QT_TRANSLATE_NOOP("KReportPageFormat", "US Folio"), 210.0, 330.0 }, // should be 209.54 mm x 330.2 mm - { KReportPageFormat::UsLedgerSize, QPrinter::Ledger, "Ledger", QT_TRANSLATE_NOOP("KReportPageFormat", "US Ledger"), 432.0, 279.0 }, // should be 431.8 mm x 297.4 mm - { KReportPageFormat::UsTabloidSize, QPrinter::Tabloid, "Tabloid", QT_TRANSLATE_NOOP("KReportPageFormat", "US Tabloid"), 279.0, 432.0 }, // should be 297.4 mm x 431.8 mm - {(KReportPageFormat::Format) - 1, (QPrinter::PageSize) - 1, "", "", -1, -1 } -}; - -QPrinter::PageSize KReportPageFormat::printerPageSize(KReportPageFormat::Format format) -{ - if (format == ScreenSize) { - kreportWarning() << "You use the page layout SCREEN. Printing in ISO A4 Landscape."; - return QPrinter::A4; - } - if (format == CustomSize) { - kreportWarning() << "The used page layout (Custom) is not supported by KQPrinter. Printing in A4."; - return QPrinter::A4; - } - return pageFormatInfo[ format ].qprinter; -} - -qreal KReportPageFormat::width(Format format, Orientation orientation) -{ - if (orientation == Landscape) - return height(format, Portrait); - return pageFormatInfo[ format ].width; -} - -qreal KReportPageFormat::height(Format format, Orientation orientation) -{ - if (orientation == Landscape) - return width(format, Portrait); - return pageFormatInfo[ format ].height; -} - -KReportPageFormat::Format KReportPageFormat::guessFormat(qreal width, qreal height) -{ - for (int i = 0; pageFormatInfo[i].format != -1 ;i++) { - // We have some tolerance. 1pt is a third of a mm, this is - // barely noticeable for a page size. - if (qAbs(width - pageFormatInfo[i].width) < 1.0 && qAbs(height - pageFormatInfo[i].height) < 1.0) - return pageFormatInfo[i].format; - } - return CustomSize; -} - -QString KReportPageFormat::formatString(Format format) -{ - return QString::fromLatin1(pageFormatInfo[ format ].shortName); -} - -KReportPageFormat::Format KReportPageFormat::formatFromString(const QString & string) -{ - for (int i = 0; pageFormatInfo[i].format != -1 ;i++) { - if (string == QString::fromLatin1(pageFormatInfo[ i ].shortName)) - return pageFormatInfo[ i ].format; - } - // We do not know the format name, so we have a custom format - return CustomSize; -} - -KReportPageFormat::Format KReportPageFormat::defaultFormat() -{ - QPrinterInfo printerInfo = QPrinterInfo::defaultPrinter(); - QPageSize size = printerInfo.defaultPageSize(); - if (size.isValid()) { - const QPageSize::PageSizeId qprinter = size.id(); - for (int i = 0; pageFormatInfo[i].format != -1 ;i++) { - if (pageFormatInfo[ i ].qprinter == static_cast(qprinter)){ - return static_cast(i); - } - } - } - return IsoA4Size; -} - -QString KReportPageFormat::name(Format format) -{ - return QCoreApplication::translate("KReportPageFormat", pageFormatInfo[ format ].descriptiveName); -} - -QStringList KReportPageFormat::localizedPageFormatNames() -{ - QStringList lst; - for (int i = 0; pageFormatInfo[i].format != -1 ;i++) { - lst << QCoreApplication::translate("KReportPageFormat", pageFormatInfo[ i ].descriptiveName); - } - return lst; -} - -QStringList KReportPageFormat::pageFormatNames() -{ - QStringList lst; - for (int i = 0; pageFormatInfo[i].format != -1 ;i++) { - lst << QLatin1String(pageFormatInfo[ i ].shortName); - } - return lst; -} diff --git a/src/common/KReportPageOptions.h b/src/common/KReportPageOptions.h --- a/src/common/KReportPageOptions.h +++ b/src/common/KReportPageOptions.h @@ -38,32 +38,31 @@ Portrait = 1 // and true }; - qreal getMarginTop(); + qreal getMarginTop() const; void setMarginTop(qreal v); - qreal getMarginBottom(); + qreal getMarginBottom() const; void setMarginBottom(qreal v); - qreal getMarginLeft(); + qreal getMarginLeft() const; void setMarginLeft(qreal v); - qreal getMarginRight(); + qreal getMarginRight() const; void setMarginRight(qreal v); - qreal widthPx(); - qreal heightPx(); + QSizeF pixelSize() const; - const QString & getPageSize(); + const QString & getPageSize() const; void setPageSize(const QString & s); - qreal getCustomWidth(); + qreal getCustomWidth() const; void setCustomWidth(qreal v); - qreal getCustomHeight(); + qreal getCustomHeight() const; void setCustomHeight(qreal v); - PageOrientation getOrientation(); - bool isPortrait(); + PageOrientation getOrientation() const; + bool isPortrait() const; void setOrientation(PageOrientation o); void setPortrait(bool yes); void setLabelType(const QString &); - const QString & getLabelType(); + const QString & getLabelType() const; Q_SIGNALS: void pageOptionsChanged(); diff --git a/src/common/KReportPageOptions.cpp b/src/common/KReportPageOptions.cpp --- a/src/common/KReportPageOptions.cpp +++ b/src/common/KReportPageOptions.cpp @@ -17,10 +17,11 @@ */ #include "KReportPageOptions.h" -#include "KReportPageFormat.h" #include "KReportUnit.h" +#include "KReportPageSize.h" +#include "KReportDpi.h" + #include -#include KReportPageOptions::KReportPageOptions() : QObject(), m_pageSize(QLatin1String("Letter")) @@ -69,7 +70,7 @@ return *this; } -qreal KReportPageOptions::getMarginTop() +qreal KReportPageOptions::getMarginTop() const { return m_marginTop; } @@ -83,7 +84,7 @@ emit pageOptionsChanged(); } -qreal KReportPageOptions::getMarginBottom() +qreal KReportPageOptions::getMarginBottom() const { return m_marginBottom; } @@ -97,7 +98,7 @@ emit pageOptionsChanged(); } -qreal KReportPageOptions::getMarginLeft() +qreal KReportPageOptions::getMarginLeft() const { return m_marginLeft; } @@ -111,7 +112,7 @@ emit pageOptionsChanged(); } -qreal KReportPageOptions::getMarginRight() +qreal KReportPageOptions::getMarginRight() const { return m_marginRight; } @@ -125,19 +126,20 @@ emit pageOptionsChanged(); } -const QString & KReportPageOptions::getPageSize() +const QString & KReportPageOptions::getPageSize() const { return m_pageSize; } + void KReportPageOptions::setPageSize(const QString & s) { if (m_pageSize == s) return; m_pageSize = s; emit pageOptionsChanged(); } -qreal KReportPageOptions::getCustomWidth() +qreal KReportPageOptions::getCustomWidth() const { return m_customWidth; } @@ -149,10 +151,12 @@ m_customWidth = v; emit pageOptionsChanged(); } -qreal KReportPageOptions::getCustomHeight() + +qreal KReportPageOptions::getCustomHeight() const { return m_customHeight; } + void KReportPageOptions::setCustomHeight(qreal v) { if (m_customHeight == v) @@ -162,12 +166,12 @@ emit pageOptionsChanged(); } -KReportPageOptions::PageOrientation KReportPageOptions::getOrientation() +KReportPageOptions::PageOrientation KReportPageOptions::getOrientation() const { return m_orientation; } -bool KReportPageOptions::isPortrait() +bool KReportPageOptions::isPortrait() const { return (m_orientation == Portrait); } @@ -180,15 +184,17 @@ m_orientation = o; emit pageOptionsChanged(); } + void KReportPageOptions::setPortrait(bool yes) { setOrientation((yes ? Portrait : Landscape)); } -const QString & KReportPageOptions::getLabelType() +const QString & KReportPageOptions::getLabelType() const { return m_labelType; } + void KReportPageOptions::setLabelType(const QString & type) { if (m_labelType == type) @@ -199,39 +205,14 @@ } //Convenience functions that return the page width/height in pixels based on the DPI -qreal KReportPageOptions::widthPx() -{ - int pageWidth; - - if (isPortrait()) { - pageWidth = KReportPageFormat::width(KReportPageFormat::formatFromString(getPageSize()), KReportPageFormat::Portrait); - } else { - pageWidth = KReportPageFormat::width(KReportPageFormat::formatFromString(getPageSize()), KReportPageFormat::Landscape); - } - - KReportUnit pageUnit(KReportUnit::Millimeter); - QScreen *srn = QApplication::screens().at(0); - - pageWidth = KReportUnit::toInch(pageUnit.fromUserValue(pageWidth)) * srn->logicalDotsPerInchX(); - - return pageWidth; -} - -qreal KReportPageOptions::heightPx() +QSizeF KReportPageOptions::pixelSize() const { - int pageHeight; - - if (isPortrait()) { - pageHeight = KReportPageFormat::height(KReportPageFormat::formatFromString(getPageSize()), KReportPageFormat::Portrait); + QSizeF xDpiSize = QPageSize(KReportPageSize::pageSize(getPageSize())).sizePixels(KReportDpi::dpiX()); + QSizeF yDpiSize = QPageSize(KReportPageSize::pageSize(getPageSize())).sizePixels(KReportDpi::dpiY()); + + if (isPortrait()){ + return QSizeF(xDpiSize.width(), yDpiSize.height()); } else { - pageHeight = KReportPageFormat::height(KReportPageFormat::formatFromString(getPageSize()), KReportPageFormat::Landscape); + return QSizeF(xDpiSize.height(), yDpiSize.width()); } - - KReportUnit pageUnit(KReportUnit::Millimeter); - QScreen *srn = QApplication::screens().at(0); - - pageHeight = KReportUnit::toInch(pageUnit.fromUserValue(pageHeight)) * srn->logicalDotsPerInchY(); - - return pageHeight; } - diff --git a/src/common/KReportPageSize.h b/src/common/KReportPageSize.h new file mode 100644 --- /dev/null +++ b/src/common/KReportPageSize.h @@ -0,0 +1,67 @@ +/* This file is part of the KDE project + Copyright (C) 2015 by Adam Pigg + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KREPORTPAGESIZE_H +#define KREPORTPAGESIZE_H +#include +#include "kreport_export.h" + +/** + * @brief Handle the page sizes we support in reports + * + * A group of helper functions and definitions of the page sizes + * we support in KReport. + * + * We don't support all the sizes supported by Qt, so here we provide a list + * of supported sizes and translatable descriptions, with helper functions to + * switch between QString and QPageSizeId + * + */ +namespace KReportPageSize +{ + +/** + * @return list of translated names of all the available formats + */ +KREPORT_EXPORT QStringList pageFormatNames(); + +/** + * @return list of non-translated names of all the available formats + */ +KREPORT_EXPORT QStringList pageFormatKeys(); + +/** + * @return the default format (based on the default printer) + */ +KREPORT_EXPORT QPageSize::PageSizeId defaultSize(); + +/** + * @return the page size Id for the given key + */ +KREPORT_EXPORT QPageSize::PageSizeId pageSize(const QString& key); + +/** + * @return the page size string for the given Id + */ +KREPORT_EXPORT QString pageSizeKey(QPageSize::PageSizeId id); + + +} + +#endif // KREPORTPAGESIZE_H diff --git a/src/common/KReportPageSize.cpp b/src/common/KReportPageSize.cpp new file mode 100644 --- /dev/null +++ b/src/common/KReportPageSize.cpp @@ -0,0 +1,86 @@ + /* This file is part of the KDE project + Copyright (C) 2015 by Adam Pigg + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "KReportPageSize.h" +#include +#include + +struct KReportPageSizeInfo { + char *name; + char *description; + QPageSize::PageSizeId pageSize; +}; + +const KReportPageSizeInfo pageSizeInfo[] = { + { "A3", QT_TRANSLATE_NOOP("KReportPageFormat", "A3"), QPageSize::A3}, + { "A4", QT_TRANSLATE_NOOP("KReportPageFormat", "A4"), QPageSize::A4}, + { "A5", QT_TRANSLATE_NOOP("KReportPageFormat", "A5"), QPageSize::A5}, + { "Letter", QT_TRANSLATE_NOOP("KReportPageFormat", "US Letter"), QPageSize::Letter}, + { "Legal", QT_TRANSLATE_NOOP("KReportPageFormat", "US Legal"), QPageSize::Legal}, + { "Screen", QT_TRANSLATE_NOOP("KReportPageFormat", "Screen"), QPageSize::A4}, + { "", "", QPageSize::LastPageSize} +}; + +QStringList KReportPageSize::pageFormatNames() +{ + QStringList lst; + for (int i = 0; pageSizeInfo[i].pageSize != QPageSize::LastPageSize ;i++) { + lst << QCoreApplication::translate("KReportPageFormat", pageSizeInfo[ i ].description); + } + return lst; +} + +QStringList KReportPageSize::pageFormatKeys() +{ + QStringList lst; + for (int i = 0; pageSizeInfo[i].pageSize != QPageSize::LastPageSize ;i++) { + lst << QLatin1String(pageSizeInfo[ i ].name); + } + return lst; +} + +QPageSize::PageSizeId KReportPageSize::defaultSize() +{ + QPrinterInfo printerInfo = QPrinterInfo::defaultPrinter(); + QPageSize size = printerInfo.defaultPageSize(); + if (size.isValid()) { + return size.id(); + } + return QPageSize::A4; +} + +QString KReportPageSize::pageSizeKey(QPageSize::PageSizeId id) +{ + for (int i = 0; pageSizeInfo[i].pageSize != QPageSize::LastPageSize ;i++) { + if (pageSizeInfo[i].pageSize == id) { + return QLatin1String(pageSizeInfo[i].name); + } + } + return QString(); +} + +QPageSize::PageSizeId KReportPageSize::pageSize(const QString &size) +{ + for (int i = 0; pageSizeInfo[i].pageSize != QPageSize::LastPageSize ;i++) { + if (QLatin1String(pageSizeInfo[i].name) == size) { + return pageSizeInfo[i].pageSize; + } + } + return defaultSize(); +} diff --git a/src/common/KReportPosition.cpp b/src/common/KReportPosition.cpp --- a/src/common/KReportPosition.cpp +++ b/src/common/KReportPosition.cpp @@ -17,10 +17,10 @@ */ #include "KReportPosition.h" +#include "KReportDpi.h" #include -#include #include KReportPosition::KReportPosition(const KReportUnit& unit) @@ -41,10 +41,8 @@ void KReportPosition::setScenePos(const QPointF& pos, UpdatePropertyFlag update) { - QScreen *srn = QApplication::screens().at(0); - - const qreal x = INCH_TO_POINT(pos.x() / srn->logicalDotsPerInchX()); - const qreal y = INCH_TO_POINT(pos.y() / srn->logicalDotsPerInchY()); + const qreal x = INCH_TO_POINT(pos.x() / KReportDpi::dpiX()); + const qreal y = INCH_TO_POINT(pos.y() / KReportDpi::dpiY()); m_pointPos.setX(x); m_pointPos.setY(y); @@ -87,10 +85,8 @@ QPointF KReportPosition::toScene() const { - QScreen *srn = QApplication::screens().at(0); - - const qreal x = POINT_TO_INCH(m_pointPos.x()) * srn->logicalDotsPerInchX(); - const qreal y = POINT_TO_INCH(m_pointPos.y()) * srn->logicalDotsPerInchY(); + const qreal x = POINT_TO_INCH(m_pointPos.x()) * KReportDpi::dpiX(); + const qreal y = POINT_TO_INCH(m_pointPos.y()) * KReportDpi::dpiY(); return QPointF(x, y); } diff --git a/src/common/KReportSize.cpp b/src/common/KReportSize.cpp --- a/src/common/KReportSize.cpp +++ b/src/common/KReportSize.cpp @@ -16,10 +16,10 @@ */ #include "KReportSize.h" +#include "KReportDpi.h" #include -#include #include KReportSize::KReportSize(const KReportUnit& unit) @@ -37,10 +37,8 @@ { qreal w, h; - QScreen *srn = QApplication::screens().at(0); - - w = INCH_TO_POINT(s.width() / srn->logicalDotsPerInchX()); - h = INCH_TO_POINT(s.height() / srn->logicalDotsPerInchY()); + w = INCH_TO_POINT(s.width() / KReportDpi::dpiX()); + h = INCH_TO_POINT(s.height() / KReportDpi::dpiY()); m_pointSize.setWidth(w); m_pointSize.setHeight(h); @@ -83,9 +81,8 @@ QSizeF KReportSize::toScene() const { qreal w, h; - QScreen *srn = QApplication::screens().at(0); - w = POINT_TO_INCH(m_pointSize.width()) * srn->logicalDotsPerInchX(); - h = POINT_TO_INCH(m_pointSize.height()) * srn->logicalDotsPerInchY(); + w = POINT_TO_INCH(m_pointSize.width()) * KReportDpi::dpiX(); + h = POINT_TO_INCH(m_pointSize.height()) * KReportDpi::dpiY(); return QSizeF(w, h); } diff --git a/src/renderer/KReportPage.cpp b/src/renderer/KReportPage.cpp --- a/src/renderer/KReportPage.cpp +++ b/src/renderer/KReportPage.cpp @@ -18,7 +18,6 @@ #include "KReportPage.h" #include "KReportRendererBase.h" -#include "KReportPageFormat.h" #include "KReportUnit.h" #include "KReportRenderObjects.h" #include "kreport_debug.h" @@ -71,8 +70,8 @@ pageHeight = (int)(d->reportDocument->pageOptions().getCustomHeight()); } else { // lookup the correct size information for the specified size paper - pageWidth = d->reportDocument->pageOptions().widthPx(); - pageHeight = d->reportDocument->pageOptions().heightPx(); + pageWidth = d->reportDocument->pageOptions().pixelSize().width(); + pageHeight = d->reportDocument->pageOptions().pixelSize().height(); } } setRect(0,0,pageWidth, pageHeight); diff --git a/src/renderer/KReportPreRenderer.cpp b/src/renderer/KReportPreRenderer.cpp --- a/src/renderer/KReportPreRenderer.cpp +++ b/src/renderer/KReportPreRenderer.cpp @@ -27,15 +27,15 @@ #include "KReportDocument.h" #include "KReportDetailSectionData.h" #include "KReportLabelSizeInfo.h" -#include "KReportPageFormat.h" +#include "KReportPageSize.h" +#include "KReportDpi.h" #ifdef KREPORT_SCRIPTING #include "scripting/KReportScriptHandler.h" #include "scripting/KReportGroupTracker.h" #endif #include -#include #include #include "kreport_debug.h" @@ -288,8 +288,7 @@ qreal KReportPreRendererPrivate::renderSectionSize(const KReportSectionData & sectionData) { - QScreen *srn = QApplication::screens().at(0); - qreal intHeight = POINT_TO_INCH(sectionData.height()) * srn->logicalDotsPerInchX(); + qreal intHeight = POINT_TO_INCH(sectionData.height()) * KReportDpi::dpiX(); int itemHeight = 0; @@ -318,8 +317,7 @@ qreal KReportPreRendererPrivate::renderSection(const KReportSectionData & sectionData) { - QScreen *srn = QApplication::screens().at(0); - qreal sectionHeight = POINT_TO_INCH(sectionData.height()) * srn->logicalDotsPerInchX(); + qreal sectionHeight = POINT_TO_INCH(sectionData.height()) * KReportDpi::dpiX(); int itemHeight = 0; //kreportDebug() << "Name: " << sectionData.name() << " Height: " << sectionHeight @@ -337,7 +335,7 @@ ORORect* bg = new ORORect(); bg->setPen(QPen(Qt::NoPen)); bg->setBrush(sectionData.backgroundColor()); - qreal w = m_page->document()->pageOptions().widthPx() - m_page->document()->pageOptions().getMarginRight() - m_leftMargin; + qreal w = m_page->document()->pageOptions().pixelSize().width() - m_page->document()->pageOptions().getMarginRight() - m_leftMargin; bg->setRect(QRectF(m_leftMargin, m_yOffset, w, sectionHeight)); m_page->addPrimitive(bg, true); @@ -419,10 +417,6 @@ ORODocument* KReportPreRenderer::generate() { - QScreen *srn = QApplication::screens().at(0); - int dpiX = srn->logicalDotsPerInchX(); - int dpiY = srn->logicalDotsPerInchY(); - if (d == 0 || !d->m_valid || d->m_reportDocument == 0 || d->m_kodata == 0) return 0; @@ -442,7 +436,7 @@ //kreportDebug() << "Calculating Margins"; if (!label.isNull()) { - if (d->m_reportDocument ->page.isPortrait()) { + if (d->m_reportDocument->page.isPortrait()) { d->m_topMargin = (label.startY() / 100.0); d->m_bottomMargin = 0; d->m_rightMargin = 0; @@ -474,12 +468,10 @@ rpo.setPageSize(label.paper()); } else { // lookup the correct size information for the specified size paper - d->m_maxWidth = KReportPageFormat::width(KReportPageFormat::formatFromString(d->m_reportDocument->page.getPageSize()), KReportPageFormat::Portrait); - d->m_maxHeight = KReportPageFormat::height(KReportPageFormat::formatFromString(d->m_reportDocument->page.getPageSize()), KReportPageFormat::Portrait); + QSizeF pageSizePx = d->m_reportDocument->page.pixelSize(); - KReportUnit pageUnit(KReportUnit::Millimeter); - d->m_maxWidth = KReportUnit::toInch(pageUnit.fromUserValue(d->m_maxWidth)) * dpiX; - d->m_maxHeight = KReportUnit::toInch(pageUnit.fromUserValue(d->m_maxHeight)) * dpiY; + d->m_maxWidth = pageSizePx.width(); + d->m_maxHeight = pageSizePx.height(); } } diff --git a/src/renderer/KReportPrintRenderer_p.cpp b/src/renderer/KReportPrintRenderer_p.cpp --- a/src/renderer/KReportPrintRenderer_p.cpp +++ b/src/renderer/KReportPrintRenderer_p.cpp @@ -19,9 +19,9 @@ #include "KReportPrintRenderer_p.h" #include "kreport_debug.h" #include "KReportRenderObjects.h" -#include "KReportPageFormat.h" +#include "KReportPageSize.h" +#include "KReportDpi.h" -#include #include #include #include @@ -47,10 +47,11 @@ pPrinter->setOrientation((document->pageOptions().isPortrait() ? QPrinter::Portrait : QPrinter::Landscape)); pPrinter->setPageOrder(QPrinter::FirstPageFirst); - if (document->pageOptions().getPageSize().isEmpty()) + if (document->pageOptions().getPageSize().isEmpty()) { pPrinter->setPageSize(QPrinter::Custom); - else - pPrinter->setPageSize(KReportPageFormat::printerPageSize(KReportPageFormat::formatFromString(document->pageOptions().getPageSize()))); + } else { + pPrinter->setPageSize(QPageSize(KReportPageSize::pageSize(document->pageOptions().getPageSize()))); + } return true; } @@ -79,9 +80,9 @@ if (toPage == 0 || toPage > document->pages()) toPage = document->pages(); - QScreen *srn = QApplication::screens().at(0); + qreal scaleX = context.printer->resolution() / qreal(KReportDpi::dpiX()); + qreal scaleY = context.printer->resolution() / qreal(KReportDpi::dpiY()); - qreal scale = context.printer->resolution() / qreal(srn->logicalDotsPerInchX()); for (int copy = 0; copy < context.printer->numCopies(); copy++) { for (int page = fromPage; page < toPage; page++) { @@ -97,8 +98,8 @@ OROPrimitive * prim = p->primitive(i); - prim->setPosition(prim->position() * scale); - prim->setSize(prim->size() * scale); + prim->setPosition(QPointF(prim->position().x() * scaleX, prim->position().y() * scaleY)); + prim->setSize(QSizeF(prim->size().width() * scaleX, prim->size().height() * scaleY)); //kreportDebug() << "Rendering object" << i << "type" << prim->type(); if (prim->type() == OROTextBox::TextBox) { //kreportDebug() << "Text Box"; @@ -124,7 +125,7 @@ context.painter->drawText(rc, tb->flags(), tb->text()); //outer line - context.painter->setPen(QPen(tb->lineStyle().color(), tb->lineStyle().width() * scale, tb->lineStyle().penStyle())); + context.painter->setPen(QPen(tb->lineStyle().color(), tb->lineStyle().width() * scaleX, tb->lineStyle().penStyle())); context.painter->drawRect(rc); //Reset back to defaults for next element @@ -134,9 +135,9 @@ //kreportDebug() << "Line"; OROLine * ln = (OROLine*) prim; QPointF s = ln->startPoint(); - QPointF e = ln->endPoint() * scale; + QPointF e(ln->endPoint().x() * scaleX, ln->endPoint().y() * scaleY); //QPen pen ( _painter->pen() ); - QPen pen(ln->lineStyle().color(), ln->lineStyle().width() * scale, ln->lineStyle().penStyle()); + QPen pen(ln->lineStyle().color(), ln->lineStyle().width() * scaleX, ln->lineStyle().penStyle()); context.painter->save(); context.painter->setRenderHint(QPainter::Antialiasing, true); @@ -204,7 +205,7 @@ if (chk->lineStyle().penStyle() == Qt::NoPen || chk->lineStyle().width() <= 0) { context.painter->setPen(QPen(Qt::lightGray)); } else { - context.painter->setPen(QPen(chk->lineStyle().color(), chk->lineStyle().width() * scale, chk->lineStyle().penStyle())); + context.painter->setPen(QPen(chk->lineStyle().color(), chk->lineStyle().width() * scaleX, chk->lineStyle().penStyle())); } qreal ox = sz.width() / 5; diff --git a/src/renderer/KReportScreenRenderer_p.cpp b/src/renderer/KReportScreenRenderer_p.cpp --- a/src/renderer/KReportScreenRenderer_p.cpp +++ b/src/renderer/KReportScreenRenderer_p.cpp @@ -18,7 +18,6 @@ #include "KReportScreenRenderer_p.h" #include "KReportRenderObjects.h" -#include "KReportPageFormat.h" #include "KReportUnit.h" #include "kreport_debug.h" diff --git a/src/wrtembed/KReportDesigner.cpp b/src/wrtembed/KReportDesigner.cpp --- a/src/wrtembed/KReportDesigner.cpp +++ b/src/wrtembed/KReportDesigner.cpp @@ -28,7 +28,7 @@ #include "KReportDesignerItemLine.h" #include "KReportRuler_p.h" #include "KReportZoomHandler.h" -#include "KReportPageFormat.h" +#include "KReportPageSize.h" #include "KReportDpi.h" #include "KReportUtils.h" #include "KReportPluginInterface.h" @@ -725,9 +725,9 @@ d->title = new KProperty("title", QLatin1String("Report"), tr("Title"), tr("Report Title")); keys.clear(); - keys = KReportPageFormat::pageFormatNames(); - strings = KReportPageFormat::localizedPageFormatNames(); - QString defaultKey = KReportPageFormat::formatString(KReportPageFormat::defaultFormat()); + keys = KReportPageSize::pageFormatKeys(); + strings = KReportPageSize::pageFormatKeys(); + QString defaultKey = KReportPageSize::pageSizeKey(KReportPageSize::defaultSize()); d->pageSize = new KProperty("page-size", keys, strings, defaultKey, tr("Page Size")); keys.clear(); strings.clear(); @@ -858,29 +858,22 @@ } int KReportDesigner::pageWidthPx() const -{ - int cw = 0; - int ch = 0; - int width = 0; - - KReportPageFormat::Format pf = KReportPageFormat::formatFromString(d->set->property("page-size").value().toString()); - - cw = POINT_TO_INCH(MM_TO_POINT(KReportPageFormat::width(pf, KReportPageFormat::Portrait))) * KReportDpi::dpiX(); - - ch = POINT_TO_INCH(MM_TO_POINT(KReportPageFormat::height(pf, KReportPageFormat::Portrait))) * KReportDpi::dpiY(); - - width = (d->set->property("print-orientation").value().toString() == QLatin1String("portrait") ? cw : ch); +{ + KReportPageOptions po; + po.setPageSize(d->set->property("page-size").value().toString()); + po.setPortrait(d->set->property("print-orientation").value().toString() == QLatin1String("portrait")); + QSizeF pageSizePx = po.pixelSize(); + int width = pageSizePx.width(); width = width - POINT_TO_INCH(d->set->property("margin-left").value().toDouble()) * KReportDpi::dpiX(); width = width - POINT_TO_INCH(d->set->property("margin-right").value().toDouble()) * KReportDpi::dpiX(); - - return width; + + return width; } void KReportDesigner::resizeEvent(QResizeEvent * event) { Q_UNUSED(event); - //hruler->setRulerLength ( vboxlayout->geometry().width() ); d->hruler->setRulerLength(pageWidthPx()); } diff --git a/src/wrtembed/KReportDesignerItemRectBase.cpp b/src/wrtembed/KReportDesignerItemRectBase.cpp --- a/src/wrtembed/KReportDesignerItemRectBase.cpp +++ b/src/wrtembed/KReportDesignerItemRectBase.cpp @@ -22,19 +22,17 @@ #include "KReportPosition.h" #include "KReportSize.h" #include "KReportDesignerSectionScene.h" +#include "KReportDpi.h" #include #include -#include #include KReportDesignerItemRectBase::KReportDesignerItemRectBase(KReportDesigner *r) : QGraphicsRectItem(), KReportDesignerItemBase(r) { - QScreen *srn = QApplication::screens().at(0); - - m_dpiX = srn->logicalDotsPerInchX(); - m_dpiY = srn->logicalDotsPerInchY(); + m_dpiX = KReportDpi::dpiX(); + m_dpiY = KReportDpi::dpiY(); m_ppos = 0; m_psize = 0; diff --git a/src/wrtembed/KReportDesignerSection.cpp b/src/wrtembed/KReportDesignerSection.cpp --- a/src/wrtembed/KReportDesignerSection.cpp +++ b/src/wrtembed/KReportDesignerSection.cpp @@ -30,6 +30,7 @@ #include "KReportDesignerItemLine.h" #include "KReportRuler_p.h" #include "KReportZoomHandler.h" +#include "KReportDpi.h" #include "kreport_debug.h" #include @@ -39,7 +40,6 @@ #include #include #include -#include #include #include @@ -103,8 +103,8 @@ d->sectionData = new KReportSectionData(this); connect(d->sectionData->propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)), this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&))); - QScreen *srn = QApplication::screens().at(0); - d->dpiY = srn->logicalDotsPerInchY(); + + d->dpiY = KReportDpi::dpiY(); d->reportDesigner = rptdes; setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); diff --git a/src/wrtembed/KReportDesignerSectionScene.cpp b/src/wrtembed/KReportDesignerSectionScene.cpp --- a/src/wrtembed/KReportDesignerSectionScene.cpp +++ b/src/wrtembed/KReportDesignerSectionScene.cpp @@ -21,11 +21,10 @@ #include "KReportDesignerItemRectBase.h" #include "KReportDesigner.h" #include "KReportLabelSizeInfo.h" -#include "KReportPageFormat.h" +#include "KReportDpi.h" #include "kreport_debug.h" #include -#include #include #include #include @@ -35,11 +34,8 @@ { m_rd = rd; m_minorSteps = 0; - - QScreen *srn = QApplication::screens().at(0); - - m_dpiX = srn->logicalDotsPerInchX(); - m_dpiY = srn->logicalDotsPerInchY(); + m_dpiX = KReportDpi::dpiX(); + m_dpiY = KReportDpi::dpiY(); if (m_unit.type() != m_rd->pageUnit().type()) { m_unit = m_rd->pageUnit();