linechartitem.cpp
457 lines
| 19.7 KiB
| text/x-c
|
CppLexer
|
r2845 | /****************************************************************************** | ||
|
r794 | ** | ||
|
r2845 | ** Copyright (C) 2015 The Qt Company Ltd. | ||
** Contact: http://www.qt.io/licensing/ | ||||
|
r794 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r794 | ** | ||
|
r2845 | ** $QT_BEGIN_LICENSE:COMM$ | ||
|
r794 | ** | ||
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
** and conditions see http://www.qt.io/terms-conditions. For further | ||||
** information use the contact form at http://www.qt.io/contact-us. | ||||
|
r794 | ** | ||
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
******************************************************************************/ | ||||
|
r794 | |||
|
r2714 | #include <private/linechartitem_p.h> | ||
#include <QtCharts/QLineSeries> | ||||
#include <private/qlineseries_p.h> | ||||
#include <private/chartpresenter_p.h> | ||||
#include <private/polardomain_p.h> | ||||
#include <private/chartthememanager_p.h> | ||||
#include <private/charttheme_p.h> | ||||
#include <QtGui/QPainter> | ||||
#include <QtWidgets/QGraphicsSceneMouseEvent> | ||||
|
r144 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r21 | |||
|
r1792 | const qreal mouseEventMinWidth(12); | ||
|
r150 | |||
|
r2483 | LineChartItem::LineChartItem(QLineSeries *series, QGraphicsItem *item) | ||
|
r2273 | : XYChart(series,item), | ||
|
r2097 | m_series(series), | ||
|
r2483 | m_pointsVisible(false), | ||
|
r2689 | m_chartType(QChart::ChartTypeUndefined), | ||
m_pointLabelsVisible(false), | ||||
|
r2695 | m_pointLabelsFormat(series->pointLabelsFormat()), | ||
|
r2689 | m_pointLabelsFont(series->pointLabelsFont()), | ||
|
r2739 | m_pointLabelsColor(series->pointLabelsColor()), | ||
|
r2815 | m_pointLabelsClipping(true), | ||
|
r2739 | m_mousePressed(false) | ||
|
r21 | { | ||
|
r2255 | setAcceptHoverEvents(true); | ||
|
r2739 | setFlag(QGraphicsItem::ItemIsSelectable); | ||
|
r262 | setZValue(ChartPresenter::LineChartZValue); | ||
|
r2097 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
|
r1342 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
|
r2067 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | ||
|
r2689 | QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)), | ||
this, SLOT(handleUpdated())); | ||||
QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)), | ||||
this, SLOT(handleUpdated())); | ||||
QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated())); | ||||
QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated())); | ||||
|
r2815 | QObject::connect(series, SIGNAL(pointLabelsClippingChanged(bool)), this, SLOT(handleUpdated())); | ||
|
r439 | handleUpdated(); | ||
|
r21 | } | ||
|
r144 | QRectF LineChartItem::boundingRect() const | ||
|
r103 | { | ||
|
r2097 | return m_rect; | ||
|
r103 | } | ||
|
r85 | |||
|
r144 | QPainterPath LineChartItem::shape() const | ||
|
r67 | { | ||
|
r2483 | return m_shapePath; | ||
|
r131 | } | ||
|
r85 | |||
|
r1217 | void LineChartItem::updateGeometry() | ||
|
r131 | { | ||
|
r2820 | if (m_series->useOpenGL()) { | ||
|
r2830 | if (!m_rect.isEmpty()) { | ||
|
r2820 | prepareGeometryChange(); | ||
|
r2830 | // Changed signal seems to trigger even with empty region | ||
m_rect = QRectF(); | ||||
|
r2820 | } | ||
update(); | ||||
return; | ||||
} | ||||
|
r2818 | // Store the points to a local variable so that the old line gets properly cleared | ||
// when animation starts. | ||||
m_linePoints = geometryPoints(); | ||||
const QVector<QPointF> &points = m_linePoints; | ||||
|
r1217 | |||
|
r2483 | if (points.size() == 0) { | ||
|
r1269 | prepareGeometryChange(); | ||
|
r2483 | m_fullPath = QPainterPath(); | ||
|
r2451 | m_linePath = QPainterPath(); | ||
|
r1269 | m_rect = QRect(); | ||
|
r545 | return; | ||
} | ||||
|
r464 | |||
|
r2483 | QPainterPath linePath; | ||
QPainterPath fullPath; | ||||
// Use worst case scenario to determine required margin. | ||||
qreal margin = m_linePen.width() * 1.42; | ||||
|
r464 | |||
|
r2483 | // Area series use component line series that aren't necessarily added to the chart themselves, | ||
// so check if chart type is forced before trying to obtain it from the chart. | ||||
QChart::ChartType chartType = m_chartType; | ||||
if (chartType == QChart::ChartTypeUndefined) | ||||
chartType = m_series->chart()->chartType(); | ||||
|
r1819 | |||
|
r2483 | // For polar charts, we need special handling for angular (horizontal) | ||
// points that are off-grid. | ||||
if (chartType == QChart::ChartTypePolar) { | ||||
QPainterPath linePathLeft; | ||||
QPainterPath linePathRight; | ||||
QPainterPath *currentSegmentPath = 0; | ||||
QPainterPath *previousSegmentPath = 0; | ||||
qreal minX = domain()->minX(); | ||||
qreal maxX = domain()->maxX(); | ||||
qreal minY = domain()->minY(); | ||||
|
r2491 | QPointF currentSeriesPoint = m_series->at(0); | ||
|
r2483 | QPointF currentGeometryPoint = points.at(0); | ||
QPointF previousGeometryPoint = points.at(0); | ||||
|
r1819 | int size = m_linePen.width(); | ||
|
r2483 | bool pointOffGrid = false; | ||
bool previousPointWasOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX); | ||||
qreal domainRadius = domain()->size().height() / 2.0; | ||||
const QPointF centerPoint(domainRadius, domainRadius); | ||||
if (!previousPointWasOffGrid) { | ||||
fullPath.moveTo(points.at(0)); | ||||
if (m_pointsVisible && currentSeriesPoint.y() >= minY) { | ||||
// Do not draw ellipses for points below minimum Y. | ||||
linePath.addEllipse(points.at(0), size, size); | ||||
fullPath.addEllipse(points.at(0), size, size); | ||||
linePath.moveTo(points.at(0)); | ||||
fullPath.moveTo(points.at(0)); | ||||
} | ||||
|
r1819 | } | ||
|
r2483 | qreal leftMarginLine = centerPoint.x() - margin; | ||
qreal rightMarginLine = centerPoint.x() + margin; | ||||
qreal horizontal = centerPoint.y(); | ||||
|
r464 | |||
|
r2489 | // See ScatterChartItem::updateGeometry() for explanation why seriesLastIndex is needed | ||
const int seriesLastIndex = m_series->count() - 1; | ||||
|
r2483 | for (int i = 1; i < points.size(); i++) { | ||
// Interpolating line fragments would be ugly when thick pen is used, | ||||
// so we work around it by utilizing three separate | ||||
// paths for line segments and clip those with custom regions at paint time. | ||||
// "Right" path contains segments that cross the axis line with visible point on the | ||||
// right side of the axis line, as well as segments that have one point within the margin | ||||
// on the right side of the axis line and another point on the right side of the chart. | ||||
// "Left" path contains points with similarly on the left side. | ||||
// "Full" path contains rest of the points. | ||||
// This doesn't yield perfect results always. E.g. when segment covers more than 90 | ||||
// degrees and both of the points are within the margin, one in the top half and one in the | ||||
// bottom half of the chart, the bottom one gets clipped incorrectly. | ||||
// However, this should be rare occurrence in any sensible chart. | ||||
|
r2491 | currentSeriesPoint = m_series->at(qMin(seriesLastIndex, i)); | ||
|
r2483 | currentGeometryPoint = points.at(i); | ||
pointOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX); | ||||
// Draw something unless both off-grid | ||||
if (!pointOffGrid || !previousPointWasOffGrid) { | ||||
QPointF intersectionPoint; | ||||
qreal y; | ||||
if (pointOffGrid != previousPointWasOffGrid) { | ||||
if (currentGeometryPoint.x() == previousGeometryPoint.x()) { | ||||
y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) / 2.0; | ||||
} else { | ||||
qreal ratio = (centerPoint.x() - currentGeometryPoint.x()) / (currentGeometryPoint.x() - previousGeometryPoint.x()); | ||||
y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) * ratio; | ||||
} | ||||
intersectionPoint = QPointF(centerPoint.x(), y); | ||||
} | ||||
bool dummyOk; // We know points are ok, but this is needed | ||||
|
r2771 | qreal currentAngle = 0; | ||
qreal previousAngle = 0; | ||||
if (const PolarDomain *pd = qobject_cast<const PolarDomain *>(domain())) { | ||||
currentAngle = pd->toAngularCoordinate(currentSeriesPoint.x(), dummyOk); | ||||
previousAngle = pd->toAngularCoordinate(m_series->at(i - 1).x(), dummyOk); | ||||
} else { | ||||
qWarning() << Q_FUNC_INFO << "Unexpected domain: " << domain(); | ||||
} | ||||
|
r2483 | if ((qAbs(currentAngle - previousAngle) > 180.0)) { | ||
// If the angle between two points is over 180 degrees (half X range), | ||||
// any direct segment between them becomes meaningless. | ||||
// In this case two line segments are drawn instead, from previous | ||||
// point to the center and from center to current point. | ||||
if ((previousAngle < 0.0 || (previousAngle <= 180.0 && previousGeometryPoint.x() < rightMarginLine)) | ||||
&& previousGeometryPoint.y() < horizontal) { | ||||
currentSegmentPath = &linePathRight; | ||||
} else if ((previousAngle > 360.0 || (previousAngle > 180.0 && previousGeometryPoint.x() > leftMarginLine)) | ||||
&& previousGeometryPoint.y() < horizontal) { | ||||
currentSegmentPath = &linePathLeft; | ||||
} else if (previousAngle > 0.0 && previousAngle < 360.0) { | ||||
currentSegmentPath = &linePath; | ||||
} else { | ||||
currentSegmentPath = 0; | ||||
} | ||||
if (currentSegmentPath) { | ||||
if (previousSegmentPath != currentSegmentPath) | ||||
currentSegmentPath->moveTo(previousGeometryPoint); | ||||
if (previousPointWasOffGrid) | ||||
fullPath.moveTo(intersectionPoint); | ||||
currentSegmentPath->lineTo(centerPoint); | ||||
fullPath.lineTo(centerPoint); | ||||
} | ||||
previousSegmentPath = currentSegmentPath; | ||||
if ((currentAngle < 0.0 || (currentAngle <= 180.0 && currentGeometryPoint.x() < rightMarginLine)) | ||||
&& currentGeometryPoint.y() < horizontal) { | ||||
currentSegmentPath = &linePathRight; | ||||
} else if ((currentAngle > 360.0 || (currentAngle > 180.0 &¤tGeometryPoint.x() > leftMarginLine)) | ||||
&& currentGeometryPoint.y() < horizontal) { | ||||
currentSegmentPath = &linePathLeft; | ||||
} else if (currentAngle > 0.0 && currentAngle < 360.0) { | ||||
currentSegmentPath = &linePath; | ||||
} else { | ||||
currentSegmentPath = 0; | ||||
} | ||||
if (currentSegmentPath) { | ||||
if (previousSegmentPath != currentSegmentPath) | ||||
currentSegmentPath->moveTo(centerPoint); | ||||
if (!previousSegmentPath) | ||||
fullPath.moveTo(centerPoint); | ||||
currentSegmentPath->lineTo(currentGeometryPoint); | ||||
if (pointOffGrid) | ||||
fullPath.lineTo(intersectionPoint); | ||||
else | ||||
fullPath.lineTo(currentGeometryPoint); | ||||
} | ||||
} else { | ||||
if (previousAngle < 0.0 || currentAngle < 0.0 | ||||
|| ((previousAngle <= 180.0 && currentAngle <= 180.0) | ||||
&& ((previousGeometryPoint.x() < rightMarginLine && previousGeometryPoint.y() < horizontal) | ||||
|| (currentGeometryPoint.x() < rightMarginLine && currentGeometryPoint.y() < horizontal)))) { | ||||
currentSegmentPath = &linePathRight; | ||||
} else if (previousAngle > 360.0 || currentAngle > 360.0 | ||||
|| ((previousAngle > 180.0 && currentAngle > 180.0) | ||||
&& ((previousGeometryPoint.x() > leftMarginLine && previousGeometryPoint.y() < horizontal) | ||||
|| (currentGeometryPoint.x() > leftMarginLine && currentGeometryPoint.y() < horizontal)))) { | ||||
currentSegmentPath = &linePathLeft; | ||||
} else { | ||||
currentSegmentPath = &linePath; | ||||
} | ||||
if (currentSegmentPath != previousSegmentPath) | ||||
currentSegmentPath->moveTo(previousGeometryPoint); | ||||
if (previousPointWasOffGrid) | ||||
fullPath.moveTo(intersectionPoint); | ||||
if (pointOffGrid) | ||||
fullPath.lineTo(intersectionPoint); | ||||
else | ||||
fullPath.lineTo(currentGeometryPoint); | ||||
currentSegmentPath->lineTo(currentGeometryPoint); | ||||
} | ||||
} else { | ||||
currentSegmentPath = 0; | ||||
} | ||||
previousPointWasOffGrid = pointOffGrid; | ||||
if (m_pointsVisible && !pointOffGrid && currentSeriesPoint.y() >= minY) { | ||||
linePath.addEllipse(points.at(i), size, size); | ||||
fullPath.addEllipse(points.at(i), size, size); | ||||
linePath.moveTo(points.at(i)); | ||||
fullPath.moveTo(points.at(i)); | ||||
} | ||||
previousSegmentPath = currentSegmentPath; | ||||
previousGeometryPoint = currentGeometryPoint; | ||||
} | ||||
m_linePathPolarRight = linePathRight; | ||||
m_linePathPolarLeft = linePathLeft; | ||||
// Note: This construction of m_fullpath is not perfect. The partial segments that are | ||||
// outside left/right clip regions at axis boundary still generate hover/click events, | ||||
// because shape doesn't get clipped. It doesn't seem possible to do sensibly. | ||||
} else { // not polar | ||||
linePath.moveTo(points.at(0)); | ||||
if (m_pointsVisible) { | ||||
int size = m_linePen.width(); | ||||
linePath.addEllipse(points.at(0), size, size); | ||||
linePath.moveTo(points.at(0)); | ||||
for (int i = 1; i < points.size(); i++) { | ||||
linePath.lineTo(points.at(i)); | ||||
linePath.addEllipse(points.at(i), size, size); | ||||
linePath.moveTo(points.at(i)); | ||||
} | ||||
} else { | ||||
for (int i = 1; i < points.size(); i++) | ||||
linePath.lineTo(points.at(i)); | ||||
} | ||||
fullPath = linePath; | ||||
} | ||||
|
r1823 | |||
|
r1819 | QPainterPathStroker stroker; | ||
|
r1823 | // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin. | ||
// This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and | ||||
// multiply line width with square root of two when defining shape and bounding rectangle. | ||||
|
r2483 | stroker.setWidth(margin); | ||
|
r1823 | stroker.setJoinStyle(Qt::MiterJoin); | ||
stroker.setCapStyle(Qt::SquareCap); | ||||
|
r1821 | stroker.setMiterLimit(m_linePen.miterLimit()); | ||
|
r1791 | |||
|
r2603 | QPainterPath checkShapePath = stroker.createStroke(fullPath); | ||
// Only zoom in if the bounding rects of the paths fit inside int limits. QWidget::update() uses | ||||
// a region that has to be compatible with QRect. | ||||
if (checkShapePath.boundingRect().height() <= INT_MAX | ||||
&& checkShapePath.boundingRect().width() <= INT_MAX | ||||
&& linePath.boundingRect().height() <= INT_MAX | ||||
&& linePath.boundingRect().width() <= INT_MAX | ||||
&& fullPath.boundingRect().height() <= INT_MAX | ||||
&& fullPath.boundingRect().width() <= INT_MAX) { | ||||
prepareGeometryChange(); | ||||
|
r1791 | |||
|
r2603 | m_linePath = linePath; | ||
m_fullPath = fullPath; | ||||
m_shapePath = checkShapePath; | ||||
|
r2483 | |||
|
r2603 | m_rect = m_shapePath.boundingRect(); | ||
} else { | ||||
update(); | ||||
} | ||||
|
r21 | } | ||
|
r392 | void LineChartItem::handleUpdated() | ||
|
r389 | { | ||
|
r2689 | // If points visibility has changed, a geometry update is needed. | ||
|
r2451 | // Also, if pen changes when points are visible, geometry update is needed. | ||
bool doGeometryUpdate = | ||||
(m_pointsVisible != m_series->pointsVisible()) | ||||
|| (m_series->pointsVisible() && (m_linePen != m_series->pen())); | ||||
|
r1346 | setVisible(m_series->isVisible()); | ||
|
r2067 | setOpacity(m_series->opacity()); | ||
|
r544 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
|
r2689 | m_pointLabelsFormat = m_series->pointLabelsFormat(); | ||
m_pointLabelsVisible = m_series->pointLabelsVisible(); | ||||
m_pointLabelsFont = m_series->pointLabelsFont(); | ||||
m_pointLabelsColor = m_series->pointLabelsColor(); | ||||
|
r2815 | m_pointLabelsClipping = m_series->pointLabelsClipping(); | ||
|
r2451 | if (doGeometryUpdate) | ||
updateGeometry(); | ||||
|
r139 | update(); | ||
} | ||||
|
r391 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r611 | Q_UNUSED(widget) | ||
Q_UNUSED(option) | ||||
|
r2820 | if (m_series->useOpenGL()) | ||
return; | ||||
|
r2483 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); | ||
|
r1828 | painter->save(); | ||
|
r1346 | painter->setPen(m_linePen); | ||
|
r2483 | bool alwaysUsePath = false; | ||
if (m_series->chart()->chartType() == QChart::ChartTypePolar) { | ||||
qreal halfWidth = domain()->size().width() / 2.0; | ||||
QRectF clipRectLeft = QRectF(0, 0, halfWidth, domain()->size().height()); | ||||
QRectF clipRectRight = QRectF(halfWidth, 0, halfWidth, domain()->size().height()); | ||||
QRegion fullPolarClipRegion(clipRect.toRect(), QRegion::Ellipse); | ||||
QRegion clipRegionLeft(fullPolarClipRegion.intersected(clipRectLeft.toRect())); | ||||
QRegion clipRegionRight(fullPolarClipRegion.intersected(clipRectRight.toRect())); | ||||
painter->setClipRegion(clipRegionLeft); | ||||
painter->drawPath(m_linePathPolarLeft); | ||||
painter->setClipRegion(clipRegionRight); | ||||
painter->drawPath(m_linePathPolarRight); | ||||
painter->setClipRegion(fullPolarClipRegion); | ||||
alwaysUsePath = true; // required for proper clipping | ||||
} else { | ||||
painter->setClipRect(clipRect); | ||||
} | ||||
|
r1819 | |||
|
r2781 | reversePainter(painter, clipRect); | ||
|
r1819 | if (m_pointsVisible) { | ||
|
r2457 | painter->setBrush(m_linePen.color()); | ||
|
r1819 | painter->drawPath(m_linePath); | ||
|
r2097 | } else { | ||
|
r2457 | painter->setBrush(QBrush(Qt::NoBrush)); | ||
|
r2483 | if (m_linePen.style() != Qt::SolidLine || alwaysUsePath) { | ||
|
r2457 | // If pen style is not solid line, always fall back to path painting | ||
// to ensure proper continuity of the pattern | ||||
painter->drawPath(m_linePath); | ||||
} else { | ||||
|
r2818 | for (int i(1); i < m_linePoints.size(); i++) | ||
painter->drawLine(m_linePoints.at(i - 1), m_linePoints.at(i)); | ||||
|
r2457 | } | ||
|
r544 | } | ||
|
r2457 | |||
|
r2781 | reversePainter(painter, clipRect); | ||
|
r2815 | if (m_pointLabelsVisible) { | ||
if (m_pointLabelsClipping) | ||||
painter->setClipping(true); | ||||
else | ||||
painter->setClipping(false); | ||||
|
r2818 | m_series->d_func()->drawSeriesPointLabels(painter, m_linePoints, m_linePen.width() / 2); | ||
|
r2815 | } | ||
|
r2689 | |||
|
r1828 | painter->restore(); | ||
|
r2689 | |||
|
r391 | } | ||
|
r1218 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
|
r2739 | emit XYChart::pressed(domain()->calculateDomainPoint(event->pos())); | ||
m_lastMousePos = event->pos(); | ||||
m_mousePressed = true; | ||||
|
r1747 | QGraphicsItem::mousePressEvent(event); | ||
|
r1218 | } | ||
|
r2255 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | ||
{ | ||||
|
r2273 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); | ||
|
r2356 | // event->accept(); | ||
QGraphicsItem::hoverEnterEvent(event); | ||||
|
r2255 | } | ||
void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
|
r2273 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); | ||
|
r2356 | // event->accept(); | ||
QGraphicsItem::hoverEnterEvent(event); | ||||
|
r2255 | } | ||
|
r2739 | void LineChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
|
r2746 | emit XYChart::released(domain()->calculateDomainPoint(m_lastMousePos)); | ||
if (m_mousePressed) | ||||
emit XYChart::clicked(domain()->calculateDomainPoint(m_lastMousePos)); | ||||
|
r2739 | m_mousePressed = false; | ||
QGraphicsItem::mouseReleaseEvent(event); | ||||
} | ||||
void LineChartItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) | ||||
{ | ||||
|
r2746 | emit XYChart::doubleClicked(domain()->calculateDomainPoint(m_lastMousePos)); | ||
|
r2739 | QGraphicsItem::mouseDoubleClickEvent(event); | ||
} | ||||
|
r144 | #include "moc_linechartitem_p.cpp" | ||
|
r131 | |||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||