Warning, file /libraries/kreport/src/common/KReportDesign.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2001-2007 by OpenMFG, LLC <info@openmfg.com>
0003  * Copyright (C) 2007-2010 by Adam Pigg <adam@piggz.co.uk>
0004  * Copyright (C) 2011-2015 Jarosław Staniek <staniek@kde.org>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "KReportDesign.h"
0021 #include "KReportDesign_p.h"
0022 #include "KReportElement.h"
0023 #include "KReportUnit.h"
0024 #include "KReportUtils.h"
0025 #include "KReportPluginManager.h"
0026 #include "KReportPluginInterface.h"
0027 
0028 #include <QDomDocument>
0029 #include <QDomElement>
0030 #include <QPrinterInfo>
0031 
0032 class Q_DECL_HIDDEN KReportDesignReadingStatus::Private
0033 {
0034 public:
0035     QString errorMessage;
0036     QString errorDetails;
0037     int errorLineNumber = -1;
0038     int errorColumnNumber = -1;
0039 };
0040 
0041 KReportDesignReadingStatus::KReportDesignReadingStatus() : d(new Private)
0042 {
0043 }
0044 
0045 KReportDesignReadingStatus::~KReportDesignReadingStatus()
0046 {
0047     delete d;
0048 }
0049 
0050 KReportDesignReadingStatus::KReportDesignReadingStatus(const KReportDesignReadingStatus& other) : d(new Private)
0051 {
0052     *this = other;
0053 }
0054 
0055 KReportDesignReadingStatus& KReportDesignReadingStatus::operator=(const KReportDesignReadingStatus &other)
0056 {
0057     if (this != &other) {
0058             setErrorMessage(other.errorMessage());
0059             setErrorDetails(other.errorDetails());
0060             setErrorLineNumber(other.errorLineNumber());
0061             setErrorColumnNumber(other.errorColumnNumber());
0062     }
0063 
0064     return *this;
0065 }
0066 
0067 
0068 bool KReportDesignReadingStatus::isError() const
0069 {
0070     return d->errorLineNumber >= 0 && d->errorColumnNumber >= 0;
0071 }
0072 
0073 int KReportDesignReadingStatus::errorColumnNumber() const
0074 {
0075     return d->errorColumnNumber;
0076 }
0077 
0078 QString KReportDesignReadingStatus::errorDetails() const
0079 {
0080     return d->errorDetails;
0081 }
0082 
0083 QString KReportDesignReadingStatus::errorMessage() const
0084 {
0085     return d->errorMessage;
0086 }
0087 
0088 
0089 int KReportDesignReadingStatus::errorLineNumber() const
0090 {
0091     return d->errorLineNumber;
0092 }
0093 
0094 void KReportDesignReadingStatus::setErrorColumnNumber(int column)
0095 {
0096     d->errorColumnNumber = column;
0097 }
0098 
0099 void KReportDesignReadingStatus::setErrorDetails(const QString& details)
0100 {
0101     d->errorDetails = details;
0102 }
0103 
0104 void KReportDesignReadingStatus::setErrorLineNumber(int line)
0105 {
0106     d->errorLineNumber = line;
0107 }
0108 
0109 void KReportDesignReadingStatus::setErrorMessage(const QString& msg)
0110 {
0111     d->errorMessage = msg;
0112 }
0113 
0114 QDebug operator<<(QDebug dbg, const KReportDesignReadingStatus& status)
0115 {
0116     if (status.isError()) {
0117         dbg.nospace() << qPrintable(
0118             QString::fromLatin1("KReportDesignReadingStatus: errorMessage=\"%1\" "
0119                                 "errorDetails=\"%2\" line=%3 column=%4")
0120                 .arg(status.errorMessage()).arg(status.errorDetails())
0121                 .arg(status.errorLineNumber()).arg(status.errorColumnNumber()));
0122     } else {
0123         dbg.nospace() << "KReportDesignReadingStatus: OK";
0124     }
0125     return dbg.space();
0126 }
0127 
0128 //-----------------------------------
0129 
0130 KReportDesign::KReportDesign()
0131     : d(new Private(this))
0132 {
0133 }
0134 
0135 KReportDesign::~KReportDesign()
0136 {
0137     delete d;
0138 }
0139 
0140 bool KReportDesign::setContent(const QString &text, KReportDesignReadingStatus *status)
0141 {
0142     QDomDocument doc;
0143     QString errorDetails;
0144     int errorLine;
0145     int errorColumn;
0146 
0147     if (!doc.setContent(text, &errorDetails, &errorLine, &errorColumn))
0148     {
0149         if (status) {
0150             status->setErrorMessage(tr("Could not parse XML document."));
0151             status->setErrorDetails(errorDetails);
0152             status->setErrorLineNumber(errorLine);
0153             status->setErrorColumnNumber(errorColumn);
0154         }
0155         return false;
0156     }
0157     bool ret = d->processDocument(doc, status);
0158     if (!ret && status) {
0159         status->setErrorMessage(tr("Error in XML document."));
0160     }
0161     return ret;
0162 }
0163 
0164 QString KReportDesign::toString(int indent) const
0165 {
0166     Q_UNUSED(indent);
0167     //! @todo
0168     return QString();
0169 }
0170 
0171 QPageLayout KReportDesign::pageLayout() const
0172 {
0173     return d->pageLayout;
0174 }
0175 
0176 QString KReportDesign::title() const
0177 {
0178     return d->title;
0179 }
0180 
0181 void KReportDesign::setTitle(const QString &title)
0182 {
0183     d->title = title;
0184 }
0185 
0186 void KReportDesign::setPageLayout(const QPageLayout &pageLayout)
0187 {
0188     d->pageLayout = pageLayout;
0189     d->pageLayout.setUnits(QPageLayout::Point);
0190 }
0191 
0192 KReportElement KReportDesign::createElement(const QString &typeName, QString *errorMessage)
0193 {
0194     QDomElement el;
0195     KReportDesignReadingStatus status;
0196     KReportPluginInterface* plugin = d->findPlugin(typeName, el, &status);
0197     if (!plugin) {
0198         if (errorMessage) {
0199             *errorMessage = status.errorMessage();
0200         }
0201         return KReportElement();
0202     }
0203     return plugin->createElement();
0204 }
0205 
0206 bool KReportDesign::hasSection(KReportSection::Type type) const
0207 {
0208     const int index = static_cast<int>(type) - 1;
0209     if (0 <= index && index < d->sections.length()) {
0210         return d->sections[index];
0211     }
0212     return false;
0213 }
0214 
0215 KReportSection KReportDesign::section(KReportSection::Type type) const
0216 {
0217     const int index = static_cast<int>(type) - 1;
0218     if (0 <= index && index < d->sections.length()) {
0219         KReportSection *section = d->sections[index];
0220         if (section) {
0221             return *section;
0222         }
0223     }
0224     return KReportSection();
0225 }
0226 
0227 void KReportDesign::addSection(const KReportSection &section)
0228 {
0229     const int index = static_cast<int>(section.type()) - 1;
0230     if (0 <= index && index < d->sections.length()) {
0231         if (d->sections[index]) {
0232             *d->sections[index] = section;
0233         } else {
0234             d->sections[index] = new KReportSection(section);
0235         }
0236     }
0237 }
0238 
0239 // static
0240 QPageLayout KReportDesign::defaultPageLayout()
0241 {
0242     QPageLayout layout = KReportDesignGlobal::self()->defaultPageLayout;
0243     if (!layout.pageSize().isValid()) {
0244         if (!QPrinterInfo::defaultPrinter().isNull()) {
0245             layout.setPageSize(QPrinterInfo::defaultPrinter().defaultPageSize());
0246         }
0247         else {
0248             layout.setPageSize(QPageSize(DEFAULT_PAGE_SIZE));
0249         }
0250     }
0251     return layout;
0252 }
0253 
0254 // static
0255 void KReportDesign::setDefaultPageLayout(const QPageLayout &pageLayout)
0256 {
0257     KReportDesignGlobal::self()->defaultPageLayout = pageLayout;
0258     KReportDesignGlobal::self()->defaultPageLayout.setUnits(QPageLayout::Point);
0259 }
0260 
0261 #ifdef KREPORT_SCRIPTING
0262 QString KReportDesign::script() const
0263 {
0264     return d->script;
0265 }
0266 #endif