KReport

KReportDesignerItemField.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
3 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "KReportDesignerItemField.h"
20#include "KReportItemField.h"
21#include "KReportDesigner.h"
22#include "kreportplugin_debug.h"
23#include "KReportLineStyle.h"
24
25#include <QDomDocument>
26#include <QPainter>
27#include <QGraphicsScene>
28#include <QGraphicsSceneMouseEvent>
29
30//
31// class ReportEntityField
32//
33
34void KReportDesignerItemField::init(QGraphicsScene *scene)
35{
36 if (scene)
37 scene->addItem(this);
38
39 connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
40 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
41
42 dataSourceProperty()->setListData(designer()->fieldKeys(), designer()->fieldNames());
43 setZValue(z());
44
45 updateRenderText(itemDataSource(), m_itemValue->value().toString(), QLatin1String("field"));
46}
47
48// methods (constructors)
49KReportDesignerItemField::KReportDesignerItemField(KReportDesigner *rw, QGraphicsScene *scene, const QPointF &pos)
51{
52 Q_UNUSED(pos);
53 init(scene);
54 setSceneRect(properRect(*rw, getTextRect().width(), getTextRect().height()));
55 nameProperty()->setValue(designer()->suggestEntityName(typeName()));
56}
57
58KReportDesignerItemField::KReportDesignerItemField(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s)
59 : KReportItemField(element), KReportDesignerItemRectBase(d, this)
60{
61 init(s);
62 setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
63}
64
65KReportDesignerItemField* KReportDesignerItemField::clone()
66{
69 QDomNode n;
70 buildXML(&d, &e);
71 n = e.firstChild();
72 return new KReportDesignerItemField(n, designer(), nullptr);
73}
74
75// methods (deconstructor)
76KReportDesignerItemField::~KReportDesignerItemField()
77{}
78
79QRectF KReportDesignerItemField::getTextRect() const
80{
81 return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), renderText());
82}
83
84
85
86void KReportDesignerItemField::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget *widget)
87{
88 Q_UNUSED(option);
89 Q_UNUSED(widget);
90
91 // store any values we plan on changing so we can restore them
92 QFont f = painter->font();
93 QPen p = painter->pen();
94
95
96 painter->setFont(font());
98
99 QColor bg = m_backgroundColor->value().value<QColor>();
100 bg.setAlphaF(m_backgroundOpacity->value().toReal() *0.01);
101
102 painter->setPen(m_foregroundColor->value().value<QColor>());
103
104 painter->fillRect(QGraphicsRectItem::rect(), bg);
105 painter->drawText(rect(), textFlags(), renderText());
106
107
108 if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) {
109 painter->setPen(QPen(Qt::lightGray));
110 } else {
111 painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
112 }
113
114 painter->drawRect(rect());
115
116
117 drawHandles(painter);
118
119 // restore an values before we started just in case
120 painter->setFont(f);
121 painter->setPen(p);
122}
123
124void KReportDesignerItemField::buildXML(QDomDocument *doc, QDomElement *parent)
125{
126 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
127
128 // properties
129 addPropertyAsAttribute(&entity, nameProperty());
130 addPropertyAsAttribute(&entity, dataSourceProperty());
131 addPropertyAsAttribute(&entity, m_verticalAlignment);
132 addPropertyAsAttribute(&entity, m_horizontalAlignment);
133 addPropertyAsAttribute(&entity, m_wordWrap);
134 addPropertyAsAttribute(&entity, m_canGrow);
135 addPropertyAsAttribute(&entity, m_itemValue);
136
137 entity.setAttribute(QLatin1String("report:z-index"), zValue());
138
139 // bounding rect
140 buildXMLRect(doc, &entity, this);
141
142 //text style info
143 buildXMLTextStyle(doc, &entity, textStyle());
144
145 //Line Style
146 buildXMLLineStyle(doc, &entity, lineStyle());
147
148
149#if 0 //Field Totals
150 if (m_trackTotal) {
151 QDomElement tracktotal = doc->createElement("tracktotal");
152 if (m_trackBuiltinFormat)
153 tracktotal.setAttribute("builtin", "true");
154 if (_useSubTotal)
155 tracktotal.setAttribute("subtotal", "true");
156 tracktotal.appendChild(doc.createTextNode(_trackTotalFormat->value().toString()));
157 entity.appendChild(tracktotal);
158 }
159#endif
160
161 parent->appendChild(entity);
162}
163
164void KReportDesignerItemField::slotPropertyChanged(KPropertySet &s, KProperty &p)
165{
166 Q_UNUSED(s);
167
168 if (p.name() == "name") {
169 //For some reason p.oldValue returns an empty string
170 if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
171 p.setValue(oldName());
172 } else {
173 setOldName(p.value().toString());
174 }
175 }
176
177 updateRenderText(itemDataSource(), m_itemValue->value().toString(), QLatin1String("field"));
178
179 KReportDesignerItemRectBase::propertyChanged(s, p);
180 if (designer())designer()->setModified(true);
181}
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QByteArray name() const
void setListData(const QStringList &keys, const QStringList &names)
void updateRenderText(const QString &itemDataSource, const QString &itemStaticValue, const QString &itemType)
Updates the text that is shown for the item in the report designer If itemDataSource is set then it i...
Base class for rectangular report items used within the designer GUI.
The ReportDesigner is the main widget for designing a report.
void setModified(bool modified)
Sets the modified status, defaulting to true for modified.
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.
qreal z() const
Return the z-value in points.
QString itemDataSource() const
QString typeName(const QJsonObject &obj)
QCA_EXPORT void init()
void setAlphaF(float alpha)
QDomElement createElement(const QString &tagName)
QDomText createTextNode(const QString &value)
void setAttribute(const QString &name, const QString &value)
QDomNode appendChild(const QDomNode &newChild)
QDomNode firstChild() const const
QRectF boundingRect(QChar ch) const const
QGraphicsScene * scene() const const
void setZValue(qreal z)
qreal x() const const
qreal y() const const
qreal zValue() const const
QRectF rect() const const
void addItem(QGraphicsItem *item)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void drawRect(const QRect &rectangle)
void drawText(const QPoint &position, const QString &text)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
const QFont & font() const const
const QPen & pen() const const
void setBackgroundMode(Qt::BGMode mode)
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
TransparentMode
lightGray
PenStyle
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 Tue Mar 26 2024 11:21:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.