KReport

KReportDesignerItemLabel.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2001-2007 by OpenMFG, LLC ([email protected])
3  * Copyright (C) 2007-2008 by Adam Pigg ([email protected])
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 
33 void 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());
42  setFlag(ItemIsFocusable);
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)
57 KReportDesignerItemLabel::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 
68 KReportDesignerItemLabel::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 
75 KReportDesignerItemLabel* KReportDesignerItemLabel::clone()
76 {
77  QDomDocument d;
78  QDomElement e = d.createElement(QLatin1String("clone"));
79  QDomNode n;
80  buildXML(&d, &e);
81  n = e.firstChild();
82  return new KReportDesignerItemLabel(n, designer(), nullptr);
83 }
84 
85 // methods (deconstructor)
86 KReportDesignerItemLabel::~KReportDesignerItemLabel()
87 {}
88 
89 QRectF KReportDesignerItemLabel::getTextRect() const
90 {
91  return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), m_text->value().toString());
92 }
93 
94 void 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 
124  painter->drawRect(QGraphicsRectItem::rect());
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 
134 void 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 
158 void 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 
178 void 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 
200 void KReportDesignerItemLabel::exitInlineEditingMode()
201 {
202  if (m_inlineEdit->isVisible()) {
203  m_inlineEdit->setVisible(false);
204  setText(m_inlineEdit->toPlainText());
205  }
206 }
207 
208 void KReportDesignerItemLabel::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
209 {
210  Q_UNUSED(event);
211  enterInlineEditingMode();
212 }
213 
214 void KReportDesignerItemLabel::keyReleaseEvent(QKeyEvent* event)
215 {
216  if (event->key() == Qt::Key_F2) {
217  enterInlineEditingMode();
218  } else {
220  }
221 }
QByteArray name() const
void setPen(const QColor &color)
void setBackgroundMode(Qt::BGMode mode)
QDomNode firstChild() const const
void setDocumentMargin(qreal margin)
void addItem(QGraphicsItem *item)
void drawRect(const QRectF &rectangle)
QCA_EXPORT void init()
TransparentMode
const QPen & pen() const const
QDomElement createElement(const QString &tagName)
void drawText(const QPointF &position, const QString &text)
void fillRect(const QRectF &rectangle, const QBrush &brush)
void setAttribute(const QString &name, const QString &value)
QRectF boundingRect(const QString &text) const const
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
Subclass of QGraphicsTextItem which simply forces its boundingRect to be the same as its parent.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
void setAlphaF(qreal alpha)
virtual void keyReleaseEvent(QKeyEvent *event)
int value() const const
void setPlainText(const QString &text)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void select(QTextCursor::SelectionType selection)
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QDomNode appendChild(const QDomNode &newChild)
QRectF rect() const const
Base class for rectangular report items used within the designer GUI.
void update(Part *part, const QByteArray &data, qint64 dataSize)
const QFont & font() const const
QVariant value() const
The ReportDesigner is the main widget for designing a report.
PenStyle
void setFont(const QFont &font)
QString toString() 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.