##// END OF EJS Templates
Removed buttons from legendmarkers example
sauimone -
r2313:c38123ef51c4
parent child
Show More
@@ -1,195 +1,177
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "mainwidget.h"
21 #include "mainwidget.h"
22 #include <QChart>
22 #include <QChart>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QLabel>
25 #include <QLabel>
26 #include <QDebug>
26 #include <QDebug>
27 #include <QLegend>
27 #include <QLegend>
28 #include <QFormLayout>
28 #include <QFormLayout>
29 #include <QLegendMarker>
29 #include <QLegendMarker>
30 #include <QLineSeries>
30 #include <QLineSeries>
31 #include <QXYLegendMarker>
31 #include <QXYLegendMarker>
32 #include <qmath.h>
32 #include <qmath.h>
33
33
34 QTCOMMERCIALCHART_USE_NAMESPACE
34 QTCOMMERCIALCHART_USE_NAMESPACE
35
35
36 MainWidget::MainWidget(QWidget *parent) :
36 MainWidget::MainWidget(QWidget *parent) :
37 QWidget(parent)
37 QWidget(parent)
38 {
38 {
39 // Create buttons for ui
40 m_buttonLayout = new QGridLayout();
41
42 QPushButton *addSliceButton = new QPushButton("add series");
43 connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSeries()));
44 m_buttonLayout->addWidget(addSliceButton, 1, 0);
45
46 QPushButton *removeSliceButton = new QPushButton("remove series");
47 connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
48 m_buttonLayout->addWidget(removeSliceButton, 2, 0);
49
50 QPushButton *connectButton = new QPushButton("Connect markers");
51 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectMarkers()));
52 m_buttonLayout->addWidget(connectButton, 3, 0);
53
54 QPushButton *disConnectButton = new QPushButton("Disconnect markers");
55 connect(disConnectButton, SIGNAL(clicked()), this, SLOT(disconnectMarkers()));
56 m_buttonLayout->addWidget(disConnectButton, 4, 0);
57
58 // Create chart view with the chart
39 // Create chart view with the chart
59 m_chart = new QChart();
40 m_chart = new QChart();
60 m_chartView = new QChartView(m_chart, this);
41 m_chartView = new QChartView(m_chart, this);
61
42
62 // Create layout for grid and detached legend
43 // Create layout for grid and detached legend
63 m_mainLayout = new QGridLayout();
44 m_mainLayout = new QGridLayout();
64 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
65 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
45 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
66 setLayout(m_mainLayout);
46 setLayout(m_mainLayout);
67
47
68 // Add few series
48 // Add few series
69 addSeries();
49 addSeries();
70 addSeries();
50 addSeries();
71 addSeries();
51 addSeries();
72 addSeries();
52 addSeries();
73
53
54 connectMarkers();
55
74 // Set the title and show legend
56 // Set the title and show legend
75 m_chart->setTitle("Legendmarker example");
57 m_chart->setTitle("Legendmarker example (click on legend)");
76 m_chart->legend()->setVisible(true);
58 m_chart->legend()->setVisible(true);
77 m_chart->legend()->setAlignment(Qt::AlignBottom);
59 m_chart->legend()->setAlignment(Qt::AlignBottom);
78
60
79 m_chartView->setRenderHint(QPainter::Antialiasing);
61 m_chartView->setRenderHint(QPainter::Antialiasing);
80 }
62 }
81
63
82 void MainWidget::addSeries()
64 void MainWidget::addSeries()
83 {
65 {
84 QLineSeries *series = new QLineSeries();
66 QLineSeries *series = new QLineSeries();
85 m_series.append(series);
67 m_series.append(series);
86
68
87 series->setName(QString("line " + QString::number(m_series.count())));
69 series->setName(QString("line " + QString::number(m_series.count())));
88
70
89 // Make some sine wave for data
71 // Make some sine wave for data
90 QList<QPointF> data;
72 QList<QPointF> data;
91 int offset = m_chart->series().count();
73 int offset = m_chart->series().count();
92 for (int i = 0; i < 360; i++) {
74 for (int i = 0; i < 360; i++) {
93 qreal x = offset * 20 + i;
75 qreal x = offset * 20 + i;
94 data.append(QPointF(i, qSin(2.0 * 3.141592 * x / 360.0)));
76 data.append(QPointF(i, qSin(2.0 * 3.141592 * x / 360.0)));
95 }
77 }
96
78
97 series->append(data);
79 series->append(data);
98 m_chart->addSeries(series);
80 m_chart->addSeries(series);
99
81
100 if (m_series.count() == 1) {
82 if (m_series.count() == 1) {
101 m_chart->createDefaultAxes();
83 m_chart->createDefaultAxes();
102 }
84 }
103 }
85 }
104
86
105 void MainWidget::removeSeries()
87 void MainWidget::removeSeries()
106 {
88 {
107 // Remove last series from chart
89 // Remove last series from chart
108 if (m_series.count() > 0) {
90 if (m_series.count() > 0) {
109 QLineSeries *series = m_series.last();
91 QLineSeries *series = m_series.last();
110 m_chart->removeSeries(series);
92 m_chart->removeSeries(series);
111 m_series.removeLast();
93 m_series.removeLast();
112 delete series;
94 delete series;
113 }
95 }
114 }
96 }
115
97
116 void MainWidget::connectMarkers()
98 void MainWidget::connectMarkers()
117 {
99 {
118 //![1]
100 //![1]
119 // Connect all markers to handler
101 // Connect all markers to handler
120 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
102 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
121 // Disconnect possible existing connection to avoid multiple connections
103 // Disconnect possible existing connection to avoid multiple connections
122 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
104 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
123 QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
105 QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
124 }
106 }
125 //![1]
107 //![1]
126 }
108 }
127
109
128 void MainWidget::disconnectMarkers()
110 void MainWidget::disconnectMarkers()
129 {
111 {
130 //![2]
112 //![2]
131 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
113 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
132 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
114 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
133 }
115 }
134 //![2]
116 //![2]
135 }
117 }
136
118
137 void MainWidget::handleMarkerClicked()
119 void MainWidget::handleMarkerClicked()
138 {
120 {
139 //![3]
121 //![3]
140 QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
122 QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
141 Q_ASSERT(marker);
123 Q_ASSERT(marker);
142 //![3]
124 //![3]
143
125
144 //![4]
126 //![4]
145 switch (marker->type())
127 switch (marker->type())
146 //![4]
128 //![4]
147 {
129 {
148 case QLegendMarker::LegendMarkerTypeXY:
130 case QLegendMarker::LegendMarkerTypeXY:
149 {
131 {
150 //![5]
132 //![5]
151 // Toggle visibility of series
133 // Toggle visibility of series
152 marker->series()->setVisible(!marker->series()->isVisible());
134 marker->series()->setVisible(!marker->series()->isVisible());
153
135
154 // Turn legend marker back to visible, since hiding series also hides the marker
136 // Turn legend marker back to visible, since hiding series also hides the marker
155 // and we don't want it to happen now.
137 // and we don't want it to happen now.
156 marker->setVisible(true);
138 marker->setVisible(true);
157 //![5]
139 //![5]
158
140
159 //![6]
141 //![6]
160 // Dim the marker, if series is not visible
142 // Dim the marker, if series is not visible
161 qreal alpha = 1.0;
143 qreal alpha = 1.0;
162
144
163 if (!marker->series()->isVisible()) {
145 if (!marker->series()->isVisible()) {
164 alpha = 0.5;
146 alpha = 0.5;
165 }
147 }
166
148
167 QColor color;
149 QColor color;
168 QBrush brush = marker->labelBrush();
150 QBrush brush = marker->labelBrush();
169 color = brush.color();
151 color = brush.color();
170 color.setAlphaF(alpha);
152 color.setAlphaF(alpha);
171 brush.setColor(color);
153 brush.setColor(color);
172 marker->setLabelBrush(brush);
154 marker->setLabelBrush(brush);
173
155
174 brush = marker->brush();
156 brush = marker->brush();
175 color = brush.color();
157 color = brush.color();
176 color.setAlphaF(alpha);
158 color.setAlphaF(alpha);
177 brush.setColor(color);
159 brush.setColor(color);
178 marker->setBrush(brush);
160 marker->setBrush(brush);
179
161
180 QPen pen = marker->pen();
162 QPen pen = marker->pen();
181 color = pen.color();
163 color = pen.color();
182 color.setAlphaF(alpha);
164 color.setAlphaF(alpha);
183 pen.setColor(color);
165 pen.setColor(color);
184 marker->setPen(pen);
166 marker->setPen(pen);
185
167
186 //![6]
168 //![6]
187 break;
169 break;
188 }
170 }
189 default:
171 default:
190 {
172 {
191 qDebug() << "Unknown marker type";
173 qDebug() << "Unknown marker type";
192 break;
174 break;
193 }
175 }
194 }
176 }
195 }
177 }
@@ -1,63 +1,62
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef MAINWIDGET_H
21 #ifndef MAINWIDGET_H
22 #define MAINWIDGET_H
22 #define MAINWIDGET_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "qchart.h"
25 #include "qchart.h"
26 #include "qchartview.h"
26 #include "qchartview.h"
27 #include <QWidget>
27 #include <QWidget>
28 #include <QGraphicsWidget>
28 #include <QGraphicsWidget>
29 #include <QGridLayout>
29 #include <QGridLayout>
30 #include <QGraphicsGridLayout>
30 #include <QGraphicsGridLayout>
31 #include <QDoubleSpinBox>
31 #include <QDoubleSpinBox>
32 #include <QGroupBox>
32 #include <QGroupBox>
33 #include <QLineSeries>
33 #include <QLineSeries>
34
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
36
37 class MainWidget : public QWidget
37 class MainWidget : public QWidget
38 {
38 {
39 Q_OBJECT
39 Q_OBJECT
40 public:
40 public:
41 explicit MainWidget(QWidget *parent = 0);
41 explicit MainWidget(QWidget *parent = 0);
42
42
43 public slots:
43 public slots:
44 void addSeries();
44 void addSeries();
45 void removeSeries();
45 void removeSeries();
46 void connectMarkers();
46 void connectMarkers();
47 void disconnectMarkers();
47 void disconnectMarkers();
48
48
49 void handleMarkerClicked();
49 void handleMarkerClicked();
50
50
51 private:
51 private:
52
52
53 QChart *m_chart;
53 QChart *m_chart;
54 QList<QLineSeries *> m_series;
54 QList<QLineSeries *> m_series;
55
55
56 QChartView *m_chartView;
56 QChartView *m_chartView;
57 QGridLayout *m_mainLayout;
57 QGridLayout *m_mainLayout;
58 QGridLayout *m_buttonLayout;
59 QGridLayout *m_fontLayout;
58 QGridLayout *m_fontLayout;
60
59
61 };
60 };
62
61
63 #endif // MAINWIDGET_H
62 #endif // MAINWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now