KReport

KReportDesignerItemText.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 "KReportDesignerItemText.h"
20#include "KReportDesignerItemBase.h"
21#include "KReportDesigner.h"
22#include "KReportLineStyle.h"
23
24#include <KPropertySet>
25#include <QDomDocument>
26#include <QPainter>
27#include <QGraphicsScene>
28#include <QGraphicsSceneMouseEvent>
29#include "kreportplugin_debug.h"
30
31//
32// class ReportEntityText
33//
34// methods (constructors)
35
36void KReportDesignerItemText::init(QGraphicsScene *scene)
37{
38 //setFlags(ItemIsSelectable | ItemIsMovable);
39 if (scene)
40 scene->addItem(this);
41
42 connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
43 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
44
45 dataSourceProperty()->setListData(designer()->fieldKeys(), designer()->fieldNames());
46 setZValue(z());
47
49 QLatin1String("textarea"));
50}
51
52KReportDesignerItemText::KReportDesignerItemText(KReportDesigner * rw, QGraphicsScene * scene, const QPointF &pos)
54{
55 Q_UNUSED(pos);
56 init(scene);
57 setSceneRect(properRect(*rw, getTextRect().width(), getTextRect().height()));
58 nameProperty()->setValue(designer()->suggestEntityName(typeName()));
59}
60
61KReportDesignerItemText::KReportDesignerItemText(const QDomNode & element, KReportDesigner *d, QGraphicsScene *scene)
62 : KReportItemText(element), KReportDesignerItemRectBase(d, this)
63{
64 init(scene);
65 setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
66}
67
68KReportDesignerItemText* KReportDesignerItemText::clone()
69{
72 QDomNode n;
73 buildXML(&d, &e);
74 n = e.firstChild();
75 return new KReportDesignerItemText(n, designer(), nullptr);
76}
77
78KReportDesignerItemText::~KReportDesignerItemText
79
80()
81{}
82
83QRectF KReportDesignerItemText::getTextRect() const
84{
85 return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), renderText());
86}
87
88void KReportDesignerItemText::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
89{
90 Q_UNUSED(option);
91 Q_UNUSED(widget)
92
93 // store any values we plan on changing so we can restore them
94 QFont f = painter->font();
95 QPen p = painter->pen();
96
97 painter->setFont(font());
99
100 QColor bg = m_backgroundColor->value().value<QColor>();
101 bg.setAlphaF(m_backgroundOpacity->value().toReal()*0.01);
102
103 painter->setPen(m_foregroundColor->value().value<QColor>());
104
105 painter->fillRect(rect(), bg);
106 painter->drawText(rect(), textFlags(), renderText());
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 painter->drawRect(rect());
114
115 painter->setPen(m_foregroundColor->value().value<QColor>());
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 KReportDesignerItemText::buildXML(QDomDocument *doc, QDomElement *parent)
125{
126 //kreportpluginDebug();
127 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
128
129 // properties
130 addPropertyAsAttribute(&entity, nameProperty());
131 addPropertyAsAttribute(&entity, dataSourceProperty());
132 addPropertyAsAttribute(&entity, m_verticalAlignment);
133 addPropertyAsAttribute(&entity, m_horizontalAlignment);
134 entity.setAttribute(QLatin1String("report:bottom-padding"), m_bottomPadding);
135 entity.setAttribute(QLatin1String("report:z-index"), z());
136 addPropertyAsAttribute(&entity, m_itemValue);
137
138 // bounding rect
139 buildXMLRect(doc, &entity, this);
140
141 //text style info
142 buildXMLTextStyle(doc, &entity, textStyle());
143
144 //Line Style
145 buildXMLLineStyle(doc, &entity, lineStyle());
146
147 parent->appendChild(entity);
148}
149
150void KReportDesignerItemText::slotPropertyChanged(KPropertySet &s, KProperty &p)
151{
152 Q_UNUSED(s);
153
154 if (p.name() == "name") {
155 //For some reason p.oldValue returns an empty string
156 if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
157 p.setValue(oldName());
158 } else {
159 setOldName(p.value().toString());
160 }
161 }
162
163 KReportDesignerItemRectBase::propertyChanged(s, p);
164 if (designer()) {
165 designer()->setModified(true);
166 }
167
168 updateRenderText(itemDataSource(), m_itemValue->value().toString(),
169 QLatin1String("textarea"));
170}
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
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
KOSM_EXPORT const char * typeName(Type type)
void setAlphaF(float alpha)
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
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
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 Fri Dec 27 2024 11:52:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.