KReport

KReportItemLine.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2007-2008 by Adam Pigg ([email protected])
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 
29 KReportItemLine::KReportItemLine()
30 {
31  createProperties();
32 }
33 
34 KReportItemLine::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 
74 KReportItemLine::~KReportItemLine()
75 {
76 }
77 
78 void 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 
100 KReportLineStyle KReportItemLine::lineStyle() const
101 {
102  KReportLineStyle ls;
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 
109 qreal KReportItemLine::weight() const
110 {
111  return m_lineWeight->value().toReal();
112 }
113 
114 void KReportItemLine::setWeight(qreal w)
115 {
116  m_lineWeight->setValue(w);
117 }
118 
119 QString KReportItemLine::typeName() const
120 {
121  return QLatin1String("line");
122 }
123 
124 int 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 
152 void 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),
163  m_end->setValue(KReportUnit::convertFromUnitToUnit(m_end->value().toPointF(), oldunit, u),
165 
166  m_start->setOption("suffix", u.symbol());
167  m_end->setOption("suffix", u.symbol());
168 }
169 
170 QPointF KReportItemLine::startPosition() const
171 {
172  return unit().convertToPoint(m_start->value().toPointF());
173 }
174 
175 void KReportItemLine::setStartPosition(const QPointF &ptPos)
176 {
177  m_start->setValue(unit().convertFromPoint(ptPos));
178 }
179 
180 QPointF KReportItemLine::endPosition() const
181 {
182  return unit().convertToPoint(m_end->value().toPointF());
183 }
184 
185 void KReportItemLine::setEndPosition(const QPointF &ptPos)
186 {
187  m_end->setValue(unit().convertFromPoint(ptPos));
188 }
int count() const const
Converts between different units.
Definition: KReportUnit.h:70
QDomElement toElement() const const
The KReportLineStyle class represents line style.
QString number(int n, int base)
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.
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
void setZ(qreal z)
Sets the z-value for the element.
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
QDomNode item(int index) const const
static qreal parseValue(const QString &value, qreal defaultVal=0.0)
Parses common KReport and ODF values, like "10cm", "5mm" to pt.
QDomNodeList childNodes() const const
static QString symbol(KReportUnit::Type type)
Returns the symbol string of given unit type Symbol for Invalid type is empty string.
QString nodeName() const const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
Represents a single a single row in a document and may contain zero or more OROPrimitives.
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
QString attribute(const QString &name, const QString &defValue) const const
Defines a line with a width/weight.
SolidLine
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:17:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.