##// END OF EJS Templates
Theme now affects background, enabled zoom by default in QChartWidget
Tero Ahola -
r77:a102be07cf92
parent child
Show More
@@ -48,6 +48,7 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
48 {
48 {
49 // Setup painter
49 // Setup painter
50 painter->setBrush(m_color);
50 painter->setBrush(m_color);
51 painter->setRenderHint(QPainter::Antialiasing);
51 QPen pen;
52 QPen pen;
52 //pen.setColor(m_color.darker());
53 //pen.setColor(m_color.darker());
53 pen.setColor(Qt::gray);
54 pen.setColor(Qt::gray);
@@ -213,6 +213,9 void QChart::setTheme(QChart::ChartThemeId theme)
213 m_themeColors.append(QColor(160, 160, 113));
213 m_themeColors.append(QColor(160, 160, 113));
214 m_themeColors.append(QColor(210, 210, 52));
214 m_themeColors.append(QColor(210, 210, 52));
215 m_themeColors.append(QColor(136, 114, 58));
215 m_themeColors.append(QColor(136, 114, 58));
216
217 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
218 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
216 break;
219 break;
217 case QChart::ChartThemeIcy:
220 case QChart::ChartThemeIcy:
218 m_themeColors.append(QColor(0, 3, 165));
221 m_themeColors.append(QColor(0, 3, 165));
@@ -221,6 +224,9 void QChart::setTheme(QChart::ChartThemeId theme)
221 m_themeColors.append(QColor(48, 97, 87));
224 m_themeColors.append(QColor(48, 97, 87));
222 m_themeColors.append(QColor(19, 71, 90));
225 m_themeColors.append(QColor(19, 71, 90));
223 m_themeColors.append(QColor(110, 70, 228));
226 m_themeColors.append(QColor(110, 70, 228));
227
228 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
229 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
224 break;
230 break;
225 case QChart::ChartThemeGrayscale:
231 case QChart::ChartThemeGrayscale:
226 m_themeColors.append(QColor(0, 0, 0));
232 m_themeColors.append(QColor(0, 0, 0));
@@ -228,12 +234,28 void QChart::setTheme(QChart::ChartThemeId theme)
228 m_themeColors.append(QColor(100, 100, 100));
234 m_themeColors.append(QColor(100, 100, 100));
229 m_themeColors.append(QColor(140, 140, 140));
235 m_themeColors.append(QColor(140, 140, 140));
230 m_themeColors.append(QColor(180, 180, 180));
236 m_themeColors.append(QColor(180, 180, 180));
237
238 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
239 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
240 break;
241 case QChart::ChartThemeUnnamed1:
242 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
243 m_themeColors.append(QColor(QRgb(0xff7AC943)));
244 m_themeColors.append(QColor(QRgb(0xffFF931E)));
245 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
246 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
247
248 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
249 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
231 break;
250 break;
232 default:
251 default:
233 Q_ASSERT(false);
252 Q_ASSERT(false);
234 break;
253 break;
235 }
254 }
236
255
256 m_background->setBrush(m_backgroundGradient);
257 m_background->setPen(Qt::NoPen);
258
237 foreach(QChartSeries* series, m_chartSeries) {
259 foreach(QChartSeries* series, m_chartSeries) {
238 // TODO: other series interested on themes?
260 // TODO: other series interested on themes?
239 if (series->type() == QChartSeries::SeriesTypeLine) {
261 if (series->type() == QChartSeries::SeriesTypeLine) {
@@ -30,7 +30,8 public:
30 enum ChartThemeId {
30 enum ChartThemeId {
31 ChartThemeVanilla = 0,
31 ChartThemeVanilla = 0,
32 ChartThemeIcy,
32 ChartThemeIcy,
33 ChartThemeGrayscale
33 ChartThemeGrayscale,
34 ChartThemeUnnamed1
34 };
35 };
35
36
36 public:
37 public:
@@ -7,7 +7,8
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 QChartWidget::QChartWidget(QWidget *parent) :
9 QChartWidget::QChartWidget(QWidget *parent) :
10 QGraphicsView(parent)
10 QGraphicsView(parent),
11 m_rubberBand(QRubberBand::Rectangle, this)
11 {
12 {
12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13 m_scene = new QGraphicsScene();
14 m_scene = new QGraphicsScene();
@@ -17,6 +18,7 QGraphicsView(parent)
17
18
18 m_chart = new QChart();
19 m_chart = new QChart();
19 m_scene->addItem(m_chart);
20 m_scene->addItem(m_chart);
21 m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default?
20 show();
22 show();
21 }
23 }
22
24
@@ -37,6 +39,41 QSize QChartWidget::sizeHint() const
37 return QSize(100, 100);
39 return QSize(100, 100);
38 }
40 }
39
41
42 void QChartWidget::mousePressEvent(QMouseEvent *event)
43 {
44 if(m_rubberBand.isEnabled() && event->button() == Qt::LeftButton) {
45 int margin = m_chart->margin();
46 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
47 m_origin = event->pos();
48
49 if (rect.contains(m_origin)) {
50 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
51 m_rubberBand.show();
52 event->accept();
53 }
54 }
55 }
56
57 void QChartWidget::mouseMoveEvent(QMouseEvent *event)
58 {
59 if(m_rubberBand.isVisible())
60 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
61 }
62
63 void QChartWidget::mouseReleaseEvent(QMouseEvent *event)
64 {
65 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
66 m_rubberBand.hide();
67 QRect rect = m_rubberBand.geometry();
68 m_chart->zoomInToRect(rect);
69 event->accept();
70 }
71
72 if(event->button()==Qt::RightButton) {
73 m_chart->zoomOut();
74 }
75 }
76
40 void QChartWidget::addSeries(QChartSeries* series)
77 void QChartWidget::addSeries(QChartSeries* series)
41 {
78 {
42 m_chart->addSeries(series);
79 m_chart->addSeries(series);
@@ -52,6 +89,11 void QChartWidget::setTheme(QChart::ChartThemeId theme)
52 m_chart->setTheme(theme);
89 m_chart->setTheme(theme);
53 }
90 }
54
91
92 void QChartWidget::setZoomEnabled(bool enabled)
93 {
94 m_rubberBand.setEnabled(enabled);
95 }
96
55 #include "moc_qchartwidget.cpp"
97 #include "moc_qchartwidget.cpp"
56
98
57 QTCOMMERCIALCHART_END_NAMESPACE
99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -4,8 +4,10
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include <QGraphicsView>
6 #include <QGraphicsView>
7 #include <QRubberBand>
7
8
8 class QGraphicsScene;
9 class QGraphicsScene;
10 class QRubberBand;
9
11
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
13
@@ -19,12 +21,9 public:
19 explicit QChartWidget(QWidget *parent = 0);
21 explicit QChartWidget(QWidget *parent = 0);
20 ~QChartWidget();
22 ~QChartWidget();
21
23
22 //implement from QWidget
23 void resizeEvent(QResizeEvent *event);
24 QSize sizeHint() const;
25
26 // TODO: addSeries and createSeries are optional solutions
24 // TODO: addSeries and createSeries are optional solutions
27 // TODO: currently createSeries assumes x, y value pairs. This isn't case with all charts. So is there another createSeries for other types (for example one list of ints)?
25 // TODO: currently createSeries assumes x, y value pairs. This isn't case with all charts. So is there another createSeries for other types (for example one list of ints)?
26 public Q_SLOTS:
28 void addSeries(QChartSeries* series);
27 void addSeries(QChartSeries* series);
29 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
28 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
30
29
@@ -34,11 +33,22 public:
34 */
33 */
35 void setTheme(QChart::ChartThemeId theme);
34 void setTheme(QChart::ChartThemeId theme);
36
35
36 void setZoomEnabled(bool enabled);
37
38 private: // From QWidget TODO: should these be protected instead? Is QChartWidget meant to be extened by the user?
39 void resizeEvent(QResizeEvent *event);
40 QSize sizeHint() const;
41 void mousePressEvent(QMouseEvent *event);
42 void mouseMoveEvent(QMouseEvent *event);
43 void mouseReleaseEvent(QMouseEvent *event);
44
37 private:
45 private:
38 Q_DISABLE_COPY(QChartWidget)
46 Q_DISABLE_COPY(QChartWidget)
47 // TODO: move the following to pimpl
39 QGraphicsScene *m_scene;
48 QGraphicsScene *m_scene;
40 QChart* m_chart;
49 QChart* m_chart;
41
50 QRubberBand m_rubberBand;
51 QPoint m_origin;
42 };
52 };
43
53
44 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,7 +7,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
9 QChartSeries(parent),
9 QChartSeries(parent),
10 m_sizeFactor(1.0)
10 m_sizeFactor(1.0),
11 m_position(PiePositionMaximized)
11 {
12 {
12 }
13 }
13
14
@@ -84,6 +85,35 void QPieSeries::resizeSlices(QRectF rect)
84 tempRect.moveCenter(rect.center());
85 tempRect.moveCenter(rect.center());
85 }
86 }
86
87
88 switch (m_position) {
89 case PiePositionTopLeft: {
90 tempRect.setHeight(tempRect.height() / 2);
91 tempRect.setWidth(tempRect.height());
92 tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2));
93 break;
94 }
95 case PiePositionTopRight: {
96 tempRect.setHeight(tempRect.height() / 2);
97 tempRect.setWidth(tempRect.height());
98 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2));
99 break;
100 }
101 case PiePositionBottomLeft: {
102 tempRect.setHeight(tempRect.height() / 2);
103 tempRect.setWidth(tempRect.height());
104 tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3));
105 break;
106 }
107 case PiePositionBottomRight: {
108 tempRect.setHeight(tempRect.height() / 2);
109 tempRect.setWidth(tempRect.height());
110 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3));
111 break;
112 }
113 default:
114 break;
115 }
116
87 foreach (PieSlice *slice, m_slices)
117 foreach (PieSlice *slice, m_slices)
88 slice->m_rect = tempRect;
118 slice->m_rect = tempRect;
89 }
119 }
@@ -101,6 +131,18 void QPieSeries::setSizeFactor(qreal factor)
101 parentItem->update();
131 parentItem->update();
102 }
132 }
103
133
134 void QPieSeries::setPosition(PiePosition position)
135 {
136 m_position = position;
137 resizeSlices(m_chartSize);
138
139 // Initiate update via the parent graphics item
140 // TODO: potential issue: what if this function is called from the parent context?
141 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
142 Q_ASSERT(parentItem);
143 parentItem->update();
144 }
145
104 #include "moc_qpieseries.cpp"
146 #include "moc_qpieseries.cpp"
105
147
106 QTCOMMERCIALCHART_END_NAMESPACE
148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -15,11 +15,18 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public:
17 public:
18 enum PiePosition {
19 PiePositionMaximized = 0,
20 PiePositionTopLeft,
21 PiePositionTopRight,
22 PiePositionBottomLeft,
23 PiePositionBottomRight
24 };
25
26 public:
18 // TODO: use a generic data class instead of x and y
27 // TODO: use a generic data class instead of x and y
19 QPieSeries(QGraphicsObject *parent = 0);
28 QPieSeries(QGraphicsObject *parent = 0);
20 ~QPieSeries();
29 ~QPieSeries();
21 void setSizeFactor(qreal sizeFactor);
22 qreal sizeFactor() { return m_sizeFactor; }
23
30
24 public: // from QChartSeries
31 public: // from QChartSeries
25 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
32 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
@@ -29,6 +36,9 public:
29 void setSliceColor(int index, QColor color);
36 void setSliceColor(int index, QColor color);
30 QColor sliceColor(int index);
37 QColor sliceColor(int index);
31 int sliceCount();
38 int sliceCount();
39 void setSizeFactor(qreal sizeFactor);
40 qreal sizeFactor() { return m_sizeFactor; }
41 void setPosition(PiePosition position);
32
42
33 public Q_SLOTS:
43 public Q_SLOTS:
34 void chartSizeChanged(QRectF rect);
44 void chartSizeChanged(QRectF rect);
@@ -43,6 +53,7 private:
43 QList<PieSlice*> m_slices;
53 QList<PieSlice*> m_slices;
44 QRectF m_chartSize;
54 QRectF m_chartSize;
45 qreal m_sizeFactor;
55 qreal m_sizeFactor;
56 PiePosition m_position;
46 };
57 };
47
58
48 QTCOMMERCIALCHART_END_NAMESPACE
59 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,14 +21,16 QTCOMMERCIALCHART_USE_NAMESPACE
21 MainWidget::MainWidget(QWidget *parent) :
21 MainWidget::MainWidget(QWidget *parent) :
22 QWidget(parent)
22 QWidget(parent)
23 {
23 {
24 m_chartWidget = new QChartWidget(this);
25
24 QPushButton *addSeriesButton = new QPushButton("Add series");
26 QPushButton *addSeriesButton = new QPushButton("Add series");
25 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
27 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
26
28
27 // Chart background
29 // Chart background
28 QComboBox *backgroundCombo = new QComboBox(this);
30 QComboBox *backgroundCombo = new QComboBox(this);
29 backgroundCombo->addItem("None");
31 backgroundCombo->addItem("Color");
30 backgroundCombo->addItem("TODO Grid");
32 backgroundCombo->addItem("Gradient");
31 backgroundCombo->addItem("TODO Image");
33 backgroundCombo->addItem("Image");
32 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
34 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
33 this, SLOT(backgroundChanged(int)));
35 this, SLOT(backgroundChanged(int)));
34
36
@@ -62,13 +64,16 MainWidget::MainWidget(QWidget *parent) :
62 chartTheme->addItem("Vanilla");
64 chartTheme->addItem("Vanilla");
63 chartTheme->addItem("Icy");
65 chartTheme->addItem("Icy");
64 chartTheme->addItem("Grayscale");
66 chartTheme->addItem("Grayscale");
65 chartTheme->addItem("Tobedefined");
67 chartTheme->addItem("Unnamed1");
66 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
68 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
67 this, SLOT(changeChartTheme(int)));
69 this, SLOT(changeChartTheme(int)));
68
70
71 QCheckBox *zoomCheckBox = new QCheckBox("Zoom enabled");
72 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
73 zoomCheckBox->setChecked(true);
74
69 QGridLayout *grid = new QGridLayout();
75 QGridLayout *grid = new QGridLayout();
70 QGridLayout *mainLayout = new QGridLayout();
76 QGridLayout *mainLayout = new QGridLayout();
71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
72 grid->addWidget(addSeriesButton, 0, 1);
77 grid->addWidget(addSeriesButton, 0, 1);
73 grid->addWidget(new QLabel("Background:"), 2, 0);
78 grid->addWidget(new QLabel("Background:"), 2, 0);
74 grid->addWidget(backgroundCombo, 2, 1);
79 grid->addWidget(backgroundCombo, 2, 1);
@@ -83,9 +88,10 MainWidget::MainWidget(QWidget *parent) :
83 grid->addWidget(m_yMaxSpin, 7, 1);
88 grid->addWidget(m_yMaxSpin, 7, 1);
84 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
89 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 grid->addWidget(chartTheme, 8, 1);
90 grid->addWidget(chartTheme, 8, 1);
91 grid->addWidget(zoomCheckBox, 9, 0);
86 // add row with empty label to make all the other rows static
92 // add row with empty label to make all the other rows static
87 grid->addWidget(new QLabel(""), 9, 0);
93 grid->addWidget(new QLabel(""), 10, 0);
88 grid->setRowStretch(9, 1);
94 grid->setRowStretch(10, 1);
89
95
90 mainLayout->addLayout(grid, 0, 0);
96 mainLayout->addLayout(grid, 0, 0);
91
97
@@ -95,21 +101,32 MainWidget::MainWidget(QWidget *parent) :
95 m_scatterLayout->setEnabled(false);
101 m_scatterLayout->setEnabled(false);
96
102
97 // Pie specific settings
103 // Pie specific settings
98 m_pieLayout = new QGridLayout();
104 // Pie size factory
99 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
100 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
105 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
101 pieSizeSpin->setMinimum(LONG_MIN);
106 pieSizeSpin->setMinimum(LONG_MIN);
102 pieSizeSpin->setMaximum(LONG_MAX);
107 pieSizeSpin->setMaximum(LONG_MAX);
103 pieSizeSpin->setValue(1.0);
108 pieSizeSpin->setValue(1.0);
104 pieSizeSpin->setSingleStep(0.1);
109 pieSizeSpin->setSingleStep(0.1);
105 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
110 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
111 // Pie position
112 QComboBox *piePosCombo = new QComboBox(this);
113 piePosCombo->addItem("Maximized");
114 piePosCombo->addItem("Top left");
115 piePosCombo->addItem("Top right");
116 piePosCombo->addItem("Bottom left");
117 piePosCombo->addItem("Bottom right");
118 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
119 this, SLOT(setPiePosition(int)));
120 m_pieLayout = new QGridLayout();
106 m_pieLayout->setEnabled(false);
121 m_pieLayout->setEnabled(false);
122 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
107 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
123 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
124 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
125 m_pieLayout->addWidget(piePosCombo, 1, 1);
108
126
109 mainLayout->addLayout(m_scatterLayout, 1, 0);
127 mainLayout->addLayout(m_scatterLayout, 1, 0);
110 mainLayout->addLayout(m_pieLayout, 2, 0);
128 mainLayout->addLayout(m_pieLayout, 2, 0);
111
129
112 m_chartWidget = new QChartWidget(this);
113 //m_chartWidget->setColor(Qt::red);
130 //m_chartWidget->setColor(Qt::red);
114 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
131 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
115 // hbox->setStretch(1, 1);
132 // hbox->setStretch(1, 1);
@@ -312,3 +329,10 void MainWidget::setPieSizeFactor(double size)
312 if (pie)
329 if (pie)
313 pie->setSizeFactor(qreal(size));
330 pie->setSizeFactor(qreal(size));
314 }
331 }
332
333 void MainWidget::setPiePosition(int position)
334 {
335 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
336 if (pie)
337 pie->setPosition((QPieSeries::PiePosition) position);
338 }
@@ -32,6 +32,7 private slots:
32 void setCurrentSeries(QChartSeries *series);
32 void setCurrentSeries(QChartSeries *series);
33 void changeChartTheme(int themeIndex);
33 void changeChartTheme(int themeIndex);
34 void setPieSizeFactor(double margin);
34 void setPieSizeFactor(double margin);
35 void setPiePosition(int position);
35
36
36 private:
37 private:
37 QChartWidget *m_chartWidget;
38 QChartWidget *m_chartWidget;
General Comments 0
You need to be logged in to leave comments. Login now