qlegend.cpp
206 lines
| 5.4 KiB
| text/x-c
|
CppLexer
/ src / qlegend.cpp
|
r524 | #include "qchartglobal.h" | ||
#include "qlegend.h" | ||||
#include "qseries.h" | ||||
|
r547 | #include "legendmarker_p.h" | ||
|
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" | ||||
|
r529 | #include <QPainter> | ||
#include <QPen> | ||||
|
r524 | |||
|
r547 | #include <QGraphicsSceneEvent> | ||
|
r529 | |||
|
r547 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r529 | |||
|
r524 | QLegend::QLegend(QGraphicsItem *parent) | ||
: QGraphicsObject(parent) | ||||
|
r529 | ,mBoundingRect(0,0,1,1) | ||
|
r540 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? | ||
|
r524 | { | ||
} | ||||
void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
|
r540 | painter->setBrush(mBackgroundBrush); | ||
painter->drawRect(mBoundingRect); | ||||
|
r529 | foreach(LegendMarker* m, mMarkers) { | ||
QRectF r = m->boundingRect(); | ||||
|
r565 | painter->setBrush(m->brush()); | ||
|
r547 | painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name()); | ||
|
r524 | } | ||
} | ||||
QRectF QLegend::boundingRect() const | ||||
{ | ||||
return mBoundingRect; | ||||
} | ||||
|
r540 | void QLegend::setBackgroundBrush(const QBrush& brush) | ||
{ | ||||
mBackgroundBrush = brush; | ||||
} | ||||
QBrush QLegend::backgroundBrush() const | ||||
{ | ||||
return mBackgroundBrush; | ||||
} | ||||
|
r524 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) | ||
{ | ||||
mSeriesList.append(series); | ||||
|
r565 | |||
switch (series->type()) | ||||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||||
createMarker(lineSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||||
createMarker(areaSeries->upperSeries()); | ||||
createMarker(areaSeries->lowerSeries()); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
createMarkers(barSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||||
createMarkers(stackedBarSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||||
createMarkers(percentBarSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | ||||
createMarker(scatterSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
createMarkers(pieSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeSpline: { | ||||
QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||||
break; | ||||
} | ||||
default: { | ||||
qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented."; | ||||
break; | ||||
} | ||||
} | ||||
|
r529 | layoutChanged(); | ||
|
r524 | } | ||
void QLegend::handleSeriesRemoved(QSeries* series) | ||||
{ | ||||
|
r565 | // TODO: delete markers, disconnect. | ||
|
r524 | mSeriesList.removeOne(series); | ||
|
r529 | layoutChanged(); | ||
} | ||||
void QLegend::handleGeometryChanged(const QRectF& size) | ||||
{ | ||||
mBoundingRect = size; | ||||
layoutChanged(); | ||||
|
r524 | } | ||
|
r565 | void QLegend::createMarker(QXYSeries* series) | ||
|
r529 | { | ||
|
r565 | LegendMarker* marker = new LegendMarker(series,this); | ||
marker->setName(series->name()); | ||||
marker->setBrush(series->brush()); | ||||
connect(marker,SIGNAL(clicked(QSeries*)),this,SIGNAL(markerClicked(QSeries*))); | ||||
connect(marker,SIGNAL(rightClicked(QSeries*)),this,SIGNAL(markerRightClicked(QSeries*))); | ||||
mMarkers.append(marker); | ||||
childItems().append(marker); | ||||
} | ||||
void QLegend::createMarkers(QBarSeries *series) | ||||
{ | ||||
foreach(QBarSet* s, series->barSets()) { | ||||
LegendMarker* marker = new LegendMarker(series,this); | ||||
marker->setName(s->name()); | ||||
marker->setBrush(s->brush()); | ||||
connect(marker,SIGNAL(clicked(QBarSet*)),this,SIGNAL(markerClicked(QBarSet*))); | ||||
connect(marker,SIGNAL(rightClicked(QBarSet*)),this,SIGNAL(markerRightClicked(QBarSet*))); | ||||
mMarkers.append(marker); | ||||
childItems().append(marker); | ||||
|
r529 | } | ||
|
r565 | } | ||
|
r529 | |||
|
r565 | void QLegend::createMarkers(QPieSeries *series) | ||
{ | ||||
foreach(QPieSlice* s, series->slices()) { | ||||
LegendMarker* marker = new LegendMarker(series,this); | ||||
marker->setName(s->label()); | ||||
marker->setBrush(s->sliceBrush()); | ||||
connect(marker,SIGNAL(clicked(QPieSlice*)),this,SIGNAL(markerClicked(QPieSlice*))); | ||||
connect(marker,SIGNAL(rightClicked(QPieSlice*)),this,SIGNAL(markerRightClicked(QPieSlice*))); | ||||
mMarkers.append(marker); | ||||
childItems().append(marker); | ||||
|
r529 | } | ||
} | ||||
void QLegend::layoutChanged() | ||||
{ | ||||
// Calculate layout for markers and text | ||||
if (mMarkers.count() <= 0) { | ||||
// Nothing to do | ||||
return; | ||||
} | ||||
// TODO: marker defined by series. | ||||
QSizeF markerSize(10,10); | ||||
// TODO: better layout, this is just concept. | ||||
// Leave some space around markers like this: | x x x x | | ||||
|
r540 | qreal steps = mMarkers.count(); | ||
|
r529 | |||
|
r540 | qreal xStep = mBoundingRect.width() / steps; | ||
|
r529 | qreal yStep = mBoundingRect.height() / steps; | ||
|
r540 | qreal x = mBoundingRect.x() + 5; | ||
qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2; | ||||
|
r529 | foreach (LegendMarker* m, mMarkers) { | ||
|
r540 | qDebug() << "marker x:" << x; | ||
qDebug() << "marker y:" << y; | ||||
|
r529 | m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height())); | ||
|
r540 | x += xStep; | ||
|
r529 | } | ||
} | ||||
|
r524 | |||
#include "moc_qlegend.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||