##// 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 49 // Setup painter
50 50 painter->setBrush(m_color);
51 painter->setRenderHint(QPainter::Antialiasing);
51 52 QPen pen;
52 53 //pen.setColor(m_color.darker());
53 54 pen.setColor(Qt::gray);
@@ -213,6 +213,9 void QChart::setTheme(QChart::ChartThemeId theme)
213 213 m_themeColors.append(QColor(160, 160, 113));
214 214 m_themeColors.append(QColor(210, 210, 52));
215 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 219 break;
217 220 case QChart::ChartThemeIcy:
218 221 m_themeColors.append(QColor(0, 3, 165));
@@ -221,6 +224,9 void QChart::setTheme(QChart::ChartThemeId theme)
221 224 m_themeColors.append(QColor(48, 97, 87));
222 225 m_themeColors.append(QColor(19, 71, 90));
223 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 230 break;
225 231 case QChart::ChartThemeGrayscale:
226 232 m_themeColors.append(QColor(0, 0, 0));
@@ -228,12 +234,28 void QChart::setTheme(QChart::ChartThemeId theme)
228 234 m_themeColors.append(QColor(100, 100, 100));
229 235 m_themeColors.append(QColor(140, 140, 140));
230 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 250 break;
232 251 default:
233 252 Q_ASSERT(false);
234 253 break;
235 254 }
236 255
256 m_background->setBrush(m_backgroundGradient);
257 m_background->setPen(Qt::NoPen);
258
237 259 foreach(QChartSeries* series, m_chartSeries) {
238 260 // TODO: other series interested on themes?
239 261 if (series->type() == QChartSeries::SeriesTypeLine) {
@@ -30,7 +30,8 public:
30 30 enum ChartThemeId {
31 31 ChartThemeVanilla = 0,
32 32 ChartThemeIcy,
33 ChartThemeGrayscale
33 ChartThemeGrayscale,
34 ChartThemeUnnamed1
34 35 };
35 36
36 37 public:
@@ -7,7 +7,8
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 QChartWidget::QChartWidget(QWidget *parent) :
10 QGraphicsView(parent)
10 QGraphicsView(parent),
11 m_rubberBand(QRubberBand::Rectangle, this)
11 12 {
12 13 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13 14 m_scene = new QGraphicsScene();
@@ -17,6 +18,7 QGraphicsView(parent)
17 18
18 19 m_chart = new QChart();
19 20 m_scene->addItem(m_chart);
21 m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default?
20 22 show();
21 23 }
22 24
@@ -37,6 +39,41 QSize QChartWidget::sizeHint() const
37 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 77 void QChartWidget::addSeries(QChartSeries* series)
41 78 {
42 79 m_chart->addSeries(series);
@@ -52,6 +89,11 void QChartWidget::setTheme(QChart::ChartThemeId theme)
52 89 m_chart->setTheme(theme);
53 90 }
54 91
92 void QChartWidget::setZoomEnabled(bool enabled)
93 {
94 m_rubberBand.setEnabled(enabled);
95 }
96
55 97 #include "moc_qchartwidget.cpp"
56 98
57 99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -4,8 +4,10
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h"
6 6 #include <QGraphicsView>
7 #include <QRubberBand>
7 8
8 9 class QGraphicsScene;
10 class QRubberBand;
9 11
10 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 13
@@ -19,12 +21,9 public:
19 21 explicit QChartWidget(QWidget *parent = 0);
20 22 ~QChartWidget();
21 23
22 //implement from QWidget
23 void resizeEvent(QResizeEvent *event);
24 QSize sizeHint() const;
25
26 24 // TODO: addSeries and createSeries are optional solutions
27 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 27 void addSeries(QChartSeries* series);
29 28 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
30 29
@@ -34,11 +33,22 public:
34 33 */
35 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 45 private:
38 46 Q_DISABLE_COPY(QChartWidget)
47 // TODO: move the following to pimpl
39 48 QGraphicsScene *m_scene;
40 49 QChart* m_chart;
41
50 QRubberBand m_rubberBand;
51 QPoint m_origin;
42 52 };
43 53
44 54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,7 +7,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
9 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 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 117 foreach (PieSlice *slice, m_slices)
88 118 slice->m_rect = tempRect;
89 119 }
@@ -101,6 +131,18 void QPieSeries::setSizeFactor(qreal factor)
101 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 146 #include "moc_qpieseries.cpp"
105 147
106 148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -15,11 +15,18 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
15 15 Q_OBJECT
16 16
17 17 public:
18 enum PiePosition {
19 PiePositionMaximized = 0,
20 PiePositionTopLeft,
21 PiePositionTopRight,
22 PiePositionBottomLeft,
23 PiePositionBottomRight
24 };
25
26 public:
18 27 // TODO: use a generic data class instead of x and y
19 28 QPieSeries(QGraphicsObject *parent = 0);
20 29 ~QPieSeries();
21 void setSizeFactor(qreal sizeFactor);
22 qreal sizeFactor() { return m_sizeFactor; }
23 30
24 31 public: // from QChartSeries
25 32 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
@@ -29,6 +36,9 public:
29 36 void setSliceColor(int index, QColor color);
30 37 QColor sliceColor(int index);
31 38 int sliceCount();
39 void setSizeFactor(qreal sizeFactor);
40 qreal sizeFactor() { return m_sizeFactor; }
41 void setPosition(PiePosition position);
32 42
33 43 public Q_SLOTS:
34 44 void chartSizeChanged(QRectF rect);
@@ -43,6 +53,7 private:
43 53 QList<PieSlice*> m_slices;
44 54 QRectF m_chartSize;
45 55 qreal m_sizeFactor;
56 PiePosition m_position;
46 57 };
47 58
48 59 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,14 +21,16 QTCOMMERCIALCHART_USE_NAMESPACE
21 21 MainWidget::MainWidget(QWidget *parent) :
22 22 QWidget(parent)
23 23 {
24 m_chartWidget = new QChartWidget(this);
25
24 26 QPushButton *addSeriesButton = new QPushButton("Add series");
25 27 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
26 28
27 29 // Chart background
28 30 QComboBox *backgroundCombo = new QComboBox(this);
29 backgroundCombo->addItem("None");
30 backgroundCombo->addItem("TODO Grid");
31 backgroundCombo->addItem("TODO Image");
31 backgroundCombo->addItem("Color");
32 backgroundCombo->addItem("Gradient");
33 backgroundCombo->addItem("Image");
32 34 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
33 35 this, SLOT(backgroundChanged(int)));
34 36
@@ -62,13 +64,16 MainWidget::MainWidget(QWidget *parent) :
62 64 chartTheme->addItem("Vanilla");
63 65 chartTheme->addItem("Icy");
64 66 chartTheme->addItem("Grayscale");
65 chartTheme->addItem("Tobedefined");
67 chartTheme->addItem("Unnamed1");
66 68 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
67 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 75 QGridLayout *grid = new QGridLayout();
70 76 QGridLayout *mainLayout = new QGridLayout();
71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
72 77 grid->addWidget(addSeriesButton, 0, 1);
73 78 grid->addWidget(new QLabel("Background:"), 2, 0);
74 79 grid->addWidget(backgroundCombo, 2, 1);
@@ -83,9 +88,10 MainWidget::MainWidget(QWidget *parent) :
83 88 grid->addWidget(m_yMaxSpin, 7, 1);
84 89 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 90 grid->addWidget(chartTheme, 8, 1);
91 grid->addWidget(zoomCheckBox, 9, 0);
86 92 // add row with empty label to make all the other rows static
87 grid->addWidget(new QLabel(""), 9, 0);
88 grid->setRowStretch(9, 1);
93 grid->addWidget(new QLabel(""), 10, 0);
94 grid->setRowStretch(10, 1);
89 95
90 96 mainLayout->addLayout(grid, 0, 0);
91 97
@@ -95,21 +101,32 MainWidget::MainWidget(QWidget *parent) :
95 101 m_scatterLayout->setEnabled(false);
96 102
97 103 // Pie specific settings
98 m_pieLayout = new QGridLayout();
99 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
104 // Pie size factory
100 105 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
101 106 pieSizeSpin->setMinimum(LONG_MIN);
102 107 pieSizeSpin->setMaximum(LONG_MAX);
103 108 pieSizeSpin->setValue(1.0);
104 109 pieSizeSpin->setSingleStep(0.1);
105 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 121 m_pieLayout->setEnabled(false);
122 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
107 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 127 mainLayout->addLayout(m_scatterLayout, 1, 0);
110 128 mainLayout->addLayout(m_pieLayout, 2, 0);
111 129
112 m_chartWidget = new QChartWidget(this);
113 130 //m_chartWidget->setColor(Qt::red);
114 131 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
115 132 // hbox->setStretch(1, 1);
@@ -312,3 +329,10 void MainWidget::setPieSizeFactor(double size)
312 329 if (pie)
313 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 32 void setCurrentSeries(QChartSeries *series);
33 33 void changeChartTheme(int themeIndex);
34 34 void setPieSizeFactor(double margin);
35 void setPiePosition(int position);
35 36
36 37 private:
37 38 QChartWidget *m_chartWidget;
General Comments 0
You need to be logged in to leave comments. Login now