##// 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),
42 d_ptr(new QChartPrivate(this))
41 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
42 d(new QChartPrivate(this))
43 43 {
44 44 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
45 45 // set axis
46 Q_D(QChart);
47 46 d->m_axisY->rotate(90);
48 47 }
49 48
@@ -51,13 +50,12 QChart::~QChart(){}
51 50
52 51 QRectF QChart::boundingRect() const
53 52 {
54 Q_D(const QChart);
55 53 return d->m_rect;
56 54 }
57 55
58 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 60 d->m_series<<series;
63 61
@@ -89,6 +87,11 void QChart::addSeries(QChartSeries* series)
89 87 break;
90 88 }
91 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 95 break;
93 96 }
94 97 }
@@ -96,18 +99,14 void QChart::addSeries(QChartSeries* series)
96 99
97 100 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
98 101 {
99 Q_D(QChart);
100
101 // TODO: support also other types
102 // TODO: support also other types in addition to scatter
102 103 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
103 104 QChartSeries *series = 0;
104 105
105 106 switch (type) {
106 107 case QChartSeries::SeriesTypeScatter: {
107 108 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
108 d->m_items.append(scatterSeries->d);
109 scene()->addItem(scatterSeries->d);
110 //series = qobject_cast<QChartSeries *>(scatterSeries);
109 scatterSeries->d->setParentItem(this);
111 110 break;
112 111 }
113 112 default:
@@ -116,21 +115,26 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries:
116 115
117 116 return series;
118 117 }
118
119 119 void QChart::setSize(const QSizeF& size)
120 120 {
121 Q_D(QChart);
122 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 123 d->m_grid->setPos(d->m_rect.topLeft());
125 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 138 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
135 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 145 int QChart::margin() const
142 146 {
143 Q_D(const QChart);
144 147 return d->m_marginSize;
145 148 }
146 149
147 150 void QChart::setMargin(int margin)
148 151 {
149 Q_D(QChart);
150 152 d->m_marginSize = margin;
151 153 }
152 154
155 #include "moc_qchart.cpp"
156
157
153 158 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,7 +3,7
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 #include <QGraphicsItem>
6 #include <QGraphicsObject>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
@@ -21,16 +21,16 class QChartPrivate;
21 21 /*!
22 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 27 public:
28 QChart(QGraphicsItem* parent = 0);
29 virtual ~QChart();
28 QChart(QGraphicsObject* parent = 0);
29 ~QChart();
30 30
31 31 //from QGraphicsItem
32 virtual QRectF boundingRect() const;
33 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
32 QRectF boundingRect() const;
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);
@@ -41,13 +41,13 public:
41 41 void setMargin(int margin);
42 42 int margin() const;
43 43
44 protected:
45 QChartPrivate * const d_ptr;
44 signals:
45 void sizeChanged(QRectF rect, qreal xscale, qreal yscale);
46 46
47 47 private:
48 // Q_DECLARE_PRIVATE(QChart)
48 49 Q_DISABLE_COPY(QChart)
49 Q_DECLARE_PRIVATE(QChart)
50
50 QChartPrivate *d;
51 51 };
52 52
53 53 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,6 +3,7
3 3 #include "qchart.h"
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 #include <QDebug>
6 7
7 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 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 36 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -33,21 +42,13 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsI
33 42 pen.setBrush(brush);
34 43 pen.setWidth(4);
35 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++) {
47 transform.reset();
48 transform.translate(m_x.at(i) * xscale, m_y.at(i) * yscale);
49 painter->setTransform(transform);
50 painter->drawArc(0, 0, 4, 4, 0, 5760);
46 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
47 // event right after construction or maybe given a size during initialization
48 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
49 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
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 56 QChartSeries(parent),
56 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 70 QScatterSeries::~QScatterSeries()
@@ -2,6 +2,7
2 2 #define QSCATTERSERIES_H
3 3
4 4 #include "qchartseries.h"
5 #include <QRectF>
5 6
6 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 8 class QScatterSeriesPrivate;
@@ -17,6 +18,9 public:
17 18 public: // from QChartSeries
18 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 24 private:
21 25 Q_DECLARE_PRIVATE(QScatterSeries)
22 26 Q_DISABLE_COPY(QScatterSeries)
@@ -15,13 +15,15 public:
15 15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
16 16
17 17 public: // from QGraphicsItem
18 void setSize();
18 void resize(QRectF rect, qreal xscale, qreal yscale);
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 QList<qreal> m_scenex;
26 QList<qreal> m_sceney;
25 27 };
26 28
27 29 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,7 +2,6
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5
6 5 TARGET = QtCommercialChart
7 6 DESTDIR = $$CHART_BUILD_LIB_DIR
8 7 TEMPLATE = lib
@@ -2,7 +2,7
2 2 #define XYLINECHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include <QGraphicsItem>
6 6 #include "xyplotdomain_p.h"
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -7,27 +7,27 error( Couldn't find the integrated.pri file! )
7 7 }
8 8
9 9
10 TARGET = chartwidgettest
11 TEMPLATE = app
12
13 QT += core gui
14 contains(QT_MAJOR_VERSION, 5) {
15 QT += widgets
16 }
17
18
19 OBJECTS_DIR = tmp
20 MOC_DIR = tmp
21
22 SOURCES += main.cpp \
23 mainwidget.cpp \
24 # qscatterseries.cpp \
25 # qseriespointgraphicsitem.cpp \
26 dataseriedialog.cpp
27
28 HEADERS += \
29 mainwidget.h \
30 # qscatterseries.h \
31 # qseriespointgraphicsitem.h \
32 dataseriedialog.h
10 TARGET = chartwidgettest
11 TEMPLATE = app
12
13 QT += core gui
14 contains(QT_MAJOR_VERSION, 5) {
15 QT += widgets
16 }
17
18
19 OBJECTS_DIR = tmp
20 MOC_DIR = tmp
21
22 SOURCES += main.cpp \
23 mainwidget.cpp \
24 # qscatterseries.cpp \
25 # qseriespointgraphicsitem.cpp \
26 dataseriedialog.cpp
27
28 HEADERS += \
29 mainwidget.h \
30 # qscatterseries.h \
31 # qseriespointgraphicsitem.h \
32 dataseriedialog.h
33 33
@@ -100,41 +100,49 void MainWidget::addSeries(QString series, QString data)
100 100 qDebug() << "addSeries: " << series << " data: " << data;
101 101 m_defaultSeries = series;
102 102
103 QXYChartSeries* series0 = QXYChartSeries::create();
104
103 105 // TODO: a dedicated data class for storing x and y values
104 106 QList<qreal> x;
105 107 QList<qreal> y;
106 108
107 109 if (data == "linear") {
108 for (int i = 0; i < 10; i++) {
110 for (int i = 0; i < 20; i++) {
109 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 116 } else if (data == "linear, 1M") {
113 117 for (int i = 0; i < 10000; i++) {
114 118 x.append(i);
115 119 y.append(20);
116 120 }
121 for (int i = 0; i < 1000000; i++)
122 series0->add(i, 10);
117 123 } else if (data == "SIN") {
118 124 for (int i = 0; i < 100; i++) {
119 125 x.append(i);
120 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 130 } else if (data == "SIN + random") {
123 131 for (qreal i = 0; i < 100; i += 0.1) {
124 132 x.append(i + (rand() % 5));
125 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 137 } else {
128 138 // TODO: check if data has a valid file name
129 139 }
130 140
131 141 // TODO: color of the series
132 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 145 } else if (series == "Line") {
135 QXYChartSeries* series0 = QXYChartSeries::create();
136 for (int i = 0; i < 1000000; i++)
137 series0->add(i, 20);
138 146 m_chartWidget->addSeries(series0);
139 147 } else {
140 148 // TODO
General Comments 0
You need to be logged in to leave comments. Login now