linechartitem.cpp
152 lines
| 4.4 KiB
| text/x-c
|
CppLexer
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r144 | #include "linechartitem_p.h" | ||
Michal Klocek
|
r349 | #include "qlineseries.h" | ||
Michal Klocek
|
r938 | #include "qlineseries_p.h" | ||
Michal Klocek
|
r131 | #include "chartpresenter_p.h" | ||
Michal Klocek
|
r21 | #include <QPainter> | ||
Michal Klocek
|
r1218 | #include <QGraphicsSceneMouseEvent> | ||
Michal Klocek
|
r144 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Michal Klocek
|
r21 | |||
Tero Ahola
|
r1792 | const qreal mouseEventMinWidth(12); | ||
Michal Klocek
|
r150 | |||
Jani Honkonen
|
r2104 | LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter) | ||
Jani Honkonen
|
r2097 | : XYChart(series, presenter), | ||
QGraphicsItem(presenter ? presenter->rootItem() : 0), | ||||
m_series(series), | ||||
m_pointsVisible(false) | ||||
Michal Klocek
|
r21 | { | ||
Marek Rosa
|
r2255 | setAcceptHoverEvents(true); | ||
Michal Klocek
|
r262 | setZValue(ChartPresenter::LineChartZValue); | ||
Jani Honkonen
|
r2097 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
Tero Ahola
|
r1342 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
Tero Ahola
|
r2067 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | ||
Michal Klocek
|
r439 | handleUpdated(); | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r144 | QRectF LineChartItem::boundingRect() const | ||
Tero Ahola
|
r103 | { | ||
Jani Honkonen
|
r2097 | return m_rect; | ||
Tero Ahola
|
r103 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r144 | QPainterPath LineChartItem::shape() const | ||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
r1819 | return m_path; | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r1217 | void LineChartItem::updateGeometry() | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r1819 | m_points = geometryPoints(); | ||
Michal Klocek
|
r1217 | |||
Jani Honkonen
|
r2097 | if (m_points.size() == 0) { | ||
Michal Klocek
|
r1269 | prepareGeometryChange(); | ||
Marek Rosa
|
r1085 | m_path = QPainterPath(); | ||
Michal Klocek
|
r1269 | m_rect = QRect(); | ||
Marek Rosa
|
r545 | return; | ||
} | ||||
Michal Klocek
|
r464 | |||
Michal Klocek
|
r1819 | QPainterPath linePath(m_points.at(0)); | ||
Michal Klocek
|
r464 | |||
Jani Honkonen
|
r2097 | if (m_pointsVisible) { | ||
Michal Klocek
|
r1819 | |||
int size = m_linePen.width(); | ||||
Jani Honkonen
|
r2097 | linePath.addEllipse(m_points.at(0), size, size); | ||
for (int i = 1; i < m_points.size(); i++) { | ||||
Michal Klocek
|
r1819 | linePath.lineTo(m_points.at(i)); | ||
Jani Honkonen
|
r2097 | linePath.addEllipse(m_points.at(i), size, size); | ||
Michal Klocek
|
r1819 | } | ||
} else { | ||||
Jani Honkonen
|
r2097 | for (int i = 1; i < m_points.size(); i++) | ||
Michal Klocek
|
r1819 | linePath.lineTo(m_points.at(i)); | ||
Michal Klocek
|
r465 | } | ||
Michal Klocek
|
r464 | |||
Tero Ahola
|
r1823 | m_linePath = linePath; | ||
Michal Klocek
|
r1819 | QPainterPathStroker stroker; | ||
Tero Ahola
|
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); | ||||
Michal Klocek
|
r1821 | stroker.setMiterLimit(m_linePen.miterLimit()); | ||
Tero Ahola
|
r1791 | |||
Michal Klocek
|
r1819 | prepareGeometryChange(); | ||
Tero Ahola
|
r1791 | |||
Michal Klocek
|
r1819 | m_path = stroker.createStroke(linePath); | ||
m_rect = m_path.boundingRect(); | ||||
Tero Ahola
|
r1791 | |||
Michal Klocek
|
r1218 | setPos(origin()); | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r392 | void LineChartItem::handleUpdated() | ||
Michal Klocek
|
r389 | { | ||
Tero Ahola
|
r1346 | setVisible(m_series->isVisible()); | ||
Tero Ahola
|
r2067 | setOpacity(m_series->opacity()); | ||
Michal Klocek
|
r544 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
Michal Klocek
|
r139 | update(); | ||
} | ||||
Michal Klocek
|
r391 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(widget) | ||
Q_UNUSED(option) | ||||
Tero Ahola
|
r1828 | painter->save(); | ||
Tero Ahola
|
r1346 | painter->setPen(m_linePen); | ||
Michal Klocek
|
r1819 | painter->setBrush(m_linePen.color()); | ||
Tero Ahola
|
r1346 | painter->setClipRect(clipRect()); | ||
Michal Klocek
|
r1819 | |||
if (m_pointsVisible) { | ||||
painter->drawPath(m_linePath); | ||||
Jani Honkonen
|
r2097 | } else { | ||
for (int i(1); i < m_points.size(); i++) | ||||
painter->drawLine(m_points.at(i - 1), m_points.at(i)); | ||||
Michal Klocek
|
r544 | } | ||
Tero Ahola
|
r1828 | painter->restore(); | ||
Michal Klocek
|
r391 | } | ||
Michal Klocek
|
r1218 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
emit XYChart::clicked(calculateDomainPoint(event->pos())); | ||||
Michal Klocek
|
r1747 | QGraphicsItem::mousePressEvent(event); | ||
Michal Klocek
|
r1218 | } | ||
Marek Rosa
|
r2255 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | ||
{ | ||||
emit XYChart::hovered(calculateDomainPoint(event->pos()), true); | ||||
Marek Rosa
|
r2256 | event->accept(); | ||
// QGraphicsItem::hoverEnterEvent(event); | ||||
Marek Rosa
|
r2255 | } | ||
void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
emit XYChart::hovered(calculateDomainPoint(event->pos()), false); | ||||
Marek Rosa
|
r2256 | event->accept(); | ||
// QGraphicsItem::hoverEnterEvent(event); | ||||
Marek Rosa
|
r2255 | } | ||
Michal Klocek
|
r144 | #include "moc_linechartitem_p.cpp" | ||
Michal Klocek
|
r131 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||