KReport

KReportItemBase.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 "KReportItemBase.h"
19 
20 #include "KReportUtils.h"
21 #include "KReportUtils_p.h"
22 
23 #include <KPropertySet>
24 #include <KPropertyListData>
25 #include <QApplication>
26 #include <QDomElement>
27 
28 class Q_DECL_HIDDEN KReportItemBase::Private
29 {
30 public:
31  Private();
32  ~Private();
33 
34  void setUnit(const KReportUnit& u, bool force)
35  {
36  if (!force && unit == u) {
37  return;
38  }
39  const QSignalBlocker blocker(set);
40  KReportUnit oldunit = unit;
41  unit = u;
42  // convert values
43 
44  if (positionProperty) {
45  positionProperty->setValue(KReportUnit::convertFromUnitToUnit(
46  positionProperty->value().toPointF(), oldunit, u),
48 
49  positionProperty->setOption("suffix", u.symbol());
50 
51  }
52  if (sizeProperty) {
53  sizeProperty->setValue(
54  KReportUnit::convertFromUnitToUnit(sizeProperty->value().toSizeF(), oldunit, u),
56 
57  sizeProperty->setOption("suffix", u.symbol());
58  }
59 
60  }
61 
62  KPropertySet *set;
63  KProperty *nameProperty;
64  KProperty *sizeProperty;
65  KProperty *positionProperty;
66  KProperty *dataSourceProperty = nullptr;
67  QString oldName;
68  qreal z = 0;
69  KReportUnit unit;
70 };
71 
72 KReportItemBase::Private::Private()
73 {
74  set = new KPropertySet();
75  nameProperty = new KProperty("name", QString(), tr("Name"), tr("Object Name"));
76  nameProperty->setValueSyncPolicy(KProperty::ValueSyncPolicy::FocusOut);
77 
78  positionProperty = new KProperty("position", QPointF(), QCoreApplication::translate("ItemPosition", "Position"));
79  sizeProperty = new KProperty("size", QSizeF(), QCoreApplication::translate("ItemSize", "Size"));
80  setUnit(DEFAULT_UNIT, true);
81 
82  set->addProperty(nameProperty);
83  set->addProperty(positionProperty);
84  set->addProperty(sizeProperty);
85 }
86 
87 KReportItemBase::Private::~Private()
88 {
89  delete set;
90 }
91 
92 
93 KReportItemBase::KReportItemBase() : d(new Private())
94 {
95  connect(propertySet(), &KPropertySet::propertyChanged,
96  this, &KReportItemBase::propertyChanged);
97 
98  connect(propertySet(), &KPropertySet::aboutToDeleteProperty, this, &KReportItemBase::aboutToDeleteProperty);
99 }
100 
101 KReportItemBase::~KReportItemBase()
102 {
103  delete d;
104 }
105 
106 void KReportItemBase::createDataSourceProperty()
107 {
108  if (d->dataSourceProperty) {
109  return;
110  }
111  d->dataSourceProperty
112  = new KProperty("item-data-source", new KPropertyListData, QVariant(), tr("Data Source"));
113  d->dataSourceProperty->setOption("extraValueAllowed", true);
114  d->set->addProperty(d->dataSourceProperty);
115 }
116 
117 bool KReportItemBase::parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts)
118 {
119  return KReportUtils::parseReportTextStyleData(elemSource, ts);
120 }
121 
122 bool KReportItemBase::parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls)
123 {
124  return KReportUtils::parseReportLineStyleData(elemSource, ls);
125 }
126 
127 
128 bool KReportItemBase::parseReportRect(const QDomElement & elemSource)
129 {
130  const QRectF r(KReportUtils::readRectAttributes(elemSource, DEFAULT_ELEMENT_RECT_PT));
131  setPosition(r.topLeft());
132  setSize(r.size());
133  return true;
134 }
135 
136 KReportUnit KReportItemBase::unit() const
137 {
138  return d->unit;
139 }
140 
142 {
143  d->setUnit(u, false);
144 }
145 
146 int KReportItemBase::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
147  const QVariant &data, KReportScriptHandler* script)
148 {
149  Q_UNUSED(page)
150  Q_UNUSED(section)
151  Q_UNUSED(offset)
152  Q_UNUSED(data)
153  Q_UNUSED(script)
154  return 0;
155 }
156 
157 int KReportItemBase::renderReportData(OROPage *page, OROSection *section, const QPointF &offset,
158  KReportDataSource *dataSource, KReportScriptHandler* script)
159 {
160  Q_UNUSED(page)
161  Q_UNUSED(section)
162  Q_UNUSED(offset)
163  Q_UNUSED(dataSource)
164  Q_UNUSED(script)
165  return 0;
166 }
167 
169 {
170  return d->dataSourceProperty ? d->dataSourceProperty->value().toString() : QString();
171 }
172 
173 void KReportItemBase::setItemDataSource(const QString& source)
174 {
175  if (d->dataSourceProperty && d->dataSourceProperty->value() != source) {
176  d->dataSourceProperty->setValue(source);
177  }
178 }
179 
180 KPropertySet* KReportItemBase::propertySet()
181 {
182  return d->set;
183 }
184 
186 {
187  return false;
188 }
189 
190 QString KReportItemBase::entityName() const
191 {
192  return d->nameProperty->value().toString();
193 }
194 
195 void KReportItemBase::setEntityName(const QString& n)
196 {
197  d->nameProperty->setValue(n);
198 }
199 
200 KProperty* KReportItemBase::nameProperty()
201 {
202  return d->nameProperty;
203 }
204 
205 QString KReportItemBase::oldName() const
206 {
207  return d->oldName;
208 }
209 
210 void KReportItemBase::setOldName(const QString& old)
211 {
212  d->oldName = old;
213 }
214 
215 KProperty* KReportItemBase::dataSourceProperty()
216 {
217  return d->dataSourceProperty;
218 }
219 
221 {
222  return d->unit.convertToPoint(d->positionProperty->value().toPointF());
223 }
224 
226 {
227  return d->unit.convertToPoint(d->sizeProperty->value().toSizeF());
228 }
229 
230 const KPropertySet * KReportItemBase::propertySet() const
231 {
232  return d->set;
233 }
234 
236 {
237  const qreal x = POINT_TO_INCH(ptPos.x()) * KReportPrivate::dpiX();
238  const qreal y = POINT_TO_INCH(ptPos.y()) * KReportPrivate::dpiY();
239  return QPointF(x, y);
240 }
241 
243 {
244  const qreal w = POINT_TO_INCH(ptSize.width()) * KReportPrivate::dpiX();
245  const qreal h = POINT_TO_INCH(ptSize.height()) * KReportPrivate::dpiY();
246  return QSizeF(w, h);
247 }
248 
249 qreal KReportItemBase::z() const
250 {
251  return d->z;
252 }
253 
255 {
256  d->z = z;
257 }
258 
260 {
261  d->positionProperty->setValue(d->unit.convertFromPoint(ptPos));
262 }
263 
264 void KReportItemBase::setSize(const QSizeF &ptSize)
265 {
266  d->sizeProperty->setValue(d->unit.convertFromPoint(ptSize));
267 }
268 
270 {
271  const qreal x = INCH_TO_POINT(pos.x() / KReportPrivate::dpiX());
272  const qreal y = INCH_TO_POINT(pos.y() / KReportPrivate::dpiY());
273  return QPointF(x, y);
274 }
275 
277 {
278  qreal w = INCH_TO_POINT(size.width() / KReportPrivate::dpiX());
279  qreal h = INCH_TO_POINT(size.height() / KReportPrivate::dpiY());
280  return QSizeF(w, h);
281 }
282 
283 void KReportItemBase::propertyChanged(KPropertySet& s, KProperty& p)
284 {
285  Q_UNUSED(s)
286  Q_UNUSED(p)
287 }
288 
289 void KReportItemBase::aboutToDeleteProperty(KPropertySet& set, KProperty& property)
290 {
291  Q_UNUSED(set)
292  if (property.name() == "size") {
293  d->sizeProperty = nullptr;
294  }
295  else if (property.name() == "position") {
296  d->positionProperty = nullptr;
297  }
298 }
static QSizeF sizeFromScene(const QSizeF &size)
Helper function mapping from screen units to points, size is in pixels.
Base class for items that are drawn syncronously.
void propertyChanged(KPropertySet &set, KProperty &property)
qreal height() const const
virtual int renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset, const QVariant &data, KReportScriptHandler *script)
Render the item into a primitive which is used by the second stage renderer.
Converts between different units.
Definition: KReportUnit.h:70
The KReportLineStyle class represents line style.
void aboutToDeleteProperty(KPropertySet &set, KProperty &property)
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.
virtual int renderReportData(OROPage *page, OROSection *section, const QPointF &offset, KReportDataSource *dataSource, KReportScriptHandler *script)
Render a complex item that uses a sub query as a data source.
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
void setPosition(const QPointF &ptPos)
Sets position for the element.
QPointF position() const
Return the position in points.
Abstraction of report data source.
qreal z() const
Return the z-value in points.
static QString symbol(KReportUnit::Type type)
Returns the symbol string of given unit type Symbol for Invalid type is empty string.
QString itemDataSource() const
static QPointF positionFromScene(const QPointF &pos)
Helper function mapping from screen units to points, pos is in pixels.
void setSize(const QSizeF &ptSize)
Sets size for the element.
qreal x() const const
qreal y() const const
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
QSizeF size() const
Return the size in points.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual bool supportsSubQuery() const
Override if the item uses a sub query and linked fields, such as a chart or sub-report.
QVariant property(const char *name) const const
qreal width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:09:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.