KReport

KReportDesignerItemChart.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17#include "KReportDesignerItemChart.h"
18
19#include <KReportDesignerItemBase.h>
20#include "KReportDesigner.h"
21
22#include <KProperty>
23#include <KPropertySet>
24
25#include <QGraphicsScene>
26#include <QGraphicsSceneMouseEvent>
27#include <QDomDocument>
28#include <QPainter>
29
30void KReportDesignerItemChart::init(QGraphicsScene* scene, KoReportDesigner *d)
31{
32 setPos(0, 0);
33
34 if (scene)
35 scene->addItem(this);
36
37 connect(m_set, SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
38 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
39
40 KoReportDesignerItemRectBase::init(&m_pos, &m_size, m_set, d);
41 setZValue(Z);
42
43 connect(m_reportDesigner, SIGNAL(reportDataChanged()), this, SLOT(slotReportDataChanged()));
44}
45
46KReportDesignerItemChart::KReportDesignerItemChart(KoReportDesigner * rd, QGraphicsScene* scene, const QPointF &pos)
47 : KoReportDesignerItemRectBase(rd)
48{
49 Q_UNUSED(pos);
50 init(scene, rd);
51 setSceneRect(properRect(*rd, m_dpiX, m_dpiY));
52 m_name->setValue(m_reportDesigner->suggestEntityName(typeName()));
53}
54
55KReportDesignerItemChart::KReportDesignerItemChart(QDomNode *element, KoReportDesigner * rd, QGraphicsScene* scene) :
56 KReportItemChart(element), KoReportDesignerItemRectBase(rd)
57{
58 init(scene, rd);
59 populateData();
60 setSceneRect(m_pos.toScene(), m_size.toScene());
61
62}
63
64KReportDesignerItemChart::~KReportDesignerItemChart()
65{
66}
67
68void KReportDesignerItemChart::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget *widget)
69{
70 Q_UNUSED(option);
71 Q_UNUSED(widget);
72
73 // store any values we plan on changing so we can restore them
74 QFont f = painter->font();
75 QPen p = painter->pen();
76 QColor bg = Qt::white;
77
78 painter->fillRect(QGraphicsRectItem::rect(), bg);
79
80 if (m_chartWidget) {
81 m_chartWidget->setFixedSize(m_size.toScene().toSize());
82 painter->drawImage(rect().left(), rect().top(),
83 QPixmap::grabWidget(m_chartWidget).toImage(),
84 0, 0, rect().width(), rect().height());
85 }
86 bg.setAlpha(255);
87
88 painter->setBackground(bg);
89 painter->setPen(Qt::black);
90 painter->drawText(rect(), 0, dataSourceAndObjectTypeName(itemDataSource(), "chart"));
91 painter->setPen(QPen(QColor(224, 224, 224)));
92 painter->drawRect(rect());
94
95 drawHandles(painter);
96
97 // restore an values before we started just in case
98 painter->setFont(f);
99 painter->setPen(p);
100}
101
102KReportDesignerItemChart* KReportDesignerItemChart::clone()
103{
104 QDomDocument d;
105 QDomElement e = d.createElement("clone");
106 QDomNode n;
107 buildXML(&d, &e);
108 n = e.firstChild();
109 return new KReportDesignerItemChart(n, designer(), 0);
110}
111
112void KReportDesignerItemChart::buildXML(QDomDocument *doc, QDomElement *parent)
113{
114 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
115
116 // properties
117 addPropertyAsAttribute(&entity, nameProperty());
118 addPropertyAsAttribute(&entity, dataSourceProperty());
119 addPropertyAsAttribute(&entity, m_chartType);
120 addPropertyAsAttribute(&entity, m_chartSubType);
121 addPropertyAsAttribute(&entity, m_threeD);
122 addPropertyAsAttribute(&entity, m_colorScheme);
123 addPropertyAsAttribute(&entity, m_aa);
124 addPropertyAsAttribute(&entity, m_xTitle);
125 addPropertyAsAttribute(&entity, m_yTitle);
126 addPropertyAsAttribute(&entity, m_backgroundColor);
127 addPropertyAsAttribute(&entity, m_displayLegend);
128 addPropertyAsAttribute(&entity, m_legendPosition);
129 addPropertyAsAttribute(&entity, m_legendOrientation);
130 addPropertyAsAttribute(&entity, m_linkChild);
131 addPropertyAsAttribute(&entity, m_linkMaster);
132 entity.setAttribute("report:z-index", zValue());
133
134 // bounding rect
135 buildXMLRect(doc, &entity, &m_pos, &m_size);
136
137 parent->appendChild(entity);
138}
139
140void KReportDesignerItemChart::slotPropertyChanged(KPropertySet &s, KProperty &p)
141{
142 if (p.name() == "name") {
143 //For some reason p.oldValue returns an empty string
144 if (!m_reportDesigner->isEntityNameUnique(p.value().toString(), this)) {
145 p.setValue(m_oldName);
146 } else {
147 m_oldName = p.value().toString();
148 }
149 } else if (p.name() == "three-dimensions") {
150 set3D(p.value().toBool());
151 } else if (p.name() == "antialiased") {
152 setAA(p.value().toBool());
153 } else if (p.name() == "color-scheme") {
154 setColorScheme(p.value().toString());
155 } else if (p.name() == "data-source") {
156 populateData();
157 } else if (p.name() == "title-x-axis" || p.name() == "title-y-axis") {
158 setAxis(m_xTitle->value().toString(), m_yTitle->value().toString());
159 } else if (p.name() == "background-color") {
160 setBackgroundColor(p.value().value<QColor>());
161 } else if (p.name() == "display-legend") {
162 if (m_chartWidget && p.value().toBool()) {
163 populateData();
164 }
165 } else if (p.name() == "legend-position") {
166 if (m_chartWidget) {
167 populateData();
168 }
169 } else if (p.name() == "legend-orientation") {
170 if (m_chartWidget) {
171 populateData();
172 }
173 } else if (p.name() == "chart-type") {
174 if (m_chartWidget) {
175 populateData();
176 //m_chartWidget->setType((KDChart::Widget::ChartType) m_chartType->value().toInt());
177 }
178 } else if (p.name() == "chart-sub-type") {
179 if (m_chartWidget) {
180 m_chartWidget->setSubType((KDChart::Widget::SubType) m_chartSubType->value().toInt());
181 }
182 }
183
184 KoReportDesignerItemRectBase::propertyChanged(s, p);
185 if (m_reportDesigner) m_reportDesigner->setModified(true);
186}
187
188void KReportDesignerItemChart::mousePressEvent(QGraphicsSceneMouseEvent * event)
189{
190 if (m_reportDesigner->reportData()) {
191 QStringList ql = m_reportDesigner->reportData()->dataSources();
192 QStringList qn = m_reportDesigner->reportData()->dataSourceNames();
193 dataSourceProperty()->setListData(ql, qn);
194 }
195 KoReportDesignerItemRectBase::mousePressEvent(event);
196}
197
198void KReportDesignerItemChart::slotReportDataChanged()
199{
200 setConnection(m_reportDesigner->reportData());
201}
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QByteArray name() const
void setListData(const QStringList &keys, const QStringList &names)
QString itemDataSource() const
KOSM_EXPORT const char * typeName(Type type)
QCA_EXPORT void init()
void setAlpha(int alpha)
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
QDomNode firstChild() const const
QGraphicsScene * scene() const const
void setPos(const QPointF &pos)
void setZValue(qreal z)
qreal zValue() const const
QRectF rect() const const
void addItem(QGraphicsItem *item)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
QObject * parent() const const
void drawImage(const QPoint &point, const QImage &image)
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 setBackground(const QBrush &brush)
void setBackgroundMode(Qt::BGMode mode)
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
TransparentMode
QTextStream & left(QTextStream &stream)
bool toBool() const const
int toInt(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 Mon Nov 18 2024 12:19:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.