KReport

KReportSection.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
3 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
4 * Copyright (C) 2010-2015 Jarosław Staniek <staniek@kde.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "KReportSection.h"
21#include "KReportDesign_p.h"
22#include "KReportDocument.h"
23#include "KReportPluginInterface.h"
24#include "KReportPluginManager.h"
25#include "KReportItemLine.h"
26#include "kreport_debug.h"
27
28#if 0 // needed by lupdate to avoid "Qualifying with unknown namespace/class"
29class KReportSection { Q_DECLARE_TR_FUNCTIONS(KReportSection) };
30#endif
31
32#if 0
33KReportSection::KReportSection(const QDomElement & elemSource, KReportReportData* report)
34 : QObject(report)
35{
36 setObjectName(elemSource.tagName());
37
38 if (objectName() != QLatin1String("report:section")) {
39 m_valid = false;
40 return;
41 }
42
43 m_type = sectionTypeFromString(KReportUtils::readSectionTypeNameAttribute(elemSource));
44 if (m_type == KReportSection::None) {
45 m_valid = false;
46 return;
47 }
48 createProperties(elemSource);
49
50 m_backgroundColor->setValue(QColor(elemSource.attribute(QLatin1String("fo:background-color"))));
51
52 KReportPluginManager* manager = KReportPluginManager::self();
53
54 QDomNodeList section = elemSource.childNodes();
55 for (int nodeCounter = 0; nodeCounter < section.count(); nodeCounter++) {
56 QDomElement elemThis = section.item(nodeCounter).toElement();
57 QString n = elemThis.tagName();
58 if (n.startsWith(QLatin1String("report:"))) {
59 QString reportItemName = n.mid(qstrlen("report:"));
60 if (reportItemName == QLatin1String("line")) {
61 KReportItemLine * line = new KReportItemLine(elemThis);
62 m_objects.append(line);
63 continue;
64 }
65 KReportPluginInterface *plugin = manager->plugin(reportItemName);
66 if (plugin) {
67 QObject *obj = plugin->createRendererInstance(elemThis);
68 if (obj) {
69 KReportItemBase *krobj = dynamic_cast<KReportItemBase*>(obj);
70 if (krobj) {
71 m_objects.append(krobj);
72 }
73 continue;
74 }
75 }
76 }
77 kreportWarning() << "While parsing section encountered an unknown element: " << n;
78 }
79 std::sort(m_objects.begin(), m_objects.end(), zLessThan);
80 m_valid = true;
81}
82bool KReportSection::zLessThan(KReportItemBase* s1, KReportItemBase* s2)
83{
84 return s1->Z < s2->Z;
85}
86
87bool KReportSection::xLessThan(KReportItemBase* s1, KReportItemBase* s2)
88{
89 return s1->position().toPoint().x() < s2->position().toPoint().x();
90}
91
92#endif
93
94KReportSection::~KReportSection()
95{
96}
97
98qreal KReportSection::height() const
99{
100 return d->height >= 0.0 ? d->height : KReportSection::defaultHeight();
101}
102
104{
105 return d->backgroundColor.isValid() ? d->backgroundColor : KReportSection::defaultBackgroundColor();
106}
107
109{
110 return d->elements;
111}
112
114{
115 if (d->elementsSet.contains(element)) {
116 kreportWarning() << "Section already contains element" << element;
117 return false;
118 }
119 d->elements.append(element);
120 d->elementsSet.insert(element);
121 return true;
122}
123
125{
126 if (i < 0 || i > d->elements.count()) {
127 kreportWarning() << "Could not insert element" << element << "at index" << i << "into section";
128 return false;
129 }
130 if (d->elementsSet.contains(element)) {
131 kreportWarning() << "Section already contains element" << element;
132 return false;
133 }
134 d->elements.insert(i, element);
135 d->elementsSet.insert(element);
136 return true;
137}
138
140{
141 if (!d->elementsSet.remove(element)) {
142 kreportWarning() << "Could not find element" << element << "in section";
143 return false;
144 }
145 if (!d->elements.removeOne(element)) {
146 kreportCritical() << "Could not find element" << element << "in section's list but found in section's set";
147 return false;
148 }
149 return true;
150}
151
153{
154 if (i < 0 || i > (d->elements.count() - 1)) {
155 kreportWarning() << "Could not find element at index" << i << "in section";
156 return false;
157 }
158 KReportElement e = d->elements.takeAt(i);
159 if (!d->elementsSet.remove(e)) {
160 kreportWarning() << "Could not find element" << e << "in section";
161 return false;
162 }
163 return true;
164}
165
166KReportSection::Data* KReportSection::Data::clone() const
167{
168 Data *data = new Data(*this);
169 data->elements.clear();
170 KReportElement eClone;
171 foreach(const KReportElement &el, elements) {
172 eClone = el.clone();
173 data->elements.append(eClone);
174 data->elementsSet.insert(eClone);
175 }
176 return data;
177}
178
179//static
181{
182 return KReportDesignGlobal::self()->defaultSectionHeight;
183}
184
185//static
187{
188 KReportDesignGlobal::self()->defaultSectionHeight = ptHeight;
189}
190
191//static
193{
194 return KReportDesignGlobal::self()->defaultSectionBackgroundColor;
195}
196
197//static
199{
200 KReportDesignGlobal::self()->defaultSectionBackgroundColor = color;
201}
The KReportElement class represents a functional visual element of a report design.
Base class for items that are drawn syncronously.
QPointF position() const
Return the position in points.
An interface for plugins delivering KReport elements.
virtual QObject * createRendererInstance(const QDomNode &element)=0
Manager class for finding and loading available plugins.
The KReportSection class represents a section of a report design.
bool insertElement(int i, const KReportElement &element)
Adds element element to this section at index position i.
static QColor defaultBackgroundColor()
bool removeElement(const KReportElement &element)
Removes element element from this section.
static qreal defaultHeight()
bool removeElementAt(int i)
Removes element from index position i from this section.
QList< KReportElement > elements
static void setDefaultHeight(qreal height)
Sets default height for report sections in points.
static void setDefaultBackgroundColor(const QColor &color)
Sets default background color for report sections.
bool addElement(const KReportElement &element)
Adds element element to this section.
bool isValid() const const
QString attribute(const QString &name, const QString &defValue) const const
QString tagName() const const
QDomNodeList childNodes() const const
QDomElement toElement() const const
int count() const const
QDomNode item(int index) const const
int x() const const
QPoint toPoint() const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:31 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.