KReport

KReportItemLine.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "KReportItemLine.h"
19#include "KReportRenderObjects.h"
20#include "KReportUtils.h"
21#include "KReportUtils_p.h"
22#include "kreport_debug.h"
23
24#include <KPropertySet>
25
26#include <QCoreApplication>
27#include <QDomNode>
28
29KReportItemLine::KReportItemLine()
30{
31 createProperties();
32}
33
34KReportItemLine::KReportItemLine(const QDomNode & element)
35 : KReportItemLine()
36{
37 nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
38 setZ(KReportUtils::readZAttribute(element.toElement()));
39
41 QLatin1String("svg:x1"), DEFAULT_ELEMENT_POS_STRING)),
43 QLatin1String("svg:y1"), DEFAULT_ELEMENT_POS_STRING)));
45 QLatin1String("svg:x2"), DEFAULT_ELEMENT_POS_STRING)),
47 QLatin1String("svg:y2"),
48 QString::number(POINT_TO_CM(DEFAULT_ELEMENT_POS_PT.y()
49 + DEFAULT_ELEMENT_SIZE_PT.height()))
50 + QLatin1String("cm"))));
51 setStartPosition(s);
52 setEndPosition(e);
53
54 QDomNodeList nl = element.childNodes();
55 QString n;
56 QDomNode node;
57 for (int i = 0; i < nl.count(); i++) {
58 node = nl.item(i);
59 n = node.nodeName();
60
61 if (n == QLatin1String("report:line-style")) {
63 if (parseReportLineStyleData(node.toElement(), &ls)) {
64 m_lineWeight->setValue(ls.weight());
65 m_lineColor->setValue(ls.color());
66 m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
67 }
68 } else {
69 kreportWarning() << "while parsing line element encountered unknown element: " << n;
70 }
71 }
72}
73
74KReportItemLine::~KReportItemLine()
75{
76}
77
78void KReportItemLine::createProperties()
79{
80 m_start = new KProperty("startposition", QPointF(), QCoreApplication::translate("StartPosition", "Start Position"));
81 m_end = new KProperty("endposition", QPointF(), QCoreApplication::translate("EndPosition", "End Position"));
82
83 m_lineWeight = new KProperty("line-weight", 1.0, tr("Line Weight"));
84 m_lineWeight->setOption("step", 1.0);
85 m_lineColor = new KProperty("line-color", QColor(Qt::black), tr("Line Color"));
86 m_lineStyle = new KProperty("line-style", static_cast<int>(Qt::SolidLine), tr("Line Style"),
88
89 //Remove the unused properies from KReportItemBase
90 propertySet()->property("size").setVisible(false);
91 propertySet()->property("position").setVisible(false);
92
93 propertySet()->addProperty(m_start);
94 propertySet()->addProperty(m_end);
95 propertySet()->addProperty(m_lineWeight);
96 propertySet()->addProperty(m_lineColor);
97 propertySet()->addProperty(m_lineStyle);
98}
99
100KReportLineStyle KReportItemLine::lineStyle() const
101{
103 ls.setWeight(m_lineWeight->value().toReal());
104 ls.setColor(m_lineColor->value().value<QColor>());
105 ls.setPenStyle((Qt::PenStyle)m_lineStyle->value().toInt());
106 return ls;
107}
108
109qreal KReportItemLine::weight() const
110{
111 return m_lineWeight->value().toReal();
112}
113
114void KReportItemLine::setWeight(qreal w)
115{
116 m_lineWeight->setValue(w);
117}
118
119QString KReportItemLine::typeName() const
120{
121 return QLatin1String("line");
122}
123
124int KReportItemLine::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
125 const QVariant &data, KReportScriptHandler *script)
126{
127 Q_UNUSED(script)
128 Q_UNUSED(data)
129
130 OROLine * ln = new OROLine();
131 QPointF s = scenePosition(startPosition());
132 QPointF e = scenePosition(endPosition());
133
134 s += offset;
135 e += offset;
136
137 ln->setStartPoint(s);
138 ln->setEndPoint(e);
139 ln->setLineStyle(lineStyle());
140 if (page) page->insertPrimitive(ln);
141
142 OROLine *l2 = dynamic_cast<OROLine*>(ln->clone());
143 if (l2) {
144 l2->setStartPoint(startPosition());
145 l2->setEndPoint(endPosition());
146
147 if (section) section->addPrimitive(l2);
148 }
149 return 0;
150}
151
152void KReportItemLine::setUnit(const KReportUnit &u)
153{
154 if (unit() == u) {
155 return;
156 }
157 KReportUnit oldunit = unit();
159
160 // convert values
161 m_start->setValue(KReportUnit::convertFromUnitToUnit(m_start->value().toPointF(), oldunit, u),
162 KProperty::ValueOption::IgnoreOld);
163 m_end->setValue(KReportUnit::convertFromUnitToUnit(m_end->value().toPointF(), oldunit, u),
164 KProperty::ValueOption::IgnoreOld);
165
166 m_start->setOption("suffix", u.symbol());
167 m_end->setOption("suffix", u.symbol());
168}
169
170QPointF KReportItemLine::startPosition() const
171{
172 return unit().convertToPoint(m_start->value().toPointF());
173}
174
175void KReportItemLine::setStartPosition(const QPointF &ptPos)
176{
177 m_start->setValue(unit().convertFromPoint(ptPos));
178}
179
180QPointF KReportItemLine::endPosition() const
181{
182 return unit().convertToPoint(m_end->value().toPointF());
183}
184
185void KReportItemLine::setEndPosition(const QPointF &ptPos)
186{
187 m_end->setValue(unit().convertFromPoint(ptPos));
188}
void addProperty(KProperty *property, const QByteArray &group="common")
KProperty & property(const QByteArray &name) const
void setVisible(bool visible)
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
void setOption(const char *name, const QVariant &val)
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
virtual void setUnit(const KReportUnit &u)
Sets unit to a and converts values of position and size property from the old unit to new if needed.
The KReportLineStyle class represents line style.
Converts between different units.
Definition KReportUnit.h:71
static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor=1.0)
convert the given value directly from one unit to another with high accuracy
static QString symbol(KReportUnit::Type type)
Returns the symbol string of given unit type Symbol for Invalid type is empty string.
qreal convertToPoint(qreal value) const
static qreal parseValue(const QString &value, qreal defaultVal=0.0)
Parses common KReport and ODF values, like "10cm", "5mm" to pt.
Defines a line with a width/weight.
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
Represents a single a single row in a document and may contain zero or more OROPrimitives.
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
QString attribute(const QString &name, const QString &defValue) const const
QDomNodeList childNodes() const const
QString nodeName() const const
QDomElement toElement() const const
int count() const const
QDomNode item(int index) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString number(double n, char format, int precision)
SolidLine
int toInt(bool *ok) const const
QPointF toPointF() const const
qreal toReal(bool *ok) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.