##// END OF EJS Templates
Removed scale from chart's sizeChanged signals
Tero Ahola -
r54:f2e3dfe7c506
parent child
Show More
@@ -1,151 +1,150
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7 7 #include "xylinechartitem_p.h"
8 8 #include "xyplotdomain_p.h"
9 9 #include "axis_p.h"
10 10 #include "xygrid_p.h"
11 11 #include <QGraphicsScene>
12 12 #include <QDebug>
13 13
14 14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
15 15
16 16 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
17 17 m_axisX(new Axis(this)),
18 18 m_axisY(new Axis(this)),
19 19 m_grid(new XYGrid(this)),
20 20 m_plotDataIndex(0),
21 21 m_marginSize(0)
22 22 {
23 23 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
24 24 // set axis
25 25 m_axisY->rotate(90);
26 26 }
27 27
28 28 QChart::~QChart(){}
29 29
30 30 QRectF QChart::boundingRect() const
31 31 {
32 32 return m_rect;
33 33 }
34 34
35 35 void QChart::addSeries(QChartSeries* series)
36 36 {
37 37 // TODO: we should check the series not already added
38 38
39 39 m_series<<series;
40 40
41 41 switch(series->type())
42 42 {
43 43 case QChartSeries::SeriesTypeLine: {
44 44
45 45 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
46 46
47 47 XYPlotDomain domain;
48 48 //TODO "nice numbers algorithm"
49 49 domain.m_ticksX=4;
50 50 domain.m_ticksY=4;
51 51
52 52 for (int i = 0 ; i < xyseries->count() ; i++)
53 53 {
54 54 qreal x = xyseries->x(i);
55 55 qreal y = xyseries->y(i);
56 56 domain.m_minX = qMin(domain.m_minX,x);
57 57 domain.m_minY = qMin(domain.m_minY,y);
58 58 domain.m_maxX = qMax(domain.m_maxX,x);
59 59 domain.m_maxY = qMax(domain.m_maxY,y);
60 60 }
61 61
62 62 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
63 63 item->updateXYPlotDomain(domain);
64 64 m_plotDomainList<<domain;
65 65 m_xyLineChartItems<<item;
66 66 break;
67 67 }
68 68 // TODO: Not tested:
69 69 // case QChartSeries::SeriesTypeScatter: {
70 70 // QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
71 71 // if (scatter) {
72 72 // scatter->d->setParentItem(this);
73 73 // scene()->addItem(scatter->d);
74 74 // }
75 75 // break;
76 76 // }
77 77 }
78 78 }
79 79
80 80 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
81 81 {
82 82 // TODO: support also other types; not only scatter and pie
83 83 Q_ASSERT(type == QChartSeries::SeriesTypeScatter
84 84 || type == QChartSeries::SeriesTypePie);
85 85
86 86 switch (type) {
87 87 case QChartSeries::SeriesTypeScatter: {
88 88 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
89 connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)),
90 scatterSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
89 connect(this, SIGNAL(sizeChanged(QRectF)),
90 scatterSeries, SLOT(chartSizeChanged(QRectF)));
91 91 scatterSeries->d->setParentItem(this);
92 92 return scatterSeries;
93 93 }
94 94 case QChartSeries::SeriesTypePie: {
95 95 // TODO: we now have also a list of y values as a parameter, it is ignored
96 96 // we should use a generic data class instead of list of x and y values
97 97 QPieSeries *pieSeries = new QPieSeries(x, this);
98 connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)),
99 pieSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
98 connect(this, SIGNAL(sizeChanged(QRectF)),
99 pieSeries, SLOT(chartSizeChanged(QRectF)));
100 100 return pieSeries;
101 101 }
102 102 default:
103 103 break;
104 104 }
105 105
106 106 return 0;
107 107 }
108 108
109 109 void QChart::setSize(const QSizeF& size)
110 110 {
111 111 m_rect = QRect(QPoint(0,0),size.toSize());
112 112 m_rect.adjust(margin(),margin(), -margin(), -margin());
113 113 m_grid->setPos(m_rect.topLeft());
114 114 m_grid->setSize(m_rect.size());
115 115
116 // TODO: calculate the scale
116 // TODO: TTD for setting scale
117 //emit scaleChanged(100, 100);
117 118 // TODO: calculate the origo
118 119 // TODO: not sure if emitting a signal here is the best from performance point of view
119 const qreal xscale = size.width() / 100;
120 const qreal yscale = size.height() / 100;
121 emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale);
120 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
122 121
123 122 for (int i(0); i < m_plotDomainList.size(); i++)
124 123 m_plotDomainList[i].m_viewportRect = m_rect;
125 124
126 125 // TODO: line chart items are updated separately as they don't support update
127 126 // via sizeChanged signal
128 127 foreach(XYLineChartItem* item ,m_xyLineChartItems)
129 128 item->updateXYPlotDomain(m_plotDomainList.at(m_plotDataIndex));
130 129
131 130
132 131 if (m_plotDomainList.count())
133 132 m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex));
134 133
135 134 update();
136 135 }
137 136
138 137 int QChart::margin() const
139 138 {
140 139 return m_marginSize;
141 140 }
142 141
143 142 void QChart::setMargin(int margin)
144 143 {
145 144 m_marginSize = margin;
146 145 }
147 146
148 147 #include "moc_qchart.cpp"
149 148
150 149
151 150 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,64
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsObject>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class Axis;
11 11 class XYGrid;
12 12 class QChartSeries;
13 13 class XYPlotDomain;
14 14 class XYLineChartItem;
15 15
16 16 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 17 //class QTCOMMERCIALCHART_EXPORT QChart
18 18 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
19 19 // public: QChartGraphicsItem(QChart &chart);
20 20
21 21 /*!
22 22 * TODO: define the responsibilities
23 23 */
24 24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
25 25 {
26 26 Q_OBJECT
27 27 public:
28 28 QChart(QGraphicsObject* parent = 0);
29 29 ~QChart();
30 30
31 31 //from QGraphicsItem
32 32 QRectF boundingRect() const;
33 33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
34 34
35 35 void addSeries(QChartSeries* series);
36 36 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
37 37 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
38 38 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
39 39
40 40 virtual void setSize(const QSizeF& rect);
41 41 void setMargin(int margin);
42 42 int margin() const;
43 43
44 44 signals:
45 void sizeChanged(QRectF rect, qreal xscale, qreal yscale);
45 void sizeChanged(QRectF rect);
46 void scaleChanged(qreal xscale, qreal yscale);
46 47
47 48 private:
48 49 Q_DISABLE_COPY(QChart)
49 50 Axis* m_axisX;
50 51 Axis* m_axisY;
51 52 XYGrid* m_grid;
52 53 QRect m_rect;
53 54 QList<const QChartSeries*> m_series;
54 55 QList<XYPlotDomain> m_plotDomainList;
55 56 QList<XYLineChartItem*> m_xyLineChartItems;
56 57 QList<QGraphicsItem*> m_items;
57 58 int m_plotDataIndex;
58 59 int m_marginSize;
59 60 };
60 61
61 62 QTCOMMERCIALCHART_END_NAMESPACE
62 63
63 64 #endif
@@ -1,54 +1,54
1 1 #include "qpieseries.h"
2 2 #include "pieslice.h"
3 3 #include <QGraphicsObject>
4 4 #include <QDebug>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
9 9 QChartSeries(parent),
10 10 m_x(x)
11 11 {
12 }
13
14 QPieSeries::~QPieSeries()
15 {
16 }
17
18 void QPieSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale)
19 {
12 // Create slices
20 13 qreal fullPie = 360;
21 14 qreal total = 0;
22 15 foreach (qreal value, m_x)
23 16 total += value;
24 17
25 // We must have a parent for the graphics items we create
26 // TODO: maybe QChartSeries needs to be a QGraphicsObject to make this clear for the users?
27 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
18 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
28 19 Q_ASSERT(parentItem);
29 20 qreal angle = 0;
21 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
30 22 foreach (qreal value, m_x) {
31 23 qreal span = value / total * fullPie;
32 24 PieSlice *slice = new PieSlice(randomColor(), angle, span);
33 25 slice->setParentItem(parentItem);
34 26 m_slices.append(slice);
35 27 angle += span;
36 28 }
37 29 }
38 30
31 QPieSeries::~QPieSeries()
32 {
33 }
34
35 void QPieSeries::chartSizeChanged(QRectF /*rect*/)
36 {
37 }
38
39 39 QColor QPieSeries::randomColor()
40 40 {
41 41 QColor c;
42 42 c.setRed(qrand() % 255);
43 43 c.setGreen(qrand() % 255);
44 44 c.setBlue(qrand() % 255);
45 45 return c;
46 46 }
47 47
48 48 void QPieSeries::setData(QList<int> data)
49 49 {
50 50 }
51 51
52 52 #include "moc_qpieseries.cpp"
53 53
54 54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,40 +1,40
1 1 #ifndef PIESERIES_H
2 2 #define PIESERIES_H
3 3
4 4 #include "qchartseries.h"
5 5 #include <QObject>
6 6 #include <QRectF>
7 7 #include <QColor>
8 8
9 9 class QGraphicsObject;
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11 class PieSlice;
12 12
13 13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 // TODO: use a generic data class instead of x and y
18 18 QPieSeries(QList<qreal> x, QGraphicsObject *parent = 0);
19 19 ~QPieSeries();
20 20 QColor randomColor();
21 21 void setData(QList<int> data);
22 22
23 23 public: // from QChartSeries
24 24 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
25 25
26 26 public Q_SLOTS:
27 void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale);
27 void chartSizeChanged(QRectF rect);
28 28
29 29 private:
30 30 //Q_DECLARE_PRIVATE(QPieSeries)
31 31 Q_DISABLE_COPY(QPieSeries)
32 32 // TODO: move the followin to internal impl
33 33 QList<qreal> m_x;
34 34 QList<PieSlice*> m_slices;
35 35 QSizeF m_size;
36 36 };
37 37
38 38 QTCOMMERCIALCHART_END_NAMESPACE
39 39
40 40 #endif // PIESERIES_H
@@ -1,76 +1,88
1 1 #include "qscatterseries.h"
2 2 #include "qscatterseries_p.h"
3 3 #include "qchart.h"
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 //#define QSeriesData QList<qreal>
11 11
12 12 QScatterSeriesPrivate::QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent) :
13 13 QGraphicsItem(parent),
14 14 m_x(x),
15 m_y(y)
15 m_y(y),
16 m_scalex(100), // TODO: let the use define the scale (or autoscaled)
17 m_scaley(100)
16 18 {
19 resize(parent->boundingRect());
17 20 }
18 21
19 void QScatterSeriesPrivate::resize(QRectF rect, qreal xscale, qreal yscale)
22 void QScatterSeriesPrivate::resize(QRectF rect)
20 23 {
21 24 m_scenex.clear();
22 25 m_sceney.clear();
23 26
24 27 foreach(qreal x, m_x)
25 m_scenex.append(rect.left() + x * xscale);
28 m_scenex.append(rect.left() + x * (rect.width() / m_scalex));
26 29
27 30 foreach(qreal y, m_y)
28 m_sceney.append(rect.bottom() - y * yscale);
31 m_sceney.append(rect.bottom() - y * (rect.height() / m_scaley));
29 32 }
30 33
34 // TODO:
35 //void QScatterSeriesPrivate::setAxisScale(qreal xscale, qreal yscale)
36
31 37 QRectF QScatterSeriesPrivate::boundingRect() const
32 38 {
33 39 return QRectF(0, 0, 55, 100);
34 40 }
35 41
36 42 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 43 {
38 44 QPen pen = painter->pen();
39 45 QBrush brush = pen.brush();
40 46 // TODO: The opacity should be user definable...
41 brush.setColor(QColor(255, 82, 0, 50));
47 brush.setColor(QColor(255, 82, 0, 100));
42 48 pen.setBrush(brush);
43 49 pen.setWidth(4);
44 50 painter->setPen(pen);
45 51
46 52 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
47 53 // event right after construction or maybe given a size during initialization
48 54 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
49 55 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
50 56 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
51 57 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
52 58 }
53 59 }
54 60
55 61 QScatterSeries::QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent) :
56 62 QChartSeries(parent),
57 63 d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent)))
58 64 {
59 65 }
60 66
61 void QScatterSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale)
67 void QScatterSeries::chartSizeChanged(QRectF rect)
62 68 {
63 69 // Recalculate scatter data point locations on the scene
64 70 // d->transform().reset();
65 71 // d->transform().translate();
66 d->resize(rect, xscale, yscale);
72 d->resize(rect);
67 73 }
68 74
75 // TODO:
76 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
77 //{
78 // d->rescale(xscale, yscale);
79 //}
80
69 81 QScatterSeries::~QScatterSeries()
70 82 {
71 83 delete d;
72 84 }
73 85
74 86 #include "moc_qscatterseries.cpp"
75 87
76 88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,33 +1,34
1 1 #ifndef QSCATTERSERIES_H
2 2 #define QSCATTERSERIES_H
3 3
4 4 #include "qchartseries.h"
5 5 #include <QRectF>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8 class QScatterSeriesPrivate;
9 9
10 10 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
11 11 {
12 12 Q_OBJECT
13 13 public:
14 14 //QScatterSeries(QSeriesData *data, QObject *chart);
15 15 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent = 0);
16 16 ~QScatterSeries();
17 17
18 18 public: // from QChartSeries
19 19 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
20 20
21 21 public Q_SLOTS:
22 void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale);
22 void chartSizeChanged(QRectF rect);
23 //void chartScaleChanged(qreal xscale, qreal yscale);
23 24
24 25 private:
25 26 Q_DECLARE_PRIVATE(QScatterSeries)
26 27 Q_DISABLE_COPY(QScatterSeries)
27 28 friend class QChart;
28 29 QScatterSeriesPrivate *const d;
29 30 };
30 31
31 32 QTCOMMERCIALCHART_END_NAMESPACE
32 33
33 34 #endif // QSCATTERSERIES_H
@@ -1,31 +1,33
1 1 #ifndef QSCATTERSERIESPRIVATE_H
2 2 #define QSCATTERSERIESPRIVATE_H
3 3
4 4 #include "qchartseries.h"
5 5 #include <QGraphicsItem>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 /*!
10 10 * The PIMPL class of QScatterSeries.
11 11 */
12 12 class QScatterSeriesPrivate : public QGraphicsItem
13 13 {
14 14 public:
15 15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
16 16
17 17 public: // from QGraphicsItem
18 void resize(QRectF rect, qreal xscale, qreal yscale);
18 void resize(QRectF rect);
19 19 QRectF boundingRect() const;
20 20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 21
22 22 // TODO: use the chart data class instead of list of x and y values?
23 23 QList<qreal> m_x;
24 24 QList<qreal> m_y;
25 qreal m_scalex;
26 qreal m_scaley;
25 27 QList<qreal> m_scenex;
26 28 QList<qreal> m_sceney;
27 29 };
28 30
29 31 QTCOMMERCIALCHART_END_NAMESPACE
30 32
31 33 #endif // QSCATTERSERIES_H
General Comments 0
You need to be logged in to leave comments. Login now