@@ -1,545 +1,529 | |||
|
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 "qlegend.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include "qseries.h" |
|
24 | 24 | #include "legendmarker_p.h" |
|
25 | 25 | #include "qxyseries.h" |
|
26 | 26 | #include "qlineseries.h" |
|
27 | 27 | #include "qareaseries.h" |
|
28 | 28 | #include "qscatterseries.h" |
|
29 | 29 | #include "qsplineseries.h" |
|
30 | 30 | #include "qbarseries.h" |
|
31 | 31 | #include "qstackedbarseries.h" |
|
32 | 32 | #include "qpercentbarseries.h" |
|
33 | 33 | #include "qbarset.h" |
|
34 | 34 | #include "qpieseries.h" |
|
35 | 35 | #include "qpieslice.h" |
|
36 | 36 | #include "chartpresenter_p.h" |
|
37 | 37 | #include <QPainter> |
|
38 | 38 | #include <QPen> |
|
39 | 39 | #include <QTimer> |
|
40 | 40 | |
|
41 | 41 | #include <QGraphicsSceneEvent> |
|
42 | 42 | |
|
43 | 43 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
44 | 44 | |
|
45 | 45 | /*! |
|
46 | 46 | \class QLegend |
|
47 | 47 | \brief part of QtCommercial chart API. |
|
48 | 48 | |
|
49 | 49 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when |
|
50 | 50 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and |
|
51 | 51 | handle the drawing manually. |
|
52 | 52 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. |
|
53 | 53 | |
|
54 | 54 | \mainclass |
|
55 | 55 | |
|
56 | 56 | \sa QChart, QSeries |
|
57 | 57 | */ |
|
58 | 58 | |
|
59 | 59 | /*! |
|
60 | 60 | \enum QLegend::Layout |
|
61 | 61 | |
|
62 | 62 | This enum describes the possible position for legend inside chart. |
|
63 | 63 | |
|
64 | 64 | \value LayoutTop |
|
65 | 65 | \value LayoutBottom |
|
66 | 66 | \value LayoutLeft |
|
67 | 67 | \value LayoutRight |
|
68 | 68 | */ |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button) |
|
73 | 73 | \brief Notifies when series has been clicked on legend \a series \a button |
|
74 | 74 | */ |
|
75 | 75 | |
|
76 | 76 | /*! |
|
77 | 77 | \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button) |
|
78 | 78 | \brief Notifies when barset has been clicked on legend \a barset \a button |
|
79 | 79 | */ |
|
80 | 80 | |
|
81 | 81 | /*! |
|
82 | 82 | \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button) |
|
83 | 83 | \brief Notifies when pie slice has been clicked on legend \a slice \a button |
|
84 | 84 | */ |
|
85 | 85 | |
|
86 | 86 | /*! |
|
87 | 87 | Constructs the legend object and sets the parent to \a parent |
|
88 | 88 | */ |
|
89 | 89 | |
|
90 | 90 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), |
|
91 | 91 | m_margin(5), |
|
92 | 92 | m_offsetX(0), |
|
93 | 93 | m_offsetY(0), |
|
94 | 94 | m_brush(Qt::darkGray), // TODO: default should come from theme |
|
95 | 95 | m_alignment(QLegend::AlignmentTop), |
|
96 | 96 | m_markers(new QGraphicsItemGroup(this)), |
|
97 | 97 | m_attachedToChart(true), |
|
98 | 98 | m_chart(chart), |
|
99 | 99 | m_minWidth(0), |
|
100 | 100 | m_minHeight(0), |
|
101 | 101 | m_width(0), |
|
102 | 102 | m_height(0), |
|
103 | 103 | m_visible(false), |
|
104 | 104 | m_dirty(false) |
|
105 | 105 | { |
|
106 | 106 | setZValue(ChartPresenter::LegendZValue); |
|
107 | 107 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | /*! |
|
111 | 111 | Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. |
|
112 | 112 | */ |
|
113 | 113 | |
|
114 | 114 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
115 | 115 | { |
|
116 | 116 | Q_UNUSED(option) |
|
117 | 117 | Q_UNUSED(widget) |
|
118 | 118 | if(!m_visible) return; |
|
119 | 119 | |
|
120 | 120 | painter->setOpacity(opacity()); |
|
121 | 121 | painter->setPen(m_pen); |
|
122 | 122 | painter->setBrush(m_brush); |
|
123 | 123 | painter->drawRect(boundingRect()); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | /*! |
|
127 | 127 | Bounding rect of legend. |
|
128 | 128 | */ |
|
129 | 129 | |
|
130 | 130 | QRectF QLegend::boundingRect() const |
|
131 | 131 | { |
|
132 | 132 | return m_rect; |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | /*! |
|
136 | 136 | Sets the \a brush of legend. Brush affects the background of legend. |
|
137 | 137 | */ |
|
138 | 138 | void QLegend::setBrush(const QBrush &brush) |
|
139 | 139 | { |
|
140 | 140 | if (m_brush != brush) { |
|
141 | 141 | m_brush = brush; |
|
142 | 142 | update(); |
|
143 | 143 | } |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | /*! |
|
147 | 147 | Returns the brush used by legend. |
|
148 | 148 | */ |
|
149 | 149 | QBrush QLegend::brush() const |
|
150 | 150 | { |
|
151 | 151 | return m_brush; |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | /*! |
|
155 | 155 | Sets the \a pen of legend. Pen affects the legend borders. |
|
156 | 156 | */ |
|
157 | 157 | void QLegend::setPen(const QPen &pen) |
|
158 | 158 | { |
|
159 | 159 | if (m_pen != pen) { |
|
160 | 160 | m_pen = pen; |
|
161 | 161 | update(); |
|
162 | 162 | } |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | /*! |
|
166 | 166 | Returns the pen used by legend |
|
167 | 167 | */ |
|
168 | 168 | |
|
169 | 169 | QPen QLegend::pen() const |
|
170 | 170 | { |
|
171 | 171 | return m_pen; |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | /*! |
|
175 | 175 | Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart. |
|
176 | 176 | \sa QLegend::Layout |
|
177 | 177 | */ |
|
178 | 178 | void QLegend::setAlignmnent(QLegend::Alignments alignment) |
|
179 | 179 | { |
|
180 | 180 | if(m_alignment!=alignment && m_attachedToChart) { |
|
181 | 181 | m_alignment = alignment; |
|
182 | 182 | updateLayout(); |
|
183 | 183 | } |
|
184 | 184 | } |
|
185 | 185 | |
|
186 | 186 | /*! |
|
187 | 187 | Returns the preferred layout for legend |
|
188 | 188 | */ |
|
189 | 189 | QLegend::Alignments QLegend::alignment() const |
|
190 | 190 | { |
|
191 | 191 | return m_alignment; |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | /*! |
|
195 | 195 | \internal \a series \a domain Should be called when series is added to chart. |
|
196 | 196 | */ |
|
197 | 197 | void QLegend::handleSeriesAdded(QSeries *series, Domain *domain) |
|
198 | 198 | { |
|
199 | 199 | Q_UNUSED(domain) |
|
200 | 200 | |
|
201 | 201 | switch (series->type()) |
|
202 | 202 | { |
|
203 | 203 | case QSeries::SeriesTypeLine: { |
|
204 | 204 | QLineSeries *lineSeries = static_cast<QLineSeries *>(series); |
|
205 | 205 | appendMarkers(lineSeries); |
|
206 | 206 | break; |
|
207 | 207 | } |
|
208 | 208 | case QSeries::SeriesTypeArea: { |
|
209 | 209 | QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); |
|
210 | 210 | appendMarkers(areaSeries); |
|
211 | 211 | break; |
|
212 | 212 | } |
|
213 | 213 | case QSeries::SeriesTypeBar: { |
|
214 | 214 | QBarSeries *barSeries = static_cast<QBarSeries *>(series); |
|
215 | 215 | appendMarkers(barSeries); |
|
216 | 216 | break; |
|
217 | 217 | } |
|
218 | 218 | case QSeries::SeriesTypeStackedBar: { |
|
219 | 219 | QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series); |
|
220 | 220 | appendMarkers(stackedBarSeries); |
|
221 | 221 | break; |
|
222 | 222 | } |
|
223 | 223 | case QSeries::SeriesTypePercentBar: { |
|
224 | 224 | QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series); |
|
225 | 225 | appendMarkers(percentBarSeries); |
|
226 | 226 | break; |
|
227 | 227 | } |
|
228 | 228 | case QSeries::SeriesTypeScatter: { |
|
229 | 229 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
230 | 230 | appendMarkers(scatterSeries); |
|
231 | 231 | break; |
|
232 | 232 | } |
|
233 | 233 | case QSeries::SeriesTypePie: { |
|
234 | 234 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
235 | 235 | appendMarkers(pieSeries); |
|
236 | 236 | connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); |
|
237 | 237 | break; |
|
238 | 238 | } |
|
239 | 239 | case QSeries::SeriesTypeSpline: { |
|
240 | 240 | QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series); |
|
241 | 241 | appendMarkers(splineSeries); |
|
242 | 242 | break; |
|
243 | 243 | } |
|
244 | 244 | default: { |
|
245 | 245 | qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type."; |
|
246 | 246 | break; |
|
247 | 247 | } |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | // wait for all series added |
|
251 | 251 | if(!m_dirty){ |
|
252 | 252 | QTimer::singleShot(0,this,SLOT(updateLayout())); |
|
253 | 253 | m_dirty=true; |
|
254 | 254 | } |
|
255 | 255 | } |
|
256 | 256 | |
|
257 | 257 | /*! |
|
258 | 258 | \internal \a series Should be called when series is removed from chart. |
|
259 | 259 | */ |
|
260 | 260 | void QLegend::handleSeriesRemoved(QSeries *series) |
|
261 | 261 | { |
|
262 | 262 | switch (series->type()) |
|
263 | 263 | { |
|
264 | 264 | case QSeries::SeriesTypeArea: { |
|
265 | 265 | QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); |
|
266 | 266 | deleteMarkers(areaSeries); |
|
267 | 267 | break; |
|
268 | 268 | } |
|
269 | 269 | case QSeries::SeriesTypePie: { |
|
270 | 270 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
271 | 271 | disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>))); |
|
272 | 272 | deleteMarkers(series); |
|
273 | 273 | break; |
|
274 | 274 | } |
|
275 | 275 | default: { |
|
276 | 276 | // All other types |
|
277 | 277 | deleteMarkers(series); |
|
278 | 278 | break; |
|
279 | 279 | } |
|
280 | 280 | } |
|
281 | 281 | |
|
282 | 282 | updateLayout(); |
|
283 | 283 | } |
|
284 | 284 | |
|
285 | 285 | /*! |
|
286 | 286 | \internal \a slices Should be called when slices are added to pie chart. |
|
287 | 287 | */ |
|
288 | 288 | void QLegend::handleAdded(QList<QPieSlice *> slices) |
|
289 | 289 | { |
|
290 | 290 | QPieSeries* series = static_cast<QPieSeries *> (sender()); |
|
291 | 291 | foreach(QPieSlice* slice, slices) { |
|
292 | 292 | PieLegendMarker* marker = new PieLegendMarker(series,slice, this); |
|
293 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | |
|
294 | 293 | m_markers->addToGroup(marker); |
|
295 | 294 | } |
|
296 | 295 | updateLayout(); |
|
297 | 296 | } |
|
298 | 297 | |
|
299 | 298 | /*! |
|
300 | 299 | \internal \a slices Should be called when slices are removed from pie chart. Currently unused, |
|
301 | 300 | because removed slices are also deleted and we listen destroyed signal |
|
302 | 301 | */ |
|
303 | 302 | void QLegend::handleRemoved(QList<QPieSlice *> slices) |
|
304 | 303 | { |
|
305 | 304 | Q_UNUSED(slices) |
|
306 | 305 | } |
|
307 | 306 | |
|
308 | ||
|
309 | /*! | |
|
310 | \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed | |
|
311 | */ | |
|
312 | void QLegend::handleMarkerDestroyed() | |
|
313 | { | |
|
314 | LegendMarker* m = static_cast<LegendMarker *> (sender()); | |
|
315 | delete m; | |
|
316 | // updateLayout(); | |
|
317 | } | |
|
318 | ||
|
319 | 307 | /*! |
|
320 | 308 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
321 | 309 | */ |
|
322 | 310 | void QLegend::detachFromChart() |
|
323 | 311 | { |
|
324 | 312 | m_attachedToChart = false; |
|
325 | 313 | } |
|
326 | 314 | |
|
327 | 315 | /*! |
|
328 | 316 | Attaches the legend to chart. Chart may change layout of the legend. |
|
329 | 317 | */ |
|
330 | 318 | void QLegend::attachToChart() |
|
331 | 319 | { |
|
332 | 320 | m_attachedToChart = true; |
|
333 | 321 | } |
|
334 | 322 | |
|
335 | 323 | /*! |
|
336 | 324 | Returns true, if legend is attached to chart. |
|
337 | 325 | */ |
|
338 | 326 | bool QLegend::isAttachedToChart() |
|
339 | 327 | { |
|
340 | 328 | return m_attachedToChart; |
|
341 | 329 | } |
|
342 | 330 | |
|
343 | 331 | /*! |
|
344 | 332 | \internal Helper function. Appends markers from \a series to legend. |
|
345 | 333 | */ |
|
346 | 334 | void QLegend::appendMarkers(QAreaSeries* series) |
|
347 | 335 | { |
|
348 | 336 | AreaLegendMarker* marker = new AreaLegendMarker(series,this); |
|
349 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | |
|
350 | 337 | m_markers->addToGroup(marker); |
|
351 | 338 | } |
|
352 | 339 | |
|
353 | 340 | /*! |
|
354 | 341 | \internal Helper function. Appends markers from \a series to legend. |
|
355 | 342 | */ |
|
356 | 343 | void QLegend::appendMarkers(QXYSeries* series) |
|
357 | 344 | { |
|
358 | 345 | XYLegendMarker* marker = new XYLegendMarker(series,this); |
|
359 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | |
|
360 | 346 | m_markers->addToGroup(marker); |
|
361 | 347 | } |
|
362 | 348 | |
|
363 | 349 | /*! |
|
364 | 350 | \internal Helper function. Appends markers from \a series to legend. |
|
365 | 351 | */ |
|
366 | 352 | void QLegend::appendMarkers(QBarSeries *series) |
|
367 | 353 | { |
|
368 | 354 | foreach(QBarSet* set, series->barSets()) { |
|
369 | 355 | BarLegendMarker* marker = new BarLegendMarker(series,set, this); |
|
370 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | |
|
371 | 356 | m_markers->addToGroup(marker); |
|
372 | 357 | } |
|
373 | 358 | } |
|
374 | 359 | |
|
375 | 360 | /*! |
|
376 | 361 | \internal Helper function. Appends markers from \a series to legend. |
|
377 | 362 | */ |
|
378 | 363 | void QLegend::appendMarkers(QPieSeries *series) |
|
379 | 364 | { |
|
380 | 365 | foreach(QPieSlice* slice, series->slices()) { |
|
381 | 366 | PieLegendMarker* marker = new PieLegendMarker(series,slice, this); |
|
382 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | |
|
383 | 367 | m_markers->addToGroup(marker); |
|
384 | 368 | } |
|
385 | 369 | } |
|
386 | 370 | |
|
387 | 371 | /*! |
|
388 | 372 | \internal Deletes all markers that are created from \a series |
|
389 | 373 | */ |
|
390 | 374 | void QLegend::deleteMarkers(QSeries *series) |
|
391 | 375 | { |
|
392 | 376 | // Search all markers that belong to given series and delete them. |
|
393 | 377 | |
|
394 | 378 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
395 | 379 | |
|
396 | foreach (QGraphicsItem *m, items) { | |
|
397 | LegendMarker *marker = static_cast<LegendMarker*>(m); | |
|
380 | foreach (QGraphicsItem *markers, items) { | |
|
381 | LegendMarker *marker = static_cast<LegendMarker*>(markers); | |
|
398 | 382 | if (marker->series() == series) { |
|
399 | 383 | delete marker; |
|
400 | 384 | } |
|
401 | 385 | } |
|
402 | 386 | } |
|
403 | 387 | |
|
404 | 388 | /*! |
|
405 | 389 | \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend. |
|
406 | 390 | If items don't fit, sets the visibility of scroll buttons accordingly. |
|
407 | 391 | Causes legend to be resized. |
|
408 | 392 | */ |
|
409 | 393 | |
|
410 | 394 | void QLegend::setOffset(const QPointF& point) |
|
411 | 395 | { |
|
412 | 396 | |
|
413 | 397 | switch(m_alignment) { |
|
414 | 398 | |
|
415 | 399 | case AlignmentTop: |
|
416 | 400 | case AlignmentBottom: { |
|
417 | 401 | if(m_width<=m_rect.width()) return; |
|
418 | 402 | |
|
419 | 403 | if (point.x() != m_offsetX) { |
|
420 | 404 | m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width()); |
|
421 | 405 | m_markers->setPos(-m_offsetX,m_rect.top()); |
|
422 | 406 | } |
|
423 | 407 | break; |
|
424 | 408 | } |
|
425 | 409 | case AlignmentLeft: |
|
426 | 410 | case AlignmentRight: { |
|
427 | 411 | |
|
428 | 412 | if(m_height<=m_rect.height()) return; |
|
429 | 413 | |
|
430 | 414 | if (point.y() != m_offsetY) { |
|
431 | 415 | m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height()); |
|
432 | 416 | m_markers->setPos(m_rect.left(),-m_offsetY); |
|
433 | 417 | } |
|
434 | 418 | break; |
|
435 | 419 | } |
|
436 | 420 | } |
|
437 | 421 | } |
|
438 | 422 | |
|
439 | 423 | QPointF QLegend::offset() const |
|
440 | 424 | { |
|
441 | 425 | return QPointF(m_offsetX,m_offsetY); |
|
442 | 426 | } |
|
443 | 427 | |
|
444 | 428 | // this function runs first to set min max values |
|
445 | 429 | void QLegend::updateLayout() |
|
446 | 430 | { |
|
447 | 431 | m_dirty=false; |
|
448 | 432 | m_offsetX=0; |
|
449 | 433 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
450 | 434 | |
|
451 | 435 | if(items.isEmpty()) return; |
|
452 | 436 | |
|
453 | 437 | m_minWidth=0; |
|
454 | 438 | m_minHeight=0; |
|
455 | 439 | |
|
456 | 440 | switch(m_alignment) { |
|
457 | 441 | |
|
458 | 442 | case AlignmentTop: |
|
459 | 443 | case AlignmentBottom: { |
|
460 | 444 | QPointF point = m_rect.topLeft(); |
|
461 | 445 | m_width = 0; |
|
462 | 446 | foreach (QGraphicsItem *item, items) { |
|
463 | 447 | item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); |
|
464 | 448 | const QRectF& rect = item->boundingRect(); |
|
465 | 449 | qreal w = rect.width(); |
|
466 | 450 | m_minWidth=qMax(m_minWidth,w); |
|
467 | 451 | m_minHeight=qMax(m_minHeight,rect.height()); |
|
468 | 452 | m_width+=w; |
|
469 | 453 | point.setX(point.x() + w); |
|
470 | 454 | } |
|
471 | 455 | if(m_width<m_rect.width()){ |
|
472 | 456 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); |
|
473 | 457 | }else{ |
|
474 | 458 | m_markers->setPos(m_rect.topLeft()); |
|
475 | 459 | } |
|
476 | 460 | m_height=m_minHeight; |
|
477 | 461 | } |
|
478 | 462 | break; |
|
479 | 463 | case AlignmentLeft: |
|
480 | 464 | case AlignmentRight:{ |
|
481 | 465 | QPointF point = m_rect.topLeft(); |
|
482 | 466 | m_height = 0; |
|
483 | 467 | foreach (QGraphicsItem *item, items) { |
|
484 | 468 | item->setPos(point); |
|
485 | 469 | const QRectF& rect = item->boundingRect(); |
|
486 | 470 | qreal h = rect.height(); |
|
487 | 471 | m_minWidth=qMax(m_minWidth,rect.width()); |
|
488 | 472 | m_minHeight=qMax(m_minHeight,h); |
|
489 | 473 | m_height+=h; |
|
490 | 474 | point.setY(point.y() + h); |
|
491 | 475 | } |
|
492 | 476 | if(m_height<m_rect.height()){ |
|
493 | 477 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); |
|
494 | 478 | }else{ |
|
495 | 479 | m_markers->setPos(m_rect.topLeft()); |
|
496 | 480 | } |
|
497 | 481 | m_width=m_minWidth; |
|
498 | 482 | } |
|
499 | 483 | break; |
|
500 | 484 | } |
|
501 | 485 | |
|
502 | 486 | m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme; |
|
503 | 487 | } |
|
504 | 488 | |
|
505 | 489 | void QLegend::setBackgroundVisible(bool visible) |
|
506 | 490 | { |
|
507 | 491 | if(m_visible!=visible) |
|
508 | 492 | { |
|
509 | 493 | m_visible=visible; |
|
510 | 494 | update(); |
|
511 | 495 | } |
|
512 | 496 | } |
|
513 | 497 | |
|
514 | 498 | bool QLegend::isBackgroundVisible() const |
|
515 | 499 | { |
|
516 | 500 | return m_visible; |
|
517 | 501 | } |
|
518 | 502 | |
|
519 | 503 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
520 | 504 | { |
|
521 | 505 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); |
|
522 | 506 | QGraphicsWidget::resizeEvent(event); |
|
523 | 507 | if(m_rect != rect){ |
|
524 | 508 | m_rect = rect; |
|
525 | 509 | updateLayout(); |
|
526 | 510 | } |
|
527 | 511 | } |
|
528 | 512 | |
|
529 | 513 | void QLegend::hideEvent(QHideEvent *event) |
|
530 | 514 | { |
|
531 | 515 | QGraphicsWidget::hideEvent(event); |
|
532 | 516 | setEnabled(false); |
|
533 | 517 | updateLayout(); |
|
534 | 518 | } |
|
535 | 519 | |
|
536 | 520 | void QLegend::showEvent(QShowEvent *event) |
|
537 | 521 | { |
|
538 | 522 | QGraphicsWidget::showEvent(event); |
|
539 | 523 | setEnabled(true); |
|
540 | 524 | updateLayout(); |
|
541 | 525 | } |
|
542 | 526 | |
|
543 | 527 | #include "moc_qlegend.cpp" |
|
544 | 528 | |
|
545 | 529 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,179 +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 | #ifndef QLEGEND_H |
|
22 | 22 | #define QLEGEND_H |
|
23 | 23 | |
|
24 | 24 | #include <QChartGlobal> |
|
25 | 25 | #include <QGraphicsWidget> |
|
26 | 26 | #include <QPen> |
|
27 | 27 | #include <QBrush> |
|
28 | 28 | #include "private/scroller_p.h" //TODO fixme |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | class Domain; |
|
33 | 33 | class LegendMarker; |
|
34 | 34 | class QPieSlice; |
|
35 | 35 | class QXYSeries; |
|
36 | 36 | class QBarSet; |
|
37 | 37 | class QBarSeries; |
|
38 | 38 | class QPieSeries; |
|
39 | 39 | class QAreaSeries; |
|
40 | 40 | class LegendScrollButton; |
|
41 | 41 | class QSeries; |
|
42 | 42 | class QChart; |
|
43 | 43 | |
|
44 | 44 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget |
|
45 | 45 | { |
|
46 | 46 | Q_OBJECT |
|
47 | 47 | public: |
|
48 | 48 | |
|
49 | 49 | // We only support these alignments (for now) |
|
50 | 50 | enum Alignment { |
|
51 | 51 | AlignmentTop = Qt::AlignTop, |
|
52 | 52 | AlignmentBottom = Qt::AlignBottom, |
|
53 | 53 | AlignmentLeft = Qt::AlignLeft, |
|
54 | 54 | AlignmentRight = Qt::AlignRight |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | Q_DECLARE_FLAGS(Alignments, Alignment) |
|
58 | 58 | |
|
59 | 59 | private: |
|
60 | 60 | explicit QLegend(QChart *chart); |
|
61 | 61 | |
|
62 | 62 | public: |
|
63 | 63 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
64 | 64 | QRectF boundingRect() const; |
|
65 | 65 | |
|
66 | 66 | void setBrush(const QBrush &brush); |
|
67 | 67 | QBrush brush() const; |
|
68 | 68 | |
|
69 | 69 | void setPen(const QPen &pen); |
|
70 | 70 | QPen pen() const; |
|
71 | 71 | |
|
72 | 72 | void setAlignmnent(QLegend::Alignments alignment); |
|
73 | 73 | QLegend::Alignments alignment() const; |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | void detachFromChart(); |
|
77 | 77 | void attachToChart(); |
|
78 | 78 | bool isAttachedToChart(); |
|
79 | 79 | |
|
80 | 80 | qreal minWidht() const { return m_minWidth;} |
|
81 | 81 | qreal minHeight() const { return m_minHeight;} |
|
82 | 82 | |
|
83 | 83 | void setBackgroundVisible(bool visible); |
|
84 | 84 | bool isBackgroundVisible() const; |
|
85 | 85 | |
|
86 | 86 | void setOffset(const QPointF& point); |
|
87 | 87 | QPointF offset() const; |
|
88 | 88 | |
|
89 | 89 | protected: |
|
90 | 90 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
91 | 91 | void hideEvent(QHideEvent *event); |
|
92 | 92 | void showEvent(QShowEvent *event); |
|
93 | 93 | |
|
94 | 94 | public Q_SLOTS: |
|
95 | 95 | // PIMPL ---> |
|
96 | 96 | void handleSeriesAdded(QSeries *series, Domain *domain); |
|
97 | 97 | void handleSeriesRemoved(QSeries *series); |
|
98 | 98 | void handleAdded(QList<QPieSlice *> slices); |
|
99 | 99 | void handleRemoved(QList<QPieSlice *> slices); |
|
100 | void handleMarkerDestroyed(); | |
|
101 | ||
|
102 | 100 | // PIMPL <--- |
|
103 | 101 | |
|
104 | 102 | private: |
|
105 | 103 | // PIMPL ---> |
|
106 | 104 | void appendMarkers(QAreaSeries *series); |
|
107 | 105 | void appendMarkers(QXYSeries *series); |
|
108 | 106 | void appendMarkers(QBarSeries *series); |
|
109 | 107 | void appendMarkers(QPieSeries *series); |
|
110 | 108 | void deleteMarkers(QSeries *series); |
|
111 | 109 | |
|
112 | 110 | |
|
113 | 111 | |
|
114 | 112 | |
|
115 | 113 | private Q_SLOTS: |
|
116 | 114 | void updateLayout(); |
|
117 | 115 | |
|
118 | 116 | private: |
|
119 | 117 | qreal m_margin; |
|
120 | 118 | |
|
121 | 119 | QRectF m_rect; |
|
122 | 120 | qreal m_offsetX; |
|
123 | 121 | qreal m_offsetY; |
|
124 | 122 | |
|
125 | 123 | //QList<LegendMarker *> m_markers; |
|
126 | 124 | |
|
127 | 125 | QBrush m_brush; |
|
128 | 126 | QPen m_pen; |
|
129 | 127 | QLegend::Alignments m_alignment; |
|
130 | 128 | QGraphicsItemGroup* m_markers; |
|
131 | 129 | |
|
132 | 130 | |
|
133 | 131 | bool m_attachedToChart; |
|
134 | 132 | |
|
135 | 133 | QChart *m_chart; |
|
136 | 134 | qreal m_minWidth; |
|
137 | 135 | qreal m_minHeight; |
|
138 | 136 | qreal m_width; |
|
139 | 137 | qreal m_height; |
|
140 | 138 | bool m_visible; |
|
141 | 139 | bool m_dirty; |
|
142 | 140 | friend class ScrolledQLegend; |
|
143 | 141 | // <--- PIMPL |
|
144 | 142 | }; |
|
145 | 143 | |
|
146 | 144 | class ScrolledQLegend: public QLegend, public Scroller |
|
147 | 145 | { |
|
148 | 146 | |
|
149 | 147 | public: |
|
150 | 148 | ScrolledQLegend(QChart *chart):QLegend(chart) |
|
151 | 149 | { |
|
152 | 150 | } |
|
153 | 151 | |
|
154 | 152 | void setOffset(const QPointF& point) |
|
155 | 153 | { |
|
156 | 154 | QLegend::setOffset(point); |
|
157 | 155 | } |
|
158 | 156 | QPointF offset() const |
|
159 | 157 | { |
|
160 | 158 | return QLegend::offset(); |
|
161 | 159 | } |
|
162 | 160 | |
|
163 | 161 | void mousePressEvent(QGraphicsSceneMouseEvent* event){ |
|
164 | 162 | Scroller::mousePressEvent(event); |
|
165 | 163 | //QLegend::mousePressEvent(event); |
|
166 | 164 | } |
|
167 | 165 | void mouseMoveEvent(QGraphicsSceneMouseEvent* event){ |
|
168 | 166 | Scroller::mouseMoveEvent(event); |
|
169 | 167 | //QLegend::mouseMoveEvent(event); |
|
170 | 168 | } |
|
171 | 169 | void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){ |
|
172 | 170 | Scroller::mouseReleaseEvent(event); |
|
173 | 171 | //QLegend::mouseReleaseEvent(event); |
|
174 | 172 | } |
|
175 | 173 | }; |
|
176 | 174 | |
|
177 | 175 | QTCOMMERCIALCHART_END_NAMESPACE |
|
178 | 176 | |
|
179 | 177 | #endif // QLEGEND_H |
General Comments 0
You need to be logged in to leave comments.
Login now