linechartitem.cpp
137 lines
| 3.9 KiB
| text/x-c
|
CppLexer
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r144 | #include "linechartitem_p.h" | ||
|
r349 | #include "qlineseries.h" | ||
|
r938 | #include "qlineseries_p.h" | ||
|
r131 | #include "chartpresenter_p.h" | ||
|
r21 | #include <QPainter> | ||
|
r1218 | #include <QGraphicsSceneMouseEvent> | ||
|
r144 | |||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r21 | |||
|
r1792 | const qreal mouseEventMinWidth(12); | ||
|
r150 | |||
|
r2104 | LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter) | ||
|
r2097 | : XYChart(series, presenter), | ||
QGraphicsItem(presenter ? presenter->rootItem() : 0), | ||||
m_series(series), | ||||
m_pointsVisible(false) | ||||
|
r21 | { | ||
|
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())); | ||
|
r439 | handleUpdated(); | ||
|
r21 | } | ||
|
r144 | QRectF LineChartItem::boundingRect() const | ||
|
r103 | { | ||
|
r2097 | return m_rect; | ||
|
r103 | } | ||
|
r85 | |||
|
r144 | QPainterPath LineChartItem::shape() const | ||
|
r67 | { | ||
|
r1819 | return m_path; | ||
|
r131 | } | ||
|
r85 | |||
|
r1217 | void LineChartItem::updateGeometry() | ||
|
r131 | { | ||
|
r1819 | m_points = geometryPoints(); | ||
|
r1217 | |||
|
r2097 | if (m_points.size() == 0) { | ||
|
r1269 | prepareGeometryChange(); | ||
|
r1085 | m_path = QPainterPath(); | ||
|
r1269 | m_rect = QRect(); | ||
|
r545 | return; | ||
} | ||||
|
r464 | |||
|
r1819 | QPainterPath linePath(m_points.at(0)); | ||
|
r464 | |||
|
r2097 | if (m_pointsVisible) { | ||
|
r1819 | |||
int size = m_linePen.width(); | ||||
|
r2097 | linePath.addEllipse(m_points.at(0), size, size); | ||
for (int i = 1; i < m_points.size(); i++) { | ||||
|
r1819 | linePath.lineTo(m_points.at(i)); | ||
|
r2097 | linePath.addEllipse(m_points.at(i), size, size); | ||
|
r1819 | } | ||
} else { | ||||
|
r2097 | for (int i = 1; i < m_points.size(); i++) | ||
|
r1819 | linePath.lineTo(m_points.at(i)); | ||
|
r465 | } | ||
|
r464 | |||
|
r1823 | m_linePath = linePath; | ||
|
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. | ||||
stroker.setWidth(m_linePen.width() * 1.42); | ||||
stroker.setJoinStyle(Qt::MiterJoin); | ||||
stroker.setCapStyle(Qt::SquareCap); | ||||
|
r1821 | stroker.setMiterLimit(m_linePen.miterLimit()); | ||
|
r1791 | |||
|
r1819 | prepareGeometryChange(); | ||
|
r1791 | |||
|
r1819 | m_path = stroker.createStroke(linePath); | ||
m_rect = m_path.boundingRect(); | ||||
|
r1791 | |||
|
r1218 | setPos(origin()); | ||
|
r21 | } | ||
|
r392 | void LineChartItem::handleUpdated() | ||
|
r389 | { | ||
|
r1346 | setVisible(m_series->isVisible()); | ||
|
r2067 | setOpacity(m_series->opacity()); | ||
|
r544 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
|
r139 | update(); | ||
} | ||||
|
r391 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r611 | Q_UNUSED(widget) | ||
Q_UNUSED(option) | ||||
|
r1828 | painter->save(); | ||
|
r1346 | painter->setPen(m_linePen); | ||
|
r1819 | painter->setBrush(m_linePen.color()); | ||
|
r1346 | painter->setClipRect(clipRect()); | ||
|
r1819 | |||
if (m_pointsVisible) { | ||||
painter->drawPath(m_linePath); | ||||
|
r2097 | } else { | ||
for (int i(1); i < m_points.size(); i++) | ||||
painter->drawLine(m_points.at(i - 1), m_points.at(i)); | ||||
|
r544 | } | ||
|
r1828 | painter->restore(); | ||
|
r391 | } | ||
|
r1218 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
emit XYChart::clicked(calculateDomainPoint(event->pos())); | ||||
|
r1747 | QGraphicsItem::mousePressEvent(event); | ||
|
r1218 | } | ||
|
r144 | #include "moc_linechartitem_p.cpp" | ||
|
r131 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||