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