##// END OF EJS Templates
update to new legend example
sauimone -
r2172:1aa296db2427
parent child
Show More
@@ -1,301 +1,315
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 <QPieSeries>
29 #include <QPieSeries>
30 #include <QPieSlice>
30 #include <QPieSlice>
31 #include <QLegendMarker>
31 #include <QLegendMarker>
32
32
33 QTCOMMERCIALCHART_USE_NAMESPACE
33 QTCOMMERCIALCHART_USE_NAMESPACE
34
34
35 MainWidget::MainWidget(QWidget *parent) :
35 MainWidget::MainWidget(QWidget *parent) :
36 QWidget(parent)
36 QWidget(parent)
37 {
37 {
38 // Create buttons for ui
38 // Create buttons for ui
39 m_buttonLayout = new QGridLayout();
39 m_buttonLayout = new QGridLayout();
40 QPushButton *detachLegendButton = new QPushButton("Toggle attached");
40 QPushButton *detachLegendButton = new QPushButton("Toggle attached");
41 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached()));
41 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached()));
42 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
43
43
44 QPushButton *addSliceButton = new QPushButton("add slice");
44 QPushButton *addSliceButton = new QPushButton("add slice");
45 connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSlice()));
45 connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSlice()));
46 m_buttonLayout->addWidget(addSliceButton, 2, 0);
46 m_buttonLayout->addWidget(addSliceButton, 2, 0);
47 QPushButton *removeSliceButton = new QPushButton("remove slice");
47 QPushButton *removeSliceButton = new QPushButton("remove slice");
48 connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSlice()));
48 connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSlice()));
49 m_buttonLayout->addWidget(removeSliceButton, 3, 0);
49 m_buttonLayout->addWidget(removeSliceButton, 3, 0);
50
50
51 QPushButton *alignButton = new QPushButton("Align (Bottom)");
51 QPushButton *alignButton = new QPushButton("Align (Bottom)");
52 connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment()));
52 connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment()));
53 m_buttonLayout->addWidget(alignButton, 4, 0);
53 m_buttonLayout->addWidget(alignButton, 4, 0);
54
54
55 QPushButton *boldButton = new QPushButton("Toggle bold");
55 QPushButton *boldButton = new QPushButton("Toggle bold");
56 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
56 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
57 m_buttonLayout->addWidget(boldButton, 5, 0);
57 m_buttonLayout->addWidget(boldButton, 5, 0);
58
58
59 QPushButton *italicButton = new QPushButton("Toggle italic");
59 QPushButton *italicButton = new QPushButton("Toggle italic");
60 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
60 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
61 m_buttonLayout->addWidget(italicButton, 6, 0);
61 m_buttonLayout->addWidget(italicButton, 6, 0);
62
62
63 QPushButton *infoButton = new QPushButton("Debug info");
63 QPushButton *infoButton = new QPushButton("Debug info");
64 connect(infoButton, SIGNAL(clicked()), this, SLOT(showDebugInfo()));
64 connect(infoButton, SIGNAL(clicked()), this, SLOT(showDebugInfo()));
65 m_buttonLayout->addWidget(infoButton, 7, 0);
65 m_buttonLayout->addWidget(infoButton, 7, 0);
66
66
67 QPushButton *connectButton = new QPushButton("Connect markers");
67 QPushButton *connectButton = new QPushButton("Connect markers");
68 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectMarkers()));
68 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectMarkers()));
69 m_buttonLayout->addWidget(connectButton, 8, 0);
69 m_buttonLayout->addWidget(connectButton, 8, 0);
70
70
71 QPushButton *disConnectButton = new QPushButton("Disconnect markers");
71 QPushButton *disConnectButton = new QPushButton("Disconnect markers");
72 connect(disConnectButton, SIGNAL(clicked()), this, SLOT(disconnectMarkers()));
72 connect(disConnectButton, SIGNAL(clicked()), this, SLOT(disconnectMarkers()));
73 m_buttonLayout->addWidget(disConnectButton, 9, 0);
73 m_buttonLayout->addWidget(disConnectButton, 9, 0);
74
74
75
75
76 m_legendPosX = new QDoubleSpinBox();
76 m_legendPosX = new QDoubleSpinBox();
77 m_legendPosY = new QDoubleSpinBox();
77 m_legendPosY = new QDoubleSpinBox();
78 m_legendWidth = new QDoubleSpinBox();
78 m_legendWidth = new QDoubleSpinBox();
79 m_legendHeight = new QDoubleSpinBox();
79 m_legendHeight = new QDoubleSpinBox();
80
80
81 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
81 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
82 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
82 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85
85
86 QFormLayout* legendLayout = new QFormLayout();
86 QFormLayout* legendLayout = new QFormLayout();
87 legendLayout->addRow("HPos", m_legendPosX);
87 legendLayout->addRow("HPos", m_legendPosX);
88 legendLayout->addRow("VPos", m_legendPosY);
88 legendLayout->addRow("VPos", m_legendPosY);
89 legendLayout->addRow("Width", m_legendWidth);
89 legendLayout->addRow("Width", m_legendWidth);
90 legendLayout->addRow("Height", m_legendHeight);
90 legendLayout->addRow("Height", m_legendHeight);
91 m_legendSettings = new QGroupBox("Detached legend");
91 m_legendSettings = new QGroupBox("Detached legend");
92 m_legendSettings->setLayout(legendLayout);
92 m_legendSettings->setLayout(legendLayout);
93 m_buttonLayout->addWidget(m_legendSettings);
93 m_buttonLayout->addWidget(m_legendSettings);
94 m_legendSettings->setVisible(false);
94 m_legendSettings->setVisible(false);
95
95
96 // Create chart view with the chart
96 // Create chart view with the chart
97 m_chart = new QChart();
97 m_chart = new QChart();
98 m_chartView = new QChartView(m_chart, this);
98 m_chartView = new QChartView(m_chart, this);
99
99
100 // Create spinbox to modify font size
100 // Create spinbox to modify font size
101 m_fontSize = new QDoubleSpinBox();
101 m_fontSize = new QDoubleSpinBox();
102 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
102 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
103 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
103 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
104
104
105 QFormLayout* fontLayout = new QFormLayout();
105 QFormLayout* fontLayout = new QFormLayout();
106 fontLayout->addRow("Legend font size", m_fontSize);
106 fontLayout->addRow("Legend font size", m_fontSize);
107
107
108 // Create layout for grid and detached legend
108 // Create layout for grid and detached legend
109 m_mainLayout = new QGridLayout();
109 m_mainLayout = new QGridLayout();
110 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
110 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
111 m_mainLayout->addLayout(fontLayout,1,0);
111 m_mainLayout->addLayout(fontLayout,1,0);
112 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
112 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
113 setLayout(m_mainLayout);
113 setLayout(m_mainLayout);
114
114
115 createSeries();
115 createSeries();
116 }
116 }
117
117
118 void MainWidget::createSeries()
118 void MainWidget::createSeries()
119 {
119 {
120 m_series = new QPieSeries();
120 m_series = new QPieSeries();
121 addSlice();
121 addSlice();
122 addSlice();
122 addSlice();
123 addSlice();
123 addSlice();
124 addSlice();
124 addSlice();
125
125
126 m_chart->addSeries(m_series);
126 m_chart->addSeries(m_series);
127 m_chart->setTitle("Legend detach example");
127 m_chart->setTitle("Legend detach example");
128 m_chart->createDefaultAxes();
128 m_chart->createDefaultAxes();
129 //![1]
129 //![1]
130 m_chart->legend()->setVisible(true);
130 m_chart->legend()->setVisible(true);
131 m_chart->legend()->setAlignment(Qt::AlignBottom);
131 m_chart->legend()->setAlignment(Qt::AlignBottom);
132 //![1]
132 //![1]
133
133
134 m_chartView->setRenderHint(QPainter::Antialiasing);
134 m_chartView->setRenderHint(QPainter::Antialiasing);
135 }
135 }
136
136
137 void MainWidget::showLegendSpinbox()
137 void MainWidget::showLegendSpinbox()
138 {
138 {
139 m_legendSettings->setVisible(true);
139 m_legendSettings->setVisible(true);
140 QRectF chartViewRect = m_chartView->rect();
140 QRectF chartViewRect = m_chartView->rect();
141
141
142 m_legendPosX->setMinimum(0);
142 m_legendPosX->setMinimum(0);
143 m_legendPosX->setMaximum(chartViewRect.width());
143 m_legendPosX->setMaximum(chartViewRect.width());
144 m_legendPosX->setValue(150);
144 m_legendPosX->setValue(150);
145
145
146 m_legendPosY->setMinimum(0);
146 m_legendPosY->setMinimum(0);
147 m_legendPosY->setMaximum(chartViewRect.height());
147 m_legendPosY->setMaximum(chartViewRect.height());
148 m_legendPosY->setValue(150);
148 m_legendPosY->setValue(150);
149
149
150 m_legendWidth->setMinimum(0);
150 m_legendWidth->setMinimum(0);
151 m_legendWidth->setMaximum(chartViewRect.width());
151 m_legendWidth->setMaximum(chartViewRect.width());
152 m_legendWidth->setValue(150);
152 m_legendWidth->setValue(150);
153
153
154 m_legendHeight->setMinimum(0);
154 m_legendHeight->setMinimum(0);
155 m_legendHeight->setMaximum(chartViewRect.height());
155 m_legendHeight->setMaximum(chartViewRect.height());
156 m_legendHeight->setValue(75);
156 m_legendHeight->setValue(75);
157 }
157 }
158
158
159 void MainWidget::hideLegendSpinbox()
159 void MainWidget::hideLegendSpinbox()
160 {
160 {
161 m_legendSettings->setVisible(false);
161 m_legendSettings->setVisible(false);
162 }
162 }
163
163
164
164
165 void MainWidget::toggleAttached()
165 void MainWidget::toggleAttached()
166 {
166 {
167 QLegend *legend = m_chart->legend();
167 QLegend *legend = m_chart->legend();
168 if (legend->isAttachedToChart()) {
168 if (legend->isAttachedToChart()) {
169 //![2]
169 //![2]
170 legend->detachFromChart();
170 legend->detachFromChart();
171 m_chart->legend()->setBackgroundVisible(true);
171 m_chart->legend()->setBackgroundVisible(true);
172 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
172 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
173 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
173 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
174 //![2]
174 //![2]
175 showLegendSpinbox();
175 showLegendSpinbox();
176 updateLegendLayout();
176 updateLegendLayout();
177 } else {
177 } else {
178 //![3]
178 //![3]
179 legend->attachToChart();
179 legend->attachToChart();
180 legend->setBackgroundVisible(false);
180 legend->setBackgroundVisible(false);
181 //![3]
181 //![3]
182 hideLegendSpinbox();
182 hideLegendSpinbox();
183 }
183 }
184 update();
184 update();
185 }
185 }
186
186
187 void MainWidget::addSlice()
187 void MainWidget::addSlice()
188 {
188 {
189 QPieSlice* slice = new QPieSlice(QString("slice " + QString::number(m_series->count())), m_series->count()+1);
189 QPieSlice* slice = new QPieSlice(QString("slice " + QString::number(m_series->count())), m_series->count()+1);
190 m_series->append(slice);
190 m_series->append(slice);
191 }
191 }
192
192
193 void MainWidget::removeSlice()
193 void MainWidget::removeSlice()
194 {
194 {
195 QList<QPieSlice*> slices = m_series->slices();
195 QList<QPieSlice*> slices = m_series->slices();
196 if (slices.count() > 0) {
196 if (slices.count() > 0) {
197 m_series->remove(slices.at(slices.count()-1));
197 m_series->remove(slices.at(slices.count()-1));
198 }
198 }
199 }
199 }
200
200
201 void MainWidget::connectMarkers()
201 void MainWidget::connectMarkers()
202 {
202 {
203 // Example use case.
203 // Example use case.
204 // Explode slice via marker.
204 // Explode slice via marker.
205 // Should be doable via public api.
205 // Should be doable via public api.
206
206
207 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
207 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
208 // Disconnect possible existing connection to avoid multiple connections
208 // Disconnect possible existing connection to avoid multiple connections
209 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
209 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
210 QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
210 QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
211 }
211 }
212 }
212 }
213
213
214 void MainWidget::disconnectMarkers()
214 void MainWidget::disconnectMarkers()
215 {
215 {
216 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
216 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
217 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
217 QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
218 }
218 }
219 }
219 }
220
220
221 void MainWidget::setLegendAlignment()
221 void MainWidget::setLegendAlignment()
222 {
222 {
223 QPushButton *button = qobject_cast<QPushButton *>(sender());
223 QPushButton *button = qobject_cast<QPushButton *>(sender());
224
224
225 switch (m_chart->legend()->alignment()) {
225 switch (m_chart->legend()->alignment()) {
226 case Qt::AlignTop:
226 case Qt::AlignTop:
227 m_chart->legend()->setAlignment(Qt::AlignLeft);
227 m_chart->legend()->setAlignment(Qt::AlignLeft);
228 if (button)
228 if (button)
229 button->setText("Align (Left)");
229 button->setText("Align (Left)");
230 break;
230 break;
231 case Qt::AlignLeft:
231 case Qt::AlignLeft:
232 m_chart->legend()->setAlignment(Qt::AlignBottom);
232 m_chart->legend()->setAlignment(Qt::AlignBottom);
233 if (button)
233 if (button)
234 button->setText("Align (Bottom)");
234 button->setText("Align (Bottom)");
235 break;
235 break;
236 case Qt::AlignBottom:
236 case Qt::AlignBottom:
237 m_chart->legend()->setAlignment(Qt::AlignRight);
237 m_chart->legend()->setAlignment(Qt::AlignRight);
238 if (button)
238 if (button)
239 button->setText("Align (Right)");
239 button->setText("Align (Right)");
240 break;
240 break;
241 default:
241 default:
242 if (button)
242 if (button)
243 button->setText("Align (Top)");
243 button->setText("Align (Top)");
244 m_chart->legend()->setAlignment(Qt::AlignTop);
244 m_chart->legend()->setAlignment(Qt::AlignTop);
245 break;
245 break;
246 }
246 }
247 }
247 }
248
248
249 void MainWidget::toggleBold()
249 void MainWidget::toggleBold()
250 {
250 {
251 QFont font = m_chart->legend()->font();
251 QFont font = m_chart->legend()->font();
252 font.setBold(!font.bold());
252 font.setBold(!font.bold());
253 m_chart->legend()->setFont(font);
253 m_chart->legend()->setFont(font);
254 }
254 }
255
255
256 void MainWidget::toggleItalic()
256 void MainWidget::toggleItalic()
257 {
257 {
258 QFont font = m_chart->legend()->font();
258 QFont font = m_chart->legend()->font();
259 font.setItalic(!font.italic());
259 font.setItalic(!font.italic());
260 m_chart->legend()->setFont(font);
260 m_chart->legend()->setFont(font);
261 }
261 }
262
262
263 void MainWidget::showDebugInfo()
263 void MainWidget::showDebugInfo()
264 {
264 {
265 qDebug() << "marker count:" << m_chart->legend()->markers().count();
265 qDebug() << "marker count:" << m_chart->legend()->markers().count();
266 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
266 foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
267 qDebug() << "marker series type:" << marker->series()->type();
267 qDebug() << "marker series type:" << marker->series()->type();
268 qDebug() << "peer object:" << marker->peerObject();
268 qDebug() << "peer object:" << marker->peerObject();
269 qDebug() << "label:" << marker->label();
269 qDebug() << "label:" << marker->label();
270 }
270 }
271 }
271 }
272
272
273 void MainWidget::fontSizeChanged()
273 void MainWidget::fontSizeChanged()
274 {
274 {
275 QFont font = m_chart->legend()->font();
275 QFont font = m_chart->legend()->font();
276 font.setPointSizeF(m_fontSize->value());
276 font.setPointSizeF(m_fontSize->value());
277 m_chart->legend()->setFont(font);
277 m_chart->legend()->setFont(font);
278 }
278 }
279
279
280 void MainWidget::updateLegendLayout()
280 void MainWidget::updateLegendLayout()
281 {
281 {
282 //![4]
282 //![4]
283 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
283 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
284 ,m_legendPosY->value()
284 ,m_legendPosY->value()
285 ,m_legendWidth->value()
285 ,m_legendWidth->value()
286 ,m_legendHeight->value()));
286 ,m_legendHeight->value()));
287 m_chart->legend()->update();
287 m_chart->legend()->update();
288 //![4]
288 //![4]
289 }
289 }
290
290
291 void MainWidget::handleMarkerClicked()
291 void MainWidget::handleMarkerClicked()
292 {
292 {
293 QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
293 QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
294
294
295 qDebug() << "marker clicked:" << marker;
295 qDebug() << "marker clicked:" << marker;
296
296
297 QPieSlice* slice = qobject_cast<QPieSlice*> (marker->peerObject());
297 switch (marker->series()->type())
298 Q_ASSERT(slice);
298 {
299 case QAbstractSeries::SeriesTypePie:
300 {
301 // Series type is pie.
302 // The peer object is QPieSlice
303 QPieSlice* slice = qobject_cast<QPieSlice*> (marker->peerObject());
304 Q_ASSERT(slice);
305 slice->setExploded(!slice->isExploded());
306 break;
307 }
308 default:
309 {
310 qDebug() << "Unknown series type";
311 break;
312 }
313 }
299
314
300 slice->setExploded(!slice->isExploded());
301 }
315 }
General Comments 0
You need to be logged in to leave comments. Login now