chartpresenter.cpp
486 lines
| 13.1 KiB
| text/x-c
|
CppLexer
/ src / chartpresenter.cpp
|
r794 | /**************************************************************************** | ||
|
r969 | ** | ||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r969 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "chartpresenter_p.h" | ||||
|
r139 | #include "qchart.h" | ||
|
r2273 | #include "chartitem_p.h" | ||
|
r855 | #include "qchart_p.h" | ||
|
r1541 | #include "qabstractaxis.h" | ||
|
r1556 | #include "qabstractaxis_p.h" | ||
|
r131 | #include "chartdataset_p.h" | ||
|
r1241 | #include "chartanimation_p.h" | ||
|
r988 | #include "qabstractseries_p.h" | ||
|
r421 | #include "qareaseries.h" | ||
|
r2483 | #include "chartaxiselement_p.h" | ||
|
r1006 | #include "chartbackground_p.h" | ||
|
r2483 | #include "cartesianchartlayout_p.h" | ||
#include "polarchartlayout_p.h" | ||||
|
r1965 | #include "charttitle_p.h" | ||
|
r1241 | #include <QTimer> | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2483 | ChartPresenter::ChartPresenter(QChart *chart, QChart::ChartType type) | ||
|
r2131 | : QObject(chart), | ||
m_chart(chart), | ||||
m_options(QChart::NoAnimation), | ||||
m_state(ShowState), | ||||
m_background(0), | ||||
|
r2498 | m_plotAreaBackground(0), | ||
|
r2131 | m_title(0) | ||
|
r131 | { | ||
|
r2483 | if (type == QChart::ChartTypeCartesian) | ||
m_layout = new CartesianChartLayout(this); | ||||
else if (type == QChart::ChartTypePolar) | ||||
m_layout = new PolarChartLayout(this); | ||||
Q_ASSERT(m_layout); | ||||
|
r131 | } | ||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
|
r2273 | |||
|
r131 | } | ||
|
r2273 | void ChartPresenter::setGeometry(const QRectF rect) | ||
|
r131 | { | ||
|
r2483 | if (m_rect != rect) { | ||
m_rect = rect; | ||||
foreach (ChartItem *chart, m_chartItems) { | ||||
chart->domain()->setSize(rect.size()); | ||||
chart->setPos(rect.topLeft()); | ||||
} | ||||
} | ||||
|
r2273 | } | ||
|
r1698 | |||
|
r2273 | QRectF ChartPresenter::geometry() const | ||
{ | ||||
|
r2483 | return m_rect; | ||
|
r2273 | } | ||
|
r1698 | |||
|
r2273 | void ChartPresenter::handleAxisAdded(QAbstractAxis *axis) | ||
{ | ||||
axis->d_ptr->initializeGraphics(rootItem()); | ||||
axis->d_ptr->initializeAnimations(m_options); | ||||
|
r2483 | ChartAxisElement *item = axis->d_ptr->axisItem(); | ||
|
r2273 | item->setPresenter(this); | ||
item->setThemeManager(m_chart->d_ptr->m_themeManager); | ||||
m_axisItems<<item; | ||||
m_axes<<axis; | ||||
|
r2105 | m_layout->invalidate(); | ||
|
r223 | } | ||
|
r2131 | void ChartPresenter::handleAxisRemoved(QAbstractAxis *axis) | ||
|
r223 | { | ||
|
r2483 | ChartAxisElement *item = axis->d_ptr->m_item.take(); | ||
|
r1729 | item->hide(); | ||
item->disconnect(); | ||||
|
r1562 | item->deleteLater(); | ||
|
r2273 | m_axisItems.removeAll(item); | ||
m_axes.removeAll(axis); | ||||
|
r2154 | m_layout->invalidate(); | ||
|
r223 | } | ||
|
r2273 | void ChartPresenter::handleSeriesAdded(QAbstractSeries *series) | ||
|
r131 | { | ||
|
r2273 | series->d_ptr->initializeGraphics(rootItem()); | ||
series->d_ptr->initializeAnimations(m_options); | ||||
ChartItem *chart = series->d_ptr->chartItem(); | ||||
chart->setPresenter(this); | ||||
chart->setThemeManager(m_chart->d_ptr->m_themeManager); | ||||
chart->domain()->setSize(m_rect.size()); | ||||
chart->setPos(m_rect.topLeft()); | ||||
chart->handleDomainUpdated(); //this could be moved to intializeGraphics when animator is refactored | ||||
m_chartItems<<chart; | ||||
m_series<<series; | ||||
|
r2105 | m_layout->invalidate(); | ||
|
r131 | } | ||
|
r2131 | void ChartPresenter::handleSeriesRemoved(QAbstractSeries *series) | ||
|
r139 | { | ||
|
r2273 | ChartItem *chart = series->d_ptr->m_item.take(); | ||
chart->hide(); | ||||
chart->disconnect(); | ||||
chart->deleteLater(); | ||||
m_chartItems.removeAll(chart); | ||||
m_series.removeAll(series); | ||||
m_layout->invalidate(); | ||||
|
r153 | } | ||
|
r298 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | ||
{ | ||||
|
r2131 | if (m_options != options) { | ||
m_options = options; | ||||
|
r530 | |||
|
r2273 | foreach(QAbstractSeries* series, m_series){ | ||
series->d_ptr->initializeAnimations(m_options); | ||||
} | ||||
foreach(QAbstractAxis* axis, m_axes){ | ||||
axis->d_ptr->initializeAnimations(m_options); | ||||
} | ||||
|
r2131 | } | ||
|
r531 | } | ||
|
r2273 | void ChartPresenter::setState(State state,QPointF point) | ||
|
r439 | { | ||
|
r2273 | m_state=state; | ||
m_statePoint=point; | ||||
|
r531 | } | ||
|
r298 | QChart::AnimationOptions ChartPresenter::animationOptions() const | ||
{ | ||||
return m_options; | ||||
} | ||||
|
r1534 | void ChartPresenter::createBackgroundItem() | ||
|
r855 | { | ||
|
r1965 | if (!m_background) { | ||
m_background = new ChartBackground(rootItem()); | ||||
|
r2516 | m_background->setPen(Qt::NoPen); // Theme doesn't touch pen so don't use default | ||
m_background->setBrush(QChartPrivate::defaultBrush()); | ||||
|
r1965 | m_background->setZValue(ChartPresenter::BackgroundZValue); | ||
|
r1534 | } | ||
} | ||||
|
r855 | |||
|
r2498 | void ChartPresenter::createPlotAreaBackgroundItem() | ||
{ | ||||
if (!m_plotAreaBackground) { | ||||
if (m_chart->chartType() == QChart::ChartTypeCartesian) | ||||
m_plotAreaBackground = new QGraphicsRectItem(rootItem()); | ||||
else | ||||
m_plotAreaBackground = new QGraphicsEllipseItem(rootItem()); | ||||
// Use transparent pen instead of Qt::NoPen, as Qt::NoPen causes | ||||
// antialising artifacts with axis lines for some reason. | ||||
m_plotAreaBackground->setPen(QPen(Qt::transparent)); | ||||
m_plotAreaBackground->setBrush(Qt::NoBrush); | ||||
m_plotAreaBackground->setZValue(ChartPresenter::PlotAreaZValue); | ||||
m_plotAreaBackground->setVisible(false); | ||||
} | ||||
} | ||||
|
r1534 | void ChartPresenter::createTitleItem() | ||
{ | ||||
|
r1965 | if (!m_title) { | ||
m_title = new ChartTitle(rootItem()); | ||||
m_title->setZValue(ChartPresenter::BackgroundZValue); | ||||
|
r1534 | } | ||
} | ||||
|
r1524 | |||
|
r855 | |||
|
r1534 | void ChartPresenter::handleAnimationFinished() | ||
{ | ||||
|
r2131 | m_animations.removeAll(qobject_cast<ChartAnimation *>(sender())); | ||
if (m_animations.empty()) | ||||
emit animationsFinished(); | ||||
|
r1534 | } | ||
|
r855 | |||
|
r2131 | void ChartPresenter::startAnimation(ChartAnimation *animation) | ||
|
r1534 | { | ||
|
r2105 | if (animation->state() != QAbstractAnimation::Stopped) animation->stop(); | ||
|
r2131 | QObject::connect(animation, SIGNAL(finished()), this, SLOT(handleAnimationFinished()), Qt::UniqueConnection); | ||
if (!m_animations.isEmpty()) | ||||
|
r1534 | m_animations.append(animation); | ||
QTimer::singleShot(0, animation, SLOT(start())); | ||||
} | ||||
|
r855 | |||
|
r2131 | void ChartPresenter::setBackgroundBrush(const QBrush &brush) | ||
|
r1534 | { | ||
createBackgroundItem(); | ||||
|
r1965 | m_background->setBrush(brush); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r855 | |||
|
r1534 | QBrush ChartPresenter::backgroundBrush() const | ||
{ | ||||
|
r2131 | if (!m_background) | ||
return QBrush(); | ||||
|
r1965 | return m_background->brush(); | ||
|
r1534 | } | ||
|
r855 | |||
|
r2131 | void ChartPresenter::setBackgroundPen(const QPen &pen) | ||
|
r1534 | { | ||
createBackgroundItem(); | ||||
|
r1965 | m_background->setPen(pen); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r855 | |||
|
r1534 | QPen ChartPresenter::backgroundPen() const | ||
{ | ||||
|
r2131 | if (!m_background) | ||
return QPen(); | ||||
|
r1965 | return m_background->pen(); | ||
|
r1534 | } | ||
|
r913 | |||
|
r2549 | void ChartPresenter::setBackgroundRoundness(qreal diameter) | ||
{ | ||||
createBackgroundItem(); | ||||
m_background->setDiameter(diameter); | ||||
m_layout->invalidate(); | ||||
} | ||||
qreal ChartPresenter::backgroundRoundness() const | ||||
{ | ||||
if (!m_background) | ||||
return 0; | ||||
return m_background->diameter(); | ||||
} | ||||
|
r2498 | void ChartPresenter::setPlotAreaBackgroundBrush(const QBrush &brush) | ||
{ | ||||
createPlotAreaBackgroundItem(); | ||||
m_plotAreaBackground->setBrush(brush); | ||||
m_layout->invalidate(); | ||||
} | ||||
QBrush ChartPresenter::plotAreaBackgroundBrush() const | ||||
{ | ||||
if (!m_plotAreaBackground) | ||||
return QBrush(); | ||||
return m_plotAreaBackground->brush(); | ||||
} | ||||
void ChartPresenter::setPlotAreaBackgroundPen(const QPen &pen) | ||||
{ | ||||
createPlotAreaBackgroundItem(); | ||||
m_plotAreaBackground->setPen(pen); | ||||
m_layout->invalidate(); | ||||
} | ||||
QPen ChartPresenter::plotAreaBackgroundPen() const | ||||
{ | ||||
if (!m_plotAreaBackground) | ||||
return QPen(); | ||||
return m_plotAreaBackground->pen(); | ||||
} | ||||
|
r2131 | void ChartPresenter::setTitle(const QString &title) | ||
|
r1534 | { | ||
createTitleItem(); | ||||
|
r1965 | m_title->setText(title); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r913 | |||
|
r1534 | QString ChartPresenter::title() const | ||
{ | ||||
|
r2131 | if (!m_title) | ||
return QString(); | ||||
|
r1965 | return m_title->text(); | ||
|
r1534 | } | ||
|
r855 | |||
|
r2131 | void ChartPresenter::setTitleFont(const QFont &font) | ||
|
r1534 | { | ||
createTitleItem(); | ||||
|
r1965 | m_title->setFont(font); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r855 | |||
|
r1534 | QFont ChartPresenter::titleFont() const | ||
{ | ||||
|
r2131 | if (!m_title) | ||
return QFont(); | ||||
|
r1965 | return m_title->font(); | ||
|
r1534 | } | ||
|
r913 | |||
|
r1534 | void ChartPresenter::setTitleBrush(const QBrush &brush) | ||
{ | ||||
createTitleItem(); | ||||
|
r2539 | m_title->setDefaultTextColor(brush.color()); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r869 | |||
|
r1534 | QBrush ChartPresenter::titleBrush() const | ||
{ | ||||
|
r2131 | if (!m_title) | ||
return QBrush(); | ||||
|
r2539 | return QBrush(m_title->defaultTextColor()); | ||
|
r1534 | } | ||
void ChartPresenter::setBackgroundVisible(bool visible) | ||||
{ | ||||
createBackgroundItem(); | ||||
|
r1965 | m_background->setVisible(visible); | ||
|
r1534 | } | ||
|
r871 | |||
|
r855 | |||
|
r1534 | bool ChartPresenter::isBackgroundVisible() const | ||
{ | ||||
|
r2131 | if (!m_background) | ||
return false; | ||||
|
r1965 | return m_background->isVisible(); | ||
|
r855 | } | ||
|
r2498 | void ChartPresenter::setPlotAreaBackgroundVisible(bool visible) | ||
{ | ||||
createPlotAreaBackgroundItem(); | ||||
m_plotAreaBackground->setVisible(visible); | ||||
} | ||||
bool ChartPresenter::isPlotAreaBackgroundVisible() const | ||||
{ | ||||
if (!m_plotAreaBackground) | ||||
return false; | ||||
return m_plotAreaBackground->isVisible(); | ||||
} | ||||
|
r1534 | void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled) | ||
|
r855 | { | ||
|
r1534 | createBackgroundItem(); | ||
|
r1965 | m_background->setDropShadowEnabled(enabled); | ||
|
r855 | } | ||
|
r1534 | bool ChartPresenter::isBackgroundDropShadowEnabled() const | ||
|
r855 | { | ||
|
r2131 | if (!m_background) | ||
return false; | ||||
|
r1965 | return m_background->isDropShadowEnabled(); | ||
|
r855 | } | ||
|
r143 | |||
|
r1534 | |||
|
r2483 | AbstractChartLayout *ChartPresenter::layout() | ||
|
r1241 | { | ||
|
r1534 | return m_layout; | ||
|
r1241 | } | ||
|
r2131 | QLegend *ChartPresenter::legend() | ||
|
r1534 | { | ||
return m_chart->legend(); | ||||
} | ||||
|
r1965 | void ChartPresenter::setVisible(bool visible) | ||
{ | ||||
m_chart->setVisible(visible); | ||||
} | ||||
|
r2131 | ChartBackground *ChartPresenter::backgroundElement() | ||
|
r1965 | { | ||
return m_background; | ||||
} | ||||
|
r2498 | QAbstractGraphicsShapeItem *ChartPresenter::plotAreaElement() | ||
{ | ||||
return m_plotAreaBackground; | ||||
} | ||||
|
r2483 | QList<ChartAxisElement *> ChartPresenter::axisItems() const | ||
|
r1534 | { | ||
|
r2273 | return m_axisItems; | ||
|
r1534 | } | ||
|
r2273 | QList<ChartItem *> ChartPresenter::chartItems() const | ||
|
r2105 | { | ||
|
r2273 | return m_chartItems; | ||
|
r2105 | } | ||
|
r2131 | ChartTitle *ChartPresenter::titleElement() | ||
|
r1534 | { | ||
|
r1965 | return m_title; | ||
|
r1241 | } | ||
|
r2539 | QRectF ChartPresenter::textBoundingRect(const QFont &font, const QString &text, qreal angle) | ||
{ | ||||
|
r2543 | static QGraphicsTextItem dummyTextItem; | ||
|
r2539 | |||
|
r2543 | dummyTextItem.setFont(font); | ||
dummyTextItem.setHtml(text); | ||||
QRectF boundingRect = dummyTextItem.boundingRect(); | ||||
|
r2539 | |||
// Take rotation into account | ||||
if (angle) { | ||||
QTransform transform; | ||||
transform.rotate(angle); | ||||
boundingRect = transform.mapRect(boundingRect); | ||||
} | ||||
return boundingRect; | ||||
} | ||||
// boundingRect parameter returns the rotated bounding rect of the text | ||||
QString ChartPresenter::truncatedText(const QFont &font, const QString &text, qreal angle, | ||||
qreal maxSize, Qt::Orientation constraintOrientation, | ||||
QRectF &boundingRect) | ||||
{ | ||||
QString truncatedString(text); | ||||
boundingRect = textBoundingRect(font, truncatedString, angle); | ||||
qreal checkDimension = ((constraintOrientation == Qt::Horizontal) | ||||
? boundingRect.width() : boundingRect.height()); | ||||
if (checkDimension > maxSize) { | ||||
|
r2541 | // It can be assumed that almost any amount of string manipulation is faster | ||
// than calculating one bounding rectangle, so first prepare a list of truncated strings | ||||
// to try. | ||||
|
r2543 | static const char *truncateMatchString = "&#?[0-9a-zA-Z]*;$"; | ||
static QRegExp truncateMatcher(truncateMatchString); | ||||
|
r2541 | QVector<QString> testStrings(text.length()); | ||
int count(0); | ||||
static QLatin1Char closeTag('>'); | ||||
static QLatin1Char openTag('<'); | ||||
static QLatin1Char semiColon(';'); | ||||
static QLatin1String ellipsis("..."); | ||||
while (truncatedString.length() > 1) { | ||||
int chopIndex(-1); | ||||
int chopCount(1); | ||||
QChar lastChar(truncatedString.at(truncatedString.length() - 1)); | ||||
if (lastChar == closeTag) | ||||
chopIndex = truncatedString.lastIndexOf(openTag); | ||||
else if (lastChar == semiColon) | ||||
|
r2543 | chopIndex = truncateMatcher.indexIn(truncatedString, 0); | ||
|
r2541 | |||
if (chopIndex != -1) | ||||
chopCount = truncatedString.length() - chopIndex; | ||||
truncatedString.chop(chopCount); | ||||
testStrings[count] = truncatedString + ellipsis; | ||||
count++; | ||||
} | ||||
|
r2539 | |||
|
r2541 | // Binary search for best fit | ||
int minIndex(0); | ||||
int maxIndex(count - 1); | ||||
int bestIndex(count); | ||||
QRectF checkRect; | ||||
while (maxIndex >= minIndex) { | ||||
int mid = (maxIndex + minIndex) / 2; | ||||
checkRect = textBoundingRect(font, testStrings.at(mid), angle); | ||||
|
r2539 | checkDimension = ((constraintOrientation == Qt::Horizontal) | ||
|
r2541 | ? checkRect.width() : checkRect.height()); | ||
if (checkDimension > maxSize) { | ||||
// Checked index too large, all under this are also too large | ||||
minIndex = mid + 1; | ||||
} else { | ||||
// Checked index fits, all over this also fit | ||||
maxIndex = mid - 1; | ||||
bestIndex = mid; | ||||
boundingRect = checkRect; | ||||
} | ||||
} | ||||
// Default to "..." if nothing fits | ||||
if (bestIndex == count) { | ||||
boundingRect = textBoundingRect(font, ellipsis, angle); | ||||
truncatedString = ellipsis; | ||||
} else { | ||||
truncatedString = testStrings.at(bestIndex); | ||||
|
r2539 | } | ||
} | ||||
return truncatedString; | ||||
} | ||||
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||