KReport

KReportItemLabel.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 "KReportItemLabel.h"
19 #include "KReportRenderObjects.h"
20 #include "KReportUtils.h"
21 #include "kreportplugin_debug.h"
22 
23 #include <KPropertySet>
24 #include <KPropertyListData>
25 
26 #include <QFontDatabase>
27 #include <QPalette>
28 #include <QDomNodeList>
29 
30 KReportItemLabel::KReportItemLabel()
31 {
32  createProperties();
33 }
34 
35 KReportItemLabel::KReportItemLabel(const QDomNode & element)
36  : KReportItemLabel()
37 {
38  nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
39  m_text->setValue(element.toElement().attribute(QLatin1String("report:caption")));
40  setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
41  m_horizontalAlignment->setValue(element.toElement().attribute(QLatin1String("report:horizontal-align")));
42  m_verticalAlignment->setValue(element.toElement().attribute(QLatin1String("report:vertical-align")));
43 
44  parseReportRect(element.toElement());
45 
46  QDomNodeList nl = element.childNodes();
47  QString n;
48  QDomNode node;
49  for (int i = 0; i < nl.count(); i++) {
50  node = nl.item(i);
51  n = node.nodeName();
52 
53  if (n == QLatin1String("report:text-style")) {
54  KReportTextStyleData ts;
55  if (parseReportTextStyleData(node.toElement(), &ts)) {
56  m_backgroundColor->setValue(ts.backgroundColor);
57  m_foregroundColor->setValue(ts.foregroundColor);
58  m_backgroundOpacity->setValue(ts.backgroundOpacity);
59  m_font->setValue(ts.font);
60 
61  }
62  } else if (n == QLatin1String("report:line-style")) {
64  if (parseReportLineStyleData(node.toElement(), &ls)) {
65  m_lineWeight->setValue(ls.weight());
66  m_lineColor->setValue(ls.color());
67  m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
68  }
69  } else {
70  kreportpluginWarning() << "while parsing label element encountered unknown element: " << n;
71  }
72  }
73 }
74 
75 KReportItemLabel::~KReportItemLabel()
76 {
77 }
78 
79 QString KReportItemLabel::text() const
80 {
81  return m_text->value().toString();
82 }
83 
84 void KReportItemLabel::setText(const QString& t)
85 {
86  m_text->setValue(t);
87 }
88 
89 void KReportItemLabel::createProperties()
90 {
91  m_text = new KProperty("caption", QLatin1String("Label"), tr("Caption"));
92  KPropertyListData *listData = new KPropertyListData(
93  { QLatin1String("left"), QLatin1String("center"), QLatin1String("right") },
94  QVariantList{ tr("Left"), tr("Center"), tr("Right") });
95  m_horizontalAlignment = new KProperty("horizontal-align", listData, QLatin1String("left"),
96  tr("Horizontal Alignment"));
97 
98  listData = new KPropertyListData(
99  { QLatin1String("top"), QLatin1String("center"), QLatin1String("bottom") },
100  QVariantList{ tr("Top"), tr("Center"), tr("Bottom") });
101  m_verticalAlignment = new KProperty("vertical-align", listData, QLatin1String("center"),
102  tr("Vertical Alignment"));
103 
104  m_font = new KProperty("font", QFontDatabase::systemFont(QFontDatabase::GeneralFont), tr("Font"), tr("Font"));
105 
106  m_backgroundColor = new KProperty("background-color", QColor(Qt::white), tr("Background Color"));
107  m_foregroundColor = new KProperty("foreground-color", QColor(Qt::black), tr("Foreground Color"));
108 
109  m_backgroundOpacity = new KProperty("background-opacity", QVariant(0), tr("Background Opacity"));
110  m_backgroundOpacity->setOption("max", 100);
111  m_backgroundOpacity->setOption("min", 0);
112  m_backgroundOpacity->setOption("suffix", QLatin1String("%"));
113 
114  m_lineWeight = new KProperty("line-weight", 1.0, tr("Line Weight"));
115  m_lineWeight->setOption("step", 1.0);
116  m_lineColor = new KProperty("line-color", QColor(Qt::black), tr("Line Color"));
117  m_lineStyle = new KProperty("line-style", static_cast<int>(Qt::NoPen), tr("Line Style"), QString(), KProperty::LineStyle);
118 
119  propertySet()->addProperty(m_text);
120  propertySet()->addProperty(m_horizontalAlignment);
121  propertySet()->addProperty(m_verticalAlignment);
122  propertySet()->addProperty(m_font);
123  propertySet()->addProperty(m_backgroundColor);
124  propertySet()->addProperty(m_foregroundColor);
125  propertySet()->addProperty(m_backgroundOpacity);
126  propertySet()->addProperty(m_lineWeight);
127  propertySet()->addProperty(m_lineColor);
128  propertySet()->addProperty(m_lineStyle);
129 }
130 
131 Qt::Alignment KReportItemLabel::textFlags() const
132 {
133  Qt::Alignment align;
134  QString t;
135  t = m_horizontalAlignment->value().toString();
136  if (t == QLatin1String("center"))
137  align = Qt::AlignHCenter;
138  else if (t == QLatin1String("right"))
139  align = Qt::AlignRight;
140  else
141  align = Qt::AlignLeft;
142 
143  t = m_verticalAlignment->value().toString();
144  if (t == QLatin1String("center"))
145  align |= Qt::AlignVCenter;
146  else if (t == QLatin1String("bottom"))
147  align |= Qt::AlignBottom;
148  else
149  align |= Qt::AlignTop;
150 
151  return align;
152 }
153 
154 KReportTextStyleData KReportItemLabel::textStyle() const
155 {
156  KReportTextStyleData d;
157  d.backgroundColor = m_backgroundColor->value().value<QColor>();
158  d.foregroundColor = m_foregroundColor->value().value<QColor>();
159  d.font = m_font->value().value<QFont>();
160  d.backgroundOpacity = m_backgroundOpacity->value().toInt();
161  return d;
162 }
163 
164 KReportLineStyle KReportItemLabel::lineStyle() const
165 {
166  KReportLineStyle ls;
167  ls.setWeight(m_lineWeight->value().toReal());
168  ls.setColor(m_lineColor->value().value<QColor>());
169  ls.setPenStyle((Qt::PenStyle)m_lineStyle->value().toInt());
170  return ls;
171 }
172 
173 // RTTI
174 QString KReportItemLabel::typeName() const
175 {
176  return QLatin1String("label");
177 }
178 
179 int KReportItemLabel::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
180  const QVariant &data, KReportScriptHandler *script)
181 {
182  Q_UNUSED(data)
183  Q_UNUSED(script)
184 
185  OROTextBox * tb = new OROTextBox();
186  tb->setPosition(scenePosition(position()) + offset);
187  tb->setSize(sceneSize(size()));
188  tb->setFont(font());
189  tb->setText(text());
190  tb->setFlags(textFlags());
191  tb->setTextStyle(textStyle());
192  tb->setLineStyle(lineStyle());
193 
194  if (page) {
195  page->insertPrimitive(tb);
196  }
197 
198  if (section) {
199  OROPrimitive *clone = tb->clone();
200  clone->setPosition(scenePosition(position()));
201  section->addPrimitive(clone);
202  }
203 
204  if (!page) {
205  delete tb;
206  }
207 
208  return 0; //Item doesn't stretch the section height
209 }
210 
typedef Alignment
int count() const const
QDomElement toElement() const const
A text box primitive it defines a box region and text that will be rendered inside that region,...
The KReportLineStyle class represents line style.
QSizeF size() const
Get the size of the barcode.
void setZ(qreal z)
Sets the z-value for the element.
QFont systemFont(QFontDatabase::SystemFont type)
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
QDomNode item(int index) const const
int value() const const
QDomNodeList childNodes() const const
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.
Represents the basic primitive with a position and type. Other primitives are subclasses with a defin...
QString attribute(const QString &name, const QString &defValue) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QPointF position() const
Get the position of the barcode.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Feb 8 2023 04:21:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.