qlegend.cpp
706 lines
| 21.9 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
|
r716 | #include "legendscrollbutton_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
|
r626 | #include "chartpresenter_p.h" | ||
sauimone
|
r529 | #include <QPainter> | ||
#include <QPen> | ||||
sauimone
|
r524 | |||
sauimone
|
r547 | #include <QGraphicsSceneEvent> | ||
sauimone
|
r529 | |||
sauimone
|
r547 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
sauimone
|
r529 | |||
sauimone
|
r728 | /*! | ||
\class QLegend | ||||
\brief part of QtCommercial chart API. | ||||
QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when | ||||
series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and | ||||
handle the drawing manually. | ||||
User isn't supposed to create or delete legend objects, but can reference it via QChart class. | ||||
\mainclass | ||||
\sa QChart, QSeries | ||||
*/ | ||||
sauimone
|
r724 | /*! | ||
sauimone
|
r766 | \enum QLegend::Layout | ||
sauimone
|
r724 | |||
This enum describes the possible position for legend inside chart. | ||||
sauimone
|
r766 | \value LayoutTop | ||
\value LayoutBottom | ||||
\value LayoutLeft | ||||
\value LayoutRight | ||||
sauimone
|
r724 | */ | ||
/*! | ||||
sauimone
|
r728 | \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button) | ||
sauimone
|
r724 | \brief Notifies when series has been clicked on legend \a series \a button | ||
*/ | ||||
/*! | ||||
sauimone
|
r728 | \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button) | ||
sauimone
|
r724 | \brief Notifies when barset has been clicked on legend \a barset \a button | ||
*/ | ||||
/*! | ||||
sauimone
|
r728 | \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button) | ||
sauimone
|
r724 | \brief Notifies when pie slice has been clicked on legend \a slice \a button | ||
*/ | ||||
/*! | ||||
Constructs the legend object and sets the parent to \a parent | ||||
*/ | ||||
sauimone
|
r762 | QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent), | ||
sauimone
|
r783 | m_margin(5), | ||
sauimone
|
r766 | m_pos(0,0), | ||
m_minimumSize(50,20), // TODO: magic numbers | ||||
m_maximumSize(150,100), | ||||
sauimone
|
r783 | m_size(m_minimumSize), | ||
sauimone
|
r785 | m_brush(Qt::darkGray), // TODO: default should come from theme | ||
sauimone
|
r766 | m_alignment(QLegend::LayoutTop), | ||
sauimone
|
r783 | mFirstMarker(0) | ||
sauimone
|
r762 | { | ||
sauimone
|
r766 | m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this); | ||
m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this); | ||||
m_scrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this); | ||||
m_scrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this); | ||||
sauimone
|
r762 | |||
sauimone
|
r766 | connect(m_scrollButtonLeft, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), | ||
Tero Ahola
|
r737 | this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); | ||
sauimone
|
r766 | connect(m_scrollButtonRight, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), | ||
Tero Ahola
|
r737 | this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); | ||
sauimone
|
r766 | connect(m_scrollButtonUp, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), | ||
Tero Ahola
|
r737 | this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); | ||
sauimone
|
r766 | connect(m_scrollButtonDown, SIGNAL(clicked(QGraphicsSceneMouseEvent*)), | ||
Tero Ahola
|
r737 | this, SLOT(handleScrollButtonClicked(QGraphicsSceneMouseEvent*))); | ||
sauimone
|
r716 | |||
sauimone
|
r626 | setZValue(ChartPresenter::LegendZValue); | ||
sauimone
|
r524 | } | ||
sauimone
|
r724 | /*! | ||
Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. | ||||
*/ | ||||
sauimone
|
r524 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
sauimone
|
r724 | painter->setOpacity(opacity()); | ||
Michal Klocek
|
r645 | painter->setPen(m_pen); | ||
painter->setBrush(m_brush); | ||||
sauimone
|
r785 | // painter->drawRect(boundingRect()); | ||
sauimone
|
r524 | } | ||
sauimone
|
r724 | /*! | ||
Bounding rect of legend. | ||||
*/ | ||||
sauimone
|
r524 | QRectF QLegend::boundingRect() const | ||
{ | ||||
sauimone
|
r766 | return QRectF(m_pos,m_size); | ||
sauimone
|
r524 | } | ||
sauimone
|
r724 | /*! | ||
Sets the \a brush of legend. Brush affects the background of legend. | ||||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::setBrush(const QBrush &brush) | ||
sauimone
|
r540 | { | ||
Tero Ahola
|
r737 | if (m_brush != brush) { | ||
Michal Klocek
|
r645 | m_brush = brush; | ||
update(); | ||||
} | ||||
} | ||||
sauimone
|
r724 | /*! | ||
Returns the brush used by legend. | ||||
*/ | ||||
Michal Klocek
|
r645 | QBrush QLegend::brush() const | ||
{ | ||||
return m_brush; | ||||
} | ||||
sauimone
|
r724 | /*! | ||
Sets the \a pen of legend. Pen affects the legend borders. | ||||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::setPen(const QPen &pen) | ||
Michal Klocek
|
r645 | { | ||
Tero Ahola
|
r737 | if (m_pen != pen) { | ||
Michal Klocek
|
r645 | m_pen = pen; | ||
update(); | ||||
} | ||||
sauimone
|
r540 | } | ||
sauimone
|
r724 | /*! | ||
Returns the pen used by legend | ||||
*/ | ||||
Michal Klocek
|
r645 | QPen QLegend::pen() const | ||
sauimone
|
r540 | { | ||
Michal Klocek
|
r645 | return m_pen; | ||
sauimone
|
r540 | } | ||
sauimone
|
r724 | /*! | ||
Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart. | ||||
sauimone
|
r766 | \sa QLegend::Layout | ||
sauimone
|
r724 | */ | ||
sauimone
|
r766 | void QLegend::setAlignmnent(QLegend::Layout alignment) | ||
sauimone
|
r616 | { | ||
sauimone
|
r766 | m_alignment = alignment; | ||
sauimone
|
r716 | updateLayout(); | ||
} | ||||
sauimone
|
r724 | /*! | ||
Returns the preferred layout for legend | ||||
*/ | ||||
sauimone
|
r766 | QLegend::Layout QLegend::alignment() const | ||
sauimone
|
r716 | { | ||
sauimone
|
r766 | return m_alignment; | ||
sauimone
|
r616 | } | ||
sauimone
|
r724 | /*! | ||
Returns the maximum size of legend. | ||||
*/ | ||||
sauimone
|
r626 | QSizeF QLegend::maximumSize() const | ||
sauimone
|
r582 | { | ||
sauimone
|
r766 | return m_maximumSize; | ||
sauimone
|
r582 | } | ||
sauimone
|
r724 | /*! | ||
Sets the maximum \a size for legend. The legend can't grow bigger than this size. If there are | ||||
more series than legend can fit to this size, scroll buttons are displayed. | ||||
*/ | ||||
sauimone
|
r626 | void QLegend::setMaximumSize(const QSizeF size) | ||
sauimone
|
r582 | { | ||
sauimone
|
r766 | m_maximumSize = size; | ||
sauimone
|
r716 | updateLayout(); | ||
sauimone
|
r626 | } | ||
sauimone
|
r724 | /*! | ||
Returns the current size of legend. | ||||
*/ | ||||
QSizeF QLegend::size() const | ||||
{ | ||||
sauimone
|
r766 | return m_size; | ||
sauimone
|
r724 | } | ||
/*! | ||||
Sets the \a size of legend. If size is bigger than maximum size of legend, the legend is resized to the maximum size. | ||||
\sa setMmaximumSize() | ||||
*/ | ||||
sauimone
|
r626 | void QLegend::setSize(const QSizeF size) | ||
{ | ||||
sauimone
|
r766 | m_size = size; | ||
if (m_size.width() > m_maximumSize.width()) { | ||||
m_size.setWidth(m_maximumSize.width()); | ||||
sauimone
|
r626 | } | ||
sauimone
|
r766 | if (m_size.height() > m_maximumSize.height()) { | ||
m_size.setHeight(m_maximumSize.height()); | ||||
sauimone
|
r626 | } | ||
} | ||||
sauimone
|
r724 | /*! | ||
Sets position of legend to \a pos | ||||
*/ | ||||
sauimone
|
r626 | void QLegend::setPos(const QPointF &pos) | ||
{ | ||||
sauimone
|
r766 | m_pos = pos; | ||
sauimone
|
r716 | updateLayout(); | ||
sauimone
|
r582 | } | ||
sauimone
|
r724 | /*! | ||
\internal \a series \a domain Should be called when series is added to chart. | ||||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::handleSeriesAdded(QSeries *series, Domain *domain) | ||
sauimone
|
r524 | { | ||
Tero Ahola
|
r611 | Q_UNUSED(domain) | ||
sauimone
|
r586 | createMarkers(series); | ||
sauimone
|
r637 | connectSeries(series); | ||
sauimone
|
r716 | updateLayout(); | ||
sauimone
|
r586 | } | ||
sauimone
|
r724 | /*! | ||
\internal \a series Should be called when series is removed from chart. | ||||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::handleSeriesRemoved(QSeries *series) | ||
sauimone
|
r586 | { | ||
sauimone
|
r637 | disconnectSeries(series); | ||
sauimone
|
r766 | if (series->type() == QSeries::SeriesTypeArea) { | ||
sauimone
|
r586 | // This is special case. Area series has upper and lower series, which each have markers | ||
Tero Ahola
|
r737 | QAreaSeries* s = static_cast<QAreaSeries *> (series); | ||
sauimone
|
r586 | deleteMarkers(s->upperSeries()); | ||
deleteMarkers(s->lowerSeries()); | ||||
} else { | ||||
deleteMarkers(series); | ||||
} | ||||
sauimone
|
r716 | updateLayout(); | ||
sauimone
|
r586 | } | ||
sauimone
|
r724 | /*! | ||
\internal \a slices Should be called when slices are added to pie chart. | ||||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::handleAdded(QList<QPieSlice *> slices) | ||
sauimone
|
r637 | { | ||
Tero Ahola
|
r737 | QPieSeries* series = static_cast<QPieSeries *> (sender()); | ||
sauimone
|
r637 | foreach(QPieSlice* s, slices) { | ||
Tero Ahola
|
r737 | LegendMarker* marker = new LegendMarker(series, s, this); | ||
sauimone
|
r637 | marker->setName(s->label()); | ||
Jani Honkonen
|
r756 | marker->setBrush(s->brush()); | ||
Tero Ahola
|
r737 | connect(marker, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)), | ||
this, SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); | ||||
connect(s, SIGNAL(changed()), marker, SLOT(changed())); | ||||
connect(s, SIGNAL(destroyed()), marker, SLOT(deleteLater())); | ||||
connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | ||||
sauimone
|
r766 | m_markers.append(marker); | ||
sauimone
|
r637 | childItems().append(marker); | ||
} | ||||
sauimone
|
r716 | updateLayout(); | ||
sauimone
|
r637 | } | ||
sauimone
|
r724 | /*! | ||
\internal \a slices Should be called when slices are removed from pie chart. Currently unused, | ||||
because removed slices are also deleted and we listen destroyed signal | ||||
*/ | ||||
sauimone
|
r643 | void QLegend::handleRemoved(QList<QPieSlice *> slices) | ||
{ | ||||
Tero Ahola
|
r647 | Q_UNUSED(slices) | ||
sauimone
|
r643 | } | ||
sauimone
|
r724 | /*! | ||
\internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed | ||||
*/ | ||||
sauimone
|
r637 | void QLegend::handleMarkerDestroyed() | ||
{ | ||||
Tero Ahola
|
r737 | LegendMarker* m = static_cast<LegendMarker *> (sender()); | ||
sauimone
|
r766 | m_markers.removeOne(m); | ||
sauimone
|
r716 | updateLayout(); | ||
} | ||||
sauimone
|
r724 | /*! | ||
\internal \a event Handles clicked signals from scroll buttons | ||||
*/ | ||||
sauimone
|
r716 | void QLegend::handleScrollButtonClicked(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
sauimone
|
r724 | Q_UNUSED(event); // Maybe later something happens with right click... | ||
sauimone
|
r716 | |||
Tero Ahola
|
r737 | LegendScrollButton* scrollButton = static_cast<LegendScrollButton *> (sender()); | ||
sauimone
|
r716 | Q_ASSERT(scrollButton); | ||
switch (scrollButton->id()) { | ||||
case LegendScrollButton::ScrollButtonIdLeft: | ||||
case LegendScrollButton::ScrollButtonIdUp: { | ||||
// Lower limit is same in these cases | ||||
mFirstMarker--; | ||||
sauimone
|
r724 | checkFirstMarkerBounds(); | ||
sauimone
|
r716 | break; | ||
} | ||||
case LegendScrollButton::ScrollButtonIdRight: | ||||
case LegendScrollButton::ScrollButtonIdDown: { | ||||
mFirstMarker++; | ||||
sauimone
|
r724 | checkFirstMarkerBounds(); | ||
sauimone
|
r716 | break; | ||
} | ||||
default: { | ||||
break; | ||||
} | ||||
} | ||||
updateLayout(); | ||||
sauimone
|
r637 | } | ||
sauimone
|
r724 | /*! | ||
\internal Connects the \a series to legend. Legend listens changes in series, for example pie slices added / removed. | ||||
Not all series notify about events | ||||
*/ | ||||
sauimone
|
r637 | void QLegend::connectSeries(QSeries *series) | ||
{ | ||||
sauimone
|
r766 | // Connect relevant signals from series. Currently only pie series has interesting signals | ||
// TODO: bar chart may have | ||||
if (series->type() == QSeries::SeriesTypePie) { | ||||
sauimone
|
r637 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||
connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); | ||||
} | ||||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Disconnects \a series from legend. No more status updates from series to legend. | ||||
*/ | ||||
sauimone
|
r637 | void QLegend::disconnectSeries(QSeries *series) | ||
{ | ||||
sauimone
|
r766 | if (series->type() == QSeries::SeriesTypePie) { | ||
sauimone
|
r637 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||
Tero Ahola
|
r737 | disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>))); | ||
sauimone
|
r637 | } | ||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Creates new markers for \a series. Marker contains the colored rectangle and series name. | ||||
With pie chart, created markers depend on pie slices. | ||||
With bar chart, created markers depend on bar sets. | ||||
*/ | ||||
sauimone
|
r586 | void QLegend::createMarkers(QSeries *series) | ||
{ | ||||
sauimone
|
r565 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
Tero Ahola
|
r737 | QLineSeries *lineSeries = static_cast<QLineSeries *>(series); | ||
sauimone
|
r586 | appendMarkers(lineSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeArea: { | ||||
Tero Ahola
|
r737 | QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); | ||
sauimone
|
r586 | appendMarkers(areaSeries->upperSeries()); | ||
Michal Klocek
|
r572 | if(areaSeries->lowerSeries()) | ||
Tero Ahola
|
r737 | appendMarkers(areaSeries->lowerSeries()); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
Tero Ahola
|
r737 | QBarSeries *barSeries = static_cast<QBarSeries *>(series); | ||
sauimone
|
r586 | appendMarkers(barSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
Tero Ahola
|
r737 | QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series); | ||
sauimone
|
r586 | appendMarkers(stackedBarSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
Tero Ahola
|
r737 | 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: { | ||||
Tero Ahola
|
r737 | QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series); | ||
sauimone
|
r586 | appendMarkers(splineSeries); | ||
sauimone
|
r565 | break; | ||
} | ||||
default: { | ||||
sauimone
|
r766 | qWarning()<< "QLegend::createMarkers" << series->type() << "unknown series type."; | ||
sauimone
|
r565 | break; | ||
} | ||||
} | ||||
sauimone
|
r576 | } | ||
sauimone
|
r724 | /*! | ||
\internal Helper function. Appends markers from \a series to legend. | ||||
*/ | ||||
sauimone
|
r586 | void QLegend::appendMarkers(QXYSeries* series) | ||
sauimone
|
r529 | { | ||
sauimone
|
r565 | LegendMarker* marker = new LegendMarker(series,this); | ||
marker->setName(series->name()); | ||||
sauimone
|
r724 | marker->setPen(series->pen()); | ||
sauimone
|
r565 | marker->setBrush(series->brush()); | ||
Tero Ahola
|
r737 | connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton))); | ||
connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | ||||
sauimone
|
r766 | m_markers.append(marker); | ||
sauimone
|
r565 | childItems().append(marker); | ||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Helper function. Appends markers from \a series to legend. | ||||
*/ | ||||
sauimone
|
r586 | void QLegend::appendMarkers(QBarSeries *series) | ||
sauimone
|
r565 | { | ||
sauimone
|
r786 | foreach(QBarSet* set, series->barSets()) { | ||
LegendMarker* marker = new LegendMarker(series, set, this); | ||||
marker->setName(set->name()); | ||||
marker->setPen(set->pen()); | ||||
marker->setBrush(set->brush()); | ||||
Tero Ahola
|
r737 | connect(marker, SIGNAL(clicked(QBarSet *, Qt::MouseButton)), | ||
this, SIGNAL(clicked(QBarSet *, Qt::MouseButton))); | ||||
sauimone
|
r786 | connect(set, SIGNAL(valueChanged()), marker, SLOT(changed())); | ||
Tero Ahola
|
r737 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | ||
sauimone
|
r766 | m_markers.append(marker); | ||
sauimone
|
r565 | childItems().append(marker); | ||
sauimone
|
r529 | } | ||
sauimone
|
r565 | } | ||
sauimone
|
r529 | |||
sauimone
|
r724 | /*! | ||
\internal Helper function. Appends markers from \a series to legend. | ||||
*/ | ||||
sauimone
|
r586 | void QLegend::appendMarkers(QPieSeries *series) | ||
sauimone
|
r565 | { | ||
sauimone
|
r786 | foreach(QPieSlice* slice, series->slices()) { | ||
LegendMarker* marker = new LegendMarker(series, slice, this); | ||||
marker->setName(slice->label()); | ||||
marker->setPen(slice->pen()); | ||||
marker->setBrush(slice->brush()); | ||||
Tero Ahola
|
r737 | connect(marker, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)), | ||
this, SIGNAL(clicked(QPieSlice *, Qt::MouseButton))); | ||||
sauimone
|
r786 | connect(slice, SIGNAL(changed()), marker, SLOT(changed())); | ||
connect(slice, SIGNAL(destroyed()), marker, SLOT(deleteLater())); | ||||
Tero Ahola
|
r737 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | ||
sauimone
|
r766 | m_markers.append(marker); | ||
sauimone
|
r565 | childItems().append(marker); | ||
sauimone
|
r529 | } | ||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Deletes all markers that are created from \a series | ||||
*/ | ||||
sauimone
|
r586 | void QLegend::deleteMarkers(QSeries *series) | ||
{ | ||||
// Search all markers that belong to given series and delete them. | ||||
sauimone
|
r766 | foreach (LegendMarker *m, m_markers) { | ||
sauimone
|
r586 | if (m->series() == series) { | ||
sauimone
|
r766 | m_markers.removeOne(m); | ||
sauimone
|
r586 | delete m; | ||
} | ||||
} | ||||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend. | ||||
If items don't fit, sets the visibility of scroll buttons accordingly. | ||||
Causes legend to be resized. | ||||
*/ | ||||
sauimone
|
r716 | void QLegend::updateLayout() | ||
sauimone
|
r529 | { | ||
// Calculate layout for markers and text | ||||
sauimone
|
r766 | if (m_markers.count() <= 0) { | ||
sauimone
|
r529 | // Nothing to do | ||
return; | ||||
} | ||||
sauimone
|
r626 | // Find out widest item. | ||
sauimone
|
r716 | QSizeF markerMaxSize = maximumMarkerSize(); | ||
sauimone
|
r724 | checkFirstMarkerBounds(); | ||
sauimone
|
r626 | |||
sauimone
|
r716 | // Use max height as scroll button size | ||
rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height())); | ||||
sauimone
|
r616 | |||
sauimone
|
r626 | qreal totalWidth = 0; | ||
qreal totalHeight = 0; | ||||
sauimone
|
r766 | switch (m_alignment) | ||
sauimone
|
r616 | { | ||
sauimone
|
r716 | // Both cases organise items horizontally | ||
sauimone
|
r766 | case QLegend::LayoutBottom: | ||
case QLegend::LayoutTop: { | ||||
sauimone
|
r716 | |||
qreal xStep = markerMaxSize.width(); | ||||
sauimone
|
r766 | qreal x = m_pos.x() + m_margin; | ||
qreal y = m_pos.y() + m_margin; | ||||
sauimone
|
r626 | int column = 0; | ||
int maxColumns = 1; | ||||
sauimone
|
r724 | qreal scrollButtonWidth = 0; | ||
sauimone
|
r716 | |||
// Set correct visibility for scroll scrollbuttons | ||||
if (scrollButtonsVisible()) { | ||||
sauimone
|
r766 | m_scrollButtonLeft->setVisible(true); | ||
m_scrollButtonRight->setVisible(true); | ||||
Tero Ahola
|
r737 | // scrollbuttons visible, so add their width to total width | ||
sauimone
|
r766 | totalWidth += (m_scrollButtonLeft->boundingRect().width() + m_margin) * 2; | ||
scrollButtonWidth = m_scrollButtonLeft->boundingRect().width() + m_margin; | ||||
Tero Ahola
|
r737 | // start position changes by scrollbutton width | ||
x += scrollButtonWidth; | ||||
sauimone
|
r716 | } else { | ||
sauimone
|
r766 | m_scrollButtonLeft->setVisible(false); | ||
m_scrollButtonRight->setVisible(false); | ||||
sauimone
|
r716 | } | ||
sauimone
|
r766 | m_scrollButtonUp->setVisible(false); | ||
m_scrollButtonDown->setVisible(false); | ||||
sauimone
|
r716 | |||
sauimone
|
r766 | for (int i=0; i < m_markers.count(); i++) { | ||
LegendMarker *m = m_markers.at(i); | ||||
Tero Ahola
|
r737 | if (i < mFirstMarker) { | ||
sauimone
|
r716 | // Markers before first are not visible. | ||
m->setVisible(false); | ||||
} else { | ||||
sauimone
|
r766 | if ((x + xStep + scrollButtonWidth + m_margin) > (m_pos.x() + m_maximumSize.width())) { | ||
sauimone
|
r716 | // This marker would go outside legend rect. | ||
m->setVisible(false); | ||||
} else { | ||||
// This marker is ok | ||||
m->setVisible(true); | ||||
Tero Ahola
|
r737 | m->setPos(x, y); | ||
sauimone
|
r716 | x += xStep; | ||
column++; | ||||
} | ||||
sauimone
|
r626 | } | ||
sauimone
|
r716 | maxColumns = column; | ||
sauimone
|
r616 | } | ||
sauimone
|
r716 | |||
sauimone
|
r766 | m_scrollButtonLeft->setPos(m_pos.x() + m_margin, y); | ||
m_scrollButtonRight->setPos(x + m_margin, y); | ||||
sauimone
|
r716 | |||
sauimone
|
r766 | totalWidth += maxColumns * markerMaxSize.width() + m_margin * 2; | ||
totalHeight = markerMaxSize.height() + m_margin * 2; | ||||
sauimone
|
r716 | |||
sauimone
|
r616 | break; | ||
sauimone
|
r529 | } | ||
sauimone
|
r716 | // Both cases organize items vertically | ||
sauimone
|
r766 | case QLegend::LayoutLeft: | ||
case QLegend::LayoutRight: { | ||||
sauimone
|
r716 | qreal yStep = markerMaxSize.height(); | ||
sauimone
|
r766 | qreal x = m_pos.x() + m_margin; | ||
qreal y = m_pos.y() + m_margin; | ||||
sauimone
|
r716 | int row = 1; | ||
int maxRows = 1; | ||||
sauimone
|
r724 | qreal scrollButtonHeight = 0; | ||
sauimone
|
r716 | |||
// Set correct visibility for scroll scrollbuttons | ||||
if (scrollButtonsVisible()) { | ||||
sauimone
|
r766 | m_scrollButtonUp->setVisible(true); | ||
m_scrollButtonDown->setVisible(true); | ||||
totalHeight += (m_scrollButtonUp->boundingRect().height() + m_margin) * 2; // scrollbuttons visible, so add their height to total height | ||||
scrollButtonHeight = m_scrollButtonUp->boundingRect().height(); | ||||
y += scrollButtonHeight + m_margin; // start position changes by scrollbutton height | ||||
sauimone
|
r716 | } else { | ||
sauimone
|
r766 | m_scrollButtonUp->setVisible(false); | ||
m_scrollButtonDown->setVisible(false); | ||||
sauimone
|
r616 | } | ||
sauimone
|
r766 | m_scrollButtonLeft->setVisible(false); | ||
m_scrollButtonRight->setVisible(false); | ||||
sauimone
|
r716 | |||
sauimone
|
r766 | for (int i=0; i < m_markers.count(); i++) { | ||
LegendMarker* m = m_markers.at(i); | ||||
Tero Ahola
|
r737 | if (i < mFirstMarker) { | ||
sauimone
|
r716 | // Markers before first are not visible. | ||
m->setVisible(false); | ||||
} else { | ||||
sauimone
|
r766 | if ((y + yStep + scrollButtonHeight) > (m_pos.y() + m_maximumSize.height())) { | ||
sauimone
|
r716 | // This marker would go outside legend rect. | ||
m->setVisible(false); | ||||
} else { | ||||
// This marker is ok | ||||
m->setVisible(true); | ||||
Tero Ahola
|
r737 | m->setPos(x, y); | ||
sauimone
|
r716 | y += yStep; | ||
row++; | ||||
} | ||||
} | ||||
maxRows = row; | ||||
} | ||||
sauimone
|
r766 | m_scrollButtonUp->setPos(m_pos.x() + m_margin, m_pos.y() + m_margin); | ||
m_scrollButtonDown->setPos(m_pos.x() + m_margin, y + m_margin); | ||||
sauimone
|
r716 | |||
sauimone
|
r766 | totalWidth += markerMaxSize.width() + m_margin * 2; | ||
totalHeight = maxRows * markerMaxSize.height() + m_margin * 4 + scrollButtonHeight; // TODO: check this | ||||
sauimone
|
r616 | break; | ||
} | ||||
default: { | ||||
break; | ||||
} | ||||
} | ||||
sauimone
|
r766 | m_size.setWidth(totalWidth); | ||
m_size.setHeight(totalHeight); | ||||
sauimone
|
r716 | |||
update(); | ||||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Sets the size of scroll buttons to \a size | ||||
*/ | ||||
sauimone
|
r716 | void QLegend::rescaleScrollButtons(const QSize &size) | ||
{ | ||||
QPolygonF left; | ||||
Tero Ahola
|
r737 | left << QPointF(size.width(), 0) << QPointF(0, size.height() / 2) << QPointF(size.width(), size.height()); | ||
sauimone
|
r716 | QPolygonF right; | ||
Tero Ahola
|
r737 | right << QPointF(0, 0) << QPointF(size.width(), size.height() / 2) << QPointF(0, size.height()); | ||
sauimone
|
r716 | QPolygonF up; | ||
Tero Ahola
|
r737 | up << QPointF(0, size.height()) << QPointF(size.width() / 2,0) << QPointF(size.width(), size.height()); | ||
sauimone
|
r716 | QPolygonF down; | ||
Tero Ahola
|
r737 | down << QPointF(0, 0) << QPointF(size.width() / 2, size.height()) << QPointF(size.width(), 0); | ||
sauimone
|
r716 | |||
sauimone
|
r766 | m_scrollButtonLeft->setPolygon(left); | ||
m_scrollButtonRight->setPolygon(right); | ||||
m_scrollButtonUp->setPolygon(up); | ||||
m_scrollButtonDown->setPolygon(down); | ||||
sauimone
|
r716 | } | ||
sauimone
|
r724 | /*! | ||
\internal Finds out maximum size of single marker. Marker sizes depend on series names. | ||||
*/ | ||||
sauimone
|
r716 | QSizeF QLegend::maximumMarkerSize() | ||
{ | ||||
QSizeF max(0,0); | ||||
sauimone
|
r766 | foreach (LegendMarker* m, m_markers) { | ||
Tero Ahola
|
r737 | if (m->boundingRect().width() > max.width()) | ||
sauimone
|
r716 | max.setWidth(m->boundingRect().width()); | ||
Tero Ahola
|
r737 | if (m->boundingRect().height() > max.height()) | ||
sauimone
|
r716 | max.setHeight(m->boundingRect().height()); | ||
} | ||||
return max; | ||||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Checks that first marker is in acceptable bounds. Bounds range from 0 to (maximum number of markers - visible markers) | ||||
If scrollbuttons are visible, they affect the number of visible markers. | ||||
*/ | ||||
void QLegend::checkFirstMarkerBounds() | ||||
sauimone
|
r716 | { | ||
sauimone
|
r766 | if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) { | ||
sauimone
|
r724 | // Bounds limited by height. | ||
sauimone
|
r716 | int max; | ||
if (scrollButtonsVisible()) { | ||||
sauimone
|
r766 | max = (m_maximumSize.height() - m_scrollButtonLeft->boundingRect().height() * 2 - m_margin * 4) / maximumMarkerSize().height(); | ||
sauimone
|
r716 | } else { | ||
sauimone
|
r766 | max = m_maximumSize.height() / maximumMarkerSize().height(); | ||
sauimone
|
r716 | } | ||
sauimone
|
r766 | if (mFirstMarker > m_markers.count() - max) | ||
mFirstMarker = m_markers.count() - max; | ||||
sauimone
|
r716 | } else { | ||
// Bounds limited by width | ||||
int max; | ||||
if (scrollButtonsVisible()) { | ||||
sauimone
|
r766 | max = (m_maximumSize.width() - m_scrollButtonLeft->boundingRect().width() * 2 - m_margin*4) / maximumMarkerSize().width(); | ||
sauimone
|
r716 | } else { | ||
sauimone
|
r766 | max = m_maximumSize.width() / maximumMarkerSize().width(); | ||
sauimone
|
r716 | } | ||
sauimone
|
r766 | if (mFirstMarker > m_markers.count() - max) | ||
mFirstMarker = m_markers.count() - max; | ||||
sauimone
|
r716 | } | ||
Tero Ahola
|
r737 | if (mFirstMarker < 0) | ||
sauimone
|
r716 | mFirstMarker = 0; | ||
} | ||||
sauimone
|
r724 | /*! | ||
\internal Helper function. Visibility of scroll buttons isn't quite obvious, so helper function clarifies the logic. | ||||
*/ | ||||
sauimone
|
r716 | bool QLegend::scrollButtonsVisible() | ||
{ | ||||
// Just a helper to clarify, what the magic below means :) | ||||
sauimone
|
r766 | if ((m_alignment == QLegend::LayoutTop) || (m_alignment == QLegend::LayoutBottom)) { | ||
return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width()); | ||||
} else if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) { | ||||
return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height()); | ||||
sauimone
|
r716 | } | ||
sauimone
|
r724 | |||
sauimone
|
r766 | return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height()); | ||
sauimone
|
r529 | } | ||
sauimone
|
r524 | #include "moc_qlegend.cpp" | ||
Tero Ahola
|
r737 | |||
sauimone
|
r524 | QTCOMMERCIALCHART_END_NAMESPACE | ||