qlegend.cpp
342 lines
| 9.3 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" | ||||
|
r626 | #include "chartpresenter_p.h" | ||
|
r529 | #include <QPainter> | ||
#include <QPen> | ||||
|
r524 | |||
|
r547 | #include <QGraphicsSceneEvent> | ||
|
r529 | |||
|
r547 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r529 | |||
|
r524 | QLegend::QLegend(QGraphicsItem *parent) | ||
: QGraphicsObject(parent) | ||||
|
r626 | ,mPos(0,0) | ||
,mSize(0,0) | ||||
|
r582 | ,mMinimumSize(50,20) // TODO: magic numbers | ||
|
r616 | ,mMaximumSize(150,100) | ||
|
r626 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? | ||
|
r616 | ,mPreferredLayout(QLegend::PreferredLayoutVertical) | ||
|
r524 | { | ||
|
r626 | // setVisible(false); | ||
setZValue(ChartPresenter::LegendZValue); | ||||
|
r524 | } | ||
void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
|
r626 | painter->setOpacity(0.5); | ||
|
r540 | painter->setBrush(mBackgroundBrush); | ||
|
r626 | painter->drawRect(boundingRect()); | ||
|
r524 | } | ||
QRectF QLegend::boundingRect() const | ||||
{ | ||||
|
r626 | return QRectF(mPos,mSize); | ||
|
r524 | } | ||
|
r540 | void QLegend::setBackgroundBrush(const QBrush& brush) | ||
{ | ||||
mBackgroundBrush = brush; | ||||
} | ||||
QBrush QLegend::backgroundBrush() const | ||||
{ | ||||
return mBackgroundBrush; | ||||
} | ||||
|
r616 | void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred) | ||
{ | ||||
mPreferredLayout = preferred; | ||||
layoutChanged(); | ||||
} | ||||
|
r626 | QSizeF QLegend::maximumSize() const | ||
|
r582 | { | ||
|
r626 | return mMaximumSize; | ||
|
r582 | } | ||
|
r626 | void QLegend::setMaximumSize(const QSizeF size) | ||
|
r582 | { | ||
|
r626 | mMaximumSize = size; | ||
} | ||||
void QLegend::setSize(const QSizeF size) | ||||
{ | ||||
mSize = size; | ||||
if (mSize.width() > mMaximumSize.width()) { | ||||
mSize.setWidth(mMaximumSize.width()); | ||||
} | ||||
if (mSize.height() > mMaximumSize.height()) { | ||||
mSize.setHeight(mMaximumSize.height()); | ||||
} | ||||
} | ||||
void QLegend::setPos(const QPointF &pos) | ||||
{ | ||||
mPos = pos; | ||||
|
r582 | } | ||
|
r611 | void QLegend::handleSeriesAdded(QSeries* series, Domain* domain) | ||
|
r524 | { | ||
|
r611 | Q_UNUSED(domain) | ||
|
r524 | mSeriesList.append(series); | ||
|
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::createMarkers(QSeries *series) | ||||
{ | ||||
|
r565 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||||
|
r586 | appendMarkers(lineSeries); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||||
|
r586 | appendMarkers(areaSeries->upperSeries()); | ||
|
r572 | if(areaSeries->lowerSeries()) | ||
|
r586 | appendMarkers(areaSeries->lowerSeries()); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
|
r586 | appendMarkers(barSeries); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||||
|
r586 | appendMarkers(stackedBarSeries); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||||
|
r586 | appendMarkers(percentBarSeries); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | ||||
|
r586 | appendMarkers(scatterSeries); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
|
r586 | appendMarkers(pieSeries); | ||
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeSpline: { | ||||
QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||||
|
r586 | appendMarkers(splineSeries); | ||
|
r565 | break; | ||
} | ||||
default: { | ||||
|
r586 | qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented."; | ||
|
r565 | break; | ||
} | ||||
} | ||||
|
r576 | } | ||
|
r586 | void QLegend::appendMarkers(QXYSeries* series) | ||
|
r529 | { | ||
|
r565 | LegendMarker* marker = new LegendMarker(series,this); | ||
marker->setName(series->name()); | ||||
marker->setBrush(series->brush()); | ||||
|
r567 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); | ||
|
r565 | mMarkers.append(marker); | ||
childItems().append(marker); | ||||
} | ||||
|
r586 | void QLegend::appendMarkers(QBarSeries *series) | ||
|
r565 | { | ||
foreach(QBarSet* s, series->barSets()) { | ||||
|
r576 | LegendMarker* marker = new LegendMarker(series,s,this); | ||
|
r565 | marker->setName(s->name()); | ||
marker->setBrush(s->brush()); | ||||
|
r567 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); | ||
|
r587 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | ||
|
r565 | mMarkers.append(marker); | ||
childItems().append(marker); | ||||
|
r529 | } | ||
|
r565 | } | ||
|
r529 | |||
|
r586 | void QLegend::appendMarkers(QPieSeries *series) | ||
|
r565 | { | ||
foreach(QPieSlice* s, series->slices()) { | ||||
|
r576 | LegendMarker* marker = new LegendMarker(series,s,this); | ||
|
r565 | marker->setName(s->label()); | ||
marker->setBrush(s->sliceBrush()); | ||||
|
r567 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); | ||
|
r587 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | ||
|
r565 | mMarkers.append(marker); | ||
childItems().append(marker); | ||||
|
r529 | } | ||
} | ||||
|
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; | ||||
} | ||||
} | ||||
} | ||||
|
r529 | void QLegend::layoutChanged() | ||
{ | ||||
// Calculate layout for markers and text | ||||
if (mMarkers.count() <= 0) { | ||||
// Nothing to do | ||||
return; | ||||
} | ||||
|
r626 | // Find out widest item. | ||
qreal itemMaxWidth = 0; | ||||
qreal itemMaxHeight = 0; | ||||
foreach (LegendMarker* m, mMarkers) { | ||||
if (m->boundingRect().width() > itemMaxWidth) { | ||||
itemMaxWidth = m->boundingRect().width(); | ||||
} | ||||
if (m->boundingRect().height() > itemMaxHeight) { | ||||
itemMaxHeight = m->boundingRect().height(); | ||||
} | ||||
} | ||||
int maxHorizontalItems = boundingRect().width() / itemMaxWidth; | ||||
int maxVerticalItems = boundingRect().height() / itemMaxHeight; | ||||
|
r616 | |||
|
r626 | if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) { | ||
// TODO: overlapping layout | ||||
qDebug() << "Warning. Not enough space to layout all legend items properly."; | ||||
} | ||||
|
r616 | |||
|
r626 | qreal margin = 5; | ||
qreal totalWidth = 0; | ||||
qreal totalHeight = 0; | ||||
|
r616 | switch (mPreferredLayout) | ||
{ | ||||
case QLegend::PreferredLayoutHorizontal: { | ||||
|
r626 | /* | ||
qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); | ||||
if (xStep > itemMaxWidth) { | ||||
xStep = itemMaxWidth; | ||||
} | ||||
qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); | ||||
if (yStep > itemMaxHeight) { | ||||
yStep = itemMaxHeight; | ||||
}*/ | ||||
qreal xStep = itemMaxWidth; | ||||
qreal yStep = itemMaxHeight; | ||||
qreal x = mPos.x() + margin; | ||||
qreal y = mPos.y() + margin; | ||||
int row = 1; | ||||
int column = 0; | ||||
int maxRows = 1; | ||||
int maxColumns = 1; | ||||
|
r616 | foreach (LegendMarker* m, mMarkers) { | ||
|
r626 | maxRows = row; | ||
m->setPos(x,y); | ||||
|
r616 | x += xStep; | ||
|
r626 | column++; | ||
if (column > maxColumns) { | ||||
maxColumns = column; | ||||
} | ||||
if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) { | ||||
x = mPos.x() + margin; | ||||
y += yStep; | ||||
row++; | ||||
column = 0; | ||||
} | ||||
|
r616 | } | ||
|
r626 | totalWidth = maxColumns * itemMaxWidth + margin * 2; | ||
totalHeight = maxRows * itemMaxHeight + margin * 2; | ||||
|
r616 | break; | ||
|
r529 | } | ||
|
r616 | case QLegend::PreferredLayoutVertical: { | ||
|
r626 | /* | ||
qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); | ||||
if (xStep > itemMaxWidth) { | ||||
xStep = itemMaxWidth; | ||||
|
r616 | } | ||
|
r626 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); | ||
if (yStep > itemMaxHeight) { | ||||
yStep = itemMaxHeight; | ||||
}*/ | ||||
qreal xStep = itemMaxWidth; | ||||
qreal yStep = itemMaxHeight; | ||||
qreal x = mPos.x() + margin; | ||||
qreal y = mPos.y() + margin; | ||||
int row = 0; | ||||
int column = 1; | ||||
int maxRows = 1; | ||||
int maxColumns = 1; | ||||
|
r616 | foreach (LegendMarker* m, mMarkers) { | ||
|
r626 | maxColumns = column; | ||
m->setPos(x,y); | ||||
|
r616 | y += yStep; | ||
|
r626 | row++; | ||
if (row > maxRows) { | ||||
maxRows = row; | ||||
} | ||||
if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) { | ||||
y = mPos.y() + margin; | ||||
x += xStep; | ||||
column++; | ||||
row = 0; | ||||
} | ||||
|
r616 | } | ||
|
r626 | totalWidth = maxColumns * itemMaxWidth + margin * 2; | ||
totalHeight = maxRows * itemMaxHeight + margin * 2; | ||||
|
r616 | break; | ||
} | ||||
default: { | ||||
break; | ||||
} | ||||
} | ||||
|
r626 | mSize.setWidth(totalWidth); | ||
mSize.setHeight(totalHeight); | ||||
|
r529 | } | ||
|
r524 | #include "moc_qlegend.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||