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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010-2015 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 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_H
0021 #define KREPORTUTILS_H
0022 
0023 #include "kreport_export.h"
0024 
0025 #include <QPageSize>
0026 #include <QRectF>
0027 #include <QColor>
0028 #include <QFont>
0029 
0030 class QDomDocument;
0031 class QDomElement;
0032 class QFont;
0033 class QPointF;
0034 class KProperty;
0035 class KReportTextStyleData;
0036 class KReportLineStyle;
0037 
0038 namespace KReportUtils
0039 {
0040     KREPORT_EXPORT QString attr(const QDomElement &el, const QString &attrName,
0041                                 const QString &defaultValue = QString());
0042 
0043     KREPORT_EXPORT QByteArray attr(const QDomElement &el, const QString &attrName,
0044                                    const QByteArray &defaultValue = QByteArray());
0045 
0046     KREPORT_EXPORT bool attr(const QDomElement &el, const QString &attrName, bool defaultValue = false);
0047 
0048     KREPORT_EXPORT int attr(const QDomElement &el, const QString &attrName, int defaultValue = 0);
0049 
0050     KREPORT_EXPORT qreal attr(const QDomElement &el, const QString &attrName, qreal defaultValue = 0.0);
0051 
0052     KREPORT_EXPORT QColor attr(const QDomElement &el, const QString &attrName, const QColor &defaultValue = QColor());
0053 
0054     //! @return percent value converted to qreal, e.g. 1.0 for "100%", 0.505 for "50.5%".
0055     //! @a defaultValue is returned if there is not "%" suffix or no proper number.
0056     KREPORT_EXPORT qreal attrPercent(const QDomElement& el, const QString &attrName, qreal defaultValue = 0.0);
0057 
0058     //! @return pen style from @a str or @a defaultValue
0059     //! Values from ODF 1.2 19.493 style:line-style are also recognized.
0060     KREPORT_EXPORT Qt::PenStyle penStyle(const QString &str, Qt::PenStyle defaultValue);
0061 
0062     //! @return vertical alignment flag from @a str or @a defaultValue
0063     KREPORT_EXPORT Qt::Alignment verticalAlignment(const QString &str, Qt::Alignment defaultValue);
0064 
0065     //! @return horizontal alignment flag from @a str or @a defaultValue
0066     KREPORT_EXPORT Qt::Alignment horizontalAlignment(const QString &str, Qt::Alignment defaultValue);
0067 
0068     //! @return vertical alignment flag name from @a alignment
0069     KREPORT_EXPORT QString verticalToString(Qt::Alignment alignment);
0070 
0071     //! @return horizontal alignment flag from @a alignment
0072     KREPORT_EXPORT QString horizontalToString(Qt::Alignment alignment);
0073 
0074     //! @return name value read from report:name attribute of @a el.
0075     //! If the attribute is missing, @a defaultValue is returned.
0076     KREPORT_EXPORT QString readNameAttribute(
0077         const QDomElement &el, const QString &defaultValue = QString());
0078 
0079     //! @return size value read from svg:width and svg:height attributes of @a el.
0080     //! If any of the attributes are missing, @a defaultValue is returned.
0081     //! @a defaultValue should be specified in Points.
0082     KREPORT_EXPORT QSizeF readSizeAttributes(
0083         const QDomElement &el, const QSizeF &defaultValue = QSizeF());
0084 
0085     //! @return rectangle value read from svg:x, svg:y, svg:width, svg:height attributes of @a el.
0086     //! If any of the attributes are missing, @a defaultValue is returned.
0087     //! @a defaultValue should be specified in Points.
0088     KREPORT_EXPORT QRectF readRectAttributes(
0089         const QDomElement &el, const QRectF &defaultValue = QRectF());
0090 
0091     //! @return Z index value read from report:z-index attribute of @a el.
0092     //! If the attribute is missing @a defaultValue is returned.
0093     //! @a defaultValue should be specified in Points.
0094     KREPORT_EXPORT qreal readZAttribute(const QDomElement &el, qreal defaultValue = 0.0);
0095 
0096     //! @return name of section type read from report:section-type attribute of @a el.
0097     //! If the attribute is missing, @a defaultValue is returned.
0098     KREPORT_EXPORT QString readSectionTypeNameAttribute(
0099         const QDomElement &el, const QString &defaultValue = QString());
0100 
0101     //! @return percent value for element @a name. If the element is missing, returns @a defaultPercentValue.
0102     //! If @a ok is not 0, *ok is set to the result.
0103     KREPORT_EXPORT int readPercent(const QDomElement &el, const QString &attrName,
0104         int defaultPercentValue, bool *ok);
0105 
0106     //! Reads all font attributes for element @a el into @a font.
0107     //! @todo add unit tests
0108     KREPORT_EXPORT void readFontAttributes(const QDomElement& el, QFont* font);
0109 
0110     //! Writes all attributes of font @a font into element @a el.
0111     //! @todo add unit tests
0112     KREPORT_EXPORT void writeFontAttributes(QDomElement *el, const QFont &font);
0113 
0114     //! Writes attributes for the rect position @a pos, @a siz
0115     KREPORT_EXPORT void buildXMLRect(QDomElement *entity, const QPointF &pos, const QSizeF &size);
0116 
0117     //! Writes attributes for text style @a ts
0118     KREPORT_EXPORT void buildXMLTextStyle(QDomDocument *doc, QDomElement *entity, const KReportTextStyleData &ts);
0119 
0120     //! Writes attributes for line style @a ls
0121     KREPORT_EXPORT void buildXMLLineStyle(QDomDocument *doc, QDomElement *entity, const KReportLineStyle &ls);
0122 
0123     //! Writes attributes for the property @a p
0124     KREPORT_EXPORT void addPropertyAsAttribute(QDomElement* e, KProperty* p);
0125 
0126     /**
0127      * Sets value of property @a p if element @e e has attribute named report:X where X is the name of @a p.
0128      *
0129      * Otherwise it does nothing and return @c false.
0130      */
0131     KREPORT_EXPORT bool setPropertyValue(KProperty *p, const QDomElement &e);
0132 
0133     //! Writes @a attribute to element @a e, @a value is stored in points with unit 'pt'
0134     KREPORT_EXPORT void setAttribute(QDomElement *e, const QString &attribute, double value);
0135 
0136     //! Writes point @a value as attributes to element @a e
0137     KREPORT_EXPORT void setAttribute(QDomElement *e, const QPointF &value);
0138 
0139     //! Writes size @a value as attributes to element @a e
0140     KREPORT_EXPORT void setAttribute(QDomElement *e, const QSizeF &value);
0141 
0142     //! Writes @a attribute to element @a e, @a value is stored as boolean
0143     KREPORT_EXPORT void setAttribute(QDomElement *e, const QString &attribute, bool value);
0144 
0145     //! Reads attributes from @a elemSource into text style @a ts
0146     KREPORT_EXPORT bool parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts);
0147 
0148     //! Reads attributes from @a elemSource into line style @a ls
0149     KREPORT_EXPORT bool parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls);
0150 
0151     //! @return page size ID for page key (the PPD standard mediaOption keyword, e.g. "A4")
0152     //! @note It's an efficient workaround because QPageSize misses this function.
0153     KREPORT_EXPORT QPageSize::PageSizeId pageSizeId(const QString &key);
0154 
0155     //! Like QPageSize::PageSizeId pageSizeId(const QString &key) but returns entire QPageSize object.
0156     KREPORT_EXPORT QPageSize pageSize(const QString &key);
0157 } // KReportUtils
0158 
0159 #endif