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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
0003  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0004  * Copyright (C) 2010-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 "KReportSection.h"
0021 #include "KReportDesign_p.h"
0022 #include "KReportDocument.h"
0023 #include "KReportPluginInterface.h"
0024 #include "KReportPluginManager.h"
0025 #include "KReportItemLine.h"
0026 #include "kreport_debug.h"
0027 
0028 #if 0 // needed by lupdate to avoid "Qualifying with unknown namespace/class"
0029 class KReportSection { Q_DECLARE_TR_FUNCTIONS(KReportSection) };
0030 #endif
0031 
0032 #if 0
0033 KReportSection::KReportSection(const QDomElement & elemSource, KReportReportData* report)
0034  : QObject(report)
0035 {
0036     setObjectName(elemSource.tagName());
0037 
0038     if (objectName() != QLatin1String("report:section")) {
0039         m_valid = false;
0040         return;
0041     }
0042 
0043     m_type = sectionTypeFromString(KReportUtils::readSectionTypeNameAttribute(elemSource));
0044     if (m_type == KReportSection::None) {
0045         m_valid = false;
0046         return;
0047     }
0048     createProperties(elemSource);
0049 
0050     m_backgroundColor->setValue(QColor(elemSource.attribute(QLatin1String("fo:background-color"))));
0051 
0052     KReportPluginManager* manager = KReportPluginManager::self();
0053 
0054     QDomNodeList section = elemSource.childNodes();
0055     for (int nodeCounter = 0; nodeCounter < section.count(); nodeCounter++) {
0056         QDomElement elemThis = section.item(nodeCounter).toElement();
0057         QString n = elemThis.tagName();
0058         if (n.startsWith(QLatin1String("report:"))) {
0059             QString reportItemName = n.mid(qstrlen("report:"));
0060             if (reportItemName == QLatin1String("line")) {
0061                 KReportItemLine * line = new KReportItemLine(elemThis);
0062                 m_objects.append(line);
0063                 continue;
0064             }
0065             KReportPluginInterface *plugin = manager->plugin(reportItemName);
0066             if (plugin) {
0067                 QObject *obj = plugin->createRendererInstance(elemThis);
0068                 if (obj) {
0069                     KReportItemBase *krobj = dynamic_cast<KReportItemBase*>(obj);
0070                     if (krobj) {
0071                         m_objects.append(krobj);
0072                     }
0073                     continue;
0074                 }
0075             }
0076         }
0077         kreportWarning() << "While parsing section encountered an unknown element: " << n;
0078     }
0079     std::sort(m_objects.begin(), m_objects.end(), zLessThan);
0080     m_valid = true;
0081 }
0082 bool KReportSection::zLessThan(KReportItemBase* s1, KReportItemBase* s2)
0083 {
0084     return s1->Z < s2->Z;
0085 }
0086 
0087 bool KReportSection::xLessThan(KReportItemBase* s1, KReportItemBase* s2)
0088 {
0089     return s1->position().toPoint().x() < s2->position().toPoint().x();
0090 }
0091 
0092 #endif
0093 
0094 KReportSection::~KReportSection()
0095 {
0096 }
0097 
0098 qreal KReportSection::height() const
0099 {
0100     return d->height >= 0.0 ? d->height : KReportSection::defaultHeight();
0101 }
0102 
0103 QColor KReportSection::backgroundColor() const
0104 {
0105     return d->backgroundColor.isValid() ? d->backgroundColor : KReportSection::defaultBackgroundColor();
0106 }
0107 
0108 QList<KReportElement> KReportSection::elements() const
0109 {
0110     return d->elements;
0111 }
0112 
0113 bool KReportSection::addElement(const KReportElement &element)
0114 {
0115     if (d->elementsSet.contains(element)) {
0116         kreportWarning() << "Section already contains element" << element;
0117         return false;
0118     }
0119     d->elements.append(element);
0120     d->elementsSet.insert(element);
0121     return true;
0122 }
0123 
0124 bool KReportSection::insertElement(int i, const KReportElement &element)
0125 {
0126     if (i < 0 || i > d->elements.count()) {
0127         kreportWarning() << "Could not insert element" << element << "at index" << i << "into section";
0128         return false;
0129     }
0130     if (d->elementsSet.contains(element)) {
0131         kreportWarning() << "Section already contains element" << element;
0132         return false;
0133     }
0134     d->elements.insert(i, element);
0135     d->elementsSet.insert(element);
0136     return true;
0137 }
0138 
0139 bool KReportSection::removeElement(const KReportElement &element)
0140 {
0141     if (!d->elementsSet.remove(element)) {
0142         kreportWarning() << "Could not find element" << element << "in section";
0143         return false;
0144     }
0145     if (!d->elements.removeOne(element)) {
0146         kreportCritical() << "Could not find element" << element << "in section's list but found in section's set";
0147         return false;
0148     }
0149     return true;
0150 }
0151 
0152 bool KReportSection::removeElementAt(int i)
0153 {
0154     if (i < 0 || i > (d->elements.count() - 1)) {
0155         kreportWarning() << "Could not find element at index" << i << "in section";
0156         return false;
0157     }
0158     KReportElement e = d->elements.takeAt(i);
0159     if (!d->elementsSet.remove(e)) {
0160         kreportWarning() << "Could not find element" << e << "in section";
0161         return false;
0162     }
0163     return true;
0164 }
0165 
0166 KReportSection::Data* KReportSection::Data::clone() const
0167 {
0168     Data *data = new Data(*this);
0169     data->elements.clear();
0170     KReportElement eClone;
0171     foreach(const KReportElement &el, elements) {
0172         eClone = el.clone();
0173         data->elements.append(eClone);
0174         data->elementsSet.insert(eClone);
0175     }
0176     return data;
0177 }
0178 
0179 //static
0180 qreal KReportSection::defaultHeight()
0181 {
0182     return KReportDesignGlobal::self()->defaultSectionHeight;
0183 }
0184 
0185 //static
0186 void KReportSection::setDefaultHeight(qreal ptHeight)
0187 {
0188     KReportDesignGlobal::self()->defaultSectionHeight = ptHeight;
0189 }
0190 
0191 //static
0192 QColor KReportSection::defaultBackgroundColor()
0193 {
0194     return KReportDesignGlobal::self()->defaultSectionBackgroundColor;
0195 }
0196 
0197 //static
0198 void KReportSection::setDefaultBackgroundColor(const QColor &color)
0199 {
0200     KReportDesignGlobal::self()->defaultSectionBackgroundColor = color;
0201 }