KReport

KReportItemCheck.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2009-2010 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 "KReportItemCheck.h"
19#include "KReportRenderObjects.h"
20#include "KReportUtils.h"
21#include "KReportUtils_p.h"
22#include "kreportplugin_debug.h"
23#ifdef KREPORT_SCRIPTING
24#include "KReportScriptHandler.h"
25#endif
26
27#include <KPropertyListData>
28#include <KPropertySet>
29
30#include <QPalette>
31#include <QDomNodeList>
32
33KReportItemCheckBox::KReportItemCheckBox()
34{
35 createProperties();
36}
37
38KReportItemCheckBox::KReportItemCheckBox(const QDomNode &element)
39 : KReportItemCheckBox()
40{
41 nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
42 setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
43 setZ(KReportUtils::readZAttribute(element.toElement()));
44 m_foregroundColor->setValue(QColor(element.toElement().attribute(QLatin1String("fo:foreground-color"))));
45 m_checkStyle->setValue(element.toElement().attribute(QLatin1String("report:check-style")));
46 m_staticValue->setValue(QVariant(element.toElement().attribute(QLatin1String("report:value"))).toBool());
47
48 parseReportRect(element.toElement());
49
50 QDomNodeList nl = element.childNodes();
51 QString n;
52 QDomNode node;
53 for (int i = 0; i < nl.count(); i++) {
54 node = nl.item(i);
55 n = node.nodeName();
56
57 if (n == QLatin1String("report:line-style")) {
59 if (parseReportLineStyleData(node.toElement(), &ls)) {
60 m_lineWeight->setValue(ls.weight());
61 m_lineColor->setValue(ls.color());
62 m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
63 }
64 } else {
65 kreportpluginWarning() << "while parsing check element encountered unknown element: " << n;
66 }
67 }
68
69}
70
71KReportItemCheckBox::~KReportItemCheckBox()
72{
73}
74
75void KReportItemCheckBox::createProperties()
76{
78 QVariantList{ QLatin1String("Cross"), QLatin1String("Tick"), QLatin1String("Dot") },
79 QVariantList{ tr("Cross"), tr("Tick"), tr("Dot") });
80 m_checkStyle = new KProperty("check-style", listData, QLatin1String("Cross"), tr("Style"));
81
82 createDataSourceProperty();
83
84 m_foregroundColor = new KProperty("foreground-color", QColor(Qt::black), tr("Foreground Color"));
85
86 m_lineWeight = new KProperty("line-weight", 1.0, tr("Line Weight"));
87 m_lineWeight->setOption("step", 1.0);
88 m_lineColor = new KProperty("line-color", QColor(Qt::black), tr("Line Color"));
89 m_lineStyle = new KProperty("line-style", static_cast<int>(Qt::SolidLine), tr("Line Style"), QString(), KProperty::LineStyle);
90 m_staticValue = new KProperty("value", QVariant(false), tr("Value"), tr("Value used if not bound to a field"));
91
92 propertySet()->addProperty(m_staticValue);
93 propertySet()->addProperty(m_checkStyle);
94 propertySet()->addProperty(m_foregroundColor);
95 propertySet()->addProperty(m_lineWeight);
96 propertySet()->addProperty(m_lineColor);
97 propertySet()->addProperty(m_lineStyle);
98}
99
100KReportLineStyle KReportItemCheckBox::lineStyle()
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
109// RTTI
110QString KReportItemCheckBox::typeName() const
111{
112 return QLatin1String("check");
113}
114
115int KReportItemCheckBox::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
116 const QVariant &data, KReportScriptHandler *script)
117{
118 OROCheckBox *chk = new OROCheckBox();
119
120 chk->setPosition(scenePosition(position()) + offset);
121 chk->setSize(sceneSize(size()));
122
123 chk->setLineStyle(lineStyle());
124 chk->setForegroundColor(m_foregroundColor->value().value<QColor>());
125 if (m_checkStyle->value().toString() == QLatin1String("Cross")) {
126 chk->setCheckType(OROCheckBox::Type::Cross);
127 } else if (m_checkStyle->value().toString() == QLatin1String("Dot")) {
128 chk->setCheckType(OROCheckBox::Type::Dot);
129 } else {
130 chk->setCheckType(OROCheckBox::Type::Tick);
131 }
132
133 QString str;
134 bool v = false;
135 QString cs = itemDataSource();
136
137 //kreportpluginDebug() << "ControlSource:" << cs;
138 if (!cs.isEmpty()) {
139#ifdef KREPORT_SCRIPTING
140 if (cs.left(1) == QLatin1String("=") && script) {
141 str = script->evaluate(cs.mid(1)).toString();
142 } else
143#else
144 Q_UNUSED(script);
145#endif
146 {
147 str = data.toString();
148 }
149
150 str = str.toLower();
151
152 //kreportpluginDebug() << "Check Value:" << str;
153 if (str == QLatin1String("t") || str == QLatin1String("y") || str == QLatin1String("true") || str == QLatin1String("1"))
154 v = true;
155
156 } else {
157 v = value();
158 }
159
160 chk->setValue(v);
161
162 if (page) {
163 page->insertPrimitive(chk);
164 }
165
166 if (section) {
167 OROCheckBox *chk2 = dynamic_cast<OROCheckBox*>(chk->clone());
168 if (chk2) {
169 chk2->setPosition(scenePosition(position()));
170 section->addPrimitive(chk2);
171 }
172 }
173
174 if (!page) {
175 delete chk;
176 }
177
178 return 0; //Item doesn't stretch the section height
179}
180
181bool KReportItemCheckBox::value() const
182{
183 return m_staticValue->value().toBool();
184}
185
186void KReportItemCheckBox::setValue(bool v)
187{
188 m_staticValue->setValue(v);
189}
void addProperty(KProperty *property, const QByteArray &group="common")
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.
QPointF position() const
Return the position in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
QSizeF size() const
Return the size in points.
QString itemDataSource() const
The KReportLineStyle class represents line style.
Defines checkbox.
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 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)
bool isEmpty() const const
QString left(qsizetype n) const const
QString mid(qsizetype position, qsizetype n) const const
QString toLower() const const
SolidLine
bool toBool() const const
int toInt(bool *ok) const const
qreal toReal(bool *ok) const const
QString toString() 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.