File indexing completed on 2024-05-12 04:43:20

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 #include "KReportDesignerItemChart.h"
0018 
0019 #include <KReportDesignerItemBase.h>
0020 #include "KReportDesigner.h"
0021 
0022 #include <KProperty>
0023 #include <KPropertySet>
0024 
0025 #include <QGraphicsScene>
0026 #include <QGraphicsSceneMouseEvent>
0027 #include <QDomDocument>
0028 #include <QPainter>
0029 
0030 void KReportDesignerItemChart::init(QGraphicsScene* scene, KoReportDesigner *d)
0031 {
0032     setPos(0, 0);
0033 
0034     if (scene)
0035         scene->addItem(this);
0036 
0037     connect(m_set, SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
0038             this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
0039 
0040     KoReportDesignerItemRectBase::init(&m_pos, &m_size, m_set, d);
0041     setZValue(Z);
0042 
0043     connect(m_reportDesigner, SIGNAL(reportDataChanged()), this, SLOT(slotReportDataChanged()));
0044 }
0045 
0046 KReportDesignerItemChart::KReportDesignerItemChart(KoReportDesigner * rd, QGraphicsScene* scene, const QPointF &pos)
0047         : KoReportDesignerItemRectBase(rd)
0048 {
0049     Q_UNUSED(pos);
0050     init(scene, rd);
0051     setSceneRect(properRect(*rd, m_dpiX, m_dpiY));
0052     m_name->setValue(m_reportDesigner->suggestEntityName(typeName()));
0053 }
0054 
0055 KReportDesignerItemChart::KReportDesignerItemChart(QDomNode *element, KoReportDesigner * rd, QGraphicsScene* scene) :
0056        KReportItemChart(element),  KoReportDesignerItemRectBase(rd)
0057 {
0058     init(scene, rd);
0059     populateData();
0060     setSceneRect(m_pos.toScene(), m_size.toScene());
0061 
0062 }
0063 
0064 KReportDesignerItemChart::~KReportDesignerItemChart()
0065 {
0066 }
0067 
0068 void KReportDesignerItemChart::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget *widget)
0069 {
0070     Q_UNUSED(option);
0071     Q_UNUSED(widget);
0072 
0073     // store any values we plan on changing so we can restore them
0074     QFont f = painter->font();
0075     QPen  p = painter->pen();
0076     QColor bg = Qt::white;
0077 
0078     painter->fillRect(QGraphicsRectItem::rect(), bg);
0079 
0080     if (m_chartWidget) {
0081         m_chartWidget->setFixedSize(m_size.toScene().toSize());
0082         painter->drawImage(rect().left(), rect().top(),
0083                            QPixmap::grabWidget(m_chartWidget).toImage(),
0084                            0, 0, rect().width(), rect().height());
0085     }
0086     bg.setAlpha(255);
0087 
0088     painter->setBackground(bg);
0089     painter->setPen(Qt::black);
0090     painter->drawText(rect(), 0, dataSourceAndObjectTypeName(itemDataSource(), "chart"));
0091     painter->setPen(QPen(QColor(224, 224, 224)));
0092     painter->drawRect(rect());
0093     painter->setBackgroundMode(Qt::TransparentMode);
0094 
0095     drawHandles(painter);
0096 
0097     // restore an values before we started just in case
0098     painter->setFont(f);
0099     painter->setPen(p);
0100 }
0101 
0102 KReportDesignerItemChart* KReportDesignerItemChart::clone()
0103 {
0104     QDomDocument d;
0105     QDomElement e = d.createElement("clone");
0106     QDomNode n;
0107     buildXML(&d, &e);
0108     n = e.firstChild();
0109     return new KReportDesignerItemChart(n, designer(), 0);
0110 }
0111 
0112 void KReportDesignerItemChart::buildXML(QDomDocument *doc, QDomElement *parent)
0113 {
0114     QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
0115 
0116     // properties
0117     addPropertyAsAttribute(&entity, nameProperty());
0118     addPropertyAsAttribute(&entity, dataSourceProperty());
0119     addPropertyAsAttribute(&entity, m_chartType);
0120     addPropertyAsAttribute(&entity, m_chartSubType);
0121     addPropertyAsAttribute(&entity, m_threeD);
0122     addPropertyAsAttribute(&entity, m_colorScheme);
0123     addPropertyAsAttribute(&entity, m_aa);
0124     addPropertyAsAttribute(&entity, m_xTitle);
0125     addPropertyAsAttribute(&entity, m_yTitle);
0126     addPropertyAsAttribute(&entity, m_backgroundColor);
0127     addPropertyAsAttribute(&entity, m_displayLegend);
0128     addPropertyAsAttribute(&entity, m_legendPosition);
0129     addPropertyAsAttribute(&entity, m_legendOrientation);
0130     addPropertyAsAttribute(&entity, m_linkChild);
0131     addPropertyAsAttribute(&entity, m_linkMaster);
0132     entity.setAttribute("report:z-index", zValue());
0133 
0134     // bounding rect
0135     buildXMLRect(doc, &entity, &m_pos, &m_size);
0136 
0137     parent->appendChild(entity);
0138 }
0139 
0140 void KReportDesignerItemChart::slotPropertyChanged(KPropertySet &s, KProperty &p)
0141 {
0142     if (p.name() == "name") {
0143         //For some reason p.oldValue returns an empty string
0144         if (!m_reportDesigner->isEntityNameUnique(p.value().toString(), this)) {
0145             p.setValue(m_oldName);
0146         } else {
0147             m_oldName = p.value().toString();
0148         }
0149     } else if (p.name() == "three-dimensions") {
0150         set3D(p.value().toBool());
0151     } else if (p.name() == "antialiased") {
0152         setAA(p.value().toBool());
0153     } else if (p.name() == "color-scheme") {
0154         setColorScheme(p.value().toString());
0155     } else if (p.name() == "data-source") {
0156         populateData();
0157     } else if (p.name() == "title-x-axis" ||   p.name() == "title-y-axis") {
0158         setAxis(m_xTitle->value().toString(), m_yTitle->value().toString());
0159     } else if (p.name() == "background-color") {
0160         setBackgroundColor(p.value().value<QColor>());
0161     } else if (p.name() == "display-legend") {
0162         if (m_chartWidget && p.value().toBool()) {
0163             populateData();
0164         }
0165     } else if (p.name() == "legend-position") {
0166         if (m_chartWidget) {
0167             populateData();
0168         }
0169     } else if (p.name() == "legend-orientation") {
0170         if (m_chartWidget) {
0171             populateData();
0172         }
0173     } else if (p.name() == "chart-type") {
0174         if (m_chartWidget) {
0175         populateData();
0176             //m_chartWidget->setType((KDChart::Widget::ChartType) m_chartType->value().toInt());
0177         }
0178     } else if (p.name() == "chart-sub-type") {
0179         if (m_chartWidget) {
0180             m_chartWidget->setSubType((KDChart::Widget::SubType) m_chartSubType->value().toInt());
0181         }
0182     }
0183 
0184     KoReportDesignerItemRectBase::propertyChanged(s, p);
0185     if (m_reportDesigner) m_reportDesigner->setModified(true);
0186 }
0187 
0188 void KReportDesignerItemChart::mousePressEvent(QGraphicsSceneMouseEvent * event)
0189 {
0190     if (m_reportDesigner->reportData()) {
0191         QStringList ql = m_reportDesigner->reportData()->dataSources();
0192         QStringList qn = m_reportDesigner->reportData()->dataSourceNames();
0193         dataSourceProperty()->setListData(ql, qn);
0194     }
0195     KoReportDesignerItemRectBase::mousePressEvent(event);
0196 }
0197 
0198 void KReportDesignerItemChart::slotReportDataChanged()
0199 {
0200     setConnection(m_reportDesigner->reportData());
0201 }