KReport

KReportDesignerItemLabel.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 "KReportDesignerItemLabel.h"
20#include "KReportDesignerItemBase.h"
21#include "KReportDesigner.h"
22#include "KReportDesignerSectionScene.h"
23#include "KReportLineStyle.h"
24
25#include <QKeyEvent>
26#include <QDomDocument>
27#include <QPainter>
28#include <QGraphicsScene>
29#include <QGraphicsSceneMouseEvent>
30#include <QTextCursor>
31#include <QTextDocument>
32
33void KReportDesignerItemLabel::init(QGraphicsScene *scene)
34{
35 if (scene)
36 scene->addItem(this);
37
38 connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
39 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
40
41 setZValue(z());
43
44 m_inlineEdit = new BoundedTextItem(this);
45 m_inlineEdit->setVisible(false);
46 m_inlineEdit->setFlag(ItemIsFocusable);
47 m_inlineEdit->setFlag(ItemIsSelectable, false);
48 QTextDocument *doc = new QTextDocument(m_inlineEdit);
49 doc->setDocumentMargin(0);
50 doc->setPlainText(text());
51 m_inlineEdit->setDocument(doc);
52
53 connect(m_inlineEdit, SIGNAL(exitEditMode()), this, SLOT(exitInlineEditingMode()));
54}
55
56// methods (constructors)
57KReportDesignerItemLabel::KReportDesignerItemLabel(KReportDesigner* d, QGraphicsScene * scene, const QPointF &pos)
59{
60 Q_UNUSED(pos);
61 init(scene);
62 setSceneRect(properRect(*d, getTextRect().width(), getTextRect().height()));
63 nameProperty()->setValue(designer()->suggestEntityName(typeName()));
64
65 enterInlineEditingMode();
66}
67
68KReportDesignerItemLabel::KReportDesignerItemLabel(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s)
69 : KReportItemLabel(element), KReportDesignerItemRectBase(d, this), m_inlineEdit(nullptr)
70{
71 init(s);
72 setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
73}
74
75KReportDesignerItemLabel* KReportDesignerItemLabel::clone()
76{
79 QDomNode n;
80 buildXML(&d, &e);
81 n = e.firstChild();
82 return new KReportDesignerItemLabel(n, designer(), nullptr);
83}
84
85// methods (deconstructor)
86KReportDesignerItemLabel::~KReportDesignerItemLabel()
87{}
88
89QRectF KReportDesignerItemLabel::getTextRect() const
90{
91 return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), m_text->value().toString());
92}
93
94void KReportDesignerItemLabel::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
95{
96 Q_UNUSED(option);
97 Q_UNUSED(widget);
98
99 if (m_inlineEdit->isVisible()) {
100 return;
101 }
102
103 // store any values we plan on changing so we can restore them
104 QFont f = painter->font();
105 QPen p = painter->pen();
106
107 painter->setFont(font());
109
110 QColor bg = m_backgroundColor->value().value<QColor>();
111 bg.setAlphaF(m_backgroundOpacity->value().toReal() * 0.01);
112
113 painter->setPen(m_foregroundColor->value().value<QColor>());
114
115 painter->fillRect(QGraphicsRectItem::rect(), bg);
116 painter->drawText(rect(), textFlags(), text());
117
118 if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) {
119 painter->setPen(QPen(QColor(224, 224, 224)));
120 } else {
121 painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
122 }
123
125 painter->setPen(m_foregroundColor->value().value<QColor>());
126
127 drawHandles(painter);
128
129 // restore an values before we started just in case
130 painter->setFont(f);
131 painter->setPen(p);
132}
133
134void KReportDesignerItemLabel::buildXML(QDomDocument *doc, QDomElement *parent)
135{
136 //kreportpluginDebug();
137 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
138
139 // properties
140 addPropertyAsAttribute(&entity, nameProperty());
141 addPropertyAsAttribute(&entity, m_text);
142 addPropertyAsAttribute(&entity, m_verticalAlignment);
143 addPropertyAsAttribute(&entity, m_horizontalAlignment);
144 entity.setAttribute(QLatin1String("report:z-index"), z());
145
146 // bounding rect
147 buildXMLRect(doc, &entity, this);
148
149 //text style info
150 buildXMLTextStyle(doc, &entity, textStyle());
151
152 //Line Style
153 buildXMLLineStyle(doc, &entity, lineStyle());
154
155 parent->appendChild(entity);
156}
157
158void KReportDesignerItemLabel::slotPropertyChanged(KPropertySet &s, KProperty &p)
159{
160 Q_UNUSED(s);
161
162 if (p.name() == "name") {
163 //For some reason p.oldValue returns an empty string
164 if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
165 p.setValue(oldName());
166 } else {
167 setOldName(p.value().toString());
168 }
169 } else if (p.name() == "caption") {
170 m_inlineEdit->setPlainText(p.value().toString());
171 }
172
173 KReportDesignerItemRectBase::propertyChanged(s, p);
174 if (designer()) designer()->setModified(true);
175
176}
177
178void KReportDesignerItemLabel::enterInlineEditingMode()
179{
180 if (!m_inlineEdit->isVisible()) {
181 m_inlineEdit->setVisible(true);
182 m_inlineEdit->setPlainText(text());
183 m_inlineEdit->setFocus();
184
185 QTextCursor c = m_inlineEdit->textCursor();
187 m_inlineEdit->setTextCursor(c);
188
189 m_inlineEdit->setFont(m_font->value().value<QFont>());
190 m_inlineEdit->setDefaultTextColor(m_foregroundColor->value().value<QColor>());
191 m_inlineEdit->setBackgroudColor(m_backgroundColor->value().value<QColor>());
192 m_inlineEdit->setBackgroudOpacity(m_backgroundOpacity->value().toDouble() / 100.0);
193 m_inlineEdit->setForegroundColor(m_foregroundColor->value().value<QColor>());
194 m_inlineEdit->setFont(m_font->value().value<QFont>());
195
196 update();
197 }
198}
199
200void KReportDesignerItemLabel::exitInlineEditingMode()
201{
202 if (m_inlineEdit->isVisible()) {
203 m_inlineEdit->setVisible(false);
204 setText(m_inlineEdit->toPlainText());
205 }
206}
207
208void KReportDesignerItemLabel::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
209{
210 Q_UNUSED(event);
211 enterInlineEditingMode();
212}
213
214void KReportDesignerItemLabel::keyReleaseEvent(QKeyEvent* event)
215{
216 if (event->key() == Qt::Key_F2) {
217 enterInlineEditingMode();
218 } else {
220 }
221}
Subclass of QGraphicsTextItem which simply forces its boundingRect to be the same as its parent.
void setBackgroudOpacity(qreal opacity)
Sets background opacity, 0..1.0.
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QByteArray name() const
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.
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
bool isVisible() const const
virtual void keyReleaseEvent(QKeyEvent *event)
QGraphicsScene * scene() const const
void setFlag(GraphicsItemFlag flag, bool enabled)
void setFocus(Qt::FocusReason focusReason)
void setVisible(bool visible)
void setZValue(qreal z)
void update(const QRectF &rect)
qreal x() const const
qreal y() const const
QRectF rect() const const
void addItem(QGraphicsItem *item)
void setDefaultTextColor(const QColor &col)
void setDocument(QTextDocument *document)
void setFont(const QFont &font)
void setPlainText(const QString &text)
QString toPlainText() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
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
PenStyle
void select(SelectionType selection)
void setDocumentMargin(qreal margin)
void setPlainText(const QString &text)
double toDouble(bool *ok) const const
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 Jul 26 2024 12:00:43 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.