KReport

KReportDesignerItemText.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 "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 
36 void 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 
48  updateRenderText(itemDataSource(), m_itemValue->value().toString(),
49  QLatin1String("textarea"));
50 }
51 
52 KReportDesignerItemText::KReportDesignerItemText(KReportDesigner * rw, QGraphicsScene * scene, const QPointF &pos)
53  : KReportDesignerItemRectBase(rw, this)
54 {
55  Q_UNUSED(pos);
56  init(scene);
57  setSceneRect(properRect(*rw, getTextRect().width(), getTextRect().height()));
58  nameProperty()->setValue(designer()->suggestEntityName(typeName()));
59 }
60 
61 KReportDesignerItemText::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 
68 KReportDesignerItemText* KReportDesignerItemText::clone()
69 {
70  QDomDocument d;
71  QDomElement e = d.createElement(QLatin1String("clone"));
72  QDomNode n;
73  buildXML(&d, &e);
74  n = e.firstChild();
75  return new KReportDesignerItemText(n, designer(), nullptr);
76 }
77 
78 KReportDesignerItemText::~KReportDesignerItemText
79 
80 ()
81 {}
82 
83 QRectF KReportDesignerItemText::getTextRect() const
84 {
85  return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), renderText());
86 }
87 
88 void 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 
124 void 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 
150 void 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 }
QByteArray name() const
void setPen(const QColor &color)
void setBackgroundMode(Qt::BGMode mode)
QDomNode firstChild() const const
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.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
void setAlphaF(qreal alpha)
QGraphicsScene * scene() const const
int value() const const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QDomNode appendChild(const QDomNode &newChild)
Base class for rectangular report items used within the designer GUI.
const QFont & font() const const
QVariant value() const
QPointF pos() const const
The ReportDesigner is the main widget for designing a report.
PenStyle
void setFont(const QFont &font)
lightGray
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.