##// END OF EJS Templates
Resizing of QGraphicItems now possible by resize signal from QChart
Tero Ahola -
r48:3cbbcb012d8d
parent child
Show More
@@ -38,12 +38,11 public:
38
38
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40
40
41 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
41 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
42 d_ptr(new QChartPrivate(this))
42 d(new QChartPrivate(this))
43 {
43 {
44 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
44 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
45 // set axis
45 // set axis
46 Q_D(QChart);
47 d->m_axisY->rotate(90);
46 d->m_axisY->rotate(90);
48 }
47 }
49
48
@@ -51,13 +50,12 QChart::~QChart(){}
51
50
52 QRectF QChart::boundingRect() const
51 QRectF QChart::boundingRect() const
53 {
52 {
54 Q_D(const QChart);
55 return d->m_rect;
53 return d->m_rect;
56 }
54 }
57
55
58 void QChart::addSeries(QChartSeries* series)
56 void QChart::addSeries(QChartSeries* series)
59 {
57 {
60 Q_D(QChart);
58 // TODO: we should check the series not already added
61
59
62 d->m_series<<series;
60 d->m_series<<series;
63
61
@@ -89,6 +87,11 void QChart::addSeries(QChartSeries* series)
89 break;
87 break;
90 }
88 }
91 case QChartSeries::SeriesTypeScatter: {
89 case QChartSeries::SeriesTypeScatter: {
90 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
91 if (scatter) {
92 scatter->d->setParentItem(this);
93 scene()->addItem(scatter->d);
94 }
92 break;
95 break;
93 }
96 }
94 }
97 }
@@ -96,18 +99,14 void QChart::addSeries(QChartSeries* series)
96
99
97 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
100 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
98 {
101 {
99 Q_D(QChart);
102 // TODO: support also other types in addition to scatter
100
101 // TODO: support also other types
102 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
103 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
103 QChartSeries *series = 0;
104 QChartSeries *series = 0;
104
105
105 switch (type) {
106 switch (type) {
106 case QChartSeries::SeriesTypeScatter: {
107 case QChartSeries::SeriesTypeScatter: {
107 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
108 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
108 d->m_items.append(scatterSeries->d);
109 scatterSeries->d->setParentItem(this);
109 scene()->addItem(scatterSeries->d);
110 //series = qobject_cast<QChartSeries *>(scatterSeries);
111 break;
110 break;
112 }
111 }
113 default:
112 default:
@@ -116,21 +115,26 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries:
116
115
117 return series;
116 return series;
118 }
117 }
118
119 void QChart::setSize(const QSizeF& size)
119 void QChart::setSize(const QSizeF& size)
120 {
120 {
121 Q_D(QChart);
122 d->m_rect = QRect(QPoint(0,0),size.toSize());
121 d->m_rect = QRect(QPoint(0,0),size.toSize());
123 d->m_rect.adjust(margin(),margin(),-margin(),-margin());
122 d->m_rect.adjust(margin(),margin(), -margin(), -margin());
124 d->m_grid->setPos(d->m_rect.topLeft());
123 d->m_grid->setPos(d->m_rect.topLeft());
125 d->m_grid->setSize(d->m_rect.size());
124 d->m_grid->setSize(d->m_rect.size());
126 // TODO: line chart items would need to be updated separately as they don't support update
127 // via paint method
128 for (int i =0; i< d->m_plotDomainList.size();i++)
129 {
130 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
131
125
132 }
126 // TODO: calculate the scale
127 // TODO: calculate the origo
128 // TODO: not sure if emitting a signal here is the best from performance point of view
129 const qreal xscale = size.width() / 100;
130 const qreal yscale = size.height() / 100;
131 emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale);
132
133 for (int i(0); i < d->m_plotDomainList.size(); i++)
134 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
133
135
136 // TODO: line chart items are updated separately as they don't support update
137 // via sizeChanged signal
134 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
138 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
135 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
139 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
136
140
@@ -140,14 +144,15 void QChart::setSize(const QSizeF& size)
140
144
141 int QChart::margin() const
145 int QChart::margin() const
142 {
146 {
143 Q_D(const QChart);
144 return d->m_marginSize;
147 return d->m_marginSize;
145 }
148 }
146
149
147 void QChart::setMargin(int margin)
150 void QChart::setMargin(int margin)
148 {
151 {
149 Q_D(QChart);
150 d->m_marginSize = margin;
152 d->m_marginSize = margin;
151 }
153 }
152
154
155 #include "moc_qchart.cpp"
156
157
153 QTCOMMERCIALCHART_END_NAMESPACE
158 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,7 +3,7
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartseries.h>
5 #include <qchartseries.h>
6 #include <QGraphicsItem>
6 #include <QGraphicsObject>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
@@ -21,16 +21,16 class QChartPrivate;
21 /*!
21 /*!
22 * TODO: define the responsibilities
22 * TODO: define the responsibilities
23 */
23 */
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsItem, public QObject
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
25 {
25 {
26
26 Q_OBJECT
27 public:
27 public:
28 QChart(QGraphicsItem* parent = 0);
28 QChart(QGraphicsObject* parent = 0);
29 virtual ~QChart();
29 ~QChart();
30
30
31 //from QGraphicsItem
31 //from QGraphicsItem
32 virtual QRectF boundingRect() const;
32 QRectF boundingRect() const;
33 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
34
34
35 void addSeries(QChartSeries* series);
35 void addSeries(QChartSeries* series);
36 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
36 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
@@ -41,13 +41,13 public:
41 void setMargin(int margin);
41 void setMargin(int margin);
42 int margin() const;
42 int margin() const;
43
43
44 protected:
44 signals:
45 QChartPrivate * const d_ptr;
45 void sizeChanged(QRectF rect, qreal xscale, qreal yscale);
46
46
47 private:
47 private:
48 // Q_DECLARE_PRIVATE(QChart)
48 Q_DISABLE_COPY(QChart)
49 Q_DISABLE_COPY(QChart)
49 Q_DECLARE_PRIVATE(QChart)
50 QChartPrivate *d;
50
51 };
51 };
52
52
53 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,6 +3,7
3 #include "qchart.h"
3 #include "qchart.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QDebug>
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9
@@ -15,13 +16,21 QScatterSeriesPrivate::QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGr
15 {
16 {
16 }
17 }
17
18
18 void QScatterSeriesPrivate::setSize()
19 void QScatterSeriesPrivate::resize(QRectF rect, qreal xscale, qreal yscale)
19 {
20 {
21 m_scenex.clear();
22 m_sceney.clear();
23
24 foreach(qreal x, m_x)
25 m_scenex.append(rect.left() + x * xscale);
26
27 foreach(qreal y, m_y)
28 m_sceney.append(rect.bottom() - y * yscale);
20 }
29 }
21
30
22 QRectF QScatterSeriesPrivate::boundingRect() const
31 QRectF QScatterSeriesPrivate::boundingRect() const
23 {
32 {
24 return QRectF(0, 0, 100, 100);
33 return QRectF(0, 0, 55, 100);
25 }
34 }
26
35
27 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
36 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -33,21 +42,13 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsI
33 pen.setBrush(brush);
42 pen.setBrush(brush);
34 pen.setWidth(4);
43 pen.setWidth(4);
35 painter->setPen(pen);
44 painter->setPen(pen);
36 QTransform transform = painter->transform();
37
38 // TODO: get min and max values of the axes from the QChart (or some dedicated class)
39 const qreal xmin = 0.0;
40 const qreal xmax = 100.0;
41 const qreal xscale = scene()->width() / (xmax - xmin);
42 const qreal ymin = 0.0;
43 const qreal ymax = 100.0;
44 const qreal yscale = scene()->height() / (ymax - ymin);
45
45
46 for (int i(0); i < m_x.count() && i < m_y.count(); i++) {
46 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
47 transform.reset();
47 // event right after construction or maybe given a size during initialization
48 transform.translate(m_x.at(i) * xscale, m_y.at(i) * yscale);
48 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
49 painter->setTransform(transform);
49 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
50 painter->drawArc(0, 0, 4, 4, 0, 5760);
50 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
51 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
51 }
52 }
52 }
53 }
53
54
@@ -55,6 +56,15 QScatterSeries::QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent)
55 QChartSeries(parent),
56 QChartSeries(parent),
56 d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent)))
57 d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent)))
57 {
58 {
59 connect(parent, SIGNAL(sizeChanged(QRectF, qreal, qreal)), this, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
60 }
61
62 void QScatterSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale)
63 {
64 // Recalculate scatter data point locations on the scene
65 // d->transform().reset();
66 // d->transform().translate();
67 d->resize(rect, xscale, yscale);
58 }
68 }
59
69
60 QScatterSeries::~QScatterSeries()
70 QScatterSeries::~QScatterSeries()
@@ -2,6 +2,7
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QRectF>
5
6
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 class QScatterSeriesPrivate;
8 class QScatterSeriesPrivate;
@@ -17,6 +18,9 public:
17 public: // from QChartSeries
18 public: // from QChartSeries
18 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
19
20
21 public Q_SLOTS:
22 void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale);
23
20 private:
24 private:
21 Q_DECLARE_PRIVATE(QScatterSeries)
25 Q_DECLARE_PRIVATE(QScatterSeries)
22 Q_DISABLE_COPY(QScatterSeries)
26 Q_DISABLE_COPY(QScatterSeries)
@@ -15,13 +15,15 public:
15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
16
16
17 public: // from QGraphicsItem
17 public: // from QGraphicsItem
18 void setSize();
18 void resize(QRectF rect, qreal xscale, qreal yscale);
19 QRectF boundingRect() const;
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
21
22 // TODO: use the chart data class instead of list of x and y values?
22 // TODO: use the chart data class instead of list of x and y values?
23 QList<qreal> m_x;
23 QList<qreal> m_x;
24 QList<qreal> m_y;
24 QList<qreal> m_y;
25 QList<qreal> m_scenex;
26 QList<qreal> m_sceney;
25 };
27 };
26
28
27 QTCOMMERCIALCHART_END_NAMESPACE
29 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,7 +2,6
2 error( Couldn't find the common.pri file! )
2 error( Couldn't find the common.pri file! )
3 }
3 }
4
4
5
6 TARGET = QtCommercialChart
5 TARGET = QtCommercialChart
7 DESTDIR = $$CHART_BUILD_LIB_DIR
6 DESTDIR = $$CHART_BUILD_LIB_DIR
8 TEMPLATE = lib
7 TEMPLATE = lib
@@ -2,7 +2,7
2 #define XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include <QGraphicsItem>
6 #include "xyplotdomain_p.h"
6 #include "xyplotdomain_p.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -100,41 +100,49 void MainWidget::addSeries(QString series, QString data)
100 qDebug() << "addSeries: " << series << " data: " << data;
100 qDebug() << "addSeries: " << series << " data: " << data;
101 m_defaultSeries = series;
101 m_defaultSeries = series;
102
102
103 QXYChartSeries* series0 = QXYChartSeries::create();
104
103 // TODO: a dedicated data class for storing x and y values
105 // TODO: a dedicated data class for storing x and y values
104 QList<qreal> x;
106 QList<qreal> x;
105 QList<qreal> y;
107 QList<qreal> y;
106
108
107 if (data == "linear") {
109 if (data == "linear") {
108 for (int i = 0; i < 10; i++) {
110 for (int i = 0; i < 20; i++) {
109 x.append(i);
111 x.append(i);
110 y.append(10);
112 y.append(i);
111 }
113 }
114 for (int i = 0; i < 20; i++)
115 series0->add(i, i);
112 } else if (data == "linear, 1M") {
116 } else if (data == "linear, 1M") {
113 for (int i = 0; i < 10000; i++) {
117 for (int i = 0; i < 10000; i++) {
114 x.append(i);
118 x.append(i);
115 y.append(20);
119 y.append(20);
116 }
120 }
121 for (int i = 0; i < 1000000; i++)
122 series0->add(i, 10);
117 } else if (data == "SIN") {
123 } else if (data == "SIN") {
118 for (int i = 0; i < 100; i++) {
124 for (int i = 0; i < 100; i++) {
119 x.append(i);
125 x.append(i);
120 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
126 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
121 }
127 }
128 for (int i = 0; i < 100; i++)
129 series0->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
122 } else if (data == "SIN + random") {
130 } else if (data == "SIN + random") {
123 for (qreal i = 0; i < 100; i += 0.1) {
131 for (qreal i = 0; i < 100; i += 0.1) {
124 x.append(i + (rand() % 5));
132 x.append(i + (rand() % 5));
125 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
133 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
126 }
134 }
135 for (qreal i = 0; i < 100; i += 0.1)
136 series0->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
127 } else {
137 } else {
128 // TODO: check if data has a valid file name
138 // TODO: check if data has a valid file name
129 }
139 }
130
140
131 // TODO: color of the series
141 // TODO: color of the series
132 if (series == "Scatter") {
142 if (series == "Scatter") {
133 /*QChartSeries* scatterSeries = */m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
143 /*QChartSeries* scatterSeries = */
144 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
134 } else if (series == "Line") {
145 } else if (series == "Line") {
135 QXYChartSeries* series0 = QXYChartSeries::create();
136 for (int i = 0; i < 1000000; i++)
137 series0->add(i, 20);
138 m_chartWidget->addSeries(series0);
146 m_chartWidget->addSeries(series0);
139 } else {
147 } else {
140 // TODO
148 // TODO
General Comments 0
You need to be logged in to leave comments. Login now