##// END OF EJS Templates
Fixes previous commit
Michal Klocek -
r791:0a28782b4a44
parent child
Show More
@@ -1,29 +1,28
1 1 #include "legendscrollbutton_p.h"
2 2 #include "qlegend.h"
3 3 #include <QGraphicsSceneEvent>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 LegendScrollButton::LegendScrollButton(ScrollButtonId id, QLegend *legend)
8 8 : QGraphicsPolygonItem(legend),
9 9 m_id(id),
10 10 m_ledgend(legend)
11 11 {
12 12 setAcceptedMouseButtons(Qt::LeftButton);
13 13 }
14 14
15 15 LegendScrollButton::ScrollButtonId LegendScrollButton::id()
16 16 {
17 17 return m_id;
18 18 }
19 19
20 20 void LegendScrollButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
21 21 {
22 22 Q_UNUSED(event);
23 23 m_ledgend->scrollButtonClicked(this);
24 24 }
25 25
26 #include "moc_legendscrollbutton_p.cpp"
27 26
28 27 QTCOMMERCIALCHART_END_NAMESPACE
29 28
@@ -1,693 +1,693
1 1 #include "qlegend.h"
2 2 #include "qchart_p.h"
3 3 #include "qseries.h"
4 4 #include "legendmarker_p.h"
5 5 #include "legendscrollbutton_p.h"
6 6 #include "qxyseries.h"
7 7 #include "qlineseries.h"
8 8 #include "qareaseries.h"
9 9 #include "qscatterseries.h"
10 10 #include "qsplineseries.h"
11 11 #include "qbarseries.h"
12 12 #include "qstackedbarseries.h"
13 13 #include "qpercentbarseries.h"
14 14 #include "qbarset.h"
15 15 #include "qpieseries.h"
16 16 #include "qpieslice.h"
17 17 #include "chartpresenter_p.h"
18 18 #include <QPainter>
19 19 #include <QPen>
20 20
21 21 #include <QGraphicsSceneEvent>
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QLegend
27 27 \brief part of QtCommercial chart API.
28 28
29 29 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
30 30 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
31 31 handle the drawing manually.
32 32 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
33 33
34 34 \mainclass
35 35
36 36 \sa QChart, QSeries
37 37 */
38 38
39 39 /*!
40 40 \enum QLegend::Layout
41 41
42 42 This enum describes the possible position for legend inside chart.
43 43
44 44 \value LayoutTop
45 45 \value LayoutBottom
46 46 \value LayoutLeft
47 47 \value LayoutRight
48 48 */
49 49
50 50
51 51 /*!
52 52 \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button)
53 53 \brief Notifies when series has been clicked on legend \a series \a button
54 54 */
55 55
56 56 /*!
57 57 \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button)
58 58 \brief Notifies when barset has been clicked on legend \a barset \a button
59 59 */
60 60
61 61 /*!
62 62 \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button)
63 63 \brief Notifies when pie slice has been clicked on legend \a slice \a button
64 64 */
65 65
66 66 /*!
67 67 Constructs the legend object and sets the parent to \a parent
68 68 */
69 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart->d_ptr->m_presenter->rootItem()),
69 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
70 70 m_margin(5),
71 71 m_pos(0,0),
72 72 m_minimumSize(50,20), // TODO: magic numbers
73 73 m_maximumSize(150,100),
74 74 m_size(m_minimumSize),
75 75 m_brush(Qt::darkGray), // TODO: default should come from theme
76 76 m_alignment(QLegend::LayoutTop),
77 77 mFirstMarker(0)
78 78 {
79 79 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
80 80 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
81 81 m_scrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this);
82 82 m_scrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this);
83 83 setZValue(ChartPresenter::LegendZValue);
84 84 }
85 85
86 86 /*!
87 87 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
88 88 */
89 89 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
90 90 {
91 91 Q_UNUSED(option)
92 92 Q_UNUSED(widget)
93 93
94 94 painter->setOpacity(opacity());
95 95 painter->setPen(m_pen);
96 96 painter->setBrush(m_brush);
97 97 // painter->drawRect(boundingRect());
98 98 }
99 99
100 100 /*!
101 101 Bounding rect of legend.
102 102 */
103 103 QRectF QLegend::boundingRect() const
104 104 {
105 105 return QRectF(m_pos,m_size);
106 106 }
107 107
108 108 /*!
109 109 Sets the \a brush of legend. Brush affects the background of legend.
110 110 */
111 111 void QLegend::setBrush(const QBrush &brush)
112 112 {
113 113 if (m_brush != brush) {
114 114 m_brush = brush;
115 115 update();
116 116 }
117 117 }
118 118
119 119 /*!
120 120 Returns the brush used by legend.
121 121 */
122 122 QBrush QLegend::brush() const
123 123 {
124 124 return m_brush;
125 125 }
126 126
127 127 /*!
128 128 Sets the \a pen of legend. Pen affects the legend borders.
129 129 */
130 130 void QLegend::setPen(const QPen &pen)
131 131 {
132 132 if (m_pen != pen) {
133 133 m_pen = pen;
134 134 update();
135 135 }
136 136 }
137 137
138 138 /*!
139 139 Returns the pen used by legend
140 140 */
141 141
142 142 QPen QLegend::pen() const
143 143 {
144 144 return m_pen;
145 145 }
146 146
147 147 /*!
148 148 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
149 149 \sa QLegend::Layout
150 150 */
151 151 void QLegend::setAlignmnent(QLegend::Layout alignment)
152 152 {
153 153 m_alignment = alignment;
154 154 updateLayout();
155 155 }
156 156
157 157 /*!
158 158 Returns the preferred layout for legend
159 159 */
160 160 QLegend::Layout QLegend::alignment() const
161 161 {
162 162 return m_alignment;
163 163 }
164 164
165 165 /*!
166 166 Returns the maximum size of legend.
167 167 */
168 168 QSizeF QLegend::maximumSize() const
169 169 {
170 170 return m_maximumSize;
171 171 }
172 172
173 173 /*!
174 174 Sets the maximum \a size for legend. The legend can't grow bigger than this size. If there are
175 175 more series than legend can fit to this size, scroll buttons are displayed.
176 176 */
177 177 void QLegend::setMaximumSize(const QSizeF size)
178 178 {
179 179 m_maximumSize = size;
180 180 updateLayout();
181 181 }
182 182
183 183 /*!
184 184 Returns the current size of legend.
185 185 */
186 186 QSizeF QLegend::size() const
187 187 {
188 188 return m_size;
189 189 }
190 190
191 191 /*!
192 192 Sets the \a size of legend. If size is bigger than maximum size of legend, the legend is resized to the maximum size.
193 193 \sa setMmaximumSize()
194 194 */
195 195 void QLegend::setSize(const QSizeF size)
196 196 {
197 197 m_size = size;
198 198 if (m_size.width() > m_maximumSize.width()) {
199 199 m_size.setWidth(m_maximumSize.width());
200 200 }
201 201 if (m_size.height() > m_maximumSize.height()) {
202 202 m_size.setHeight(m_maximumSize.height());
203 203 }
204 204 }
205 205
206 206 /*!
207 207 Sets position of legend to \a pos
208 208 */
209 209 void QLegend::setPos(const QPointF &pos)
210 210 {
211 211 m_pos = pos;
212 212 updateLayout();
213 213 }
214 214
215 215 /*!
216 216 \internal \a series \a domain Should be called when series is added to chart.
217 217 */
218 218 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
219 219 {
220 220 Q_UNUSED(domain)
221 221
222 222 createMarkers(series);
223 223 connectSeries(series);
224 224 updateLayout();
225 225 }
226 226
227 227 /*!
228 228 \internal \a series Should be called when series is removed from chart.
229 229 */
230 230 void QLegend::handleSeriesRemoved(QSeries *series)
231 231 {
232 232 disconnectSeries(series);
233 233
234 234 if (series->type() == QSeries::SeriesTypeArea) {
235 235 // This is special case. Area series has upper and lower series, which each have markers
236 236 QAreaSeries* s = static_cast<QAreaSeries *> (series);
237 237 deleteMarkers(s->upperSeries());
238 238 deleteMarkers(s->lowerSeries());
239 239 } else {
240 240 deleteMarkers(series);
241 241 }
242 242
243 243 updateLayout();
244 244 }
245 245
246 246 /*!
247 247 \internal \a slices Should be called when slices are added to pie chart.
248 248 */
249 249 void QLegend::handleAdded(QList<QPieSlice *> slices)
250 250 {
251 251 QPieSeries* series = static_cast<QPieSeries *> (sender());
252 252 foreach(QPieSlice* s, slices) {
253 253 LegendMarker* marker = new LegendMarker(series, s, this);
254 254 marker->setName(s->label());
255 255 marker->setBrush(s->brush());
256 256 connect(marker, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),
257 257 this, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
258 258 connect(s, SIGNAL(changed()), marker, SLOT(changed()));
259 259 connect(s, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
260 260 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
261 261 m_markers.append(marker);
262 262 childItems().append(marker);
263 263 }
264 264 updateLayout();
265 265 }
266 266
267 267 /*!
268 268 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
269 269 because removed slices are also deleted and we listen destroyed signal
270 270 */
271 271 void QLegend::handleRemoved(QList<QPieSlice *> slices)
272 272 {
273 273 Q_UNUSED(slices)
274 274 }
275 275
276 276
277 277 /*!
278 278 \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed
279 279 */
280 280 void QLegend::handleMarkerDestroyed()
281 281 {
282 282 LegendMarker* m = static_cast<LegendMarker *> (sender());
283 283 m_markers.removeOne(m);
284 284 updateLayout();
285 285 }
286 286
287 287 /*!
288 288 \internal \a event Handles clicked signals from scroll buttons
289 289 */
290 290 void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton)
291 291 {
292 292 Q_ASSERT(scrollButton);
293 293
294 294 switch (scrollButton->id()) {
295 295 case LegendScrollButton::ScrollButtonIdLeft:
296 296 case LegendScrollButton::ScrollButtonIdUp: {
297 297 // Lower limit is same in these cases
298 298 mFirstMarker--;
299 299 checkFirstMarkerBounds();
300 300 break;
301 301 }
302 302 case LegendScrollButton::ScrollButtonIdRight:
303 303 case LegendScrollButton::ScrollButtonIdDown: {
304 304 mFirstMarker++;
305 305 checkFirstMarkerBounds();
306 306 break;
307 307 }
308 308 default: {
309 309 break;
310 310 }
311 311 }
312 312 updateLayout();
313 313 }
314 314
315 315 /*!
316 316 \internal Connects the \a series to legend. Legend listens changes in series, for example pie slices added / removed.
317 317 Not all series notify about events
318 318 */
319 319 void QLegend::connectSeries(QSeries *series)
320 320 {
321 321 // Connect relevant signals from series. Currently only pie series has interesting signals
322 322 // TODO: bar chart may have
323 323 if (series->type() == QSeries::SeriesTypePie) {
324 324 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
325 325 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
326 326 }
327 327 }
328 328
329 329 /*!
330 330 \internal Disconnects \a series from legend. No more status updates from series to legend.
331 331 */
332 332 void QLegend::disconnectSeries(QSeries *series)
333 333 {
334 334 if (series->type() == QSeries::SeriesTypePie) {
335 335 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
336 336 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
337 337 }
338 338 }
339 339
340 340 /*!
341 341 \internal Creates new markers for \a series. Marker contains the colored rectangle and series name.
342 342 With pie chart, created markers depend on pie slices.
343 343 With bar chart, created markers depend on bar sets.
344 344 */
345 345 void QLegend::createMarkers(QSeries *series)
346 346 {
347 347 switch (series->type())
348 348 {
349 349 case QSeries::SeriesTypeLine: {
350 350 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
351 351 appendMarkers(lineSeries);
352 352 break;
353 353 }
354 354 case QSeries::SeriesTypeArea: {
355 355 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
356 356 appendMarkers(areaSeries->upperSeries());
357 357 if(areaSeries->lowerSeries())
358 358 appendMarkers(areaSeries->lowerSeries());
359 359 break;
360 360 }
361 361 case QSeries::SeriesTypeBar: {
362 362 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
363 363 appendMarkers(barSeries);
364 364 break;
365 365 }
366 366 case QSeries::SeriesTypeStackedBar: {
367 367 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
368 368 appendMarkers(stackedBarSeries);
369 369 break;
370 370 }
371 371 case QSeries::SeriesTypePercentBar: {
372 372 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
373 373 appendMarkers(percentBarSeries);
374 374 break;
375 375 }
376 376 case QSeries::SeriesTypeScatter: {
377 377 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
378 378 appendMarkers(scatterSeries);
379 379 break;
380 380 }
381 381 case QSeries::SeriesTypePie: {
382 382 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
383 383 appendMarkers(pieSeries);
384 384 break;
385 385 }
386 386 case QSeries::SeriesTypeSpline: {
387 387 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
388 388 appendMarkers(splineSeries);
389 389 break;
390 390 }
391 391 default: {
392 392 qWarning()<< "QLegend::createMarkers" << series->type() << "unknown series type.";
393 393 break;
394 394 }
395 395 }
396 396 }
397 397
398 398 /*!
399 399 \internal Helper function. Appends markers from \a series to legend.
400 400 */
401 401 void QLegend::appendMarkers(QXYSeries* series)
402 402 {
403 403 LegendMarker* marker = new LegendMarker(series,this);
404 404 marker->setName(series->name());
405 405 marker->setPen(series->pen());
406 406 marker->setBrush(series->brush());
407 407 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
408 408 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
409 409 m_markers.append(marker);
410 410 childItems().append(marker);
411 411 }
412 412
413 413 /*!
414 414 \internal Helper function. Appends markers from \a series to legend.
415 415 */
416 416 void QLegend::appendMarkers(QBarSeries *series)
417 417 {
418 418 foreach(QBarSet* set, series->barSets()) {
419 419 LegendMarker* marker = new LegendMarker(series, set, this);
420 420 marker->setName(set->name());
421 421 marker->setPen(set->pen());
422 422 marker->setBrush(set->brush());
423 423 connect(marker, SIGNAL(clicked(QBarSet *, Qt::MouseButton)),
424 424 this, SIGNAL(clicked(QBarSet *, Qt::MouseButton)));
425 425 connect(set, SIGNAL(valueChanged()), marker, SLOT(changed()));
426 426 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
427 427 m_markers.append(marker);
428 428 childItems().append(marker);
429 429 }
430 430 }
431 431
432 432 /*!
433 433 \internal Helper function. Appends markers from \a series to legend.
434 434 */
435 435 void QLegend::appendMarkers(QPieSeries *series)
436 436 {
437 437 foreach(QPieSlice* slice, series->slices()) {
438 438 LegendMarker* marker = new LegendMarker(series, slice, this);
439 439 marker->setName(slice->label());
440 440 marker->setPen(slice->pen());
441 441 marker->setBrush(slice->brush());
442 442 connect(marker, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)),
443 443 this, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)));
444 444 connect(slice, SIGNAL(changed()), marker, SLOT(changed()));
445 445 connect(slice, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
446 446 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
447 447 m_markers.append(marker);
448 448 childItems().append(marker);
449 449 }
450 450 }
451 451
452 452 /*!
453 453 \internal Deletes all markers that are created from \a series
454 454 */
455 455 void QLegend::deleteMarkers(QSeries *series)
456 456 {
457 457 // Search all markers that belong to given series and delete them.
458 458 foreach (LegendMarker *m, m_markers) {
459 459 if (m->series() == series) {
460 460 m_markers.removeOne(m);
461 461 delete m;
462 462 }
463 463 }
464 464 }
465 465
466 466 /*!
467 467 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
468 468 If items don't fit, sets the visibility of scroll buttons accordingly.
469 469 Causes legend to be resized.
470 470 */
471 471 void QLegend::updateLayout()
472 472 {
473 473 // Calculate layout for markers and text
474 474 if (m_markers.count() <= 0) {
475 475 // Nothing to do
476 476 return;
477 477 }
478 478
479 479 // Find out widest item.
480 480 QSizeF markerMaxSize = maximumMarkerSize();
481 481 checkFirstMarkerBounds();
482 482
483 483 // Use max height as scroll button size
484 484 rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height()));
485 485
486 486 qreal totalWidth = 0;
487 487 qreal totalHeight = 0;
488 488 switch (m_alignment)
489 489 {
490 490 // Both cases organise items horizontally
491 491 case QLegend::LayoutBottom:
492 492 case QLegend::LayoutTop: {
493 493
494 494 qreal xStep = markerMaxSize.width();
495 495 qreal x = m_pos.x() + m_margin;
496 496 qreal y = m_pos.y() + m_margin;
497 497 int column = 0;
498 498 int maxColumns = 1;
499 499 qreal scrollButtonWidth = 0;
500 500
501 501 // Set correct visibility for scroll scrollbuttons
502 502 if (scrollButtonsVisible()) {
503 503 m_scrollButtonLeft->setVisible(true);
504 504 m_scrollButtonRight->setVisible(true);
505 505 // scrollbuttons visible, so add their width to total width
506 506 totalWidth += (m_scrollButtonLeft->boundingRect().width() + m_margin) * 2;
507 507 scrollButtonWidth = m_scrollButtonLeft->boundingRect().width() + m_margin;
508 508 // start position changes by scrollbutton width
509 509 x += scrollButtonWidth;
510 510 } else {
511 511 m_scrollButtonLeft->setVisible(false);
512 512 m_scrollButtonRight->setVisible(false);
513 513 }
514 514 m_scrollButtonUp->setVisible(false);
515 515 m_scrollButtonDown->setVisible(false);
516 516
517 517 for (int i=0; i < m_markers.count(); i++) {
518 518 LegendMarker *m = m_markers.at(i);
519 519 if (i < mFirstMarker) {
520 520 // Markers before first are not visible.
521 521 m->setVisible(false);
522 522 } else {
523 523 if ((x + xStep + scrollButtonWidth + m_margin) > (m_pos.x() + m_maximumSize.width())) {
524 524 // This marker would go outside legend rect.
525 525 m->setVisible(false);
526 526 } else {
527 527 // This marker is ok
528 528 m->setVisible(true);
529 529 m->setPos(x, y);
530 530 x += xStep;
531 531 column++;
532 532 }
533 533 }
534 534 maxColumns = column;
535 535 }
536 536
537 537 m_scrollButtonLeft->setPos(m_pos.x() + m_margin, y);
538 538 m_scrollButtonRight->setPos(x + m_margin, y);
539 539
540 540 totalWidth += maxColumns * markerMaxSize.width() + m_margin * 2;
541 541 totalHeight = markerMaxSize.height() + m_margin * 2;
542 542
543 543 break;
544 544 }
545 545 // Both cases organize items vertically
546 546 case QLegend::LayoutLeft:
547 547 case QLegend::LayoutRight: {
548 548 qreal yStep = markerMaxSize.height();
549 549 qreal x = m_pos.x() + m_margin;
550 550 qreal y = m_pos.y() + m_margin;
551 551 int row = 1;
552 552 int maxRows = 1;
553 553 qreal scrollButtonHeight = 0;
554 554
555 555 // Set correct visibility for scroll scrollbuttons
556 556 if (scrollButtonsVisible()) {
557 557 m_scrollButtonUp->setVisible(true);
558 558 m_scrollButtonDown->setVisible(true);
559 559 totalHeight += (m_scrollButtonUp->boundingRect().height() + m_margin) * 2; // scrollbuttons visible, so add their height to total height
560 560 scrollButtonHeight = m_scrollButtonUp->boundingRect().height();
561 561 y += scrollButtonHeight + m_margin; // start position changes by scrollbutton height
562 562 } else {
563 563 m_scrollButtonUp->setVisible(false);
564 564 m_scrollButtonDown->setVisible(false);
565 565 }
566 566 m_scrollButtonLeft->setVisible(false);
567 567 m_scrollButtonRight->setVisible(false);
568 568
569 569 for (int i=0; i < m_markers.count(); i++) {
570 570 LegendMarker* m = m_markers.at(i);
571 571 if (i < mFirstMarker) {
572 572 // Markers before first are not visible.
573 573 m->setVisible(false);
574 574 } else {
575 575 if ((y + yStep + scrollButtonHeight) > (m_pos.y() + m_maximumSize.height())) {
576 576 // This marker would go outside legend rect.
577 577 m->setVisible(false);
578 578 } else {
579 579 // This marker is ok
580 580 m->setVisible(true);
581 581 m->setPos(x, y);
582 582 y += yStep;
583 583 row++;
584 584 }
585 585 }
586 586 maxRows = row;
587 587 }
588 588
589 589 m_scrollButtonUp->setPos(m_pos.x() + m_margin, m_pos.y() + m_margin);
590 590 m_scrollButtonDown->setPos(m_pos.x() + m_margin, y + m_margin);
591 591
592 592 totalWidth += markerMaxSize.width() + m_margin * 2;
593 593 totalHeight = maxRows * markerMaxSize.height() + m_margin * 4 + scrollButtonHeight; // TODO: check this
594 594 break;
595 595 }
596 596 default: {
597 597 break;
598 598 }
599 599 }
600 600
601 601 m_size.setWidth(totalWidth);
602 602 m_size.setHeight(totalHeight);
603 603
604 604 update();
605 605 }
606 606
607 607 /*!
608 608 \internal Sets the size of scroll buttons to \a size
609 609 */
610 610 void QLegend::rescaleScrollButtons(const QSize &size)
611 611 {
612 612 QPolygonF left;
613 613 left << QPointF(size.width(), 0) << QPointF(0, size.height() / 2) << QPointF(size.width(), size.height());
614 614 QPolygonF right;
615 615 right << QPointF(0, 0) << QPointF(size.width(), size.height() / 2) << QPointF(0, size.height());
616 616 QPolygonF up;
617 617 up << QPointF(0, size.height()) << QPointF(size.width() / 2,0) << QPointF(size.width(), size.height());
618 618 QPolygonF down;
619 619 down << QPointF(0, 0) << QPointF(size.width() / 2, size.height()) << QPointF(size.width(), 0);
620 620
621 621 m_scrollButtonLeft->setPolygon(left);
622 622 m_scrollButtonRight->setPolygon(right);
623 623 m_scrollButtonUp->setPolygon(up);
624 624 m_scrollButtonDown->setPolygon(down);
625 625 }
626 626
627 627 /*!
628 628 \internal Finds out maximum size of single marker. Marker sizes depend on series names.
629 629 */
630 630 QSizeF QLegend::maximumMarkerSize()
631 631 {
632 632 QSizeF max(0,0);
633 633 foreach (LegendMarker* m, m_markers) {
634 634 if (m->boundingRect().width() > max.width())
635 635 max.setWidth(m->boundingRect().width());
636 636 if (m->boundingRect().height() > max.height())
637 637 max.setHeight(m->boundingRect().height());
638 638 }
639 639 return max;
640 640 }
641 641
642 642 /*!
643 643 \internal Checks that first marker is in acceptable bounds. Bounds range from 0 to (maximum number of markers - visible markers)
644 644 If scrollbuttons are visible, they affect the number of visible markers.
645 645 */
646 646 void QLegend::checkFirstMarkerBounds()
647 647 {
648 648 if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) {
649 649 // Bounds limited by height.
650 650 int max;
651 651 if (scrollButtonsVisible()) {
652 652 max = (m_maximumSize.height() - m_scrollButtonLeft->boundingRect().height() * 2 - m_margin * 4) / maximumMarkerSize().height();
653 653 } else {
654 654 max = m_maximumSize.height() / maximumMarkerSize().height();
655 655 }
656 656
657 657 if (mFirstMarker > m_markers.count() - max)
658 658 mFirstMarker = m_markers.count() - max;
659 659 } else {
660 660 // Bounds limited by width
661 661 int max;
662 662 if (scrollButtonsVisible()) {
663 663 max = (m_maximumSize.width() - m_scrollButtonLeft->boundingRect().width() * 2 - m_margin*4) / maximumMarkerSize().width();
664 664 } else {
665 665 max = m_maximumSize.width() / maximumMarkerSize().width();
666 666 }
667 667
668 668 if (mFirstMarker > m_markers.count() - max)
669 669 mFirstMarker = m_markers.count() - max;
670 670 }
671 671
672 672 if (mFirstMarker < 0)
673 673 mFirstMarker = 0;
674 674 }
675 675
676 676 /*!
677 677 \internal Helper function. Visibility of scroll buttons isn't quite obvious, so helper function clarifies the logic.
678 678 */
679 679 bool QLegend::scrollButtonsVisible()
680 680 {
681 681 // Just a helper to clarify, what the magic below means :)
682 682 if ((m_alignment == QLegend::LayoutTop) || (m_alignment == QLegend::LayoutBottom)) {
683 683 return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width());
684 684 } else if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) {
685 685 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
686 686 }
687 687
688 688 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
689 689 }
690 690
691 691 #include "moc_qlegend.cpp"
692 692
693 693 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now