KReport

KReportItemField.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 "KReportItemField.h"
19 #include "KReportRenderObjects.h"
20 #include "KReportUtils.h"
21 #include "kreportplugin_debug.h"
22 #ifdef KREPORT_SCRIPTING
23 #include "KReportScriptHandler.h"
24 #endif
25 
26 #include <KPropertyListData>
27 #include <KPropertySet>
28 
29 #include <QPalette>
30 #include <QDomNodeList>
31 #include <QFontMetrics>
32 #include <QApplication>
33 
34 KReportItemField::KReportItemField()
35 {
36  createProperties();
37 }
38 
39 KReportItemField::KReportItemField(const QDomNode & element)
40  : KReportItemField()
41 {
42  nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
43  setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
44  m_itemValue->setValue(element.toElement().attribute(QLatin1String("report:value")));
45  setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
46  m_horizontalAlignment->setValue(element.toElement().attribute(QLatin1String("report:horizontal-align")));
47  m_verticalAlignment->setValue(element.toElement().attribute(QLatin1String("report:vertical-align")));
48  KReportUtils::setPropertyValue(m_canGrow, element.toElement());
49  KReportUtils::setPropertyValue(m_wordWrap, element.toElement());
50 
51  parseReportRect(element.toElement());
52 
53  QDomNodeList nl = element.childNodes();
54  QString n;
55  QDomNode node;
56  for (int i = 0; i < nl.count(); i++) {
57  node = nl.item(i);
58  n = node.nodeName();
59 
60  if (n == QLatin1String("report:text-style")) {
61  KReportTextStyleData ts;
62  if (parseReportTextStyleData(node.toElement(), &ts)) {
63  m_backgroundColor->setValue(ts.backgroundColor);
64  m_foregroundColor->setValue(ts.foregroundColor);
65  m_backgroundOpacity->setValue(ts.backgroundOpacity);
66  m_font->setValue(ts.font);
67 
68  }
69  } else if (n == QLatin1String("report:line-style")) {
71  if (parseReportLineStyleData(node.toElement(), &ls)) {
72  m_lineWeight->setValue(ls.weight());
73  m_lineColor->setValue(ls.color());
74  m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
75  }
76  } else {
77  kreportpluginWarning() << "while parsing field element encountered unknown element: " << n;
78  }
79  }
80 }
81 
82 KReportItemField::~KReportItemField()
83 {
84 }
85 
86 void KReportItemField::createProperties()
87 {
88  createDataSourceProperty();
89 
90  m_itemValue = new KProperty("value", QString(), tr("Value"), tr("Value used if not bound to a field"));
91 
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", QApplication::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  m_wordWrap = new KProperty("word-wrap", QVariant(false), tr("Word Wrap"));
120  m_canGrow = new KProperty("can-grow", QVariant(false), tr("Can Grow"));
121 
122  //! @todo I do not think we need these
123 #if 0 //Field Totals
124  m_trackTotal = new KProperty("trackTotal", QVariant(false), futureI18n("Track Total"));
125  m_trackBuiltinFormat = new KProperty("trackBuiltinFormat", QVariant(false), futureI18n("Track Builtin Format"));
126  _useSubTotal = new KProperty("useSubTotal", QVariant(false), futureI18n("Use Sub Total"_));
127  _trackTotalFormat = new KProperty("trackTotalFormat", QString(), futureI18n("Track Total Format"));
128 #endif
129 
130  propertySet()->addProperty(m_itemValue);
131  propertySet()->addProperty(m_horizontalAlignment);
132  propertySet()->addProperty(m_verticalAlignment);
133  propertySet()->addProperty(m_font);
134  propertySet()->addProperty(m_backgroundColor);
135  propertySet()->addProperty(m_foregroundColor);
136  propertySet()->addProperty(m_backgroundOpacity);
137  propertySet()->addProperty(m_lineWeight);
138  propertySet()->addProperty(m_lineColor);
139  propertySet()->addProperty(m_lineStyle);
140  propertySet()->addProperty(m_wordWrap);
141  propertySet()->addProperty(m_canGrow);
142 
143  //_set->addProperty ( _trackTotal );
144  //_set->addProperty ( _trackBuiltinFormat );
145  //_set->addProperty ( _useSubTotal );
146  //_set->addProperty ( _trackTotalFormat );
147 }
148 
149 int KReportItemField::textFlags() const
150 {
151  int flags;
152  QString t;
153  t = m_horizontalAlignment->value().toString();
154  if (t == QLatin1String("center"))
155  flags = Qt::AlignHCenter;
156  else if (t == QLatin1String("right"))
157  flags = Qt::AlignRight;
158  else
159  flags = Qt::AlignLeft;
160 
161  t = m_verticalAlignment->value().toString();
162  if (t == QLatin1String("center"))
163  flags |= Qt::AlignVCenter;
164  else if (t == QLatin1String("bottom"))
165  flags |= Qt::AlignBottom;
166  else
167  flags |= Qt::AlignTop;
168 
169  if (m_wordWrap->value().toBool() == true) {
170  flags |= Qt::TextWordWrap;
171  }
172  return flags;
173 }
174 
175 KReportTextStyleData KReportItemField::textStyle() const
176 {
177  KReportTextStyleData d;
178  d.backgroundColor = m_backgroundColor->value().value<QColor>();
179  d.foregroundColor = m_foregroundColor->value().value<QColor>();
180  d.font = m_font->value().value<QFont>();
181  d.backgroundOpacity = m_backgroundOpacity->value().toInt();
182 
183  return d;
184 }
185 
186 KReportLineStyle KReportItemField::lineStyle() const
187 {
188  KReportLineStyle ls;
189  ls.setWeight(m_lineWeight->value().toReal());
190  ls.setColor(m_lineColor->value().value<QColor>());
191  ls.setPenStyle((Qt::PenStyle)m_lineStyle->value().toInt());
192  return ls;
193 }
194 // RTTI
195 QString KReportItemField::typeName() const
196 {
197  return QLatin1String("field");
198 }
199 
200 int KReportItemField::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
201  const QVariant &data, KReportScriptHandler *script)
202 {
203  OROTextBox * tb = new OROTextBox();
204  tb->setPosition(scenePosition(position()) + offset);
205  tb->setSize(sceneSize(size()));
206  tb->setFont(font());
207  tb->setFlags(textFlags());
208  tb->setTextStyle(textStyle());
209  tb->setLineStyle(lineStyle());
210  tb->setCanGrow(m_canGrow->value().toBool());
211  tb->setWordWrap(m_wordWrap->value().toBool());
212 
213  QString str;
214 
215  QString ids = itemDataSource();
216  if (!ids.isEmpty()) {
217 #ifdef KREPORT_SCRIPTING
218  if (ids.left(1) == QLatin1String("=") && script) { //Everything after = is treated as code
219  if (!ids.contains(QLatin1String("PageTotal()"))) {
220  QVariant v = script->evaluate(ids.mid(1));
221  str = v.toString();
222  } else {
223  str = ids.mid(1);
224  tb->setRequiresPostProcessing(true);
225  }
226  } else
227 #else
228  Q_UNUSED(script);
229 #endif
230  str = data.toString();
231  } else {
232  str = m_itemValue->value().toString();
233  }
234 
235  tb->setText(str);
236 
237  //Work out the size of the text
238  if (tb->canGrow()) {
239  QRectF r;
240  if (tb->wordWrap()) {
241  //Grow vertically
242  QFontMetricsF metrics(font());
243  QRectF temp(tb->position().x(), tb->position().y(), tb->size().width(), 5000); // a large vertical height
244  r = metrics.boundingRect(temp, tb->flags(), str);
245  } else {
246  //Grow Horizontally
247  QFontMetricsF metrics(font());
248  QRectF temp(tb->position().x(), tb->position().y(), 5000, tb->size().height()); // a large vertical height
249  r = metrics.boundingRect(temp, tb->flags(), str);
250  }
251  tb->setSize(r.size() + QSize(4,4));
252  }
253 
254  if (page) {
255  page->insertPrimitive(tb);
256  }
257 
258  if (section) {
259  OROPrimitive *clone = tb->clone();
260  clone->setPosition(scenePosition(position()));
261  section->addPrimitive(clone);
262  }
263  int height = scenePosition(position()).y() + tb->size().height();
264  //If there is no page to add the item to, delete it now because it wont be deleted later
265  if (!page) {
266  delete tb;
267  }
268  return height;
269 }
AlignHCenter
int count() const const
qreal height() 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.
void setZ(qreal z)
Sets the z-value for the element.
TextWordWrap
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
QDomNode item(int index) const const
bool isEmpty() const const
int value() const const
QDomNodeList childNodes() const const
QString nodeName() const const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
qreal x() const const
qreal y() const const
Represents a single a single row in a document and may contain zero or more OROPrimitives.
QString left(int n) const const
QFont font()
QSizeF size() const const
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
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QPointF position() const
QString mid(int position, int n) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString toString() const const
qreal width() const const
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.