qlegend.cpp
751 lines
| 22.0 KiB
| text/x-c
|
CppLexer
/ src / qlegend.cpp
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
sauimone
|
r524 | #include "qlegend.h" | ||
Michal Klocek
|
r790 | #include "qchart_p.h" | ||
sauimone
|
r524 | #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 | ||||
*/ | ||||
Michal Klocek
|
r791 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), | ||
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
|
r800 | m_alignment(QLegend::AlignmentTop), | ||
sauimone
|
r799 | mFirstMarker(0), | ||
sauimone
|
r803 | m_attachedToChart(true), | ||
m_chart(chart) | ||||
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
|
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
|
r803 | void QLegend::setAlignmnent(QLegend::Alignments alignment) | ||
sauimone
|
r616 | { | ||
sauimone
|
r803 | // if (!m_attachedToChart) { | ||
m_alignment = alignment; | ||||
updateLayout(); | ||||
m_chart->resize(m_chart->size()); | ||||
// } | ||||
sauimone
|
r716 | } | ||
sauimone
|
r724 | /*! | ||
Returns the preferred layout for legend | ||||
*/ | ||||
sauimone
|
r803 | QLegend::Alignments 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
|
r792 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
QLineSeries *lineSeries = static_cast<QLineSeries *>(series); | ||||
appendMarkers(lineSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); | ||||
appendMarkers(areaSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
QBarSeries *barSeries = static_cast<QBarSeries *>(series); | ||||
appendMarkers(barSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series); | ||||
appendMarkers(stackedBarSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series); | ||||
appendMarkers(percentBarSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | ||||
appendMarkers(scatterSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
appendMarkers(pieSeries); | ||||
connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeSpline: { | ||||
QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series); | ||||
appendMarkers(splineSeries); | ||||
break; | ||||
} | ||||
default: { | ||||
qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type."; | ||||
break; | ||||
} | ||||
} | ||||
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
|
r792 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); | ||||
deleteMarkers(areaSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>))); | ||||
deleteMarkers(series); | ||||
break; | ||||
} | ||||
default: { | ||||
// All other types | ||||
sauimone
|
r586 | deleteMarkers(series); | ||
sauimone
|
r792 | break; | ||
} | ||||
sauimone
|
r586 | } | ||
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 | ||||
*/ | ||||
Michal Klocek
|
r790 | void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton) | ||
sauimone
|
r716 | { | ||
Michal Klocek
|
r790 | Q_ASSERT(scrollButton); | ||
sauimone
|
r716 | |||
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
|
r799 | /*! | ||
Detaches the legend from chart. Chart won't change layout of the legend. | ||||
*/ | ||||
void QLegend::detachFromChart() | ||||
{ | ||||
m_attachedToChart = false; | ||||
} | ||||
/*! | ||||
Attaches the legend to chart. Chart may change layout of the legend. | ||||
*/ | ||||
void QLegend::attachToChart() | ||||
{ | ||||
m_attachedToChart = true; | ||||
} | ||||
/*! | ||||
Returns true, if legend is attached to chart. | ||||
*/ | ||||
bool QLegend::attachedToChart() | ||||
{ | ||||
return m_attachedToChart; | ||||
} | ||||
sauimone
|
r724 | /*! | ||
sauimone
|
r792 | \internal Helper function. Appends markers from \a series to legend. | ||
sauimone
|
r724 | */ | ||
sauimone
|
r792 | void QLegend::appendMarkers(QAreaSeries* series) | ||
sauimone
|
r586 | { | ||
sauimone
|
r792 | LegendMarker* marker = new LegendMarker(series,this); | ||
connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton))); | ||||
connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | ||||
connect(series,SIGNAL(updated()),marker,SLOT(changed())); | ||||
sauimone
|
r800 | marker->changed(); | ||
sauimone
|
r792 | m_markers.append(marker); | ||
childItems().append(marker); | ||||
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); | ||
Tero Ahola
|
r737 | connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton))); | ||
connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); | ||||
sauimone
|
r792 | connect(series,SIGNAL(updated()),marker,SLOT(changed())); | ||
sauimone
|
r800 | marker->changed(); | ||
sauimone
|
r766 | m_markers.append(marker); | ||
sauimone
|
r565 | childItems().append(marker); | ||
} | ||||
sauimone
|
r724 | /*! | ||
sauimone
|
r792 | \internal Helper function. Appends markers from \a series to legend. | ||
sauimone
|
r724 | */ | ||
sauimone
|
r586 | void QLegend::appendMarkers(QBarSeries *series) | ||
sauimone
|
r565 | { | ||
sauimone
|
r786 | foreach(QBarSet* set, series->barSets()) { | ||
LegendMarker* marker = new LegendMarker(series, set, this); | ||||
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
|
r800 | marker->changed(); | ||
sauimone
|
r766 | m_markers.append(marker); | ||
sauimone
|
r565 | childItems().append(marker); | ||
sauimone
|
r529 | } | ||
sauimone
|
r565 | } | ||
sauimone
|
r529 | |||
sauimone
|
r724 | /*! | ||
sauimone
|
r792 | \internal Helper function. Appends markers from \a series to legend. | ||
sauimone
|
r724 | */ | ||
sauimone
|
r586 | void QLegend::appendMarkers(QPieSeries *series) | ||
sauimone
|
r565 | { | ||
sauimone
|
r786 | foreach(QPieSlice* slice, series->slices()) { | ||
LegendMarker* marker = new LegendMarker(series, slice, this); | ||||
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
|
r800 | marker->changed(); | ||
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
|
r724 | checkFirstMarkerBounds(); | ||
sauimone
|
r626 | |||
sauimone
|
r766 | switch (m_alignment) | ||
sauimone
|
r616 | { | ||
sauimone
|
r716 | // Both cases organise items horizontally | ||
sauimone
|
r799 | case QLegend::AlignmentBottom: | ||
case QLegend::AlignmentTop: { | ||||
sauimone
|
r810 | layoutHorizontal(); | ||
break; | ||||
} | ||||
// Both cases organize items vertically | ||||
case QLegend::AlignmentLeft: | ||||
case QLegend::AlignmentRight: { | ||||
layoutVertical(); | ||||
break; | ||||
} | ||||
default: { | ||||
break; | ||||
} | ||||
} | ||||
sauimone
|
r716 | |||
sauimone
|
r810 | } | ||
/*! | ||||
\internal Organizes markers horizontally. | ||||
Causes legend to be resized. | ||||
*/ | ||||
void QLegend::layoutHorizontal() | ||||
{ | ||||
// Find out widest item. | ||||
QSizeF markerMaxSize = maximumMarkerSize(); | ||||
// Use max height as scroll button size | ||||
rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height())); | ||||
sauimone
|
r716 | |||
sauimone
|
r810 | qreal totalWidth = 0; | ||
qreal totalHeight = 0; | ||||
qreal xStep = markerMaxSize.width(); | ||||
qreal x = m_pos.x() + m_margin; | ||||
qreal y = m_pos.y() + m_margin; | ||||
int column = 0; | ||||
int maxColumns = 1; | ||||
qreal scrollButtonWidth = 0; | ||||
// Set correct visibility for scroll scrollbuttons | ||||
if (scrollButtonsVisible()) { | ||||
m_scrollButtonLeft->setVisible(true); | ||||
m_scrollButtonRight->setVisible(true); | ||||
// scrollbuttons visible, so add their width to total width | ||||
totalWidth += (m_scrollButtonLeft->boundingRect().width() + m_margin) * 2; | ||||
scrollButtonWidth = m_scrollButtonLeft->boundingRect().width() + m_margin; | ||||
// start position changes by scrollbutton width | ||||
x += scrollButtonWidth; | ||||
} else { | ||||
m_scrollButtonLeft->setVisible(false); | ||||
m_scrollButtonRight->setVisible(false); | ||||
} | ||||
m_scrollButtonUp->setVisible(false); | ||||
m_scrollButtonDown->setVisible(false); | ||||
for (int i=0; i < m_markers.count(); i++) { | ||||
LegendMarker *m = m_markers.at(i); | ||||
if (i < mFirstMarker) { | ||||
// Markers before first are not visible. | ||||
m->setVisible(false); | ||||
} else { | ||||
if ((x + xStep + scrollButtonWidth + m_margin) > (m_pos.x() + m_maximumSize.width())) { | ||||
// This marker would go outside legend rect. | ||||
sauimone
|
r716 | m->setVisible(false); | ||
} else { | ||||
sauimone
|
r810 | // This marker is ok | ||
m->setVisible(true); | ||||
m->setPos(x, y); | ||||
x += xStep; | ||||
column++; | ||||
sauimone
|
r626 | } | ||
sauimone
|
r616 | } | ||
sauimone
|
r810 | maxColumns = column; | ||
} | ||||
sauimone
|
r716 | |||
sauimone
|
r810 | m_scrollButtonLeft->setPos(m_pos.x() + m_margin, y); | ||
m_scrollButtonRight->setPos(x + m_margin, y); | ||||
sauimone
|
r716 | |||
sauimone
|
r810 | totalWidth += maxColumns * markerMaxSize.width() + m_margin * 2; | ||
totalHeight = markerMaxSize.height() + m_margin * 2; | ||||
sauimone
|
r716 | |||
sauimone
|
r810 | m_size.setWidth(totalWidth); | ||
m_size.setHeight(totalHeight); | ||||
} | ||||
/*! | ||||
\internal Organizes markers vertically. | ||||
Causes legend to be resized. | ||||
*/ | ||||
void QLegend::layoutVertical() | ||||
{ | ||||
// Find out widest item. | ||||
QSizeF markerMaxSize = maximumMarkerSize(); | ||||
// Use max height as scroll button size | ||||
rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height())); | ||||
qreal totalWidth = 0; | ||||
qreal totalHeight = 0; | ||||
qreal yStep = markerMaxSize.height(); | ||||
qreal x = m_pos.x() + m_margin; | ||||
qreal y = m_pos.y() + m_margin; | ||||
int row = 1; | ||||
int maxRows = 1; | ||||
qreal scrollButtonHeight = 0; | ||||
// Set correct visibility for scroll scrollbuttons | ||||
if (scrollButtonsVisible()) { | ||||
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 | ||||
} else { | ||||
m_scrollButtonUp->setVisible(false); | ||||
m_scrollButtonDown->setVisible(false); | ||||
sauimone
|
r529 | } | ||
sauimone
|
r810 | m_scrollButtonLeft->setVisible(false); | ||
m_scrollButtonRight->setVisible(false); | ||||
for (int i=0; i < m_markers.count(); i++) { | ||||
LegendMarker* m = m_markers.at(i); | ||||
if (i < mFirstMarker) { | ||||
// Markers before first are not visible. | ||||
m->setVisible(false); | ||||
sauimone
|
r716 | } else { | ||
sauimone
|
r810 | if ((y + yStep + scrollButtonHeight) > (m_pos.y() + m_maximumSize.height())) { | ||
// This marker would go outside legend rect. | ||||
sauimone
|
r716 | m->setVisible(false); | ||
} else { | ||||
sauimone
|
r810 | // This marker is ok | ||
m->setVisible(true); | ||||
m->setPos(x, y); | ||||
y += yStep; | ||||
row++; | ||||
sauimone
|
r716 | } | ||
} | ||||
sauimone
|
r810 | maxRows = row; | ||
} | ||||
sauimone
|
r716 | |||
sauimone
|
r810 | 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
|
r810 | totalWidth += markerMaxSize.width() + m_margin * 2; | ||
totalHeight = maxRows * markerMaxSize.height() + m_margin * 4 + scrollButtonHeight; // TODO: check this | ||||
sauimone
|
r616 | |||
sauimone
|
r766 | m_size.setWidth(totalWidth); | ||
m_size.setHeight(totalHeight); | ||||
sauimone
|
r716 | } | ||
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
|
r799 | if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) { | ||
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
|
r799 | if ((m_alignment == QLegend::AlignmentTop) || (m_alignment == QLegend::AlignmentBottom)) { | ||
sauimone
|
r766 | return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width()); | ||
sauimone
|
r799 | } else if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) { | ||
sauimone
|
r766 | 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 | ||