##// END OF EJS Templates
Resizing of QGraphicItems now possible by resize signal from QChart
Tero Ahola -
r48:3cbbcb012d8d
parent child
Show More
@@ -1,153 +1,158
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qxychartseries.h"
6 6 #include "xylinechartitem_p.h"
7 7 #include "xyplotdomain_p.h"
8 8 #include "axis_p.h"
9 9 #include "xygrid_p.h"
10 10 #include <QGraphicsScene>
11 11 #include <QDebug>
12 12
13 13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 14
15 15 class QChartPrivate
16 16 {
17 17 public:
18 18
19 19 QChartPrivate(QChart* parent):
20 20 m_axisX(new Axis(parent)),
21 21 m_axisY(new Axis(parent)),
22 22 m_grid(new XYGrid(parent)),
23 23 m_plotDataIndex(0),
24 24 m_marginSize(0){}
25 25
26 26 Axis* m_axisX;
27 27 Axis* m_axisY;
28 28 XYGrid* m_grid;
29 29 QRect m_rect;
30 30 QList<const QChartSeries*> m_series;
31 31 QList<XYPlotDomain> m_plotDomainList;
32 32 QList<XYLineChartItem*> m_xyLineChartItems;
33 33 QList<QGraphicsItem*> m_items;
34 34 int m_plotDataIndex;
35 35 int m_marginSize;
36 36
37 37 };
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
50 49 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
64 62 switch(series->type())
65 63 {
66 64 case QChartSeries::SeriesTypeLine: {
67 65
68 66 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
69 67
70 68 XYPlotDomain domain;
71 69 //TODO "nice numbers algorithm"
72 70 domain.m_ticksX=4;
73 71 domain.m_ticksY=4;
74 72
75 73 for (int i = 0 ; i < xyseries->count() ; i++)
76 74 {
77 75 qreal x = xyseries->x(i);
78 76 qreal y = xyseries->y(i);
79 77 domain.m_minX = qMin(domain.m_minX,x);
80 78 domain.m_minY = qMin(domain.m_minY,y);
81 79 domain.m_maxX = qMax(domain.m_maxX,x);
82 80 domain.m_maxY = qMax(domain.m_maxY,y);
83 81 }
84 82
85 83 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
86 84 item->updateXYPlotDomain(domain);
87 85 d->m_plotDomainList<<domain;
88 86 d->m_xyLineChartItems<<item;
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 }
95 98 }
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:
114 113 break;
115 114 }
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
137 141 d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex));
138 142 update();
139 143 }
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
@@ -1,55 +1,55
1 1 #ifndef CHART_H
2 2 #define CHART_H
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
10 10 class Axis;
11 11 class XYGrid;
12 12 class QChartSeries;
13 13 class XYPlotDomain;
14 14 class QChartPrivate;
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 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);
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 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
54 54
55 55 #endif
@@ -1,67 +1,77
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 #include <QDebug>
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9
9 10 //#define QSeriesData QList<qreal>
10 11
11 12 QScatterSeriesPrivate::QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent) :
12 13 QGraphicsItem(parent),
13 14 m_x(x),
14 15 m_y(y)
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)
28 37 {
29 38 QPen pen = painter->pen();
30 39 QBrush brush = pen.brush();
31 40 // TODO: The opacity should be user definable...
32 41 brush.setColor(QColor(255, 82, 0, 50));
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
54 55 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()
61 71 {
62 72 delete d;
63 73 }
64 74
65 75 #include "moc_qscatterseries.cpp"
66 76
67 77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,29 +1,33
1 1 #ifndef QSCATTERSERIES_H
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;
8 9
9 10 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
10 11 {
11 12 Q_OBJECT
12 13 public:
13 14 //QScatterSeries(QSeriesData *data, QObject *chart);
14 15 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *chart = 0);
15 16 ~QScatterSeries();
16 17
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)
23 27 friend class QChart;
24 28 QScatterSeriesPrivate *const d;
25 29 };
26 30
27 31 QTCOMMERCIALCHART_END_NAMESPACE
28 32
29 33 #endif // QSCATTERSERIES_H
@@ -1,29 +1,31
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 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
28 30
29 31 #endif // QSCATTERSERIES_H
@@ -1,76 +1,75
1 1 !include( ../common.pri ) {
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
9 8 QT += core \
10 9 gui
11 10
12 11 CONFIG += debug_and_release
13 12 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
14 13
15 14 SOURCES += \
16 15 xylinechart/qxychartseries.cpp \
17 16 xylinechart/xylinechartitem.cpp \
18 17 xylinechart/xygrid.cpp \
19 18 xylinechart/xyplotdomain.cpp \
20 19 qscatterseries.cpp \
21 20 qchart.cpp \
22 21 axis.cpp \
23 22 qchartwidget.cpp
24 23
25 24 PRIVATE_HEADERS += \
26 25 xylinechart/xylinechartitem_p.h \
27 26 xylinechart/xyplotdomain_p.h \
28 27 xylinechart/xygrid_p.h \
29 28 qscatterseries_p.h \
30 29 axis_p.h
31 30
32 31 PUBLIC_HEADERS += \
33 32 qchartseries.h \
34 33 qscatterseries.h \
35 34 qchart.h \
36 35 qchartwidget.h \
37 36 qchartglobal.h \
38 37 xylinechart/qxychartseries.h
39 38
40 39 HEADERS += $$PUBLIC_HEADERS
41 40 HEADERS += $$PRIVATE_HEADERS
42 41
43 42 INCLUDEPATH += xylinechart \
44 43 .
45 44
46 45 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
47 46 MOC_DIR = $$CHART_BUILD_DIR/lib
48 47 UI_DIR = $$CHART_BUILD_DIR/lib
49 48 RCC_DIR = $$CHART_BUILD_DIR/lib
50 49
51 50
52 51 DEFINES += QTCOMMERCIALCHART_LIBRARY
53 52
54 53 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
55 54 public_headers.files = $$PUBLIC_HEADERS
56 55 target.path = $$[QT_INSTALL_LIBS]
57 56 INSTALLS += target \
58 57 public_headers
59 58
60 59
61 60 install_build_headers.name = bild_headers
62 61 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
63 62 install_build_headers.input = PUBLIC_HEADERS
64 63 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
65 64 install_build_headers.CONFIG += target_predeps no_link
66 65 QMAKE_EXTRA_COMPILERS += install_build_headers
67 66
68 67 chartversion.target = qchartversion_p.h
69 68 chartversion.commands = @echo "build_time" > $$chartversion.target;
70 69 chartversion.depends = $$HEADERS $$SOURCES
71 70 PRE_TARGETDEPS += qchartversion_p.h
72 71 QMAKE_CLEAN+= qchartversion_p.h
73 72 QMAKE_EXTRA_TARGETS += chartversion
74 73
75 74 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
76 75 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
@@ -1,33 +1,33
1 1 #ifndef XYLINECHARTITEM_H
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
9 9
10 10 class QXYChartSeries;
11 11
12 12 class XYLineChartItem : public QGraphicsItem
13 13 {
14 14
15 15 public:
16 16 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 17 virtual ~ XYLineChartItem(){};
18 18
19 19 //from QGraphicsItem
20 20 virtual QRectF boundingRect() const;
21 21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 22
23 23 void updateXYPlotDomain(const XYPlotDomain& data);
24 24
25 25 private:
26 26 QPolygonF m_polyline;
27 27 QXYChartSeries* m_series;
28 28 XYPlotDomain m_xyPlotData;
29 29 };
30 30
31 31 QTCOMMERCIALCHART_END_NAMESPACE
32 32
33 33 #endif
@@ -1,33 +1,33
1 1 !include( ../../common.pri ) {
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5 5 !include( ../../integrated.pri ) {
6 6 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
@@ -1,231 +1,239
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include <qxychartseries.h>
4 4 #include <QPushButton>
5 5 #include <QComboBox>
6 6 #include <QSpinBox>
7 7 #include <QCheckBox>
8 8 #include <QGridLayout>
9 9 #include <QHBoxLayout>
10 10 #include <QLabel>
11 11 #include <QSpacerItem>
12 12 #include <QMessageBox>
13 13 #include <cmath>
14 14 #include <QDebug>
15 15
16 16 QTCOMMERCIALCHART_USE_NAMESPACE
17 17
18 18 MainWidget::MainWidget(QWidget *parent) :
19 19 QWidget(parent)
20 20 {
21 21 QPushButton *addSeriesButton = new QPushButton("Add series");
22 22 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
23 23
24 24 // Chart background
25 25 QComboBox *backgroundCombo = new QComboBox(this);
26 26 backgroundCombo->addItem("None");
27 27 backgroundCombo->addItem("TODO Grid");
28 28 backgroundCombo->addItem("TODO Image");
29 29 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
30 30 this, SLOT(backgroundChanged(int)));
31 31
32 32 // Axis
33 33 // TODO: multiple axes?
34 34 m_autoScaleCheck = new QCheckBox("Automatic scaling");
35 35 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
36 36 // Allow setting also non-sense values (like -2147483648 and 2147483647)
37 37 m_xMinSpin = new QSpinBox();
38 38 m_xMinSpin->setMinimum(INT_MIN);
39 39 m_xMinSpin->setMaximum(INT_MAX);
40 40 m_xMinSpin->setValue(0);
41 41 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
42 42 m_xMaxSpin = new QSpinBox();
43 43 m_xMaxSpin->setMinimum(INT_MIN);
44 44 m_xMaxSpin->setMaximum(INT_MAX);
45 45 m_xMaxSpin->setValue(10);
46 46 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
47 47 m_yMinSpin = new QSpinBox();
48 48 m_yMinSpin->setMinimum(INT_MIN);
49 49 m_yMinSpin->setMaximum(INT_MAX);
50 50 m_yMinSpin->setValue(0);
51 51 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
52 52 m_yMaxSpin = new QSpinBox();
53 53 m_yMaxSpin->setMinimum(INT_MIN);
54 54 m_yMaxSpin->setMaximum(INT_MAX);
55 55 m_yMaxSpin->setValue(10);
56 56 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
57 57
58 58 QGridLayout *grid = new QGridLayout();
59 59 QHBoxLayout *hbox = new QHBoxLayout();
60 60 //grid->addWidget(new QLabel("Add series:"), 0, 0);
61 61 grid->addWidget(addSeriesButton, 0, 1);
62 62 grid->addWidget(new QLabel("Background:"), 2, 0);
63 63 grid->addWidget(backgroundCombo, 2, 1);
64 64 grid->addWidget(m_autoScaleCheck, 3, 0);
65 65 grid->addWidget(new QLabel("x min:"), 4, 0);
66 66 grid->addWidget(m_xMinSpin, 4, 1);
67 67 grid->addWidget(new QLabel("x max:"), 5, 0);
68 68 grid->addWidget(m_xMaxSpin, 5, 1);
69 69 grid->addWidget(new QLabel("y min:"), 6, 0);
70 70 grid->addWidget(m_yMinSpin, 6, 1);
71 71 grid->addWidget(new QLabel("y max:"), 7, 0);
72 72 grid->addWidget(m_yMaxSpin, 7, 1);
73 73 // add row with empty label to make all the other rows static
74 74 grid->addWidget(new QLabel(""), 8, 0);
75 75 grid->setRowStretch(8, 1);
76 76
77 77 hbox->addLayout(grid);
78 78
79 79 m_chartWidget = new QChartWidget(this);
80 80 //m_chartWidget->setColor(Qt::red);
81 81 hbox->addWidget(m_chartWidget);
82 82 // hbox->setStretch(1, 1);
83 83
84 84 setLayout(hbox);
85 85
86 86 m_autoScaleCheck->setChecked(true);
87 87 chartTypeChanged(4);
88 88 testDataChanged(0);
89 89 }
90 90
91 91 void MainWidget::addSeries()
92 92 {
93 93 DataSerieDialog dialog(m_defaultSeries, this);
94 94 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
95 95 dialog.exec();
96 96 }
97 97
98 98 void MainWidget::addSeries(QString series, QString data)
99 99 {
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
141 149 }
142 150 }
143 151
144 152 void MainWidget::chartTypeChanged(int itemIndex)
145 153 {
146 154 // TODO: change chart type
147 155 switch (itemIndex) {
148 156 case 4:
149 157 //m_chartWidget->setType(4);
150 158 break;
151 159 default: {
152 160 //m_chartWidget->setType(0);
153 161 break;
154 162 }
155 163 }
156 164 }
157 165
158 166 void MainWidget::testDataChanged(int itemIndex)
159 167 {
160 168 qDebug() << "testDataChanged: " << itemIndex;
161 169
162 170 // switch (itemIndex) {
163 171 // case 0: {
164 172 // QList<QChartDataPoint> data;
165 173 // for (int x = 0; x < 20; x++) {
166 174 // data.append(QChartDataPoint() << x << x / 2);
167 175 // }
168 176 // m_chartWidget->setData(data);
169 177 // break;
170 178 // }
171 179 // case 1: {
172 180 // QList<QChartDataPoint> data;
173 181 // for (int x = 0; x < 100; x++) {
174 182 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
175 183 // }
176 184 // m_chartWidget->setData(data);
177 185 // break;
178 186 // }
179 187 // case 2: {
180 188 // QList<QChartDataPoint> data;
181 189 // for (int x = 0; x < 1000; x++) {
182 190 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
183 191 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
184 192 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
185 193 // }
186 194 // m_chartWidget->setData(data);
187 195 // break;
188 196 // }
189 197 // default:
190 198 // break;
191 199 // }
192 200 }
193 201
194 202 void MainWidget::backgroundChanged(int itemIndex)
195 203 {
196 204 qDebug() << "backgroundChanged: " << itemIndex;
197 205 }
198 206
199 207 void MainWidget::autoScaleChanged(int value)
200 208 {
201 209 if (value) {
202 210 // TODO: enable auto scaling
203 211 } else {
204 212 // TODO: set scaling manually (and disable auto scaling)
205 213 }
206 214
207 215 m_xMinSpin->setEnabled(!value);
208 216 m_xMaxSpin->setEnabled(!value);
209 217 m_yMinSpin->setEnabled(!value);
210 218 m_yMaxSpin->setEnabled(!value);
211 219 }
212 220
213 221 void MainWidget::xMinChanged(int value)
214 222 {
215 223 qDebug() << "xMinChanged: " << value;
216 224 }
217 225
218 226 void MainWidget::xMaxChanged(int value)
219 227 {
220 228 qDebug() << "xMaxChanged: " << value;
221 229 }
222 230
223 231 void MainWidget::yMinChanged(int value)
224 232 {
225 233 qDebug() << "yMinChanged: " << value;
226 234 }
227 235
228 236 void MainWidget::yMaxChanged(int value)
229 237 {
230 238 qDebug() << "yMaxChanged: " << value;
231 239 }
General Comments 0
You need to be logged in to leave comments. Login now