qlegend.cpp
217 lines
| 5.7 KiB
| text/x-c
|
CppLexer
/ src / qlegend.cpp
sauimone
|
r524 | #include "qchartglobal.h" | ||
#include "qlegend.h" | ||||
#include "qseries.h" | ||||
sauimone
|
r547 | #include "legendmarker_p.h" | ||
sauimone
|
r565 | #include "qxyseries.h" | ||
#include "qlineseries.h" | ||||
#include "qareaseries.h" | ||||
#include "qscatterseries.h" | ||||
#include "qsplineseries.h" | ||||
#include "qbarseries.h" | ||||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
#include "qbarset.h" | ||||
#include "qpieseries.h" | ||||
#include "qpieslice.h" | ||||
sauimone
|
r529 | #include <QPainter> | ||
#include <QPen> | ||||
sauimone
|
r524 | |||
sauimone
|
r547 | #include <QGraphicsSceneEvent> | ||
sauimone
|
r529 | |||
sauimone
|
r547 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
sauimone
|
r529 | |||
sauimone
|
r524 | QLegend::QLegend(QGraphicsItem *parent) | ||
: QGraphicsObject(parent) | ||||
sauimone
|
r529 | ,mBoundingRect(0,0,1,1) | ||
sauimone
|
r540 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? | ||
sauimone
|
r582 | ,mMinimumSize(50,20) // TODO: magic numbers | ||
sauimone
|
r524 | { | ||
sauimone
|
r596 | setVisible(false); | ||
sauimone
|
r524 | } | ||
void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
sauimone
|
r540 | painter->setBrush(mBackgroundBrush); | ||
painter->drawRect(mBoundingRect); | ||||
sauimone
|
r524 | } | ||
QRectF QLegend::boundingRect() const | ||||
{ | ||||
return mBoundingRect; | ||||
} | ||||
sauimone
|
r540 | void QLegend::setBackgroundBrush(const QBrush& brush) | ||
{ | ||||
mBackgroundBrush = brush; | ||||
} | ||||
QBrush QLegend::backgroundBrush() const | ||||
{ | ||||
return mBackgroundBrush; | ||||
} | ||||
sauimone
|
r582 | QSizeF QLegend::minimumSize() const | ||
{ | ||||
return mMinimumSize; | ||||
} | ||||
void QLegend::setMinimumSize(const QSizeF size) | ||||
{ | ||||
mMinimumSize = size; | ||||
} | ||||
sauimone
|
r524 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) | ||
{ | ||||
mSeriesList.append(series); | ||||
sauimone
|
r586 | createMarkers(series); | ||
layoutChanged(); | ||||
} | ||||
void QLegend::handleSeriesRemoved(QSeries* series) | ||||
{ | ||||
if (series->type() == QSeries::SeriesTypeArea) | ||||
{ | ||||
// This is special case. Area series has upper and lower series, which each have markers | ||||
QAreaSeries* s = static_cast<QAreaSeries*> (series); | ||||
deleteMarkers(s->upperSeries()); | ||||
deleteMarkers(s->lowerSeries()); | ||||
} else { | ||||
deleteMarkers(series); | ||||
} | ||||
mSeriesList.removeOne(series); | ||||
layoutChanged(); | ||||
} | ||||
void QLegend::handleGeometryChanged(const QRectF& size) | ||||
{ | ||||
mBoundingRect = size; | ||||
layoutChanged(); | ||||
} | ||||
sauimone
|
r565 | |||
sauimone
|
r586 | void QLegend::createMarkers(QSeries *series) | ||
{ | ||||
sauimone
|
r565 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||||
sauimone
|
r586 | appendMarkers(lineSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||||
sauimone
|
r586 | appendMarkers(areaSeries->upperSeries()); | ||
Michal Klocek
|
r572 | if(areaSeries->lowerSeries()) | ||
sauimone
|
r586 | appendMarkers(areaSeries->lowerSeries()); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
sauimone
|
r586 | appendMarkers(barSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||||
sauimone
|
r586 | appendMarkers(stackedBarSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||||
sauimone
|
r586 | appendMarkers(percentBarSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | ||||
sauimone
|
r586 | appendMarkers(scatterSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
sauimone
|
r586 | appendMarkers(pieSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeSpline: { | ||||
QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||||
sauimone
|
r586 | appendMarkers(splineSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
default: { | ||||
sauimone
|
r586 | qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented."; | ||
sauimone
|
r565 | break; | ||
} | ||||
} | ||||
sauimone
|
r576 | } | ||
sauimone
|
r586 | void QLegend::appendMarkers(QXYSeries* series) | ||
sauimone
|
r529 | { | ||
sauimone
|
r565 | LegendMarker* marker = new LegendMarker(series,this); | ||
marker->setName(series->name()); | ||||
marker->setBrush(series->brush()); | ||||
sauimone
|
r567 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); | ||
sauimone
|
r565 | mMarkers.append(marker); | ||
childItems().append(marker); | ||||
} | ||||
sauimone
|
r586 | void QLegend::appendMarkers(QBarSeries *series) | ||
sauimone
|
r565 | { | ||
foreach(QBarSet* s, series->barSets()) { | ||||
sauimone
|
r576 | LegendMarker* marker = new LegendMarker(series,s,this); | ||
sauimone
|
r565 | marker->setName(s->name()); | ||
marker->setBrush(s->brush()); | ||||
sauimone
|
r567 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); | ||
sauimone
|
r587 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | ||
sauimone
|
r565 | mMarkers.append(marker); | ||
childItems().append(marker); | ||||
sauimone
|
r529 | } | ||
sauimone
|
r565 | } | ||
sauimone
|
r529 | |||
sauimone
|
r586 | void QLegend::appendMarkers(QPieSeries *series) | ||
sauimone
|
r565 | { | ||
foreach(QPieSlice* s, series->slices()) { | ||||
sauimone
|
r576 | LegendMarker* marker = new LegendMarker(series,s,this); | ||
sauimone
|
r565 | marker->setName(s->label()); | ||
marker->setBrush(s->sliceBrush()); | ||||
sauimone
|
r567 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); | ||
sauimone
|
r587 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | ||
sauimone
|
r565 | mMarkers.append(marker); | ||
childItems().append(marker); | ||||
sauimone
|
r529 | } | ||
} | ||||
sauimone
|
r586 | void QLegend::deleteMarkers(QSeries *series) | ||
{ | ||||
// Search all markers that belong to given series and delete them. | ||||
foreach (LegendMarker *m, mMarkers) { | ||||
if (m->series() == series) { | ||||
mMarkers.removeOne(m); | ||||
delete m; | ||||
} | ||||
} | ||||
} | ||||
sauimone
|
r529 | void QLegend::layoutChanged() | ||
{ | ||||
// Calculate layout for markers and text | ||||
if (mMarkers.count() <= 0) { | ||||
// Nothing to do | ||||
return; | ||||
} | ||||
sauimone
|
r540 | qreal steps = mMarkers.count(); | ||
qreal xStep = mBoundingRect.width() / steps; | ||||
sauimone
|
r585 | qreal x=mBoundingRect.x(); | ||
qreal y = mBoundingRect.y() + (mBoundingRect.height()/4); | ||||
sauimone
|
r529 | foreach (LegendMarker* m, mMarkers) { | ||
sauimone
|
r585 | m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2)); | ||
sauimone
|
r540 | x += xStep; | ||
sauimone
|
r529 | } | ||
} | ||||
sauimone
|
r524 | #include "moc_qlegend.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||