summaryrefslogtreecommitdiffstats
path: root/src/linechart/linechartitem.cpp
blob: 1f10d53e67302c413d0da1058ab1674dff586bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/****************************************************************************
**
** Copyright (C) 2013 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$
**
****************************************************************************/

#include "linechartitem_p.h"
#include "qlineseries.h"
#include "qlineseries_p.h"
#include "chartpresenter_p.h"
#include "abstractdomain_p.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>

QTCOMMERCIALCHART_BEGIN_NAMESPACE

const qreal mouseEventMinWidth(12);

LineChartItem::LineChartItem(QLineSeries *series,QGraphicsItem* item)
    : XYChart(series,item),
      m_series(series),
      m_pointsVisible(false)
{
    setAcceptHoverEvents(true);
    setZValue(ChartPresenter::LineChartZValue);
    QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
    QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
    QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
    handleUpdated();
}

QRectF LineChartItem::boundingRect() const
{
    return m_rect;
}

QPainterPath LineChartItem::shape() const
{
    return m_path;
}

void LineChartItem::updateGeometry()
{
    m_points = geometryPoints();

    if (m_points.size() == 0) {
        prepareGeometryChange();
        m_path = QPainterPath();
        m_linePath = QPainterPath();
        m_rect = QRect();
        return;
    }

    QPainterPath linePath(m_points.at(0));

    if (m_pointsVisible) {

        int size = m_linePen.width();
        linePath.addEllipse(m_points.at(0), size, size);
        linePath.moveTo(m_points.at(0));
        for (int i = 1; i < m_points.size(); i++) {
            linePath.lineTo(m_points.at(i));
            linePath.addEllipse(m_points.at(i), size, size);
            linePath.moveTo(m_points.at(i));
        }

    } else {
        for (int i = 1; i < m_points.size(); i++)
            linePath.lineTo(m_points.at(i));
    }

    m_linePath = linePath;

    QPainterPathStroker stroker;
    // 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);
    stroker.setMiterLimit(m_linePen.miterLimit());

    prepareGeometryChange();

    m_path = stroker.createStroke(linePath);
    m_rect = m_path.boundingRect();
}

void LineChartItem::handleUpdated()
{
    // If points visiblity has changed, a geometry update is needed.
    // 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()));
    setVisible(m_series->isVisible());
    setOpacity(m_series->opacity());
    m_pointsVisible = m_series->pointsVisible();
    m_linePen = m_series->pen();
    if (doGeometryUpdate)
        updateGeometry();
    update();
}

void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget)
    Q_UNUSED(option)

    painter->save();
    painter->setPen(m_linePen);
    painter->setClipRect(QRectF(QPointF(0,0),domain()->size()));

    if (m_pointsVisible) {
        painter->setBrush(m_linePen.color());
        painter->drawPath(m_linePath);
    } else {
        painter->setBrush(QBrush(Qt::NoBrush));
        if (m_linePen.style() != Qt::SolidLine) {
            // 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 {
            for (int i(1); i < m_points.size(); i++)
                painter->drawLine(m_points.at(i - 1), m_points.at(i));
        }
    }

    painter->restore();
}

void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit XYChart::clicked(domain()->calculateDomainPoint(event->pos()));
    QGraphicsItem::mousePressEvent(event);
}

void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true);
//    event->accept();
    QGraphicsItem::hoverEnterEvent(event);
}

void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false);
//    event->accept();
    QGraphicsItem::hoverEnterEvent(event);
}

#include "moc_linechartitem_p.cpp"

QTCOMMERCIALCHART_END_NAMESPACE