diff --git a/test/chartwidgettest/chartwidgettest.pro b/test/chartwidgettest/chartwidgettest.pro index 97cd4e8..25b59ad 100644 --- a/test/chartwidgettest/chartwidgettest.pro +++ b/test/chartwidgettest/chartwidgettest.pro @@ -13,10 +13,16 @@ OBJECTS_DIR = tmp MOC_DIR = tmp SOURCES += main.cpp \ - mainwidget.cpp + mainwidget.cpp \ + qscatterseries.cpp \ + qchartwidget.cpp HEADERS += \ - mainwidget.h + mainwidget.h \ + qscatterseries.h \ + qchartwidget.h + + diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index a7a56bc..549cf44 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -1,5 +1,6 @@ #include "mainwidget.h" -#include +#include "qchartwidget.h" +//#include #include #include #include @@ -14,10 +15,9 @@ MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { - m_chartWidget = new ChartWidget(this); - m_chartWidget->resize(QSize(200,200)); - m_chartWidget->setColor(Qt::red); - + m_chartWidget = new QChartWidget(this); +// m_chartWidget->resize(QSize(200,200)); +// m_chartWidget->setColor(Qt::red); // Chart type // TODO: How about multiple types? // Should the type be a property of a graph instead of the chart? @@ -32,10 +32,8 @@ MainWidget::MainWidget(QWidget *parent) : this, SLOT(chartTypeChanged(int))); // Test data selector - QComboBox *dataCombo = new QComboBox(this); - dataCombo->addItem("todo: add test data"); - connect(dataCombo, SIGNAL(currentIndexChanged(QString)), - this, SLOT(dataChanged(QString))); + QPushButton *fileButton = new QPushButton("From file"); + QPushButton *urlButton = new QPushButton("From URL"); // Chart background QComboBox *backgroundCombo = new QComboBox(this); @@ -45,8 +43,8 @@ MainWidget::MainWidget(QWidget *parent) : // Axis // TODO: multiple axes? - QCheckBox *autoScaleCheck = new QCheckBox("Automatic scaling"); - connect(autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); + m_autoScaleCheck = new QCheckBox("Automatic scaling"); + connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); // Allow setting also non-sense values (like -2147483648 and 2147483647) m_xMinSpin = new QSpinBox(); m_xMinSpin->setMinimum(INT_MIN); @@ -72,20 +70,21 @@ MainWidget::MainWidget(QWidget *parent) : QGridLayout *grid = new QGridLayout(); QHBoxLayout *hbox = new QHBoxLayout(); grid->addWidget(new QLabel("Chart type:"), 0, 0); - grid->addWidget(chartTypeCombo, 0, 1); + grid->addWidget(chartTypeCombo, 0, 1, 1, 2); grid->addWidget(new QLabel("Data:"), 1, 0); - grid->addWidget(dataCombo, 1, 1); + grid->addWidget(fileButton, 1, 1); + grid->addWidget(urlButton, 1, 2); grid->addWidget(new QLabel("Background:"), 2, 0); - grid->addWidget(backgroundCombo, 2, 1); - grid->addWidget(autoScaleCheck, 3, 0); + grid->addWidget(backgroundCombo, 2, 1, 1, 2); + grid->addWidget(m_autoScaleCheck, 3, 0); grid->addWidget(new QLabel("x min:"), 4, 0); - grid->addWidget(m_xMinSpin, 4, 1); + grid->addWidget(m_xMinSpin, 4, 1, 1, 2); grid->addWidget(new QLabel("x max:"), 5, 0); - grid->addWidget(m_xMaxSpin, 5, 1); + grid->addWidget(m_xMaxSpin, 5, 1, 1, 2); grid->addWidget(new QLabel("y min:"), 6, 0); - grid->addWidget(m_yMinSpin, 6, 1); + grid->addWidget(m_yMinSpin, 6, 1, 1, 2); grid->addWidget(new QLabel("y max:"), 7, 0); - grid->addWidget(m_yMaxSpin, 7, 1); + grid->addWidget(m_yMaxSpin, 7, 1, 1, 2); // add row with empty label to make all the other rows static grid->addWidget(new QLabel(""), 8, 0); grid->setRowStretch(8, 1); @@ -96,17 +95,21 @@ MainWidget::MainWidget(QWidget *parent) : setLayout(hbox); - // Setting auto scale affects min/max value spin boxes, so it needs to be done after they have - // been constructed - autoScaleCheck->setChecked(true); + m_autoScaleCheck->setChecked(true); } void MainWidget::chartTypeChanged(int itemIndex) { - qDebug() << "chartTypeChanged: " << itemIndex; - QMessageBox msg; - msg.setText("TODO: API for setting chart type"); - msg.exec(); + // TODO: change chart type + switch (itemIndex) { + case 4: { + m_chartWidget->setType(4); + break; + } + default: + m_chartWidget->setType(0); + break; + } } void MainWidget::dataChanged(QString itemText) @@ -121,17 +124,16 @@ void MainWidget::backgroundChanged(int itemIndex) void MainWidget::autoScaleChanged(int value) { - if (this->isVisible()) { - QMessageBox msg; - msg.setText("TODO: APIs for using either auto scaling or explicit values"); - msg.exec(); + if (value) { + // TODO: enable auto scaling + } else { + // TODO: set scaling manually (and disable auto scaling) } - // TODO: get initial spin box axis values from charts widget - m_xMinSpin->setEnabled(value == Qt::Unchecked); - m_xMaxSpin->setEnabled(value == Qt::Unchecked); - m_yMinSpin->setEnabled(value == Qt::Unchecked); - m_yMaxSpin->setEnabled(value == Qt::Unchecked); + m_xMinSpin->setEnabled(!value); + m_xMaxSpin->setEnabled(!value); + m_yMinSpin->setEnabled(!value); + m_yMaxSpin->setEnabled(!value); } void MainWidget::xMinChanged(int value) diff --git a/test/chartwidgettest/mainwidget.h b/test/chartwidgettest/mainwidget.h index 8b703a6..8748bf3 100644 --- a/test/chartwidgettest/mainwidget.h +++ b/test/chartwidgettest/mainwidget.h @@ -3,8 +3,9 @@ #include -class ChartWidget; +class QChartWidget; class QSpinBox; +class QCheckBox; class MainWidget : public QWidget { @@ -25,7 +26,8 @@ private slots: void yMaxChanged(int value); private: - ChartWidget *m_chartWidget; + QChartWidget *m_chartWidget; + QCheckBox *m_autoScaleCheck; QSpinBox *m_xMinSpin; QSpinBox *m_xMaxSpin; QSpinBox *m_yMinSpin; diff --git a/test/chartwidgettest/qchartwidget.cpp b/test/chartwidgettest/qchartwidget.cpp new file mode 100644 index 0000000..9e479f4 --- /dev/null +++ b/test/chartwidgettest/qchartwidget.cpp @@ -0,0 +1,41 @@ +#include "qchartwidget.h" +#include "qscatterseries.h" +#include +#include + +QChartWidget::QChartWidget(QWidget *parent) : + QWidget(parent) +{ + // scatter + m_scene = new QGraphicsScene(); + m_view = new QGraphicsView(m_scene, this); + m_view->resize(300, 300); + + // TODO: implement graphics item for axis + m_scene->addLine(0, 0, 0, 100); + m_scene->addLine(0, 100, 100, 100); + + m_view->show(); +} + +QChartWidget::~QChartWidget() +{ + delete m_view; + delete m_scene; +} + +void QChartWidget::setType(/*TODO QChart::Type*/ int type) +{ + if (type == 4) { + if (!m_scatter) { + m_scatter = new QScatterSeries(); + m_scene->addItem(m_scatter); + } + } else { + if (m_scatter) { + m_scene->removeItem(m_scatter); + delete m_scatter; + m_scatter = 0; + } + } +} diff --git a/test/chartwidgettest/qchartwidget.h b/test/chartwidgettest/qchartwidget.h new file mode 100644 index 0000000..b574248 --- /dev/null +++ b/test/chartwidgettest/qchartwidget.h @@ -0,0 +1,28 @@ +#ifndef QCHARTWIDGET_H +#define QCHARTWIDGET_H + +#include + +class QGraphicsView; +class QGraphicsScene; +class QScatterSeries; + +class QChartWidget : public QWidget +{ + Q_OBJECT +public: + explicit QChartWidget(QWidget *parent = 0); + ~QChartWidget(); + void setType(/*TODO QChart::Type*/ int type); + +signals: + +public slots: + +private: + QGraphicsView *m_view; + QGraphicsScene *m_scene; + QScatterSeries *m_scatter; +}; + +#endif // QCHARTWIDGET_H diff --git a/test/chartwidgettest/qscatterseries.cpp b/test/chartwidgettest/qscatterseries.cpp new file mode 100644 index 0000000..a008e84 --- /dev/null +++ b/test/chartwidgettest/qscatterseries.cpp @@ -0,0 +1,29 @@ +#include "qscatterseries.h" +#include + +QScatterSeries::QScatterSeries(QGraphicsItem *parent) : + QGraphicsItem(parent) +{ +} + +QRectF QScatterSeries::boundingRect() const +{ +// qreal penWidth = 1; +// return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, +// 20 + penWidth, 20 + penWidth); + return QRectF(0, 0, 100, 100); +} + +void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + QPen pen = painter->pen(); + QBrush brush = pen.brush(); + brush.setColor(Qt::darkRed); + pen.setBrush(brush); + painter->setPen(pen); + painter->drawRoundedRect(0, 0, 10, 10, 5, 5); + painter->drawRoundedRect(5, 5, 10, 10, 5, 5); + painter->drawRoundedRect(54, 25, 10, 10, 5, 5); + painter->drawRoundedRect(34, 35, 10, 10, 5, 5); + //painter->drawEllipse(QPoint(10, 10), 100, 100); +} diff --git a/test/chartwidgettest/qscatterseries.h b/test/chartwidgettest/qscatterseries.h new file mode 100644 index 0000000..56d570a --- /dev/null +++ b/test/chartwidgettest/qscatterseries.h @@ -0,0 +1,20 @@ +#ifndef QSCATTERSERIES_H +#define QSCATTERSERIES_H + +#include + +class QScatterSeries : public QGraphicsItem +{ +public: + explicit QScatterSeries(QGraphicsItem *parent = 0); + +signals: + +public slots: + +private: + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; + +#endif // QSCATTERSERIES_H