linechartitem.cpp
132 lines
| 3.8 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 | |||
Tero Ahola
|
r1346 | LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): | ||
XYChart(series, presenter), | ||||
QGraphicsItem(presenter ? presenter->rootItem() : 0), | ||||
m_series(series), | ||||
m_pointsVisible(false) | ||||
Michal Klocek
|
r21 | { | ||
Michal Klocek
|
r262 | setZValue(ChartPresenter::LineChartZValue); | ||
Michal Klocek
|
r938 | QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); | ||
Tero Ahola
|
r1342 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
Michal Klocek
|
r439 | handleUpdated(); | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r144 | QRectF LineChartItem::boundingRect() const | ||
Tero Ahola
|
r103 | { | ||
Michal Klocek
|
r131 | return m_rect; | ||
Tero Ahola
|
r103 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r144 | QPainterPath LineChartItem::shape() const | ||
Michal Klocek
|
r67 | { | ||
Tero Ahola
|
r1791 | // Increase the size of the path slightly to make mouse interactions more natural | ||
QPainterPathStroker s; | ||||
s.setCapStyle(Qt::RoundCap); | ||||
s.setJoinStyle(Qt::RoundJoin); | ||||
qreal spacing = qMax(mouseEventMinWidth, (qreal) m_linePen.width()); | ||||
s.setWidth(spacing); | ||||
return s.createStroke(m_path); | ||||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r1217 | void LineChartItem::updateGeometry() | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r1217 | const QVector<QPointF>& points = geometryPoints(); | ||
Marek Rosa
|
r545 | if(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
|
r544 | QPainterPath linePath(points.at(0)); | ||
Michal Klocek
|
r464 | |||
Michal Klocek
|
r465 | for(int i=1; i< points.size();i++) { | ||
Michal Klocek
|
r544 | linePath.lineTo(points.at(i)); | ||
Michal Klocek
|
r465 | } | ||
Michal Klocek
|
r464 | |||
Michal Klocek
|
r465 | prepareGeometryChange(); | ||
Tero Ahola
|
r1791 | |||
Michal Klocek
|
r544 | m_path = linePath; | ||
Tero Ahola
|
r1791 | |||
// When defining bounding rectangle, | ||||
// 1. take the line width into account (otherwise you will get drawing artifacts) and | ||||
// 2. take the shape into account (otherwise you will not get mouse events through on border | ||||
// areas). | ||||
const qreal sqrtOf2 = 1.414214; | ||||
const qreal spacing = qMax(mouseEventMinWidth / 2.0, | ||||
sqrtOf2 * (qreal) m_linePen.width() / 2.0); | ||||
m_rect = m_path.boundingRect().adjusted(-spacing, -spacing, spacing, spacing); | ||||
Michal Klocek
|
r1218 | setPos(origin()); | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r392 | void LineChartItem::handleUpdated() | ||
Michal Klocek
|
r389 | { | ||
Tero Ahola
|
r1346 | setVisible(m_series->isVisible()); | ||
Michal Klocek
|
r544 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
m_pointPen = m_series->pen(); | ||||
m_pointPen.setWidthF(2*m_pointPen.width()); | ||||
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
|
r1346 | painter->save(); | ||
painter->setPen(m_linePen); | ||||
painter->setClipRect(clipRect()); | ||||
Tero Ahola
|
r1780 | // Draw lines | ||
const QVector<QPointF> &points = geometryPoints(); | ||||
for (int i(1); i < points.size();i++) | ||||
painter->drawLine(points.at(i-1), points.at(i)); | ||||
// Draw points | ||||
Tero Ahola
|
r1346 | if (m_pointsVisible){ | ||
painter->setPen(m_pointPen); | ||||
painter->drawPoints(geometryPoints()); | ||||
Michal Klocek
|
r544 | } | ||
Tero Ahola
|
r1346 | 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 | } | ||
Michal Klocek
|
r144 | #include "moc_linechartitem_p.cpp" | ||
Michal Klocek
|
r131 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||