KD Reports API Documentation  2.2
KDReportsChartElement.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Reports library.
4 **
5 ** SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDReportsChartElement.h"
14 #include "KDReportsReport.h"
16 #include "KDReportsReport_p.h" // modelForKey
18 #ifdef HAVE_KDCHART
19 #include <KDChartBarDiagram>
20 #include <KDChartChart>
21 // #include <KDChartSerializer>
22 #endif
23 #include <QDebug>
24 #include <QFile>
25 #include <QLabel>
26 #include <QPainter>
27 #include <QTextDocument>
28 #include <QUrl>
29 
30 class KDReports::ChartElementPrivate : public QSharedData
31 {
32 public:
33  ChartElementPrivate()
34  : m_tableModel(nullptr)
35  , m_size(100, 100)
36  , m_unit(KDReports::Millimeters)
37  ,
38 #ifdef HAVE_KDCHART
39  m_chart(0)
40  ,
41 #endif
42  m_deleteChart(false)
43  {
44  }
45 
46  ~ChartElementPrivate()
47  {
48 #ifdef HAVE_KDCHART
49  if (m_deleteChart)
50  delete m_chart;
51 #endif
52  }
53  QAbstractItemModel *m_tableModel;
54  QSizeF m_size;
55  KDReports::Unit m_unit;
56 #ifdef HAVE_KDCHART
57  mutable KDChart::Chart *m_chart;
58 #endif
59  mutable bool m_deleteChart;
60 };
61 
62 KDReports::ChartElement::ChartElement(QAbstractItemModel *tableModel)
63  : d(new ChartElementPrivate)
64 {
65  d->m_tableModel = tableModel;
66 }
67 
68 KDReports::ChartElement::ChartElement(const QString &modelKey)
69  : d(new ChartElementPrivate)
70 {
71  if (!modelKey.isEmpty())
72  d->m_tableModel = KDReports::modelForKey(modelKey);
73 }
74 
76  : d(new ChartElementPrivate)
77 {
78  d->m_tableModel = nullptr;
79 #ifdef HAVE_KDCHART
80  d->m_chart = chart;
81 #else
82  Q_UNUSED(chart);
83  qWarning("ChartElement: cannot use chart, KD Reports was compiled without KD Chart support");
84 #endif
85 }
86 
88  : Element(other)
89  , d(other.d)
90 {
91 }
92 
94 {
95  if (&other == this)
96  return *this;
97  Element::operator=(other);
98  d = other.d;
99  return *this;
100 }
101 
103 {
104 }
105 
106 void KDReports::ChartElement::setChart(KDChart::Chart *chart)
107 {
108 #ifdef HAVE_KDCHART
109  if (d->m_deleteChart)
110  delete d->m_chart;
111  d->m_chart = chart;
112  d->m_deleteChart = false;
113 #else
114  Q_UNUSED(chart);
115 #endif
116 }
117 
118 void KDReports::ChartElement::setSize(qreal width, qreal height, Unit unit)
119 {
120  d->m_size = QSizeF(width, height);
121  d->m_unit = unit;
122 }
123 
125 {
126 #ifdef HAVE_KDCHART
127  if (!d->m_tableModel && !d->m_chart)
128  return; // no model has been associated yet - give up
129 #else
130  if (!d->m_tableModel)
131  return; // no model has been associated yet - give up
132 #endif
133 
134 #ifdef HAVE_KDCHART
135 
136  if (!d->m_chart) {
137  d->m_chart = new KDChart::Chart;
138  d->m_deleteChart = true;
139  }
140 
141  if (d->m_chart->coordinatePlane()->diagram() == 0) {
142  KDChart::BarDiagram *diagram = new KDChart::BarDiagram();
143  diagram->setModel(d->m_tableModel);
144  d->m_chart->coordinatePlane()->replaceDiagram(diagram);
145  }
146 
147 #if 0 // TODO, but not here...
148  KDChart::Serializer serializer( d->m_chart, d->m_tableModel );
149  QFile file("testSettings.xml");
150  if ( file.open( QIODevice::ReadOnly ) ) {
151  if ( serializer.read( &file ) ) {
152  qDebug() << "Success loading XML for chart";
153  }
154  }
155 #endif
156 
157  QTextCursor &cursor = builder.cursor();
158  QTextCharFormat fmt;
159 
160  fmt.setObjectType(ChartTextObject::ChartObjectTextFormat);
161  fmt.setProperty(ChartTextObject::Size, d->m_size);
162  fmt.setProperty(ChartTextObject::Unit, int(d->m_unit));
163  fmt.setProperty(ChartTextObject::ChartObject, QVariant::fromValue(d->m_chart));
164 
165  cursor.insertText(QString(QChar::ObjectReplacementCharacter), fmt);
166 
167 #else
168  Q_UNUSED(builder);
169  qWarning("ChartElement: cannot create chart, KD Reports was compiled without KD Chart support");
170 #endif
171 }
172 
174 {
175  return new ChartElement(*this);
176 }
177 
179 {
180  return d->m_size;
181 }
182 
184 {
185  return d->m_unit;
186 }
187 
188 void KDReports::ChartElement::setModelKey(const QString &modelKey)
189 {
190  if (!modelKey.isEmpty())
191  d->m_tableModel = KDReports::modelForKey(modelKey);
192 }
193 
194 void KDReports::ChartElement::setTableModel(QAbstractItemModel *tableModel)
195 {
196  d->m_tableModel = tableModel;
197 }
ChartElement & operator=(const ChartElement &other)
ChartElement(QAbstractItemModel *tableModel)
void setChart(KDChart::Chart *chart)
Element * clone() const override
void setTableModel(QAbstractItemModel *tableModel)
void build(ReportBuilder &) const override
void setSize(qreal width, qreal height, Unit unit=Millimeters)
void setModelKey(const QString &modelKey)
Element & operator=(const Element &other)
@ Millimeters
Millimeters (the default)
Definition: KDReportsUnit.h:21
QAbstractItemModel * modelForKey(const QString &key)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-reports/
Generated by doxygen 1.9.1