@@ -0,0 +1,41 | |||||
|
1 | #include "qchartwidget.h" | |||
|
2 | #include "qscatterseries.h" | |||
|
3 | #include <QGraphicsView> | |||
|
4 | #include <QGraphicsScene> | |||
|
5 | ||||
|
6 | QChartWidget::QChartWidget(QWidget *parent) : | |||
|
7 | QWidget(parent) | |||
|
8 | { | |||
|
9 | // scatter | |||
|
10 | m_scene = new QGraphicsScene(); | |||
|
11 | m_view = new QGraphicsView(m_scene, this); | |||
|
12 | m_view->resize(300, 300); | |||
|
13 | ||||
|
14 | // TODO: implement graphics item for axis | |||
|
15 | m_scene->addLine(0, 0, 0, 100); | |||
|
16 | m_scene->addLine(0, 100, 100, 100); | |||
|
17 | ||||
|
18 | m_view->show(); | |||
|
19 | } | |||
|
20 | ||||
|
21 | QChartWidget::~QChartWidget() | |||
|
22 | { | |||
|
23 | delete m_view; | |||
|
24 | delete m_scene; | |||
|
25 | } | |||
|
26 | ||||
|
27 | void QChartWidget::setType(/*TODO QChart::Type*/ int type) | |||
|
28 | { | |||
|
29 | if (type == 4) { | |||
|
30 | if (!m_scatter) { | |||
|
31 | m_scatter = new QScatterSeries(); | |||
|
32 | m_scene->addItem(m_scatter); | |||
|
33 | } | |||
|
34 | } else { | |||
|
35 | if (m_scatter) { | |||
|
36 | m_scene->removeItem(m_scatter); | |||
|
37 | delete m_scatter; | |||
|
38 | m_scatter = 0; | |||
|
39 | } | |||
|
40 | } | |||
|
41 | } |
@@ -0,0 +1,28 | |||||
|
1 | #ifndef QCHARTWIDGET_H | |||
|
2 | #define QCHARTWIDGET_H | |||
|
3 | ||||
|
4 | #include <QWidget> | |||
|
5 | ||||
|
6 | class QGraphicsView; | |||
|
7 | class QGraphicsScene; | |||
|
8 | class QScatterSeries; | |||
|
9 | ||||
|
10 | class QChartWidget : public QWidget | |||
|
11 | { | |||
|
12 | Q_OBJECT | |||
|
13 | public: | |||
|
14 | explicit QChartWidget(QWidget *parent = 0); | |||
|
15 | ~QChartWidget(); | |||
|
16 | void setType(/*TODO QChart::Type*/ int type); | |||
|
17 | ||||
|
18 | signals: | |||
|
19 | ||||
|
20 | public slots: | |||
|
21 | ||||
|
22 | private: | |||
|
23 | QGraphicsView *m_view; | |||
|
24 | QGraphicsScene *m_scene; | |||
|
25 | QScatterSeries *m_scatter; | |||
|
26 | }; | |||
|
27 | ||||
|
28 | #endif // QCHARTWIDGET_H |
@@ -0,0 +1,29 | |||||
|
1 | #include "qscatterseries.h" | |||
|
2 | #include <QPainter> | |||
|
3 | ||||
|
4 | QScatterSeries::QScatterSeries(QGraphicsItem *parent) : | |||
|
5 | QGraphicsItem(parent) | |||
|
6 | { | |||
|
7 | } | |||
|
8 | ||||
|
9 | QRectF QScatterSeries::boundingRect() const | |||
|
10 | { | |||
|
11 | // qreal penWidth = 1; | |||
|
12 | // return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, | |||
|
13 | // 20 + penWidth, 20 + penWidth); | |||
|
14 | return QRectF(0, 0, 100, 100); | |||
|
15 | } | |||
|
16 | ||||
|
17 | void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
18 | { | |||
|
19 | QPen pen = painter->pen(); | |||
|
20 | QBrush brush = pen.brush(); | |||
|
21 | brush.setColor(Qt::darkRed); | |||
|
22 | pen.setBrush(brush); | |||
|
23 | painter->setPen(pen); | |||
|
24 | painter->drawRoundedRect(0, 0, 10, 10, 5, 5); | |||
|
25 | painter->drawRoundedRect(5, 5, 10, 10, 5, 5); | |||
|
26 | painter->drawRoundedRect(54, 25, 10, 10, 5, 5); | |||
|
27 | painter->drawRoundedRect(34, 35, 10, 10, 5, 5); | |||
|
28 | //painter->drawEllipse(QPoint(10, 10), 100, 100); | |||
|
29 | } |
@@ -0,0 +1,20 | |||||
|
1 | #ifndef QSCATTERSERIES_H | |||
|
2 | #define QSCATTERSERIES_H | |||
|
3 | ||||
|
4 | #include <QGraphicsItem> | |||
|
5 | ||||
|
6 | class QScatterSeries : public QGraphicsItem | |||
|
7 | { | |||
|
8 | public: | |||
|
9 | explicit QScatterSeries(QGraphicsItem *parent = 0); | |||
|
10 | ||||
|
11 | signals: | |||
|
12 | ||||
|
13 | public slots: | |||
|
14 | ||||
|
15 | private: | |||
|
16 | QRectF boundingRect() const; | |||
|
17 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |||
|
18 | }; | |||
|
19 | ||||
|
20 | #endif // QSCATTERSERIES_H |
@@ -13,10 +13,16 OBJECTS_DIR = tmp | |||||
13 | MOC_DIR = tmp |
|
13 | MOC_DIR = tmp | |
14 |
|
14 | |||
15 | SOURCES += main.cpp \ |
|
15 | SOURCES += main.cpp \ | |
16 | mainwidget.cpp |
|
16 | mainwidget.cpp \ | |
|
17 | qscatterseries.cpp \ | |||
|
18 | qchartwidget.cpp | |||
17 |
|
19 | |||
18 | HEADERS += \ |
|
20 | HEADERS += \ | |
19 | mainwidget.h |
|
21 | mainwidget.h \ | |
|
22 | qscatterseries.h \ | |||
|
23 | qchartwidget.h | |||
|
24 | ||||
|
25 | ||||
20 |
|
26 | |||
21 |
|
27 | |||
22 |
|
28 |
@@ -1,5 +1,6 | |||||
1 | #include "mainwidget.h" |
|
1 | #include "mainwidget.h" | |
2 |
#include |
|
2 | #include "qchartwidget.h" | |
|
3 | //#include <chartwidget.h> | |||
3 | #include <QPushButton> |
|
4 | #include <QPushButton> | |
4 | #include <QComboBox> |
|
5 | #include <QComboBox> | |
5 | #include <QSpinBox> |
|
6 | #include <QSpinBox> | |
@@ -14,10 +15,9 | |||||
14 | MainWidget::MainWidget(QWidget *parent) : |
|
15 | MainWidget::MainWidget(QWidget *parent) : | |
15 | QWidget(parent) |
|
16 | QWidget(parent) | |
16 | { |
|
17 | { | |
17 | m_chartWidget = new ChartWidget(this); |
|
18 | m_chartWidget = new QChartWidget(this); | |
18 | m_chartWidget->resize(QSize(200,200)); |
|
19 | // m_chartWidget->resize(QSize(200,200)); | |
19 | m_chartWidget->setColor(Qt::red); |
|
20 | // m_chartWidget->setColor(Qt::red); | |
20 |
|
||||
21 | // Chart type |
|
21 | // Chart type | |
22 | // TODO: How about multiple types? |
|
22 | // TODO: How about multiple types? | |
23 | // Should the type be a property of a graph instead of the chart? |
|
23 | // Should the type be a property of a graph instead of the chart? | |
@@ -32,10 +32,8 MainWidget::MainWidget(QWidget *parent) : | |||||
32 | this, SLOT(chartTypeChanged(int))); |
|
32 | this, SLOT(chartTypeChanged(int))); | |
33 |
|
33 | |||
34 | // Test data selector |
|
34 | // Test data selector | |
35 | QComboBox *dataCombo = new QComboBox(this); |
|
35 | QPushButton *fileButton = new QPushButton("From file"); | |
36 | dataCombo->addItem("todo: add test data"); |
|
36 | QPushButton *urlButton = new QPushButton("From URL"); | |
37 | connect(dataCombo, SIGNAL(currentIndexChanged(QString)), |
|
|||
38 | this, SLOT(dataChanged(QString))); |
|
|||
39 |
|
37 | |||
40 | // Chart background |
|
38 | // Chart background | |
41 | QComboBox *backgroundCombo = new QComboBox(this); |
|
39 | QComboBox *backgroundCombo = new QComboBox(this); | |
@@ -45,8 +43,8 MainWidget::MainWidget(QWidget *parent) : | |||||
45 |
|
43 | |||
46 | // Axis |
|
44 | // Axis | |
47 | // TODO: multiple axes? |
|
45 | // TODO: multiple axes? | |
48 |
|
|
46 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); | |
49 | connect(autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
47 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); | |
50 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
48 | // Allow setting also non-sense values (like -2147483648 and 2147483647) | |
51 | m_xMinSpin = new QSpinBox(); |
|
49 | m_xMinSpin = new QSpinBox(); | |
52 | m_xMinSpin->setMinimum(INT_MIN); |
|
50 | m_xMinSpin->setMinimum(INT_MIN); | |
@@ -72,20 +70,21 MainWidget::MainWidget(QWidget *parent) : | |||||
72 | QGridLayout *grid = new QGridLayout(); |
|
70 | QGridLayout *grid = new QGridLayout(); | |
73 | QHBoxLayout *hbox = new QHBoxLayout(); |
|
71 | QHBoxLayout *hbox = new QHBoxLayout(); | |
74 | grid->addWidget(new QLabel("Chart type:"), 0, 0); |
|
72 | grid->addWidget(new QLabel("Chart type:"), 0, 0); | |
75 | grid->addWidget(chartTypeCombo, 0, 1); |
|
73 | grid->addWidget(chartTypeCombo, 0, 1, 1, 2); | |
76 | grid->addWidget(new QLabel("Data:"), 1, 0); |
|
74 | grid->addWidget(new QLabel("Data:"), 1, 0); | |
77 |
grid->addWidget( |
|
75 | grid->addWidget(fileButton, 1, 1); | |
|
76 | grid->addWidget(urlButton, 1, 2); | |||
78 | grid->addWidget(new QLabel("Background:"), 2, 0); |
|
77 | grid->addWidget(new QLabel("Background:"), 2, 0); | |
79 | grid->addWidget(backgroundCombo, 2, 1); |
|
78 | grid->addWidget(backgroundCombo, 2, 1, 1, 2); | |
80 | grid->addWidget(autoScaleCheck, 3, 0); |
|
79 | grid->addWidget(m_autoScaleCheck, 3, 0); | |
81 | grid->addWidget(new QLabel("x min:"), 4, 0); |
|
80 | grid->addWidget(new QLabel("x min:"), 4, 0); | |
82 | grid->addWidget(m_xMinSpin, 4, 1); |
|
81 | grid->addWidget(m_xMinSpin, 4, 1, 1, 2); | |
83 | grid->addWidget(new QLabel("x max:"), 5, 0); |
|
82 | grid->addWidget(new QLabel("x max:"), 5, 0); | |
84 | grid->addWidget(m_xMaxSpin, 5, 1); |
|
83 | grid->addWidget(m_xMaxSpin, 5, 1, 1, 2); | |
85 | grid->addWidget(new QLabel("y min:"), 6, 0); |
|
84 | grid->addWidget(new QLabel("y min:"), 6, 0); | |
86 | grid->addWidget(m_yMinSpin, 6, 1); |
|
85 | grid->addWidget(m_yMinSpin, 6, 1, 1, 2); | |
87 | grid->addWidget(new QLabel("y max:"), 7, 0); |
|
86 | grid->addWidget(new QLabel("y max:"), 7, 0); | |
88 | grid->addWidget(m_yMaxSpin, 7, 1); |
|
87 | grid->addWidget(m_yMaxSpin, 7, 1, 1, 2); | |
89 | // add row with empty label to make all the other rows static |
|
88 | // add row with empty label to make all the other rows static | |
90 | grid->addWidget(new QLabel(""), 8, 0); |
|
89 | grid->addWidget(new QLabel(""), 8, 0); | |
91 | grid->setRowStretch(8, 1); |
|
90 | grid->setRowStretch(8, 1); | |
@@ -96,17 +95,21 MainWidget::MainWidget(QWidget *parent) : | |||||
96 |
|
95 | |||
97 | setLayout(hbox); |
|
96 | setLayout(hbox); | |
98 |
|
97 | |||
99 | // Setting auto scale affects min/max value spin boxes, so it needs to be done after they have |
|
98 | m_autoScaleCheck->setChecked(true); | |
100 | // been constructed |
|
|||
101 | autoScaleCheck->setChecked(true); |
|
|||
102 | } |
|
99 | } | |
103 |
|
100 | |||
104 | void MainWidget::chartTypeChanged(int itemIndex) |
|
101 | void MainWidget::chartTypeChanged(int itemIndex) | |
105 | { |
|
102 | { | |
106 | qDebug() << "chartTypeChanged: " << itemIndex; |
|
103 | // TODO: change chart type | |
107 | QMessageBox msg; |
|
104 | switch (itemIndex) { | |
108 | msg.setText("TODO: API for setting chart type"); |
|
105 | case 4: { | |
109 | msg.exec(); |
|
106 | m_chartWidget->setType(4); | |
|
107 | break; | |||
|
108 | } | |||
|
109 | default: | |||
|
110 | m_chartWidget->setType(0); | |||
|
111 | break; | |||
|
112 | } | |||
110 | } |
|
113 | } | |
111 |
|
114 | |||
112 | void MainWidget::dataChanged(QString itemText) |
|
115 | void MainWidget::dataChanged(QString itemText) | |
@@ -121,17 +124,16 void MainWidget::backgroundChanged(int itemIndex) | |||||
121 |
|
124 | |||
122 | void MainWidget::autoScaleChanged(int value) |
|
125 | void MainWidget::autoScaleChanged(int value) | |
123 | { |
|
126 | { | |
124 | if (this->isVisible()) { |
|
127 | if (value) { | |
125 | QMessageBox msg; |
|
128 | // TODO: enable auto scaling | |
126 | msg.setText("TODO: APIs for using either auto scaling or explicit values"); |
|
129 | } else { | |
127 | msg.exec(); |
|
130 | // TODO: set scaling manually (and disable auto scaling) | |
128 | } |
|
131 | } | |
129 |
|
132 | |||
130 | // TODO: get initial spin box axis values from charts widget |
|
133 | m_xMinSpin->setEnabled(!value); | |
131 |
m_xM |
|
134 | m_xMaxSpin->setEnabled(!value); | |
132 |
m_ |
|
135 | m_yMinSpin->setEnabled(!value); | |
133 |
m_yM |
|
136 | m_yMaxSpin->setEnabled(!value); | |
134 | m_yMaxSpin->setEnabled(value == Qt::Unchecked); |
|
|||
135 | } |
|
137 | } | |
136 |
|
138 | |||
137 | void MainWidget::xMinChanged(int value) |
|
139 | void MainWidget::xMinChanged(int value) |
@@ -3,8 +3,9 | |||||
3 |
|
3 | |||
4 | #include <QWidget> |
|
4 | #include <QWidget> | |
5 |
|
5 | |||
6 | class ChartWidget; |
|
6 | class QChartWidget; | |
7 | class QSpinBox; |
|
7 | class QSpinBox; | |
|
8 | class QCheckBox; | |||
8 |
|
9 | |||
9 | class MainWidget : public QWidget |
|
10 | class MainWidget : public QWidget | |
10 | { |
|
11 | { | |
@@ -25,7 +26,8 private slots: | |||||
25 | void yMaxChanged(int value); |
|
26 | void yMaxChanged(int value); | |
26 |
|
27 | |||
27 | private: |
|
28 | private: | |
28 | ChartWidget *m_chartWidget; |
|
29 | QChartWidget *m_chartWidget; | |
|
30 | QCheckBox *m_autoScaleCheck; | |||
29 | QSpinBox *m_xMinSpin; |
|
31 | QSpinBox *m_xMinSpin; | |
30 | QSpinBox *m_xMaxSpin; |
|
32 | QSpinBox *m_xMaxSpin; | |
31 | QSpinBox *m_yMinSpin; |
|
33 | QSpinBox *m_yMinSpin; |
General Comments 0
You need to be logged in to leave comments.
Login now