linechartitem.cpp
139 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 | { | ||
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 | |||
Michal Klocek
|
r1819 | if(m_points.size()==0) | ||
Marek Rosa
|
r545 | { | ||
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 | |||
Michal Klocek
|
r1819 | if(m_pointsVisible) { | ||
int size = m_linePen.width(); | ||||
linePath.addEllipse(m_points.at(0),size,size); | ||||
for(int i=1; i< m_points.size();i++) { | ||||
linePath.lineTo(m_points.at(i)); | ||||
linePath.addEllipse(m_points.at(i),size,size); | ||||
} | ||||
} else { | ||||
for(int i=1; i< m_points.size();i++) { | ||||
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()); | ||
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); | ||||
} | ||||
else { | ||||
for (int i(1); i < m_points.size();i++) | ||||
Tero Ahola
|
r1823 | 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 | } | ||
Michal Klocek
|
r144 | #include "moc_linechartitem_p.cpp" | ||
Michal Klocek
|
r131 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||