##// END OF EJS Templates
Added size factor property to pie
Tero Ahola -
r60:0be2c26f8dc1
parent child
Show More
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,32 +1,32
1 integrated_build:{
1 integrated_build:{
2 message('Internal build within charts core source tree')
2 message('Internal build within charts core source tree')
3 INCLUDEPATH += $$CHART_BUILD_HEADER_DIR
3 INCLUDEPATH += $$CHART_BUILD_HEADER_DIR
4 LIBS += -L $$CHART_BUILD_LIB_DIR -Wl,-rpath,$$CHART_BUILD_LIB_DIR
4 LIBS += -L $$CHART_BUILD_LIB_DIR -Wl,-rpath,$$CHART_BUILD_LIB_DIR
5 DESTDIR = $$CHART_BUILD_BIN_DIR
5 DESTDIR = $$CHART_BUILD_BIN_DIR
6 CONFIG(debug, debug|release) {
6 CONFIG(debug, debug|release) {
7 LIBS += -lQtCommercialChartd
7 LIBS += -lQtCommercialChartd
8
8
9 #this is ugly hack to work around missing rpath, it simply copies lib
9 #this is ugly hack to work around missing rpath, it simply copies lib
10 win32:{
10 win32:{
11 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
11 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
12 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\QtCommercialChartd.dll $$CHART_BUILD_BIN_DIR
12 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChartd.dll $$CHART_BUILD_BIN_DIR
13 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChartd.dll
13 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChartd.dll
14 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
14 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
15 QMAKE_EXTRA_TARGETS +=copylib
15 QMAKE_EXTRA_TARGETS +=copylib
16 }
16 }
17
17
18 } else {
18 } else {
19 LIBS += -lQtCommercialChart
19 LIBS += -lQtCommercialChart
20
20
21 #this is ugly hack to work around missing rpath, it simply copies lib
21 #this is ugly hack to work around missing rpath, it simply copies lib
22 win32:{
22 win32: {
23 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChart
23 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChart
24 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\QtCommercialChart.dll $$CHART_BUILD_BIN_DIR
24 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChart.dll $$CHART_BUILD_BIN_DIR
25 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChart.dll
25 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChart.dll
26 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChart
26 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChart
27 QMAKE_EXTRA_TARGETS +=copylib
27 QMAKE_EXTRA_TARGETS +=copylib
28 }
28 }
29 }
29 }
30 }else{
30 } else {
31 CONFIG+=qtcommercialchart
31 CONFIG+=qtcommercialchart
32 }
32 }
@@ -1,75 +1,76
1 #include "pieslice.h"
1 #include "pieslice.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <QDebug>
3 #include <QDebug>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span)
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect)
8 : m_color(color),
8 : m_color(color),
9 m_startAngle(startAngle),
9 m_startAngle(startAngle),
10 m_span(span)
10 m_span(span),
11 m_rect(rect)
11 {
12 {
12 setAcceptHoverEvents(true);
13 setAcceptHoverEvents(true);
13 }
14 }
14
15
15 PieSlice::~PieSlice()
16 PieSlice::~PieSlice()
16 {
17 {
17 }
18 }
18
19
19 QRectF PieSlice::boundingRect() const
20 QRectF PieSlice::boundingRect() const
20 {
21 {
21 return parentItem()->boundingRect();
22 return m_rect;
22 }
23 }
23
24
24 QPainterPath PieSlice::shape() const
25 QPainterPath PieSlice::shape() const
25 {
26 {
26 qreal angle = (-m_startAngle) + (90);
27 qreal angle = (-m_startAngle) + (90);
27 qreal span = -m_span;
28 qreal span = -m_span;
28
29
29 QPainterPath path;
30 QPainterPath path;
30 path.moveTo(boundingRect().center());
31 path.moveTo(boundingRect().center());
31 path.arcTo(boundingRect(), angle, span);
32 path.arcTo(boundingRect(), angle, span);
32
33
33 // TODO: draw the shape so that it might have a hole in the center
34 // TODO: draw the shape so that it might have a hole in the center
34 // - Sin & Cos will be needed to find inner/outer arc endpoints
35 // - Sin & Cos will be needed to find inner/outer arc endpoints
35
36
36 // dx, dy are offsets from the center
37 // dx, dy are offsets from the center
37 //qreal l = boundingRect().height();
38 //qreal l = boundingRect().height();
38 //qreal dx = qSin(angle*(M_PI/180)) * l;
39 //qreal dx = qSin(angle*(M_PI/180)) * l;
39 //qreal dy = qCos(angle*(M_PI/180)) * l;
40 //qreal dy = qCos(angle*(M_PI/180)) * l;
40
41
41 // TODO: exploded slice?
42 // TODO: exploded slice?
42
43
43 return path;
44 return path;
44 }
45 }
45
46
46 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 {
48 {
48 // Setup painter
49 // Setup painter
49 painter->setBrush(m_color);
50 painter->setBrush(m_color);
50 QPen pen;
51 QPen pen;
51 //pen.setColor(m_color.darker());
52 //pen.setColor(m_color.darker());
52 pen.setColor(Qt::gray);
53 pen.setColor(Qt::gray);
53 pen.setWidth(1);
54 pen.setWidth(1);
54 painter->setPen(pen);
55 painter->setPen(pen);
55
56
56 // From Qt docs:
57 // From Qt docs:
57 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
58 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
58 // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction.
59 // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction.
59 // Zero degrees is at the 3 o'clock position.
60 // Zero degrees is at the 3 o'clock position.
60 //
61 //
61 // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360.
62 // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360.
62 //qreal angle = (-m_startAngle*16) + (90*16);
63 //qreal angle = (-m_startAngle*16) + (90*16);
63 //qreal span = -m_span*16;
64 //qreal span = -m_span*16;
64 //painter->drawPie(boundingRect(), angle, span);
65 //painter->drawPie(boundingRect(), angle, span);
65
66
66 painter->drawPath(shape());
67 painter->drawPath(shape());
67 }
68 }
68
69
69 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
70 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
70 {
71 {
71 QGraphicsItem::hoverEnterEvent(event);
72 QGraphicsItem::hoverEnterEvent(event);
72 qDebug() << "hover" << m_color << m_startAngle << m_span;
73 qDebug() << "hover" << m_color << m_startAngle << m_span;
73 }
74 }
74
75
75 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,31 +1,32
1 #ifndef PIESLICE_H
1 #ifndef PIESLICE_H
2 #define PIESLICE_H
2 #define PIESLICE_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
5 #include <QGraphicsItem>
6 #include <QRectF>
6 #include <QRectF>
7 #include <QColor>
7 #include <QColor>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class PieSlice : public QGraphicsItem
11 class PieSlice : public QGraphicsItem
12 {
12 {
13 public:
13 public:
14 PieSlice(const QColor& color, qreal startAngle, qreal span);
14 PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect);
15 ~PieSlice();
15 ~PieSlice();
16
16
17 public: // from QGraphicsItem
17 public: // from QGraphicsItem
18 QRectF boundingRect() const;
18 QRectF boundingRect() const;
19 QPainterPath shape() const;
19 QPainterPath shape() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
21 void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
22
22
23 private:
23 public:
24 QColor m_color;
24 QColor m_color;
25 qreal m_startAngle;
25 qreal m_startAngle;
26 qreal m_span;
26 qreal m_span;
27 QRectF m_rect;
27 };
28 };
28
29
29 QTCOMMERCIALCHART_END_NAMESPACE
30 QTCOMMERCIALCHART_END_NAMESPACE
30
31
31 #endif // PIESLICE_H
32 #endif // PIESLICE_H
@@ -1,54 +1,91
1 #include "qpieseries.h"
1 #include "qpieseries.h"
2 #include "pieslice.h"
2 #include "pieslice.h"
3 #include <QGraphicsObject>
3 #include <QGraphicsObject>
4 #include <QDebug>
4 #include <QDebug>
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
9 QChartSeries(parent),
9 QChartSeries(parent),
10 m_x(x)
10 m_x(x),
11 m_sizeFactor(1.0)
11 {
12 {
12 // Create slices
13 // Create slices
13 qreal fullPie = 360;
14 qreal fullPie = 360;
14 qreal total = 0;
15 qreal total = 0;
15 foreach (qreal value, m_x)
16 foreach (qreal value, m_x)
16 total += value;
17 total += value;
17
18
18 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
19 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
19 Q_ASSERT(parentItem);
20 Q_ASSERT(parentItem);
21 m_chartSize = parentItem->boundingRect();
20 qreal angle = 0;
22 qreal angle = 0;
21 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
23 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
22 foreach (qreal value, m_x) {
24 foreach (qreal value, m_x) {
23 qreal span = value / total * fullPie;
25 qreal span = value / total * fullPie;
24 PieSlice *slice = new PieSlice(randomColor(), angle, span);
26 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
25 slice->setParentItem(parentItem);
27 slice->setParentItem(parentItem);
26 m_slices.append(slice);
28 m_slices.append(slice);
27 angle += span;
29 angle += span;
28 }
30 }
31
32 resizeSlices(m_chartSize);
29 }
33 }
30
34
31 QPieSeries::~QPieSeries()
35 QPieSeries::~QPieSeries()
32 {
36 {
37 while (m_slices.count())
38 delete m_slices.takeLast();
39 }
40
41 void QPieSeries::chartSizeChanged(QRectF chartRect)
42 {
43 // TODO: allow user setting the size?
44 // TODO: allow user defining the margins?
45 m_chartSize = chartRect;
46 resizeSlices(m_chartSize);
33 }
47 }
34
48
35 void QPieSeries::chartSizeChanged(QRectF /*rect*/)
49 void QPieSeries::resizeSlices(QRectF rect)
36 {
50 {
51 QRectF tempRect = rect;
52 if (tempRect.width() < tempRect.height()) {
53 tempRect.setWidth(tempRect.width() * m_sizeFactor);
54 tempRect.setHeight(tempRect.width());
55 tempRect.moveCenter(rect.center());
56 } else {
57 tempRect.setHeight(tempRect.height() * m_sizeFactor);
58 tempRect.setWidth(tempRect.height());
59 tempRect.moveCenter(rect.center());
60 }
61
62 foreach (PieSlice *slice, m_slices)
63 slice->m_rect = tempRect;
64 }
65
66 void QPieSeries::setSizeFactor(qreal factor)
67 {
68 if (factor > 0.0)
69 m_sizeFactor = factor;
70 resizeSlices(m_chartSize);
71
72 // Initiate update via the parent graphics item
73 // TODO: potential issue: what if this function is called from the parent context?
74 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
75 Q_ASSERT(parentItem);
76 parentItem->update();
37 }
77 }
38
78
39 QColor QPieSeries::randomColor()
79 QColor QPieSeries::randomColor()
40 {
80 {
41 QColor c;
81 QColor c;
42 c.setRed(qrand() % 255);
82 c.setRed(qrand() % 255);
43 c.setGreen(qrand() % 255);
83 c.setGreen(qrand() % 255);
44 c.setBlue(qrand() % 255);
84 c.setBlue(qrand() % 255);
45 return c;
85 return c;
46 }
86 }
47
87
48 void QPieSeries::setData(QList<int> data)
49 {
50 }
51
88
52 #include "moc_qpieseries.cpp"
89 #include "moc_qpieseries.cpp"
53
90
54 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,40 +1,44
1 #ifndef PIESERIES_H
1 #ifndef PIESERIES_H
2 #define PIESERIES_H
2 #define PIESERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QObject>
5 #include <QObject>
6 #include <QRectF>
6 #include <QRectF>
7 #include <QColor>
7 #include <QColor>
8
8
9 class QGraphicsObject;
9 class QGraphicsObject;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class PieSlice;
11 class PieSlice;
12
12
13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16
16 public:
17 public:
17 // TODO: use a generic data class instead of x and y
18 // TODO: use a generic data class instead of x and y
18 QPieSeries(QList<qreal> x, QGraphicsObject *parent = 0);
19 QPieSeries(QList<qreal> x, QGraphicsObject *parent = 0);
19 ~QPieSeries();
20 ~QPieSeries();
20 QColor randomColor();
21 QColor randomColor();
21 void setData(QList<int> data);
22 void setSizeFactor(qreal sizeFactor);
23 qreal sizeFactor() { return m_sizeFactor; }
22
24
23 public: // from QChartSeries
25 public: // from QChartSeries
24 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
26 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
25
27
26 public Q_SLOTS:
28 public Q_SLOTS:
27 void chartSizeChanged(QRectF rect);
29 void chartSizeChanged(QRectF rect);
28
30
29 private:
31 private:
32 void resizeSlices(QRectF rect);
30 //Q_DECLARE_PRIVATE(QPieSeries)
33 //Q_DECLARE_PRIVATE(QPieSeries)
31 Q_DISABLE_COPY(QPieSeries)
34 Q_DISABLE_COPY(QPieSeries)
32 // TODO: move the followin to internal impl
35 // TODO: move the followin to internal impl
33 QList<qreal> m_x;
36 QList<qreal> m_x;
34 QList<PieSlice*> m_slices;
37 QList<PieSlice*> m_slices;
35 QSizeF m_size;
38 QRectF m_chartSize;
39 qreal m_sizeFactor;
36 };
40 };
37
41
38 QTCOMMERCIALCHART_END_NAMESPACE
42 QTCOMMERCIALCHART_END_NAMESPACE
39
43
40 #endif // PIESERIES_H
44 #endif // PIESERIES_H
@@ -1,27 +1,25
1 include( ../../common.pri )
1 !include( ../../common.pri ) {
2 include( ../../integrated.pri )
2 error( "Couldn't find the common.pri file!" )
3 }
4 !include( ../../integrated.pri ) {
5 error( "Couldn't find the integrated.pri file !")
6 }
3
7
4 TARGET = chartwidgettest
8 TARGET = chartwidgettest
5 TEMPLATE = app
9 TEMPLATE = app
6
10
7 QT += core gui
11 QT += core gui
8 contains(QT_MAJOR_VERSION, 5) {
12 contains(QT_MAJOR_VERSION, 5) {
9 QT += widgets
13 QT += widgets
10 }
14 }
11
15
12
13 OBJECTS_DIR = tmp
16 OBJECTS_DIR = tmp
14 MOC_DIR = tmp
17 MOC_DIR = tmp
15
18
16 SOURCES += main.cpp \
19 SOURCES += main.cpp \
17 mainwidget.cpp \
20 mainwidget.cpp \
18 # qscatterseries.cpp \
19 # qseriespointgraphicsitem.cpp \
20 dataseriedialog.cpp
21 dataseriedialog.cpp
21
22
22 HEADERS += \
23 HEADERS += \
23 mainwidget.h \
24 mainwidget.h \
24 # qscatterseries.h \
25 # qseriespointgraphicsitem.h \
26 dataseriedialog.h
25 dataseriedialog.h
27
@@ -1,241 +1,271
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
3 #include <qxychartseries.h>
5 #include <qxychartseries.h>
4 #include <QPushButton>
6 #include <QPushButton>
5 #include <QComboBox>
7 #include <QComboBox>
6 #include <QSpinBox>
8 #include <QSpinBox>
7 #include <QCheckBox>
9 #include <QCheckBox>
8 #include <QGridLayout>
10 #include <QGridLayout>
9 #include <QHBoxLayout>
11 #include <QHBoxLayout>
10 #include <QLabel>
12 #include <QLabel>
11 #include <QSpacerItem>
13 #include <QSpacerItem>
12 #include <QMessageBox>
14 #include <QMessageBox>
13 #include <cmath>
15 #include <cmath>
14 #include <QDebug>
16 #include <QDebug>
15
17
16 QTCOMMERCIALCHART_USE_NAMESPACE
18 QTCOMMERCIALCHART_USE_NAMESPACE
17
19
18 MainWidget::MainWidget(QWidget *parent) :
20 MainWidget::MainWidget(QWidget *parent) :
19 QWidget(parent)
21 QWidget(parent)
20 {
22 {
21 QPushButton *addSeriesButton = new QPushButton("Add series");
23 QPushButton *addSeriesButton = new QPushButton("Add series");
22 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
24 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
23
25
24 // Chart background
26 // Chart background
25 QComboBox *backgroundCombo = new QComboBox(this);
27 QComboBox *backgroundCombo = new QComboBox(this);
26 backgroundCombo->addItem("None");
28 backgroundCombo->addItem("None");
27 backgroundCombo->addItem("TODO Grid");
29 backgroundCombo->addItem("TODO Grid");
28 backgroundCombo->addItem("TODO Image");
30 backgroundCombo->addItem("TODO Image");
29 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
31 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
30 this, SLOT(backgroundChanged(int)));
32 this, SLOT(backgroundChanged(int)));
31
33
32 // Axis
34 // Axis
33 // TODO: multiple axes?
35 // TODO: multiple axes?
34 m_autoScaleCheck = new QCheckBox("Automatic scaling");
36 m_autoScaleCheck = new QCheckBox("Automatic scaling");
35 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
37 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
36 // Allow setting also non-sense values (like -2147483648 and 2147483647)
38 // Allow setting also non-sense values (like -2147483648 and 2147483647)
37 m_xMinSpin = new QSpinBox();
39 m_xMinSpin = new QSpinBox();
38 m_xMinSpin->setMinimum(INT_MIN);
40 m_xMinSpin->setMinimum(INT_MIN);
39 m_xMinSpin->setMaximum(INT_MAX);
41 m_xMinSpin->setMaximum(INT_MAX);
40 m_xMinSpin->setValue(0);
42 m_xMinSpin->setValue(0);
41 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
43 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
42 m_xMaxSpin = new QSpinBox();
44 m_xMaxSpin = new QSpinBox();
43 m_xMaxSpin->setMinimum(INT_MIN);
45 m_xMaxSpin->setMinimum(INT_MIN);
44 m_xMaxSpin->setMaximum(INT_MAX);
46 m_xMaxSpin->setMaximum(INT_MAX);
45 m_xMaxSpin->setValue(10);
47 m_xMaxSpin->setValue(10);
46 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
48 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
47 m_yMinSpin = new QSpinBox();
49 m_yMinSpin = new QSpinBox();
48 m_yMinSpin->setMinimum(INT_MIN);
50 m_yMinSpin->setMinimum(INT_MIN);
49 m_yMinSpin->setMaximum(INT_MAX);
51 m_yMinSpin->setMaximum(INT_MAX);
50 m_yMinSpin->setValue(0);
52 m_yMinSpin->setValue(0);
51 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
53 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
52 m_yMaxSpin = new QSpinBox();
54 m_yMaxSpin = new QSpinBox();
53 m_yMaxSpin->setMinimum(INT_MIN);
55 m_yMaxSpin->setMinimum(INT_MIN);
54 m_yMaxSpin->setMaximum(INT_MAX);
56 m_yMaxSpin->setMaximum(INT_MAX);
55 m_yMaxSpin->setValue(10);
57 m_yMaxSpin->setValue(10);
56 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
58 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
57
59
58 QGridLayout *grid = new QGridLayout();
60 QGridLayout *grid = new QGridLayout();
59 QHBoxLayout *hbox = new QHBoxLayout();
61 QGridLayout *mainLayout = new QGridLayout();
60 //grid->addWidget(new QLabel("Add series:"), 0, 0);
62 //grid->addWidget(new QLabel("Add series:"), 0, 0);
61 grid->addWidget(addSeriesButton, 0, 1);
63 grid->addWidget(addSeriesButton, 0, 1);
62 grid->addWidget(new QLabel("Background:"), 2, 0);
64 grid->addWidget(new QLabel("Background:"), 2, 0);
63 grid->addWidget(backgroundCombo, 2, 1);
65 grid->addWidget(backgroundCombo, 2, 1);
64 grid->addWidget(m_autoScaleCheck, 3, 0);
66 grid->addWidget(m_autoScaleCheck, 3, 0);
65 grid->addWidget(new QLabel("x min:"), 4, 0);
67 grid->addWidget(new QLabel("x min:"), 4, 0);
66 grid->addWidget(m_xMinSpin, 4, 1);
68 grid->addWidget(m_xMinSpin, 4, 1);
67 grid->addWidget(new QLabel("x max:"), 5, 0);
69 grid->addWidget(new QLabel("x max:"), 5, 0);
68 grid->addWidget(m_xMaxSpin, 5, 1);
70 grid->addWidget(m_xMaxSpin, 5, 1);
69 grid->addWidget(new QLabel("y min:"), 6, 0);
71 grid->addWidget(new QLabel("y min:"), 6, 0);
70 grid->addWidget(m_yMinSpin, 6, 1);
72 grid->addWidget(m_yMinSpin, 6, 1);
71 grid->addWidget(new QLabel("y max:"), 7, 0);
73 grid->addWidget(new QLabel("y max:"), 7, 0);
72 grid->addWidget(m_yMaxSpin, 7, 1);
74 grid->addWidget(m_yMaxSpin, 7, 1);
73 // add row with empty label to make all the other rows static
75 // add row with empty label to make all the other rows static
74 grid->addWidget(new QLabel(""), 8, 0);
76 grid->addWidget(new QLabel(""), 8, 0);
75 grid->setRowStretch(8, 1);
77 grid->setRowStretch(8, 1);
76
78
77 hbox->addLayout(grid);
79 mainLayout->addLayout(grid, 0, 0);
80
81 // Scatter specific settings
82 m_scatterLayout = new QGridLayout();
83 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
84 m_scatterLayout->setEnabled(false);
85
86 // Pie specific settings
87 m_pieLayout = new QGridLayout();
88 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
89 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
90 pieSizeSpin->setMinimum(LONG_MIN);
91 pieSizeSpin->setMaximum(LONG_MAX);
92 pieSizeSpin->setValue(1.0);
93 pieSizeSpin->setSingleStep(0.1);
94 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
95 m_pieLayout->setEnabled(false);
96 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
97
98 mainLayout->addLayout(m_scatterLayout, 1, 0);
99 mainLayout->addLayout(m_pieLayout, 2, 0);
78
100
79 m_chartWidget = new QChartWidget(this);
101 m_chartWidget = new QChartWidget(this);
80 //m_chartWidget->setColor(Qt::red);
102 //m_chartWidget->setColor(Qt::red);
81 hbox->addWidget(m_chartWidget);
103 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
82 // hbox->setStretch(1, 1);
104 // hbox->setStretch(1, 1);
83
105
84 setLayout(hbox);
106 setLayout(mainLayout);
85
107
86 m_autoScaleCheck->setChecked(true);
108 m_autoScaleCheck->setChecked(true);
87 chartTypeChanged(4);
88 testDataChanged(0);
109 testDataChanged(0);
89 }
110 }
90
111
91 void MainWidget::addSeries()
112 void MainWidget::addSeries()
92 {
113 {
93 DataSerieDialog dialog(m_defaultSeries, this);
114 DataSerieDialog dialog(m_defaultSeriesName, this);
94 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
115 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
95 dialog.exec();
116 dialog.exec();
96 }
117 }
97
118
98 void MainWidget::addSeries(QString series, QString data)
119 void MainWidget::addSeries(QString series, QString data)
99 {
120 {
100 qDebug() << "addSeries: " << series << " data: " << data;
121 qDebug() << "addSeries: " << series << " data: " << data;
101 m_defaultSeries = series;
122 m_defaultSeriesName = series;
102
123 QChartSeries *newSeries = QXYChartSeries::create();
103 QXYChartSeries* series0 = QXYChartSeries::create();
104
124
105 // TODO: a dedicated data class for storing x and y values
125 // TODO: a dedicated data class for storing x and y values
106 QList<qreal> x;
126 QList<qreal> x;
107 QList<qreal> y;
127 QList<qreal> y;
108
128
109 if (data == "linear") {
129 if (data == "linear") {
110 for (int i = 0; i < 20; i++) {
130 for (int i = 0; i < 20; i++) {
111 x.append(i);
131 x.append(i);
112 y.append(i);
132 y.append(i);
113 }
133 }
114 for (int i = 0; i < 20; i++)
134 for (int i = 0; i < 20; i++)
115 series0->add(i, i);
135 ((QXYChartSeries *)newSeries)->add(i, i);
116 } else if (data == "linear, 1M") {
136 } else if (data == "linear, 1M") {
117 for (int i = 0; i < 10000; i++) {
137 for (int i = 0; i < 10000; i++) {
118 x.append(i);
138 x.append(i);
119 y.append(20);
139 y.append(20);
120 }
140 }
121 for (int i = 0; i < 1000000; i++)
141 for (int i = 0; i < 1000000; i++)
122 series0->add(i, 10);
142 ((QXYChartSeries *)newSeries)->add(i, 10);
123 } else if (data == "SIN") {
143 } else if (data == "SIN") {
124 for (int i = 0; i < 100; i++) {
144 for (int i = 0; i < 100; i++) {
125 x.append(i);
145 x.append(i);
126 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
146 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
127 }
147 }
128 for (int i = 0; i < 100; i++)
148 for (int i = 0; i < 100; i++)
129 series0->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
149 ((QXYChartSeries *)newSeries)->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
130 } else if (data == "SIN + random") {
150 } else if (data == "SIN + random") {
131 for (qreal i = 0; i < 100; i += 0.1) {
151 for (qreal i = 0; i < 100; i += 0.1) {
132 x.append(i + (rand() % 5));
152 x.append(i + (rand() % 5));
133 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
153 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
134 }
154 }
135 for (qreal i = 0; i < 100; i += 0.1)
155 for (qreal i = 0; i < 100; i += 0.1)
136 series0->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
156 ((QXYChartSeries *)newSeries)->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
137 } else {
157 } else {
138 // TODO: check if data has a valid file name
158 // TODO: check if data has a valid file name
139 }
159 }
140
160
141 // TODO: color of the series
161 // TODO: color of the series
142 if (series == "Scatter") {
162 if (series == "Scatter") {
143 /*QChartSeries* scatterSeries = */
163 newSeries = m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
144 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
145 } else if (series == "Pie") {
164 } else if (series == "Pie") {
146 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie);
165 newSeries = m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie);
147 } else if (series == "Line") {
166 } else if (series == "Line") {
148 m_chartWidget->addSeries(series0);
167 m_chartWidget->addSeries(newSeries);
149 } else {
168 } else {
150 // TODO
169 // TODO
151 }
170 }
171
172 setCurrentSeries(newSeries);
152 }
173 }
153
174
154 void MainWidget::chartTypeChanged(int itemIndex)
175 void MainWidget::setCurrentSeries(QChartSeries *series)
155 {
176 {
156 // TODO: change chart type
177 m_currentSeries = series;
157 switch (itemIndex) {
178 switch (m_currentSeries->type()) {
158 case 4:
179 case QChartSeries::SeriesTypeLine:
159 //m_chartWidget->setType(4);
160 break;
180 break;
161 default: {
181 case QChartSeries::SeriesTypeScatter:
162 //m_chartWidget->setType(0);
182 break;
183 case QChartSeries::SeriesTypePie:
184 break;
185 default:
186 Q_ASSERT(false);
163 break;
187 break;
164 }
165 }
188 }
166 }
189 }
167
190
168 void MainWidget::testDataChanged(int itemIndex)
191 void MainWidget::testDataChanged(int itemIndex)
169 {
192 {
170 qDebug() << "testDataChanged: " << itemIndex;
193 qDebug() << "testDataChanged: " << itemIndex;
171
194
172 // switch (itemIndex) {
195 // switch (itemIndex) {
173 // case 0: {
196 // case 0: {
174 // QList<QChartDataPoint> data;
197 // QList<QChartDataPoint> data;
175 // for (int x = 0; x < 20; x++) {
198 // for (int x = 0; x < 20; x++) {
176 // data.append(QChartDataPoint() << x << x / 2);
199 // data.append(QChartDataPoint() << x << x / 2);
177 // }
200 // }
178 // m_chartWidget->setData(data);
201 // m_chartWidget->setData(data);
179 // break;
202 // break;
180 // }
203 // }
181 // case 1: {
204 // case 1: {
182 // QList<QChartDataPoint> data;
205 // QList<QChartDataPoint> data;
183 // for (int x = 0; x < 100; x++) {
206 // for (int x = 0; x < 100; x++) {
184 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
207 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
185 // }
208 // }
186 // m_chartWidget->setData(data);
209 // m_chartWidget->setData(data);
187 // break;
210 // break;
188 // }
211 // }
189 // case 2: {
212 // case 2: {
190 // QList<QChartDataPoint> data;
213 // QList<QChartDataPoint> data;
191 // for (int x = 0; x < 1000; x++) {
214 // for (int x = 0; x < 1000; x++) {
192 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
215 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
193 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
216 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
194 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
217 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
195 // }
218 // }
196 // m_chartWidget->setData(data);
219 // m_chartWidget->setData(data);
197 // break;
220 // break;
198 // }
221 // }
199 // default:
222 // default:
200 // break;
223 // break;
201 // }
224 // }
202 }
225 }
203
226
204 void MainWidget::backgroundChanged(int itemIndex)
227 void MainWidget::backgroundChanged(int itemIndex)
205 {
228 {
206 qDebug() << "backgroundChanged: " << itemIndex;
229 qDebug() << "backgroundChanged: " << itemIndex;
207 }
230 }
208
231
209 void MainWidget::autoScaleChanged(int value)
232 void MainWidget::autoScaleChanged(int value)
210 {
233 {
211 if (value) {
234 if (value) {
212 // TODO: enable auto scaling
235 // TODO: enable auto scaling
213 } else {
236 } else {
214 // TODO: set scaling manually (and disable auto scaling)
237 // TODO: set scaling manually (and disable auto scaling)
215 }
238 }
216
239
217 m_xMinSpin->setEnabled(!value);
240 m_xMinSpin->setEnabled(!value);
218 m_xMaxSpin->setEnabled(!value);
241 m_xMaxSpin->setEnabled(!value);
219 m_yMinSpin->setEnabled(!value);
242 m_yMinSpin->setEnabled(!value);
220 m_yMaxSpin->setEnabled(!value);
243 m_yMaxSpin->setEnabled(!value);
221 }
244 }
222
245
223 void MainWidget::xMinChanged(int value)
246 void MainWidget::xMinChanged(int value)
224 {
247 {
225 qDebug() << "xMinChanged: " << value;
248 qDebug() << "xMinChanged: " << value;
226 }
249 }
227
250
228 void MainWidget::xMaxChanged(int value)
251 void MainWidget::xMaxChanged(int value)
229 {
252 {
230 qDebug() << "xMaxChanged: " << value;
253 qDebug() << "xMaxChanged: " << value;
231 }
254 }
232
255
233 void MainWidget::yMinChanged(int value)
256 void MainWidget::yMinChanged(int value)
234 {
257 {
235 qDebug() << "yMinChanged: " << value;
258 qDebug() << "yMinChanged: " << value;
236 }
259 }
237
260
238 void MainWidget::yMaxChanged(int value)
261 void MainWidget::yMaxChanged(int value)
239 {
262 {
240 qDebug() << "yMaxChanged: " << value;
263 qDebug() << "yMaxChanged: " << value;
241 }
264 }
265
266 void MainWidget::setPieSizeFactor(double size)
267 {
268 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
269 Q_ASSERT(pie);
270 pie->setSizeFactor(qreal(size));
271 }
@@ -1,43 +1,48
1 #ifndef MAINWIDGET_H
1 #ifndef MAINWIDGET_H
2 #define MAINWIDGET_H
2 #define MAINWIDGET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartwidget.h>
5 #include <qchartwidget.h>
6 #include <QWidget>
6 #include <QWidget>
7
7
8 class QSpinBox;
8 class QSpinBox;
9 class QCheckBox;
9 class QCheckBox;
10 class QGridLayout;
10
11
11 QTCOMMERCIALCHART_USE_NAMESPACE
12 QTCOMMERCIALCHART_USE_NAMESPACE
12
13
13 class MainWidget : public QWidget
14 class MainWidget : public QWidget
14 {
15 {
15 Q_OBJECT
16 Q_OBJECT
16 public:
17 public:
17 explicit MainWidget(QWidget *parent = 0);
18 explicit MainWidget(QWidget *parent = 0);
18
19
19 signals:
20 signals:
20
21
21 private slots:
22 private slots:
22 void chartTypeChanged(int itemIndex);
23 void addSeries();
23 void addSeries();
24 void addSeries(QString series, QString data);
24 void addSeries(QString series, QString data);
25 void testDataChanged(int itemIndex);
25 void testDataChanged(int itemIndex);
26 void backgroundChanged(int itemIndex);
26 void backgroundChanged(int itemIndex);
27 void autoScaleChanged(int value);
27 void autoScaleChanged(int value);
28 void xMinChanged(int value);
28 void xMinChanged(int value);
29 void xMaxChanged(int value);
29 void xMaxChanged(int value);
30 void yMinChanged(int value);
30 void yMinChanged(int value);
31 void yMaxChanged(int value);
31 void yMaxChanged(int value);
32 void setCurrentSeries(QChartSeries *series);
33 void setPieSizeFactor(double margin);
32
34
33 private:
35 private:
34 QChartWidget *m_chartWidget;
36 QChartWidget *m_chartWidget;
35 QCheckBox *m_autoScaleCheck;
37 QCheckBox *m_autoScaleCheck;
36 QSpinBox *m_xMinSpin;
38 QSpinBox *m_xMinSpin;
37 QSpinBox *m_xMaxSpin;
39 QSpinBox *m_xMaxSpin;
38 QSpinBox *m_yMinSpin;
40 QSpinBox *m_yMinSpin;
39 QSpinBox *m_yMaxSpin;
41 QSpinBox *m_yMaxSpin;
40 QString m_defaultSeries;
42 QString m_defaultSeriesName;
43 QChartSeries *m_currentSeries;
44 QGridLayout *m_scatterLayout;
45 QGridLayout *m_pieLayout;
41 };
46 };
42
47
43 #endif // MAINWIDGET_H
48 #endif // MAINWIDGET_H
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now