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