File indexing completed on 2024-04-21 04:41:52

0001 /* This file is part of the KDE project
0002    Copyright (C) 2015-2019 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2.1 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KREPORTUTILS_P_H
0021 #define KREPORTUTILS_P_H
0022 
0023 #include "KReportUnit.h"
0024 
0025 #include <QPageLayout>
0026 #include <QRect>
0027 #include <QStandardPaths>
0028 
0029 class QPrinter;
0030 
0031 const bool DEFAULT_SHOW_GRID = true;
0032 const bool DEFAULT_SNAP_TO_GRID = true;
0033 const int DEFAULT_GRID_DIVISIONS = 4;
0034 const KReportUnit::Type DEFAULT_UNIT_TYPE = KReportUnit::Type::Centimeter;
0035 #define DEFAULT_UNIT KReportUnit(DEFAULT_UNIT_TYPE)
0036 #define DEFAULT_UNIT_STRING QLatin1String("cm")
0037 const QPointF DEFAULT_ELEMENT_POS_PT(CM_TO_POINT(1.0), CM_TO_POINT(1.0));
0038 const QSizeF DEFAULT_ELEMENT_SIZE_PT(CM_TO_POINT(1.0), CM_TO_POINT(1.0));
0039 const QRectF DEFAULT_ELEMENT_RECT_PT(DEFAULT_ELEMENT_POS_PT, DEFAULT_ELEMENT_SIZE_PT);
0040 #define DEFAULT_ELEMENT_POS_STRING QLatin1String("1.0cm") // both X and Y
0041 #define DEFAULT_ELEMENT_SIZE_STRING QLatin1String("1.0cm") // both width and height
0042 const qreal DEFAULT_SECTION_SIZE_PT = CM_TO_POINT(2.0);
0043 #define DEFAULT_SECTION_SIZE_STRING QLatin1String("2.0cm")
0044 const qreal DEFAULT_PAGE_MARGIN_PT = CM_TO_POINT(1.0);
0045 #define DEFAULT_PAGE_MARGIN_STRING QLatin1String("1.0cm")
0046 const QPageSize::PageSizeId DEFAULT_PAGE_SIZE = QPageSize::A4;
0047 const QPageLayout::Orientation DEFAULT_PAGE_ORIENTATION = QPageLayout::Landscape;
0048 const QSizeF DEFAULT_CUSTOM_PAGE_SIZE(10.0,10.0);
0049 const qreal SMALLEST_PAGE_SIZE_PT = MM_TO_POINT(5);
0050 
0051 // This is a private code made inline for use in the lib and examples.
0052 //! @todo Move to a shared lib to use in other Kexi libraries as well.
0053 
0054 namespace KReportPrivate {
0055 
0056 //! @todo Support other themes
0057 const QString supportedIconTheme = QLatin1String("breeze");
0058 
0059 //! @brief @return true if @a path is readable
0060 bool fileReadable(const QString &path);
0061 
0062 //! @brief Used for a workaround: locations for QStandardPaths::AppDataLocation end with app name.
0063 //! If this is not an expected app but for example a test app, replace
0064 //! the subdir name with app name so we can find resource file(s).
0065 QStringList correctStandardLocations(const QString &privateName,
0066                                      QStandardPaths::StandardLocation location,
0067                                      const QString &extraLocation);
0068 
0069 
0070 
0071 /*! @brief Locates a file path for specified parameters
0072  * @param privateName Name to be used instead of application name for resource lookup
0073  * @param path Relative path to the resource file
0074  * @param location Standard file location to use for file lookup
0075  * @param extraLocation Extra directory path for file lookup
0076  * @return Empty string on failure
0077  */
0078 QString locateFile(const QString &privateName,
0079                    const QString& path, QStandardPaths::StandardLocation location,
0080                    const QString &extraLocation);
0081 
0082 /*! @brief Registers icons resource file
0083  * @param privateName Name to be used instead of application name for resource lookup
0084  * @param path Relative path to the resource file
0085  * @param location Standard file location to use for file lookup
0086  * @param resourceRoot A resource root for QResource::registerResource()
0087  * @param errorMessage On failure it is set to a brief error message.
0088  * @param errorDescription On failure it is set to a detailed error message.
0089  * other for warning
0090  */
0091 bool registerIconsResource(const QString &privateName, const QString& path,
0092                              QStandardPaths::StandardLocation location,
0093                              const QString &resourceRoot, const QString &extraLocation,
0094                              QString *errorMessage, QString *detailedErrorMessage);
0095 
0096 /*! @brief Registers a global icon resource file
0097  * @param themeName A name of icon theme to use.
0098  * @param errorMessage On failure it is set to a brief error message.
0099  * @param errorDescription On failure it is set to a detailed error message.
0100  * other for warning
0101  */
0102 bool registerGlobalIconsResource(const QString &themeName,
0103                                  QString *errorMessage,
0104                                  QString *detailedErrorMessage);
0105 
0106 /*! @brief Registers a global icon resource file
0107  * @param themeName A name of icon theme to use.
0108  */
0109 bool registerGlobalIconsResource(const QString &themeName);
0110 
0111 /*! @brief Registers a global icon resource file for default theme name.
0112  */
0113 bool registerGlobalIconsResource();
0114 
0115 /*! @brief Sets up a private icon resource file
0116  * @return @c false on failure and sets error message. Does not warn or exit on failure.
0117  * @param privateName Name to be used instead of application name for resource lookup
0118  * @param path Relative path to the resource file
0119  * @param themeName Icon theme to use. It affects filename.
0120  * @param errorMessage On failure it is set to a brief error message
0121  * @param errorDescription On failure it is set to a detailed error message
0122  * other for warning
0123  * @param prefix Resource path prefix. The default is useful for library-global resource,
0124  * other values is useful for plugins.
0125  */
0126 bool setupPrivateIconsResource(const QString &privateName, const QString& path,
0127                                const QString &themeName,
0128                                QString *errorMessage, QString *detailedErrorMessage,
0129                                const QString &prefix = QLatin1String(":/icons"));
0130 
0131 /*! @brief Sets up a private icon resource file
0132  * @return @c false on failure and sets error message.
0133  * @param privateName Name to be used instead of application name for resource lookup
0134  * @param path Relative path to the resource file
0135  * @param themeName Icon theme to use. It affects filename.
0136  * @param errorMessage On failure it is set to a brief error message.
0137  * @param errorDescription On failure it is set to a detailed error message.
0138  * other for warning
0139  * @param prefix Resource path prefix. The default is useful for library-global resource,
0140  * other values is useful for plugins.
0141  */
0142 bool setupPrivateIconsResourceWithMessage(const QString &privateName, const QString& path,
0143                                           const QString &themeName,
0144                                           QString *errorMessage, QString *detailedErrorMessage,
0145                                           const QString &prefix = QLatin1String(":/icons"));
0146 
0147 /*! @overload setupPrivateIconsResourceWithMessage(QString &privateName, const QString& path,
0148                                           const QString &themeName,
0149                                           QString *errorMessage, QString *detailedErrorMessage,
0150                                           const QString &prefix = QLatin1String(":/icons"))
0151     Uses default theme name.
0152  */
0153 bool setupPrivateIconsResourceWithMessage(const QString &privateName, const QString& path,
0154                                           QString *errorMessage, QString *detailedErrorMessage,
0155                                           const QString &prefix = QLatin1String(":/icons"));
0156 
0157 /*! @brief Sets up a private icon resource file
0158  * Warns on failure and returns @c false.
0159  * @param privateName Name to be used instead of application name for resource lookup
0160  * @param path Relative path to the resource file
0161  * @param messageType Type of message to use on error, QtFatalMsg for fatal exit and any
0162  * other for warning
0163  * @param prefix Resource path prefix. The default is useful for library-global resource,
0164  * other values is useful for plugins.
0165  */
0166 bool setupPrivateIconsResourceWithMessage(const QString &privateName, const QString& path,
0167                                           QtMsgType messageType,
0168                                           const QString &prefix = QLatin1String(":/icons"));
0169 
0170 //! Sets up a global icon theme if it is different from supported.
0171 //! Warns on failure and returns @c false.
0172 bool setupGlobalIconTheme();
0173 
0174 int dpiX();
0175 
0176 int dpiY();
0177 
0178 /*!
0179  * Returns a high-resolution printer
0180  *
0181  * The QPrinter(QPrinter::HighResolution)) instance is created on first call.
0182  * The global printer helps to optimize access to QPrinter when report items need it.
0183  */
0184 QPrinter* highResolutionPrinter();
0185 
0186 //! This class is wrapper that fixes a critical QTBUG-47551 bug in default constructor of QPageLayout
0187 //! Default constructor of QPageLayout does not initialize units.
0188 //! https://bugreports.qt.io/browse/QTBUG-47551
0189 //! @todo remove this class and go back to QPageLayout when the faulty QPageLayout implementations are no longer on the wild. That's probably for Qt 6.
0190 class PageLayout: public QPageLayout {
0191 public:
0192         PageLayout();
0193         PageLayout(const QPageLayout& pageLayout);
0194         PageLayout& operator=(const QPageLayout& other);
0195 };
0196 
0197 } // KReportPrivate
0198 
0199 #endif