##// END OF EJS Templates
legend fixes
sauimone -
r766:a9c9a730026d
parent child
Show More
@@ -81,7 +81,7 private Q_SLOTS:
81 81 void barsetChanged();
82 82
83 83 protected:
84 BarChartModel *m_internalModel;
84 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
85 85
86 86 QAbstractItemModel* m_model;
87 87 int m_mapCategories;
@@ -9,50 +9,47
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
13 : QGraphicsObject(parent)
14 ,mPos(0,0)
15 ,mSize(0,0)
16 ,mBoundingRect(0,0,0,0)
17 ,mMarkerBoundingRect(0,0,0,0)
18 ,mSeries(series)
19 ,mBarset(0)
20 ,mPieslice(0)
21 ,mType(LegendMarkerTypeSeries)
22 ,mTextItem(new QGraphicsSimpleTextItem(this))
23 {
24 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
25 }
12 LegendMarker::LegendMarker(QSeries *series, QGraphicsItem *parent) : QGraphicsObject(parent),
13 mPos(0,0),
14 mSize(0,0),
15 mBoundingRect(0,0,0,0),
16 mMarkerBoundingRect(0,0,0,0),
17 mSeries(series),
18 mBarset(0),
19 mPieslice(0),
20 mType(LegendMarkerTypeSeries),
21 mTextItem(new QGraphicsSimpleTextItem(this))
22 {
23 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
24 }
26 25
27 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
28 : QGraphicsObject(parent)
29 ,mPos(0,0)
30 ,mSize(0,0)
31 ,mBoundingRect(0,0,0,0)
32 ,mMarkerBoundingRect(0,0,0,0)
33 ,mSeries(series)
34 ,mBarset(barset)
35 ,mPieslice(0)
36 ,mType(LegendMarkerTypeBarset)
37 ,mTextItem(new QGraphicsSimpleTextItem(this))
38 {
39 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
40 }
26 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent) : QGraphicsObject(parent),
27 mPos(0,0),
28 mSize(0,0),
29 mBoundingRect(0,0,0,0),
30 mMarkerBoundingRect(0,0,0,0),
31 mSeries(series),
32 mBarset(barset),
33 mPieslice(0),
34 mType(LegendMarkerTypeBarset),
35 mTextItem(new QGraphicsSimpleTextItem(this))
36 {
37 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
38 }
41 39
42 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
43 : QGraphicsObject(parent)
44 ,mPos(0,0)
45 ,mSize(0,0)
46 ,mBoundingRect(0,0,0,0)
47 ,mMarkerBoundingRect(0,0,0,0)
48 ,mSeries(series)
49 ,mBarset(0)
50 ,mPieslice(pieslice)
51 ,mType(LegendMarkerTypePieslice)
52 ,mTextItem(new QGraphicsSimpleTextItem(this))
53 {
54 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
55 }
40 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent) : QGraphicsObject(parent),
41 mPos(0,0),
42 mSize(0,0),
43 mBoundingRect(0,0,0,0),
44 mMarkerBoundingRect(0,0,0,0),
45 mSeries(series),
46 mBarset(0),
47 mPieslice(pieslice),
48 mType(LegendMarkerTypePieslice),
49 mTextItem(new QGraphicsSimpleTextItem(this))
50 {
51 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
52 }
56 53
57 54 void LegendMarker::setPos(qreal x, qreal y)
58 55 {
@@ -60,7 +57,7 void LegendMarker::setPos(qreal x, qreal y)
60 57 layoutChanged();
61 58 }
62 59
63 void LegendMarker::setPen(const QPen pen)
60 void LegendMarker::setPen(const QPen &pen)
64 61 {
65 62 mPen = pen;
66 63 }
@@ -70,7 +67,7 QPen LegendMarker::pen() const
70 67 return mPen;
71 68 }
72 69
73 void LegendMarker::setBrush(const QBrush brush)
70 void LegendMarker::setBrush(const QBrush &brush)
74 71 {
75 72 mBrush = brush;
76 73 }
@@ -25,16 +25,16 class LegendMarker : public QGraphicsObject
25 25 };
26 26
27 27 public:
28 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
29 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
30 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
28 LegendMarker(QSeries *series, QGraphicsItem *parent = 0);
29 LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent = 0);
30 LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent = 0);
31 31
32 32 void setPos(qreal x, qreal y);
33 33
34 void setPen(const QPen pen);
34 void setPen(const QPen &pen);
35 35 QPen pen() const;
36 36
37 void setBrush(const QBrush brush);
37 void setBrush(const QBrush &brush);
38 38 QBrush brush() const;
39 39
40 40 void setName(const QString name);
@@ -53,9 +53,9 public:
53 53 void mousePressEvent(QGraphicsSceneMouseEvent *event);
54 54
55 55 Q_SIGNALS:
56 void clicked(QSeries* series, Qt::MouseButton button);
57 void clicked(QBarSet* barset, Qt::MouseButton button);
58 void clicked(QPieSlice* pieslice, Qt::MouseButton button);
56 void clicked(QSeries *series, Qt::MouseButton button);
57 void clicked(QBarSet *barset, Qt::MouseButton button);
58 void clicked(QPieSlice *pieslice, Qt::MouseButton button);
59 59
60 60 public Q_SLOTS:
61 61 void changed();
@@ -68,9 +68,9 private:
68 68 QBrush mBrush;
69 69 QPen mPen;
70 70
71 QSeries* mSeries;
72 QBarSet* mBarset;
73 QPieSlice* mPieslice;
71 QSeries *mSeries;
72 QBarSet *mBarset;
73 QPieSlice *mPieslice;
74 74
75 75 LegendMarkerType mType;
76 76 QGraphicsSimpleTextItem mTextItem;
@@ -59,17 +59,6 QLineSeries::~QLineSeries()
59 59 }
60 60
61 61 /*!
62 Sets \a pen used for drawing line connecting points.
63 */
64 void QLineSeries::setLinePen(const QPen &pen)
65 {
66 if (pen != m_pen){
67 m_pen = pen;
68 emit QXYSeries::updated();
69 }
70 }
71
72 /*!
73 62 Sets if data points are \a visible and should be drawn on line.
74 63 */
75 64 void QLineSeries::setPointsVisible(bool visible)
@@ -13,9 +13,6 public:
13 13 QLineSeries(QObject *parent=0);
14 14 virtual ~QLineSeries();
15 15
16 void setLinePen(const QPen &pen);
17 QPen linePen() const {return m_pen;}
18
19 16 void setPointsVisible(bool visible);
20 17 bool pointsVisible() const {return m_pointsVisible;}
21 18
@@ -23,7 +20,6 public: // from QChartSeries
23 20 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
24 21 friend QDebug operator<< (QDebug d, const QLineSeries series);
25 22 private:
26 QPen m_pen;
27 23 bool m_pointsVisible;
28 24
29 25 };
@@ -397,22 +397,22 void QChartPrivate::updateLegendLayout()
397 397 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
398 398 QRectF legendRect;
399 399
400 switch (m_legend->preferredLayout())
400 switch (m_legend->alignment())
401 401 {
402 case QLegend::PreferredLayoutTop: {
402 case QLegend::LayoutTop: {
403 403 // legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height());
404 404 legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height());
405 405 break;
406 406 }
407 case QLegend::PreferredLayoutBottom: {
407 case QLegend::LayoutBottom: {
408 408 legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0);
409 409 break;
410 410 }
411 case QLegend::PreferredLayoutLeft: {
411 case QLegend::LayoutLeft: {
412 412 legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding);
413 413 break;
414 414 }
415 case QLegend::PreferredLayoutRight: {
415 case QLegend::LayoutRight: {
416 416 legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding);
417 417 break;
418 418 }
@@ -37,14 +37,14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37 */
38 38
39 39 /*!
40 \enum QLegend::PreferredLayout
40 \enum QLegend::Layout
41 41
42 42 This enum describes the possible position for legend inside chart.
43 43
44 \value PreferredLayoutTop
45 \value PreferredLayoutBottom
46 \value PreferredLayoutLeft
47 \value PreferredLayoutRight
44 \value LayoutTop
45 \value LayoutBottom
46 \value LayoutLeft
47 \value LayoutRight
48 48 */
49 49
50 50
@@ -67,27 +67,27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
67 67 Constructs the legend object and sets the parent to \a parent
68 68 */
69 69 QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent),
70 m_Pos(0,0),
71 m_Size(0,0),
72 m_MinimumSize(50,20), // TODO: magic numbers
73 m_MaximumSize(150,100),
70 m_pos(0,0),
71 m_size(0,0),
72 m_minimumSize(50,20), // TODO: magic numbers
73 m_maximumSize(150,100),
74 74 m_brush(Qt::darkGray), // TODO: from theme?
75 m_PreferredLayout(QLegend::PreferredLayoutTop),
75 m_alignment(QLegend::LayoutTop),
76 76 mFirstMarker(0),
77 m_Margin(5)
77 m_margin(5)
78 78 {
79 m_ScrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
80 m_ScrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
81 m_ScrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this);
82 m_ScrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this);
79 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
80 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
81 m_scrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this);
82 m_scrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this);
83 83
84 connect(m_ScrollButtonLeft, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
84 connect(m_scrollButtonLeft, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
85 85 this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*)));
86 connect(m_ScrollButtonRight, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
86 connect(m_scrollButtonRight, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
87 87 this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*)));
88 connect(m_ScrollButtonUp, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
88 connect(m_scrollButtonUp, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
89 89 this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*)));
90 connect(m_ScrollButtonDown, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
90 connect(m_scrollButtonDown, SIGNAL(clicked(QGraphicsSceneMouseEvent*)),
91 91 this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*)));
92 92
93 93 setZValue(ChartPresenter::LegendZValue);
@@ -112,7 +112,7 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
112 112 */
113 113 QRectF QLegend::boundingRect() const
114 114 {
115 return QRectF(m_Pos,m_Size);
115 return QRectF(m_pos,m_size);
116 116 }
117 117
118 118 /*!
@@ -156,20 +156,20 QPen QLegend::pen() const
156 156
157 157 /*!
158 158 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
159 \sa QLegend::PreferredLayout
159 \sa QLegend::Layout
160 160 */
161 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
161 void QLegend::setAlignmnent(QLegend::Layout alignment)
162 162 {
163 m_PreferredLayout = preferred;
163 m_alignment = alignment;
164 164 updateLayout();
165 165 }
166 166
167 167 /*!
168 168 Returns the preferred layout for legend
169 169 */
170 QLegend::PreferredLayout QLegend::preferredLayout() const
170 QLegend::Layout QLegend::alignment() const
171 171 {
172 return m_PreferredLayout;
172 return m_alignment;
173 173 }
174 174
175 175 /*!
@@ -177,7 +177,7 QLegend::PreferredLayout QLegend::preferredLayout() const
177 177 */
178 178 QSizeF QLegend::maximumSize() const
179 179 {
180 return m_MaximumSize;
180 return m_maximumSize;
181 181 }
182 182
183 183 /*!
@@ -186,7 +186,7 QSizeF QLegend::maximumSize() const
186 186 */
187 187 void QLegend::setMaximumSize(const QSizeF size)
188 188 {
189 m_MaximumSize = size;
189 m_maximumSize = size;
190 190 updateLayout();
191 191 }
192 192
@@ -195,7 +195,7 void QLegend::setMaximumSize(const QSizeF size)
195 195 */
196 196 QSizeF QLegend::size() const
197 197 {
198 return m_Size;
198 return m_size;
199 199 }
200 200
201 201 /*!
@@ -204,12 +204,12 QSizeF QLegend::size() const
204 204 */
205 205 void QLegend::setSize(const QSizeF size)
206 206 {
207 m_Size = size;
208 if (m_Size.width() > m_MaximumSize.width()) {
209 m_Size.setWidth(m_MaximumSize.width());
207 m_size = size;
208 if (m_size.width() > m_maximumSize.width()) {
209 m_size.setWidth(m_maximumSize.width());
210 210 }
211 if (m_Size.height() > m_MaximumSize.height()) {
212 m_Size.setHeight(m_MaximumSize.height());
211 if (m_size.height() > m_maximumSize.height()) {
212 m_size.setHeight(m_maximumSize.height());
213 213 }
214 214 }
215 215
@@ -218,7 +218,7 void QLegend::setSize(const QSizeF size)
218 218 */
219 219 void QLegend::setPos(const QPointF &pos)
220 220 {
221 m_Pos = pos;
221 m_pos = pos;
222 222 updateLayout();
223 223 }
224 224
@@ -241,8 +241,7 void QLegend::handleSeriesRemoved(QSeries *series)
241 241 {
242 242 disconnectSeries(series);
243 243
244 if (series->type() == QSeries::SeriesTypeArea)
245 {
244 if (series->type() == QSeries::SeriesTypeArea) {
246 245 // This is special case. Area series has upper and lower series, which each have markers
247 246 QAreaSeries* s = static_cast<QAreaSeries *> (series);
248 247 deleteMarkers(s->upperSeries());
@@ -269,7 +268,7 void QLegend::handleAdded(QList<QPieSlice *> slices)
269 268 connect(s, SIGNAL(changed()), marker, SLOT(changed()));
270 269 connect(s, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
271 270 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
272 m_Markers.append(marker);
271 m_markers.append(marker);
273 272 childItems().append(marker);
274 273 }
275 274 updateLayout();
@@ -290,9 +289,8 void QLegend::handleRemoved(QList<QPieSlice *> slices)
290 289 */
291 290 void QLegend::handleMarkerDestroyed()
292 291 {
293 // TODO: what if more than one markers are destroyed and we update layout after first one?
294 292 LegendMarker* m = static_cast<LegendMarker *> (sender());
295 m_Markers.removeOne(m);
293 m_markers.removeOne(m);
296 294 updateLayout();
297 295 }
298 296
@@ -333,47 +331,11 void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event)
333 331 */
334 332 void QLegend::connectSeries(QSeries *series)
335 333 {
336 // Connect relevant signals from series
337 switch (series->type())
338 {
339 case QSeries::SeriesTypeLine: {
340 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
341 break;
342 }
343 case QSeries::SeriesTypeArea: {
344 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
345 break;
346 }
347 case QSeries::SeriesTypeBar: {
348 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
349 break;
350 }
351 case QSeries::SeriesTypeStackedBar: {
352 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
353 break;
354 }
355 case QSeries::SeriesTypePercentBar: {
356 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
357 break;
358 }
359 case QSeries::SeriesTypeScatter: {
360 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
361 break;
362 }
363 case QSeries::SeriesTypePie: {
334 // Connect relevant signals from series. Currently only pie series has interesting signals
335 // TODO: bar chart may have
336 if (series->type() == QSeries::SeriesTypePie) {
364 337 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
365 338 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
366 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
367 break;
368 }
369 case QSeries::SeriesTypeSpline: {
370 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
371 break;
372 }
373 default: {
374 qWarning() << "QLegend::connectSeries" << series->type() << "not implemented.";
375 break;
376 }
377 339 }
378 340 }
379 341
@@ -382,47 +344,9 void QLegend::connectSeries(QSeries *series)
382 344 */
383 345 void QLegend::disconnectSeries(QSeries *series)
384 346 {
385 // Connect relevant signals from series
386 switch (series->type())
387 {
388 case QSeries::SeriesTypeLine: {
389 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
390 break;
391 }
392 case QSeries::SeriesTypeArea: {
393 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
394 break;
395 }
396 case QSeries::SeriesTypeBar: {
397 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
398 break;
399 }
400 case QSeries::SeriesTypeStackedBar: {
401 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
402 break;
403 }
404 case QSeries::SeriesTypePercentBar: {
405 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
406 break;
407 }
408 case QSeries::SeriesTypeScatter: {
409 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
410 break;
411 }
412 case QSeries::SeriesTypePie: {
347 if (series->type() == QSeries::SeriesTypePie) {
413 348 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
414 349 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
415 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
416 break;
417 }
418 case QSeries::SeriesTypeSpline: {
419 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
420 break;
421 }
422 default: {
423 qWarning()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
424 break;
425 }
426 350 }
427 351 }
428 352
@@ -447,44 +371,38 void QLegend::createMarkers(QSeries *series)
447 371 appendMarkers(areaSeries->lowerSeries());
448 372 break;
449 373 }
450
451 374 case QSeries::SeriesTypeBar: {
452 375 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
453 376 appendMarkers(barSeries);
454 377 break;
455 378 }
456
457 379 case QSeries::SeriesTypeStackedBar: {
458 380 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
459 381 appendMarkers(stackedBarSeries);
460 382 break;
461 383 }
462
463 384 case QSeries::SeriesTypePercentBar: {
464 385 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
465 386 appendMarkers(percentBarSeries);
466 387 break;
467 388 }
468
469 389 case QSeries::SeriesTypeScatter: {
470 390 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
471 391 appendMarkers(scatterSeries);
472 392 break;
473 393 }
474
475 394 case QSeries::SeriesTypePie: {
476 395 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
477 396 appendMarkers(pieSeries);
478 397 break;
479 398 }
480
481 399 case QSeries::SeriesTypeSpline: {
482 400 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
483 401 appendMarkers(splineSeries);
484 402 break;
485 403 }
486 404 default: {
487 qWarning()<< "QLegend::createMarkers" << series->type() << "not implemented.";
405 qWarning()<< "QLegend::createMarkers" << series->type() << "unknown series type.";
488 406 break;
489 407 }
490 408 }
@@ -501,7 +419,7 void QLegend::appendMarkers(QXYSeries* series)
501 419 marker->setBrush(series->brush());
502 420 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
503 421 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
504 m_Markers.append(marker);
422 m_markers.append(marker);
505 423 childItems().append(marker);
506 424 }
507 425
@@ -519,7 +437,7 void QLegend::appendMarkers(QBarSeries *series)
519 437 this, SIGNAL(clicked(QBarSet *, Qt::MouseButton)));
520 438 connect(s, SIGNAL(valueChanged()), marker, SLOT(changed()));
521 439 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
522 m_Markers.append(marker);
440 m_markers.append(marker);
523 441 childItems().append(marker);
524 442 }
525 443 }
@@ -539,7 +457,7 void QLegend::appendMarkers(QPieSeries *series)
539 457 connect(s, SIGNAL(changed()), marker, SLOT(changed()));
540 458 connect(s, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
541 459 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
542 m_Markers.append(marker);
460 m_markers.append(marker);
543 461 childItems().append(marker);
544 462 }
545 463 }
@@ -550,9 +468,9 void QLegend::appendMarkers(QPieSeries *series)
550 468 void QLegend::deleteMarkers(QSeries *series)
551 469 {
552 470 // Search all markers that belong to given series and delete them.
553 foreach (LegendMarker *m, m_Markers) {
471 foreach (LegendMarker *m, m_markers) {
554 472 if (m->series() == series) {
555 m_Markers.removeOne(m);
473 m_markers.removeOne(m);
556 474 delete m;
557 475 }
558 476 }
@@ -566,7 +484,7 void QLegend::deleteMarkers(QSeries *series)
566 484 void QLegend::updateLayout()
567 485 {
568 486 // Calculate layout for markers and text
569 if (m_Markers.count() <= 0) {
487 if (m_markers.count() <= 0) {
570 488 // Nothing to do
571 489 return;
572 490 }
@@ -580,42 +498,42 void QLegend::updateLayout()
580 498
581 499 qreal totalWidth = 0;
582 500 qreal totalHeight = 0;
583 switch (m_PreferredLayout)
501 switch (m_alignment)
584 502 {
585 503 // Both cases organise items horizontally
586 case QLegend::PreferredLayoutBottom:
587 case QLegend::PreferredLayoutTop: {
504 case QLegend::LayoutBottom:
505 case QLegend::LayoutTop: {
588 506
589 507 qreal xStep = markerMaxSize.width();
590 qreal x = m_Pos.x() + m_Margin;
591 qreal y = m_Pos.y() + m_Margin;
508 qreal x = m_pos.x() + m_margin;
509 qreal y = m_pos.y() + m_margin;
592 510 int column = 0;
593 511 int maxColumns = 1;
594 512 qreal scrollButtonWidth = 0;
595 513
596 514 // Set correct visibility for scroll scrollbuttons
597 515 if (scrollButtonsVisible()) {
598 m_ScrollButtonLeft->setVisible(true);
599 m_ScrollButtonRight->setVisible(true);
516 m_scrollButtonLeft->setVisible(true);
517 m_scrollButtonRight->setVisible(true);
600 518 // scrollbuttons visible, so add their width to total width
601 totalWidth += (m_ScrollButtonLeft->boundingRect().width() + m_Margin) * 2;
602 scrollButtonWidth = m_ScrollButtonLeft->boundingRect().width() + m_Margin;
519 totalWidth += (m_scrollButtonLeft->boundingRect().width() + m_margin) * 2;
520 scrollButtonWidth = m_scrollButtonLeft->boundingRect().width() + m_margin;
603 521 // start position changes by scrollbutton width
604 522 x += scrollButtonWidth;
605 523 } else {
606 m_ScrollButtonLeft->setVisible(false);
607 m_ScrollButtonRight->setVisible(false);
524 m_scrollButtonLeft->setVisible(false);
525 m_scrollButtonRight->setVisible(false);
608 526 }
609 m_ScrollButtonUp->setVisible(false);
610 m_ScrollButtonDown->setVisible(false);
527 m_scrollButtonUp->setVisible(false);
528 m_scrollButtonDown->setVisible(false);
611 529
612 for (int i=0; i < m_Markers.count(); i++) {
613 LegendMarker *m = m_Markers.at(i);
530 for (int i=0; i < m_markers.count(); i++) {
531 LegendMarker *m = m_markers.at(i);
614 532 if (i < mFirstMarker) {
615 533 // Markers before first are not visible.
616 534 m->setVisible(false);
617 535 } else {
618 if ((x + xStep + scrollButtonWidth + m_Margin) > (m_Pos.x() + m_MaximumSize.width())) {
536 if ((x + xStep + scrollButtonWidth + m_margin) > (m_pos.x() + m_maximumSize.width())) {
619 537 // This marker would go outside legend rect.
620 538 m->setVisible(false);
621 539 } else {
@@ -629,45 +547,45 void QLegend::updateLayout()
629 547 maxColumns = column;
630 548 }
631 549
632 m_ScrollButtonLeft->setPos(m_Pos.x() + m_Margin, y);
633 m_ScrollButtonRight->setPos(x + m_Margin, y);
550 m_scrollButtonLeft->setPos(m_pos.x() + m_margin, y);
551 m_scrollButtonRight->setPos(x + m_margin, y);
634 552
635 totalWidth += maxColumns * markerMaxSize.width() + m_Margin * 2;
636 totalHeight = markerMaxSize.height() + m_Margin * 2;
553 totalWidth += maxColumns * markerMaxSize.width() + m_margin * 2;
554 totalHeight = markerMaxSize.height() + m_margin * 2;
637 555
638 556 break;
639 557 }
640 558 // Both cases organize items vertically
641 case QLegend::PreferredLayoutLeft:
642 case QLegend::PreferredLayoutRight: {
559 case QLegend::LayoutLeft:
560 case QLegend::LayoutRight: {
643 561 qreal yStep = markerMaxSize.height();
644 qreal x = m_Pos.x() + m_Margin;
645 qreal y = m_Pos.y() + m_Margin;
562 qreal x = m_pos.x() + m_margin;
563 qreal y = m_pos.y() + m_margin;
646 564 int row = 1;
647 565 int maxRows = 1;
648 566 qreal scrollButtonHeight = 0;
649 567
650 568 // Set correct visibility for scroll scrollbuttons
651 569 if (scrollButtonsVisible()) {
652 m_ScrollButtonUp->setVisible(true);
653 m_ScrollButtonDown->setVisible(true);
654 totalHeight += (m_ScrollButtonUp->boundingRect().height() + m_Margin) * 2; // scrollbuttons visible, so add their height to total height
655 scrollButtonHeight = m_ScrollButtonUp->boundingRect().height();
656 y += scrollButtonHeight + m_Margin; // start position changes by scrollbutton height
570 m_scrollButtonUp->setVisible(true);
571 m_scrollButtonDown->setVisible(true);
572 totalHeight += (m_scrollButtonUp->boundingRect().height() + m_margin) * 2; // scrollbuttons visible, so add their height to total height
573 scrollButtonHeight = m_scrollButtonUp->boundingRect().height();
574 y += scrollButtonHeight + m_margin; // start position changes by scrollbutton height
657 575 } else {
658 m_ScrollButtonUp->setVisible(false);
659 m_ScrollButtonDown->setVisible(false);
576 m_scrollButtonUp->setVisible(false);
577 m_scrollButtonDown->setVisible(false);
660 578 }
661 m_ScrollButtonLeft->setVisible(false);
662 m_ScrollButtonRight->setVisible(false);
579 m_scrollButtonLeft->setVisible(false);
580 m_scrollButtonRight->setVisible(false);
663 581
664 for (int i=0; i < m_Markers.count(); i++) {
665 LegendMarker* m = m_Markers.at(i);
582 for (int i=0; i < m_markers.count(); i++) {
583 LegendMarker* m = m_markers.at(i);
666 584 if (i < mFirstMarker) {
667 585 // Markers before first are not visible.
668 586 m->setVisible(false);
669 587 } else {
670 if ((y + yStep + scrollButtonHeight) > (m_Pos.y() + m_MaximumSize.height())) {
588 if ((y + yStep + scrollButtonHeight) > (m_pos.y() + m_maximumSize.height())) {
671 589 // This marker would go outside legend rect.
672 590 m->setVisible(false);
673 591 } else {
@@ -681,11 +599,11 void QLegend::updateLayout()
681 599 maxRows = row;
682 600 }
683 601
684 m_ScrollButtonUp->setPos(m_Pos.x() + m_Margin, m_Pos.y() + m_Margin);
685 m_ScrollButtonDown->setPos(m_Pos.x() + m_Margin, y + m_Margin);
602 m_scrollButtonUp->setPos(m_pos.x() + m_margin, m_pos.y() + m_margin);
603 m_scrollButtonDown->setPos(m_pos.x() + m_margin, y + m_margin);
686 604
687 totalWidth += markerMaxSize.width() + m_Margin * 2;
688 totalHeight = maxRows * markerMaxSize.height() + m_Margin * 4 + scrollButtonHeight; // TODO: check this
605 totalWidth += markerMaxSize.width() + m_margin * 2;
606 totalHeight = maxRows * markerMaxSize.height() + m_margin * 4 + scrollButtonHeight; // TODO: check this
689 607 break;
690 608 }
691 609 default: {
@@ -693,8 +611,8 void QLegend::updateLayout()
693 611 }
694 612 }
695 613
696 m_Size.setWidth(totalWidth);
697 m_Size.setHeight(totalHeight);
614 m_size.setWidth(totalWidth);
615 m_size.setHeight(totalHeight);
698 616
699 617 update();
700 618 }
@@ -713,10 +631,10 void QLegend::rescaleScrollButtons(const QSize &size)
713 631 QPolygonF down;
714 632 down << QPointF(0, 0) << QPointF(size.width() / 2, size.height()) << QPointF(size.width(), 0);
715 633
716 m_ScrollButtonLeft->setPolygon(left);
717 m_ScrollButtonRight->setPolygon(right);
718 m_ScrollButtonUp->setPolygon(up);
719 m_ScrollButtonDown->setPolygon(down);
634 m_scrollButtonLeft->setPolygon(left);
635 m_scrollButtonRight->setPolygon(right);
636 m_scrollButtonUp->setPolygon(up);
637 m_scrollButtonDown->setPolygon(down);
720 638 }
721 639
722 640 /*!
@@ -725,7 +643,7 void QLegend::rescaleScrollButtons(const QSize &size)
725 643 QSizeF QLegend::maximumMarkerSize()
726 644 {
727 645 QSizeF max(0,0);
728 foreach (LegendMarker* m, m_Markers) {
646 foreach (LegendMarker* m, m_markers) {
729 647 if (m->boundingRect().width() > max.width())
730 648 max.setWidth(m->boundingRect().width());
731 649 if (m->boundingRect().height() > max.height())
@@ -740,28 +658,28 QSizeF QLegend::maximumMarkerSize()
740 658 */
741 659 void QLegend::checkFirstMarkerBounds()
742 660 {
743 if ((m_PreferredLayout == QLegend::PreferredLayoutLeft) || (m_PreferredLayout == QLegend::PreferredLayoutRight)) {
661 if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) {
744 662 // Bounds limited by height.
745 663 int max;
746 664 if (scrollButtonsVisible()) {
747 max = (m_MaximumSize.height() - m_ScrollButtonLeft->boundingRect().height() * 2 - m_Margin * 4) / maximumMarkerSize().height();
665 max = (m_maximumSize.height() - m_scrollButtonLeft->boundingRect().height() * 2 - m_margin * 4) / maximumMarkerSize().height();
748 666 } else {
749 max = m_MaximumSize.height() / maximumMarkerSize().height();
667 max = m_maximumSize.height() / maximumMarkerSize().height();
750 668 }
751 669
752 if (mFirstMarker > m_Markers.count() - max)
753 mFirstMarker = m_Markers.count() - max;
670 if (mFirstMarker > m_markers.count() - max)
671 mFirstMarker = m_markers.count() - max;
754 672 } else {
755 673 // Bounds limited by width
756 674 int max;
757 675 if (scrollButtonsVisible()) {
758 max = (m_MaximumSize.width() - m_ScrollButtonLeft->boundingRect().width() * 2 - m_Margin*4) / maximumMarkerSize().width();
676 max = (m_maximumSize.width() - m_scrollButtonLeft->boundingRect().width() * 2 - m_margin*4) / maximumMarkerSize().width();
759 677 } else {
760 max = m_MaximumSize.width() / maximumMarkerSize().width();
678 max = m_maximumSize.width() / maximumMarkerSize().width();
761 679 }
762 680
763 if (mFirstMarker > m_Markers.count() - max)
764 mFirstMarker = m_Markers.count() - max;
681 if (mFirstMarker > m_markers.count() - max)
682 mFirstMarker = m_markers.count() - max;
765 683 }
766 684
767 685 if (mFirstMarker < 0)
@@ -774,13 +692,13 void QLegend::checkFirstMarkerBounds()
774 692 bool QLegend::scrollButtonsVisible()
775 693 {
776 694 // Just a helper to clarify, what the magic below means :)
777 if ((m_PreferredLayout == QLegend::PreferredLayoutTop) || (m_PreferredLayout == QLegend::PreferredLayoutBottom)) {
778 return (maximumMarkerSize().width() * m_Markers.count() + m_Margin * 2 > m_MaximumSize.width());
779 } else if ((m_PreferredLayout == QLegend::PreferredLayoutLeft) || (m_PreferredLayout == QLegend::PreferredLayoutRight)) {
780 return (maximumMarkerSize().height() * m_Markers.count() + m_Margin * 2 > m_MaximumSize.height());
695 if ((m_alignment == QLegend::LayoutTop) || (m_alignment == QLegend::LayoutBottom)) {
696 return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width());
697 } else if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) {
698 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
781 699 }
782 700
783 return (maximumMarkerSize().height() * m_Markers.count() + m_Margin * 2 > m_MaximumSize.height());
701 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
784 702 }
785 703
786 704 #include "moc_qlegend.cpp"
@@ -18,16 +18,18 class QPieSeries;
18 18 class LegendScrollButton;
19 19 class QSeries;
20 20
21 // TODO: This as widget
21 22 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
22 23 {
23 24 Q_OBJECT
24 25 public:
25 26
26 enum PreferredLayout {
27 PreferredLayoutTop,
28 PreferredLayoutBottom,
29 PreferredLayoutLeft,
30 PreferredLayoutRight
27 // We only support these alignments (for now)
28 enum Layout {
29 LayoutTop = Qt::AlignTop,
30 LayoutBottom = Qt::AlignBottom,
31 LayoutLeft = Qt::AlignLeft,
32 LayoutRight = Qt::AlignRight
31 33 };
32 34
33 35 explicit QLegend(QGraphicsItem *parent = 0);
@@ -41,8 +43,8 public:
41 43 void setPen(const QPen &pen);
42 44 QPen pen() const;
43 45
44 void setPreferredLayout(QLegend::PreferredLayout preferred);
45 QLegend::PreferredLayout preferredLayout() const;
46 void setAlignmnent(QLegend::Layout alignment);
47 QLegend::Layout alignment() const;
46 48
47 49 QSizeF maximumSize() const;
48 50 void setMaximumSize(const QSizeF size);
@@ -82,25 +84,25 private:
82 84 void checkFirstMarkerBounds();
83 85 bool scrollButtonsVisible();
84 86
85 QPointF m_Pos;
86 QSizeF m_Size;
87 QSizeF m_MinimumSize;
88 QSizeF m_MaximumSize;
87 QPointF m_pos;
88 QSizeF m_size;
89 QSizeF m_minimumSize;
90 QSizeF m_maximumSize;
89 91
90 QList<LegendMarker *> m_Markers;
92 QList<LegendMarker *> m_markers;
91 93
92 94 QBrush m_brush;
93 95 QPen m_pen;
94 QLegend::PreferredLayout m_PreferredLayout;
96 QLegend::Layout m_alignment;
95 97
96 98 int mFirstMarker;
97 99
98 LegendScrollButton *m_ScrollButtonLeft;
99 LegendScrollButton *m_ScrollButtonRight;
100 LegendScrollButton *m_ScrollButtonUp;
101 LegendScrollButton *m_ScrollButtonDown;
100 LegendScrollButton *m_scrollButtonLeft;
101 LegendScrollButton *m_scrollButtonRight;
102 LegendScrollButton *m_scrollButtonUp;
103 LegendScrollButton *m_scrollButtonDown;
102 104
103 qreal m_Margin;
105 qreal m_margin;
104 106 // <--- PIMPL
105 107 };
106 108
General Comments 0
You need to be logged in to leave comments. Login now