KReport

KReportDesignerItemField.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 "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 
34 void 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)
49 KReportDesignerItemField::KReportDesignerItemField(KReportDesigner *rw, QGraphicsScene *scene, const QPointF &pos)
50  : KReportDesignerItemRectBase(rw, this)
51 {
52  Q_UNUSED(pos);
53  init(scene);
54  setSceneRect(properRect(*rw, getTextRect().width(), getTextRect().height()));
55  nameProperty()->setValue(designer()->suggestEntityName(typeName()));
56 }
57 
58 KReportDesignerItemField::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 
65 KReportDesignerItemField* KReportDesignerItemField::clone()
66 {
67  QDomDocument d;
68  QDomElement e = d.createElement(QLatin1String("clone"));
69  QDomNode n;
70  buildXML(&d, &e);
71  n = e.firstChild();
72  return new KReportDesignerItemField(n, designer(), nullptr);
73 }
74 
75 // methods (deconstructor)
76 KReportDesignerItemField::~KReportDesignerItemField()
77 {}
78 
79 QRectF KReportDesignerItemField::getTextRect() const
80 {
81  return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), renderText());
82 }
83 
84 
85 
86 void 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 
124 void 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 
164 void 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 }
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
QDomText createTextNode(const QString &value)
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)
int value() const const
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.
const QFont & font() const const
QVariant value() 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.