1 /* This file is part of the KDE project
2    Copyright (C) 2015-2016 Jarosław Staniek <staniek@kde.org>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef KREPORTUTILS_P_H
21 #define KREPORTUTILS_P_H
22 
23 #include "KReportUnit.h"
24 
25 #include <QPageLayout>
26 #include <QRect>
27 #include <QStandardPaths>
28 
29 const bool DEFAULT_SHOW_GRID = true;
30 const bool DEFAULT_SNAP_TO_GRID = true;
31 const int DEFAULT_GRID_DIVISIONS = 4;
32 const KReportUnit::Type DEFAULT_UNIT_TYPE = KReportUnit::Type::Centimeter;
33 #define DEFAULT_UNIT KReportUnit(DEFAULT_UNIT_TYPE)
34 #define DEFAULT_UNIT_STRING QLatin1String("cm")
35 const QPointF DEFAULT_ELEMENT_POS_PT(CM_TO_POINT(1.0), CM_TO_POINT(1.0));
36 const QSizeF DEFAULT_ELEMENT_SIZE_PT(CM_TO_POINT(1.0), CM_TO_POINT(1.0));
37 const QRectF DEFAULT_ELEMENT_RECT_PT(DEFAULT_ELEMENT_POS_PT, DEFAULT_ELEMENT_SIZE_PT);
38 #define DEFAULT_ELEMENT_POS_STRING QLatin1String("1.0cm") // both X and Y
39 #define DEFAULT_ELEMENT_SIZE_STRING QLatin1String("1.0cm") // both width and height
40 const qreal DEFAULT_SECTION_SIZE_PT = CM_TO_POINT(2.0);
41 #define DEFAULT_SECTION_SIZE_STRING QLatin1String("2.0cm")
42 const qreal DEFAULT_PAGE_MARGIN_PT = CM_TO_POINT(1.0);
43 #define DEFAULT_PAGE_MARGIN_STRING QLatin1String("1.0cm")
44 const QPageSize::PageSizeId DEFAULT_PAGE_SIZE = QPageSize::A4;
45 const QPageLayout::Orientation DEFAULT_PAGE_ORIENTATION = QPageLayout::Landscape;
46 const QSizeF DEFAULT_CUSTOM_PAGE_SIZE(10.0,10.0);
47 const qreal SMALLEST_PAGE_SIZE_PT = MM_TO_POINT(5);
48 
49 // This is a private code made inline for use in the lib and examples.
50 //! @todo Move to a shared lib to use in other Kexi libraries as well.
51 
52 namespace KReportPrivate {
53 
54 //! @todo Support other themes
55 const QString supportedIconTheme = QLatin1String("breeze");
56 
57 //! @brief @return true if @a path is readable
58 bool fileReadable(const QString &path);
59 
60 //! @brief Used for a workaround: locations for QStandardPaths::AppDataLocation end with app name.
61 //! If this is not an expected app but for example a test app, replace
62 //! the subdir name with app name so we can find resource file(s).
63 QStringList correctStandardLocations(const QString &privateName,
64                                      QStandardPaths::StandardLocation location,
65                                      const QString &extraLocation);
66 
67 
68 
69 /*! @brief Locates a file path for specified parameters
70  * @param privateName Name to be used instead of application name for resource lookup
71  * @param path Relative path to the resource file
72  * @param location Standard file location to use for file lookup
73  * @param extraLocation Extra directory path for file lookup
74  * @return Empty string on failure
75  */
76 QString locateFile(const QString &privateName,
77                    const QString& path, QStandardPaths::StandardLocation location,
78                    const QString &extraLocation);
79 
80 /*! @brief Registers icons resource file
81  * @param privateName Name to be used instead of application name for resource lookup
82  * @param path Relative path to the resource file
83  * @param location Standard file location to use for file lookup
84  * @param resourceRoot A resource root for QResource::registerResource()
85  * @param errorMessage On failure it is set to a brief error message.
86  * @param errorDescription On failure it is set to a detailed error message.
87  * other for warning
88  */
89 bool registerIconsResource(const QString &privateName, const QString& path,
90                              QStandardPaths::StandardLocation location,
91                              const QString &resourceRoot, const QString &extraLocation,
92                              QString *errorMessage, QString *detailedErrorMessage);
93 
94 /*! @brief Registers a global icon resource file
95  * @param themeName A name of icon theme to use.
96  * @param errorMessage On failure it is set to a brief error message.
97  * @param errorDescription On failure it is set to a detailed error message.
98  * other for warning
99  */
100 bool registerGlobalIconsResource(const QString &themeName,
101                                  QString *errorMessage,
102                                  QString *detailedErrorMessage);
103 
104 /*! @brief Registers a global icon resource file
105  * @param themeName A name of icon theme to use.
106  */
107 bool registerGlobalIconsResource(const QString &themeName);
108 
109 /*! @brief Registers a global icon resource file for default theme name.
110  */
111 bool registerGlobalIconsResource();
112 
113 /*! @brief Sets up a private icon resource file
114  * @return @c false on failure and sets error message. Does not warn or exit on failure.
115  * @param privateName Name to be used instead of application name for resource lookup
116  * @param path Relative path to the resource file
117  * @param themeName Icon theme to use. It affects filename.
118  * @param errorMessage On failure it is set to a brief error message
119  * @param errorDescription On failure it is set to a detailed error message
120  * other for warning
121  * @param prefix Resource path prefix. The default is useful for library-global resource,
122  * other values is useful for plugins.
123  */
124 bool setupPrivateIconsResource(const QString &privateName, const QString& path,
125                                const QString &themeName,
126                                QString *errorMessage, QString *detailedErrorMessage,
127                                const QString &prefix = QLatin1String(":/icons"));
128 
129 /*! @brief Sets up a private icon resource file
130  * @return @c false on failure and sets error message.
131  * @param privateName Name to be used instead of application name for resource lookup
132  * @param path Relative path to the resource file
133  * @param themeName Icon theme to use. It affects filename.
134  * @param errorMessage On failure it is set to a brief error message.
135  * @param errorDescription On failure it is set to a detailed error message.
136  * other for warning
137  * @param prefix Resource path prefix. The default is useful for library-global resource,
138  * other values is useful for plugins.
139  */
140 bool setupPrivateIconsResourceWithMessage(const QString &privateName, const QString& path,
141                                           const QString &themeName,
142                                           QString *errorMessage, QString *detailedErrorMessage,
143                                           const QString &prefix = QLatin1String(":/icons"));
144 
145 /*! @overload setupPrivateIconsResourceWithMessage(QString &privateName, const QString& path,
146                                           const QString &themeName,
147                                           QString *errorMessage, QString *detailedErrorMessage,
148                                           const QString &prefix = QLatin1String(":/icons"))
149     Uses default theme name.
150  */
151 bool setupPrivateIconsResourceWithMessage(const QString &privateName, const QString& path,
152                                           QString *errorMessage, QString *detailedErrorMessage,
153                                           const QString &prefix = QLatin1String(":/icons"));
154 
155 /*! @brief Sets up a private icon resource file
156  * Warns on failure and returns @c false.
157  * @param privateName Name to be used instead of application name for resource lookup
158  * @param path Relative path to the resource file
159  * @param messageType Type of message to use on error, QtFatalMsg for fatal exit and any
160  * other for warning
161  * @param prefix Resource path prefix. The default is useful for library-global resource,
162  * other values is useful for plugins.
163  */
164 bool setupPrivateIconsResourceWithMessage(const QString &privateName, const QString& path,
165                                           QtMsgType messageType,
166                                           const QString &prefix = QLatin1String(":/icons"));
167 
168 //! Sets up a global icon theme if it is different from supported.
169 //! Warns on failure and returns @c false.
170 bool setupGlobalIconTheme();
171 
172 int dpiX();
173 
174 int dpiY();
175 
176 //! This class is wrapper that fixes a critical QTBUG-47551 bug in default constructor of QPageLayout
177 //! Default constructor of QPageLayout does not initialize units.
178 //! https://bugreports.qt.io/browse/QTBUG-47551
179 //! @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.
180 class PageLayout: public QPageLayout {
181 public:
182         PageLayout();
183         PageLayout(const QPageLayout& pageLayout);
184         PageLayout& operator=(const QPageLayout& other);
185 };
186 
187 } // KReportPrivate
188 
189 #endif
190