##// END OF EJS Templates
layout fix to legend. Documented legend functions.
sauimone -
r724:2a4cd4554920
parent child
Show More
@@ -60,6 +60,16 void LegendMarker::setPos(qreal x, qreal y)
60 layoutChanged();
60 layoutChanged();
61 }
61 }
62
62
63 void LegendMarker::setPen(const QPen pen)
64 {
65 mPen = pen;
66 }
67
68 QPen LegendMarker::pen() const
69 {
70 return mPen;
71 }
72
63 void LegendMarker::setBrush(const QBrush brush)
73 void LegendMarker::setBrush(const QBrush brush)
64 {
74 {
65 mBrush = brush;
75 mBrush = brush;
@@ -91,6 +101,7 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
91 Q_UNUSED(option)
101 Q_UNUSED(option)
92 Q_UNUSED(widget)
102 Q_UNUSED(widget)
93
103
104 painter->setPen(mPen);
94 painter->setBrush(mBrush);
105 painter->setBrush(mBrush);
95 painter->drawRect(mMarkerBoundingRect);
106 painter->drawRect(mMarkerBoundingRect);
96 }
107 }
@@ -112,7 +123,7 void LegendMarker::layoutChanged()
112
123
113 mMarkerBoundingRect = QRectF(mPos.x() + margin, mPos.y() + margin, markerSize.width(),markerSize.height());
124 mMarkerBoundingRect = QRectF(mPos.x() + margin, mPos.y() + margin, markerSize.width(),markerSize.height());
114
125
115 mTextItem.setPos(mPos.x() + markerSize.width() + 2 * margin, mPos.y() + margin );
126 mTextItem.setPos(mPos.x() + markerSize.width() + 2 * margin, mPos.y() + margin);
116 }
127 }
117
128
118 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
129 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
@@ -4,6 +4,7
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
5 #include <QGraphicsObject>
6 #include <QBrush>
6 #include <QBrush>
7 #include <QPen>
7 #include <QGraphicsSimpleTextItem>
8 #include <QGraphicsSimpleTextItem>
8
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -30,6 +31,9 public:
30
31
31 void setPos(qreal x, qreal y);
32 void setPos(qreal x, qreal y);
32
33
34 void setPen(const QPen pen);
35 QPen pen() const;
36
33 void setBrush(const QBrush brush);
37 void setBrush(const QBrush brush);
34 QBrush brush() const;
38 QBrush brush() const;
35
39
@@ -62,6 +66,8 private:
62 QRectF mBoundingRect;
66 QRectF mBoundingRect;
63 QRectF mMarkerBoundingRect;
67 QRectF mMarkerBoundingRect;
64 QBrush mBrush;
68 QBrush mBrush;
69 QPen mPen;
70
65 QSeries* mSeries;
71 QSeries* mSeries;
66 QBarSet* mBarset;
72 QBarSet* mBarset;
67 QPieSlice* mPieslice;
73 QPieSlice* mPieslice;
@@ -350,19 +350,20 void QChart::updateLegendLayout()
350 switch (m_legend->preferredLayout())
350 switch (m_legend->preferredLayout())
351 {
351 {
352 case QLegend::PreferredLayoutTop:{
352 case QLegend::PreferredLayoutTop:{
353 legendRect = m_rect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height());
353 // legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height());
354 legendRect = plotRect.adjusted(0,0,0,-m_padding - plotRect.height());
354 break;
355 break;
355 }
356 }
356 case QLegend::PreferredLayoutBottom: {
357 case QLegend::PreferredLayoutBottom: {
357 legendRect = m_rect.adjusted(m_padding,m_padding + plotRect.height(),-m_padding,0);
358 legendRect = plotRect.adjusted(m_padding,m_padding + plotRect.height(),-m_padding,0);
358 break;
359 break;
359 }
360 }
360 case QLegend::PreferredLayoutLeft: {
361 case QLegend::PreferredLayoutLeft: {
361 legendRect = m_rect.adjusted(0,m_padding,-m_padding - plotRect.width(),-m_padding);
362 legendRect = plotRect.adjusted(0,m_padding,-m_padding - plotRect.width(),-m_padding);
362 break;
363 break;
363 }
364 }
364 case QLegend::PreferredLayoutRight: {
365 case QLegend::PreferredLayoutRight: {
365 legendRect = m_rect.adjusted(m_padding + plotRect.width(),m_padding,0,-m_padding);
366 legendRect = plotRect.adjusted(m_padding + plotRect.width(),m_padding,0,-m_padding);
366 break;
367 break;
367 }
368 }
368 default: {
369 default: {
@@ -22,6 +22,36
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 /*!
26 \enum QLegend::PreferredLayout
27
28 This enum describes the possible position for legend inside chart.
29
30 \value PreferredLayoutTop
31 \value PreferredLayoutBottom
32 \value PreferredLayoutLeft
33 \value PreferredLayoutRight
34 */
35
36
37 /*!
38 \fn void clicked(QSeries* series, Qt::MouseButton button)
39 \brief Notifies when series has been clicked on legend \a series \a button
40 */
41
42 /*!
43 \fn void clicked(QBarSet* barset, Qt::MouseButton button);
44 \brief Notifies when barset has been clicked on legend \a barset \a button
45 */
46
47 /*!
48 \fn void clicked(QPieSlice* slice, Qt::MouseButton button);
49 \brief Notifies when pie slice has been clicked on legend \a slice \a button
50 */
51
52 /*!
53 Constructs the legend object and sets the parent to \a parent
54 */
25 QLegend::QLegend(QGraphicsItem *parent)
55 QLegend::QLegend(QGraphicsItem *parent)
26 : QGraphicsObject(parent)
56 : QGraphicsObject(parent)
27 ,mPos(0,0)
57 ,mPos(0,0)
@@ -48,22 +78,31 QLegend::QLegend(QGraphicsItem *parent)
48 setZValue(ChartPresenter::LegendZValue);
78 setZValue(ChartPresenter::LegendZValue);
49 }
79 }
50
80
81 /*!
82 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
83 */
51 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
84 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
85 {
53 Q_UNUSED(option)
86 Q_UNUSED(option)
54 Q_UNUSED(widget)
87 Q_UNUSED(widget)
55
88
56 painter->setOpacity(0.8);
89 painter->setOpacity(opacity());
57 painter->setPen(m_pen);
90 painter->setPen(m_pen);
58 painter->setBrush(m_brush);
91 painter->setBrush(m_brush);
59 painter->drawRect(boundingRect());
92 painter->drawRect(boundingRect());
60 }
93 }
61
94
95 /*!
96 Bounding rect of legend.
97 */
62 QRectF QLegend::boundingRect() const
98 QRectF QLegend::boundingRect() const
63 {
99 {
64 return QRectF(mPos,mSize);
100 return QRectF(mPos,mSize);
65 }
101 }
66
102
103 /*!
104 Sets the \a brush of legend. Brush affects the background of legend.
105 */
67 void QLegend::setBrush(const QBrush& brush)
106 void QLegend::setBrush(const QBrush& brush)
68 {
107 {
69 if(m_brush!=brush){
108 if(m_brush!=brush){
@@ -72,11 +111,17 void QLegend::setBrush(const QBrush& brush)
72 }
111 }
73 }
112 }
74
113
114 /*!
115 Returns the brush used by legend.
116 */
75 QBrush QLegend::brush() const
117 QBrush QLegend::brush() const
76 {
118 {
77 return m_brush;
119 return m_brush;
78 }
120 }
79
121
122 /*!
123 Sets the \a pen of legend. Pen affects the legend borders.
124 */
80 void QLegend::setPen(const QPen& pen)
125 void QLegend::setPen(const QPen& pen)
81 {
126 {
82 if(m_pen!=pen){
127 if(m_pen!=pen){
@@ -85,33 +130,63 void QLegend::setPen(const QPen& pen)
85 }
130 }
86 }
131 }
87
132
133 /*!
134 Returns the pen used by legend
135 */
136
88 QPen QLegend::pen() const
137 QPen QLegend::pen() const
89 {
138 {
90 return m_pen;
139 return m_pen;
91 }
140 }
92
141
142 /*!
143 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
144 sa \QLegend::PreferredLayout
145 */
93 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
146 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
94 {
147 {
95 mPreferredLayout = preferred;
148 mPreferredLayout = preferred;
96 updateLayout();
149 updateLayout();
97 }
150 }
98
151
152 /*!
153 Returns the preferred layout for legend
154 */
99 QLegend::PreferredLayout QLegend::preferredLayout() const
155 QLegend::PreferredLayout QLegend::preferredLayout() const
100 {
156 {
101 return mPreferredLayout;
157 return mPreferredLayout;
102 }
158 }
103
159
160 /*!
161 Returns the maximum size of legend.
162 */
104 QSizeF QLegend::maximumSize() const
163 QSizeF QLegend::maximumSize() const
105 {
164 {
106 return mMaximumSize;
165 return mMaximumSize;
107 }
166 }
108
167
168 /*!
169 Sets the maximum \a size for legend. The legend can't grow bigger than this size. If there are
170 more series than legend can fit to this size, scroll buttons are displayed.
171 */
109 void QLegend::setMaximumSize(const QSizeF size)
172 void QLegend::setMaximumSize(const QSizeF size)
110 {
173 {
111 mMaximumSize = size;
174 mMaximumSize = size;
112 updateLayout();
175 updateLayout();
113 }
176 }
114
177
178 /*!
179 Returns the current size of legend.
180 */
181 QSizeF QLegend::size() const
182 {
183 return mSize;
184 }
185
186 /*!
187 Sets the \a size of legend. If size is bigger than maximum size of legend, the legend is resized to the maximum size.
188 \sa setMmaximumSize()
189 */
115 void QLegend::setSize(const QSizeF size)
190 void QLegend::setSize(const QSizeF size)
116 {
191 {
117 mSize = size;
192 mSize = size;
@@ -123,12 +198,18 void QLegend::setSize(const QSizeF size)
123 }
198 }
124 }
199 }
125
200
201 /*!
202 Sets position of legend to \a pos
203 */
126 void QLegend::setPos(const QPointF &pos)
204 void QLegend::setPos(const QPointF &pos)
127 {
205 {
128 mPos = pos;
206 mPos = pos;
129 updateLayout();
207 updateLayout();
130 }
208 }
131
209
210 /*!
211 \internal \a series \a domain Should be called when series is added to chart.
212 */
132 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
213 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
133 {
214 {
134 Q_UNUSED(domain)
215 Q_UNUSED(domain)
@@ -138,6 +219,9 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
138 updateLayout();
219 updateLayout();
139 }
220 }
140
221
222 /*!
223 \internal \a series Should be called when series is removed from chart.
224 */
141 void QLegend::handleSeriesRemoved(QSeries* series)
225 void QLegend::handleSeriesRemoved(QSeries* series)
142 {
226 {
143 disconnectSeries(series);
227 disconnectSeries(series);
@@ -155,6 +239,9 void QLegend::handleSeriesRemoved(QSeries* series)
155 updateLayout();
239 updateLayout();
156 }
240 }
157
241
242 /*!
243 \internal \a slices Should be called when slices are added to pie chart.
244 */
158 void QLegend::handleAdded(QList<QPieSlice*> slices)
245 void QLegend::handleAdded(QList<QPieSlice*> slices)
159 {
246 {
160 QPieSeries* series = static_cast<QPieSeries*> (sender());
247 QPieSeries* series = static_cast<QPieSeries*> (sender());
@@ -172,6 +259,10 void QLegend::handleAdded(QList<QPieSlice*> slices)
172 updateLayout();
259 updateLayout();
173 }
260 }
174
261
262 /*!
263 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
264 because removed slices are also deleted and we listen destroyed signal
265 */
175 void QLegend::handleRemoved(QList<QPieSlice *> slices)
266 void QLegend::handleRemoved(QList<QPieSlice *> slices)
176 {
267 {
177 Q_UNUSED(slices)
268 Q_UNUSED(slices)
@@ -180,6 +271,9 void QLegend::handleRemoved(QList<QPieSlice *> slices)
180 }
271 }
181
272
182
273
274 /*!
275 \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed
276 */
183 void QLegend::handleMarkerDestroyed()
277 void QLegend::handleMarkerDestroyed()
184 {
278 {
185 // TODO: what if more than one markers are destroyed and we update layout after first one?
279 // TODO: what if more than one markers are destroyed and we update layout after first one?
@@ -188,11 +282,13 void QLegend::handleMarkerDestroyed()
188 updateLayout();
282 updateLayout();
189 }
283 }
190
284
285 /*!
286 \internal \a event Handles clicked signals from scroll buttons
287 */
191 void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event)
288 void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event)
192 {
289 {
193 Q_UNUSED(event); // Maybe later somethin happens with right click...
290 Q_UNUSED(event); // Maybe later something happens with right click...
194
291
195 // TODO: detect sender object. scroll to appropiate direction.
196 LegendScrollButton* scrollButton = static_cast<LegendScrollButton*> (sender());
292 LegendScrollButton* scrollButton = static_cast<LegendScrollButton*> (sender());
197 Q_ASSERT(scrollButton);
293 Q_ASSERT(scrollButton);
198
294
@@ -201,13 +297,13 void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event)
201 case LegendScrollButton::ScrollButtonIdUp: {
297 case LegendScrollButton::ScrollButtonIdUp: {
202 // Lower limit is same in these cases
298 // Lower limit is same in these cases
203 mFirstMarker--;
299 mFirstMarker--;
204 checkMarkerBounds();
300 checkFirstMarkerBounds();
205 break;
301 break;
206 }
302 }
207 case LegendScrollButton::ScrollButtonIdRight:
303 case LegendScrollButton::ScrollButtonIdRight:
208 case LegendScrollButton::ScrollButtonIdDown: {
304 case LegendScrollButton::ScrollButtonIdDown: {
209 mFirstMarker++;
305 mFirstMarker++;
210 checkMarkerBounds();
306 checkFirstMarkerBounds();
211 break;
307 break;
212 }
308 }
213 default: {
309 default: {
@@ -217,6 +313,10 void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event)
217 updateLayout();
313 updateLayout();
218 }
314 }
219
315
316 /*!
317 \internal Connects the \a series to legend. Legend listens changes in series, for example pie slices added / removed.
318 Not all series notify about events
319 */
220 void QLegend::connectSeries(QSeries *series)
320 void QLegend::connectSeries(QSeries *series)
221 {
321 {
222 // Connect relevant signals from series
322 // Connect relevant signals from series
@@ -263,6 +363,9 void QLegend::connectSeries(QSeries *series)
263 }
363 }
264 }
364 }
265
365
366 /*!
367 \internal Disconnects \a series from legend. No more status updates from series to legend.
368 */
266 void QLegend::disconnectSeries(QSeries *series)
369 void QLegend::disconnectSeries(QSeries *series)
267 {
370 {
268 // Connect relevant signals from series
371 // Connect relevant signals from series
@@ -309,6 +412,11 void QLegend::disconnectSeries(QSeries *series)
309 }
412 }
310 }
413 }
311
414
415 /*!
416 \internal Creates new markers for \a series. Marker contains the colored rectangle and series name.
417 With pie chart, created markers depend on pie slices.
418 With bar chart, created markers depend on bar sets.
419 */
312 void QLegend::createMarkers(QSeries *series)
420 void QLegend::createMarkers(QSeries *series)
313 {
421 {
314 switch (series->type())
422 switch (series->type())
@@ -368,10 +476,14 void QLegend::createMarkers(QSeries *series)
368 }
476 }
369 }
477 }
370
478
479 /*!
480 \internal Helper function. Appends markers from \a series to legend.
481 */
371 void QLegend::appendMarkers(QXYSeries* series)
482 void QLegend::appendMarkers(QXYSeries* series)
372 {
483 {
373 LegendMarker* marker = new LegendMarker(series,this);
484 LegendMarker* marker = new LegendMarker(series,this);
374 marker->setName(series->name());
485 marker->setName(series->name());
486 marker->setPen(series->pen());
375 marker->setBrush(series->brush());
487 marker->setBrush(series->brush());
376 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
488 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
377 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
489 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
@@ -379,11 +491,15 void QLegend::appendMarkers(QXYSeries* series)
379 childItems().append(marker);
491 childItems().append(marker);
380 }
492 }
381
493
494 /*!
495 \internal Helper function. Appends markers from \a series to legend.
496 */
382 void QLegend::appendMarkers(QBarSeries *series)
497 void QLegend::appendMarkers(QBarSeries *series)
383 {
498 {
384 foreach(QBarSet* s, series->barSets()) {
499 foreach(QBarSet* s, series->barSets()) {
385 LegendMarker* marker = new LegendMarker(series,s,this);
500 LegendMarker* marker = new LegendMarker(series,s,this);
386 marker->setName(s->name());
501 marker->setName(s->name());
502 marker->setPen(s->pen());
387 marker->setBrush(s->brush());
503 marker->setBrush(s->brush());
388 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
504 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
389 connect(s,SIGNAL(valueChanged()),marker,SLOT(changed()));
505 connect(s,SIGNAL(valueChanged()),marker,SLOT(changed()));
@@ -393,11 +509,15 void QLegend::appendMarkers(QBarSeries *series)
393 }
509 }
394 }
510 }
395
511
512 /*!
513 \internal Helper function. Appends markers from \a series to legend.
514 */
396 void QLegend::appendMarkers(QPieSeries *series)
515 void QLegend::appendMarkers(QPieSeries *series)
397 {
516 {
398 foreach(QPieSlice* s, series->slices()) {
517 foreach(QPieSlice* s, series->slices()) {
399 LegendMarker* marker = new LegendMarker(series,s,this);
518 LegendMarker* marker = new LegendMarker(series,s,this);
400 marker->setName(s->label());
519 marker->setName(s->label());
520 marker->setPen(s->slicePen());
401 marker->setBrush(s->sliceBrush());
521 marker->setBrush(s->sliceBrush());
402 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
522 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
403 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
523 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
@@ -408,6 +528,9 void QLegend::appendMarkers(QPieSeries *series)
408 }
528 }
409 }
529 }
410
530
531 /*!
532 \internal Deletes all markers that are created from \a series
533 */
411 void QLegend::deleteMarkers(QSeries *series)
534 void QLegend::deleteMarkers(QSeries *series)
412 {
535 {
413 // Search all markers that belong to given series and delete them.
536 // Search all markers that belong to given series and delete them.
@@ -419,6 +542,11 void QLegend::deleteMarkers(QSeries *series)
419 }
542 }
420 }
543 }
421
544
545 /*!
546 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
547 If items don't fit, sets the visibility of scroll buttons accordingly.
548 Causes legend to be resized.
549 */
422 void QLegend::updateLayout()
550 void QLegend::updateLayout()
423 {
551 {
424 // Calculate layout for markers and text
552 // Calculate layout for markers and text
@@ -426,14 +554,15 void QLegend::updateLayout()
426 // Nothing to do
554 // Nothing to do
427 return;
555 return;
428 }
556 }
429 checkMarkerBounds();
430
557
431 // Find out widest item.
558 // Find out widest item.
432 QSizeF markerMaxSize = maximumMarkerSize();
559 QSizeF markerMaxSize = maximumMarkerSize();
560 checkFirstMarkerBounds();
433
561
434 // Use max height as scroll button size
562 // Use max height as scroll button size
435 rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height()));
563 rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height()));
436
564
565
437 qreal totalWidth = 0;
566 qreal totalWidth = 0;
438 qreal totalHeight = 0;
567 qreal totalHeight = 0;
439 switch (mPreferredLayout)
568 switch (mPreferredLayout)
@@ -447,15 +576,15 void QLegend::updateLayout()
447 qreal y = mPos.y() + mMargin;
576 qreal y = mPos.y() + mMargin;
448 int column = 0;
577 int column = 0;
449 int maxColumns = 1;
578 int maxColumns = 1;
450 qreal scrollButtonPadding = 0;
579 qreal scrollButtonWidth = 0;
451
580
452 // Set correct visibility for scroll scrollbuttons
581 // Set correct visibility for scroll scrollbuttons
453 if (scrollButtonsVisible()) {
582 if (scrollButtonsVisible()) {
454 mScrollButtonLeft->setVisible(true);
583 mScrollButtonLeft->setVisible(true);
455 mScrollButtonRight->setVisible(true);
584 mScrollButtonRight->setVisible(true);
456 totalWidth += (mScrollButtonLeft->boundingRect().width() + mMargin) * 2; // scrollbuttons visible, so add their width to total width
585 totalWidth += (mScrollButtonLeft->boundingRect().width() + mMargin) * 2; // scrollbuttons visible, so add their width to total width
457 x += mScrollButtonLeft->boundingRect().width() + mMargin; // start position changes by scrollbutton width
586 scrollButtonWidth = mScrollButtonLeft->boundingRect().width() + mMargin;
458 scrollButtonPadding = mScrollButtonLeft->boundingRect().width();
587 x += scrollButtonWidth; // start position changes by scrollbutton width
459 } else {
588 } else {
460 mScrollButtonLeft->setVisible(false);
589 mScrollButtonLeft->setVisible(false);
461 mScrollButtonRight->setVisible(false);
590 mScrollButtonRight->setVisible(false);
@@ -469,7 +598,7 void QLegend::updateLayout()
469 // Markers before first are not visible.
598 // Markers before first are not visible.
470 m->setVisible(false);
599 m->setVisible(false);
471 } else {
600 } else {
472 if ((x + xStep + scrollButtonPadding) > (mPos.x() + mMaximumSize.width())) {
601 if ((x + xStep + scrollButtonWidth + mMargin) > (mPos.x() + mMaximumSize.width())) {
473 // This marker would go outside legend rect.
602 // This marker would go outside legend rect.
474 m->setVisible(false);
603 m->setVisible(false);
475 } else {
604 } else {
@@ -499,15 +628,15 void QLegend::updateLayout()
499 qreal y = mPos.y() + mMargin;
628 qreal y = mPos.y() + mMargin;
500 int row = 1;
629 int row = 1;
501 int maxRows = 1;
630 int maxRows = 1;
502 qreal scrollButtonPadding = 0;
631 qreal scrollButtonHeight = 0;
503
632
504 // Set correct visibility for scroll scrollbuttons
633 // Set correct visibility for scroll scrollbuttons
505 if (scrollButtonsVisible()) {
634 if (scrollButtonsVisible()) {
506 mScrollButtonUp->setVisible(true);
635 mScrollButtonUp->setVisible(true);
507 mScrollButtonDown->setVisible(true);
636 mScrollButtonDown->setVisible(true);
508 totalHeight += (mScrollButtonUp->boundingRect().height() + mMargin) * 2; // scrollbuttons visible, so add their height to total height
637 totalHeight += (mScrollButtonUp->boundingRect().height() + mMargin) * 2; // scrollbuttons visible, so add their height to total height
509 y += mScrollButtonUp->boundingRect().height() + mMargin; // start position changes by scrollbutton height
638 scrollButtonHeight = mScrollButtonUp->boundingRect().height();
510 scrollButtonPadding = mScrollButtonUp->boundingRect().height();
639 y += scrollButtonHeight + mMargin; // start position changes by scrollbutton height
511 } else {
640 } else {
512 mScrollButtonUp->setVisible(false);
641 mScrollButtonUp->setVisible(false);
513 mScrollButtonDown->setVisible(false);
642 mScrollButtonDown->setVisible(false);
@@ -521,7 +650,7 void QLegend::updateLayout()
521 // Markers before first are not visible.
650 // Markers before first are not visible.
522 m->setVisible(false);
651 m->setVisible(false);
523 } else {
652 } else {
524 if ((y + yStep + scrollButtonPadding) > (mPos.y() + mMaximumSize.height())) {
653 if ((y + yStep + scrollButtonHeight) > (mPos.y() + mMaximumSize.height())) {
525 // This marker would go outside legend rect.
654 // This marker would go outside legend rect.
526 m->setVisible(false);
655 m->setVisible(false);
527 } else {
656 } else {
@@ -539,38 +668,7 void QLegend::updateLayout()
539 mScrollButtonDown->setPos(mPos.x() + mMargin, y + mMargin);
668 mScrollButtonDown->setPos(mPos.x() + mMargin, y + mMargin);
540
669
541 totalWidth += markerMaxSize.width() + mMargin * 2;
670 totalWidth += markerMaxSize.width() + mMargin * 2;
542 totalHeight = maxRows * markerMaxSize.height() + mMargin * 4 + scrollButtonPadding; // TODO: check this
671 totalHeight = maxRows * markerMaxSize.height() + mMargin * 4 + scrollButtonHeight; // TODO: check this
543
544 /*
545 qreal yStep = markerMaxSize.height();
546 qreal x = mPos.x() + mMargin;
547 qreal y = mPos.y() + mMargin;
548 int row = 0;
549 int maxRows = 1;
550 int maxColumns = 1;
551 for (int i=0; i<mMarkers.count(); i++) {
552 LegendMarker* m = mMarkers.at(i);
553 if (i<mFirstMarker) {
554 // Markers before first are not visible.
555 m->setVisible(false);
556 } else {
557 if ((y + markerMaxSize.height() + mMargin*2) > (mPos.y() + mMaximumSize.height())) {
558 // This marker would go outside legend rect.
559 m->setVisible(false);
560 } else {
561 // This marker is ok
562 m->setVisible(true);
563 maxRows = row;
564 m->setPos(x,y);
565 y += yStep;
566 row++;
567 }
568 }
569 maxRows = row;
570 }
571 totalWidth = maxColumns * markerMaxSize.width() + mMargin * 2;
572 totalHeight = maxRows * markerMaxSize.height() + mMargin * 2;
573 */
574 break;
672 break;
575 }
673 }
576 default: {
674 default: {
@@ -584,6 +682,9 void QLegend::updateLayout()
584 update();
682 update();
585 }
683 }
586
684
685 /*!
686 \internal Sets the size of scroll buttons to \a size
687 */
587 void QLegend::rescaleScrollButtons(const QSize &size)
688 void QLegend::rescaleScrollButtons(const QSize &size)
588 {
689 {
589 QPolygonF left;
690 QPolygonF left;
@@ -601,6 +702,9 void QLegend::rescaleScrollButtons(const QSize &size)
601 mScrollButtonDown->setPolygon(down);
702 mScrollButtonDown->setPolygon(down);
602 }
703 }
603
704
705 /*!
706 \internal Finds out maximum size of single marker. Marker sizes depend on series names.
707 */
604 QSizeF QLegend::maximumMarkerSize()
708 QSizeF QLegend::maximumMarkerSize()
605 {
709 {
606 QSizeF max(0,0);
710 QSizeF max(0,0);
@@ -615,13 +719,17 QSizeF QLegend::maximumMarkerSize()
615 return max;
719 return max;
616 }
720 }
617
721
618 void QLegend::checkMarkerBounds()
722 /*!
723 \internal Checks that first marker is in acceptable bounds. Bounds range from 0 to (maximum number of markers - visible markers)
724 If scrollbuttons are visible, they affect the number of visible markers.
725 */
726 void QLegend::checkFirstMarkerBounds()
619 {
727 {
620 if ((mPreferredLayout == QLegend::PreferredLayoutLeft) || (mPreferredLayout == QLegend::PreferredLayoutRight)) {
728 if ((mPreferredLayout == QLegend::PreferredLayoutLeft) || (mPreferredLayout == QLegend::PreferredLayoutRight)) {
621 // Bounds limited by height
729 // Bounds limited by height.
622 int max;
730 int max;
623 if (scrollButtonsVisible()) {
731 if (scrollButtonsVisible()) {
624 max = (mMaximumSize.height() - mScrollButtonLeft->boundingRect().height() * 2 - mMargin*2) / maximumMarkerSize().height();
732 max = (mMaximumSize.height() - mScrollButtonLeft->boundingRect().height() * 2 - mMargin*4) / maximumMarkerSize().height();
625 } else {
733 } else {
626 max = mMaximumSize.height() / maximumMarkerSize().height();
734 max = mMaximumSize.height() / maximumMarkerSize().height();
627 }
735 }
@@ -633,7 +741,7 void QLegend::checkMarkerBounds()
633 // Bounds limited by width
741 // Bounds limited by width
634 int max;
742 int max;
635 if (scrollButtonsVisible()) {
743 if (scrollButtonsVisible()) {
636 max = (mMaximumSize.width() - mScrollButtonLeft->boundingRect().width() * 2 - mMargin*2) / maximumMarkerSize().width();
744 max = (mMaximumSize.width() - mScrollButtonLeft->boundingRect().width() * 2 - mMargin*4) / maximumMarkerSize().width();
637 } else {
745 } else {
638 max = mMaximumSize.width() / maximumMarkerSize().width();
746 max = mMaximumSize.width() / maximumMarkerSize().width();
639 }
747 }
@@ -648,13 +756,19 void QLegend::checkMarkerBounds()
648 }
756 }
649 }
757 }
650
758
759 /*!
760 \internal Helper function. Visibility of scroll buttons isn't quite obvious, so helper function clarifies the logic.
761 */
651 bool QLegend::scrollButtonsVisible()
762 bool QLegend::scrollButtonsVisible()
652 {
763 {
653 // Just a helper to clarify, what the magic below means :)
764 // Just a helper to clarify, what the magic below means :)
654 if ((mPreferredLayout == QLegend::PreferredLayoutTop) || (mPreferredLayout == QLegend::PreferredLayoutBottom)) {
765 if ((mPreferredLayout == QLegend::PreferredLayoutTop) || (mPreferredLayout == QLegend::PreferredLayoutBottom)) {
655 return (maximumMarkerSize().width() * mMarkers.count() + mMargin * 3 > mMaximumSize.width());
766 return (maximumMarkerSize().width() * mMarkers.count() + mMargin * 2 > mMaximumSize.width());
767 } else if ((mPreferredLayout == QLegend::PreferredLayoutLeft) || (mPreferredLayout == QLegend::PreferredLayoutRight)) {
768 return (maximumMarkerSize().height() * mMarkers.count() + mMargin * 2 > mMaximumSize.height());
656 }
769 }
657 return (maximumMarkerSize().height() * mMarkers.count() + mMargin * 3 > mMaximumSize.height());
770
771 return (maximumMarkerSize().height() * mMarkers.count() + mMargin * 2 > mMaximumSize.height());
658 }
772 }
659
773
660 #include "moc_qlegend.cpp"
774 #include "moc_qlegend.cpp"
@@ -45,6 +45,7 public:
45 QSizeF maximumSize() const;
45 QSizeF maximumSize() const;
46 void setMaximumSize(const QSizeF size);
46 void setMaximumSize(const QSizeF size);
47
47
48 QSizeF size() const;
48 void setSize(const QSizeF size);
49 void setSize(const QSizeF size);
49 void setPos(const QPointF &pos);
50 void setPos(const QPointF &pos);
50
51
@@ -55,12 +56,14 signals:
55 void clicked(QPieSlice* slice, Qt::MouseButton button);
56 void clicked(QPieSlice* slice, Qt::MouseButton button);
56
57
57 public slots:
58 public slots:
59 // PIMPL --->
58 void handleSeriesAdded(QSeries* series,Domain* domain);
60 void handleSeriesAdded(QSeries* series,Domain* domain);
59 void handleSeriesRemoved(QSeries* series);
61 void handleSeriesRemoved(QSeries* series);
60 void handleAdded(QList<QPieSlice*> slices);
62 void handleAdded(QList<QPieSlice*> slices);
61 void handleRemoved(QList<QPieSlice*> slices);
63 void handleRemoved(QList<QPieSlice*> slices);
62 void handleMarkerDestroyed();
64 void handleMarkerDestroyed();
63 void handleScrollButtonClicked(QGraphicsSceneMouseEvent* event);
65 void handleScrollButtonClicked(QGraphicsSceneMouseEvent* event);
66 // PIMPL <---
64
67
65 private:
68 private:
66 // PIMPL --->
69 // PIMPL --->
@@ -71,13 +74,11 private:
71 void appendMarkers(QBarSeries* series);
74 void appendMarkers(QBarSeries* series);
72 void appendMarkers(QPieSeries* series);
75 void appendMarkers(QPieSeries* series);
73 void deleteMarkers(QSeries* series);
76 void deleteMarkers(QSeries* series);
74 // void layoutChanged(); // This tries to fit all items to legend
77 void updateLayout();
75 void updateLayout(); // New version of layout. Fits items only to row or column and adds scrollbars.
76 void rescaleScrollButtons(const QSize& size);
78 void rescaleScrollButtons(const QSize& size);
77 QSizeF maximumMarkerSize();
79 QSizeF maximumMarkerSize();
78 void checkMarkerBounds();
80 void checkFirstMarkerBounds();
79 bool scrollButtonsVisible();
81 bool scrollButtonsVisible();
80 // void updateScrollButtonsLayout();
81
82
82 QPointF mPos;
83 QPointF mPos;
83 QSizeF mSize;
84 QSizeF mSize;
General Comments 0
You need to be logged in to leave comments. Login now