summaryrefslogtreecommitdiffstats
path: root/src/charts/piechart/piesliceitem.cpp
blob: b42fb0788f84e60e579388518e6ca81c1b5168d4 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/****************************************************************************
**
** Copyright (C) 2014 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 Enterprise Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise 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 <private/piesliceitem_p.h>
#include <private/piechartitem_p.h>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <private/chartpresenter_p.h>
#include <QtGui/QPainter>
#include <QtCore/QtMath>
#include <QtWidgets/QGraphicsSceneEvent>
#include <QtCore/QTime>
#include <QtGui/QTextDocument>
#include <QtCore/QDebug>

QT_CHARTS_BEGIN_NAMESPACE

QPointF offset(qreal angle, qreal length)
{
    qreal dx = qSin(angle * (M_PI / 180)) * length;
    qreal dy = qCos(angle * (M_PI / 180)) * length;
    return QPointF(dx, -dy);
}

PieSliceItem::PieSliceItem(QGraphicsItem *parent)
    : QGraphicsObject(parent),
      m_hovered(false),
      m_mousePressed(false)
{
    setAcceptHoverEvents(true);
    setAcceptedMouseButtons(Qt::MouseButtonMask);
    setZValue(ChartPresenter::PieSeriesZValue);
    setFlag(QGraphicsItem::ItemIsSelectable);
    m_labelItem = new QGraphicsTextItem(this);
    m_labelItem->document()->setDocumentMargin(1.0);
}

PieSliceItem::~PieSliceItem()
{
    // If user is hovering over the slice and it gets destroyed we do
    // not get a hover leave event. So we must emit the signal here.
    if (m_hovered)
        emit hovered(false);
}

QRectF PieSliceItem::boundingRect() const
{
    return m_boundingRect;
}

QPainterPath PieSliceItem::shape() const
{
    // Don't include the label and label arm.
    // This is used to detect a mouse clicks. We do not want clicks from label.
    return m_slicePath;
}

void PieSliceItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
    painter->save();
    painter->setClipRect(parentItem()->boundingRect());
    painter->setPen(m_data.m_slicePen);
    painter->setBrush(m_data.m_sliceBrush);
    painter->drawPath(m_slicePath);
    painter->restore();

    if (m_data.m_isLabelVisible) {
        painter->save();

        // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
        painter->setBrush(m_data.m_labelBrush);

        if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
            painter->setClipRect(parentItem()->boundingRect());
            painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
        }

        painter->restore();
    }
}

void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent * /*event*/)
{
    m_hovered = true;
    emit hovered(true);
}

void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * /*event*/)
{
    m_hovered = false;
    emit hovered(false);
}

void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit pressed(event->buttons());
    m_lastMousePos = event->pos();
    m_mousePressed = true;
}

void PieSliceItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    emit released(event->buttons());
    if (m_lastMousePos == event->pos() && m_mousePressed)
        emit clicked(event->buttons());
}

void PieSliceItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    // For Pie slice a press signal needs to be explicitly fired for mouseDoubleClickEvent
    emit pressed(event->buttons());
    emit doubleClicked(event->buttons());
}

void PieSliceItem::setLayout(const PieSliceData &sliceData)
{
    m_data = sliceData;
    updateGeometry();
    update();
}

void PieSliceItem::updateGeometry()
{
    if (m_data.m_radius <= 0)
        return;

    prepareGeometryChange();

    // slice path
    qreal centerAngle;
    QPointF armStart;
    m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);

    m_labelItem->setVisible(m_data.m_isLabelVisible);

    if (m_data.m_isLabelVisible) {
        // text rect
        m_labelTextRect = ChartPresenter::textBoundingRect(m_data.m_labelFont,
                                                           m_data.m_labelText,
                                                           0);

        QString label(m_data.m_labelText);
        m_labelItem->setDefaultTextColor(m_data.m_labelBrush.color());
        m_labelItem->setFont(m_data.m_labelFont);

        // text position
        if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
            setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);

            // label arm path
            QPointF labelTextStart;
            m_labelArmPath = labelArmPath(armStart, centerAngle,
                                          m_data.m_radius * m_data.m_labelArmLengthFactor,
                                          m_labelTextRect.width(), &labelTextStart);

            m_labelTextRect.moveBottomLeft(labelTextStart);
            if (m_labelTextRect.left() < 0)
                m_labelTextRect.setLeft(0);
            else if (m_labelTextRect.left() < parentItem()->boundingRect().left())
                m_labelTextRect.setLeft(parentItem()->boundingRect().left());
            if (m_labelTextRect.right() > parentItem()->boundingRect().right())
                m_labelTextRect.setRight(parentItem()->boundingRect().right());

            label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText,
                                                  qreal(0.0), m_labelTextRect.width(),
                                                  m_labelTextRect.height(), m_labelTextRect);
            m_labelArmPath = labelArmPath(armStart, centerAngle,
                                          m_data.m_radius * m_data.m_labelArmLengthFactor,
                                          m_labelTextRect.width(), &labelTextStart);
            m_labelTextRect.moveBottomLeft(labelTextStart);

            m_labelItem->setTextWidth(m_labelTextRect.width()
                                      + m_labelItem->document()->documentMargin());
            m_labelItem->setHtml(label);
            m_labelItem->setRotation(0);
            m_labelItem->setPos(m_labelTextRect.x(), m_labelTextRect.y() + 1.0);
        } else {
            // label inside
            setFlag(QGraphicsItem::ItemClipsChildrenToShape);
            m_labelItem->setTextWidth(m_labelTextRect.width()
                                      + m_labelItem->document()->documentMargin());
            m_labelItem->setHtml(label);

            QPointF textCenter;
            if (m_data.m_holeRadius > 0) {
                textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius
                                                      + (m_data.m_radius
                                                         - m_data.m_holeRadius) / 2);
            } else {
                textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
            }
            m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2,
                                textCenter.y() - m_labelTextRect.height() / 2);

            QPointF labelCenter = m_labelItem->boundingRect().center();
            m_labelItem->setTransformOriginPoint(labelCenter);

            if (m_data.m_labelPosition == QPieSlice::LabelInsideTangential) {
                m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2);
            } else if (m_data.m_labelPosition == QPieSlice::LabelInsideNormal) {
                if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180)
                    m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90);
                else
                    m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90);
            } else {
                m_labelItem->setRotation(0);
            }
        }
        // Hide label if it's outside the bounding rect of parent item
        QRectF labelRect(m_labelItem->boundingRect());
        labelRect.moveTopLeft(m_labelItem->pos());
        if ((parentItem()->boundingRect().left()
                    < (labelRect.left() + m_labelItem->document()->documentMargin() + 1.0))
                && (parentItem()->boundingRect().right()
                    > (labelRect.right() - m_labelItem->document()->documentMargin() - 1.0))
                && (parentItem()->boundingRect().top()
                    < (labelRect.top() + m_labelItem->document()->documentMargin() + 1.0))
                && (parentItem()->boundingRect().bottom()
                    > (labelRect.bottom() - m_labelItem->document()->documentMargin() - 1.0)))
            m_labelItem->show();
        else
            m_labelItem->hide();
    }

    //  bounding rect
    if (m_data.m_isLabelVisible)
        m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
    else
        m_boundingRect = m_slicePath.boundingRect();

    // Inflate bounding rect by 2/3 pen width to make sure it encompasses whole slice also for thick pens
    // and miter joins.
    int penWidth = (m_data.m_slicePen.width() * 2) / 3;
    m_boundingRect = m_boundingRect.adjusted(-penWidth, -penWidth, penWidth, penWidth);
}

QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
{
    if (slice->isExploded()) {
        qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2);
        qreal len = radius * slice->explodeDistanceFactor();
        point += offset(centerAngle, len);
    }
    return point;
}

QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart)
{
    // calculate center angle
    *centerAngle = startAngle + (angleSpan / 2);

    // calculate slice rectangle
    QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2);

    // slice path
    QPainterPath path;
    if (m_data.m_holeRadius > 0) {
        QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2);
        path.arcMoveTo(rect, -startAngle + 90);
        path.arcTo(rect, -startAngle + 90, -angleSpan);
        path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
        path.closeSubpath();
    } else {
        path.moveTo(rect.center());
        path.arcTo(rect, -startAngle + 90, -angleSpan);
        path.closeSubpath();
    }

    // calculate label arm start point
    *armStart = center;
    *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);

    return path;
}

QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
{
    // Normalize the angle to 0-360 range
    // NOTE: We are using int here on purpose. Depenging on platform and hardware
    // qreal can be a double, float or something the user gives to the Qt configure
    // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
    // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
    // that might break we just use int. Precision for this is just fine for our needs.
    int normalized = angle * 10.0;
    normalized = normalized % 3600;
    if (normalized < 0)
        normalized += 3600;
    angle = (qreal) normalized / 10.0;

    // prevent label arm pointing straight down because it will look bad
    if (angle < 180 && angle > 170)
        angle = 170;
    if (angle > 180 && angle < 190)
        angle = 190;

    // line from slice to label
    QPointF parm1 = start + offset(angle, length);

    // line to underline the label
    QPointF parm2 = parm1;
    if (angle < 180) { // arm swings the other way on the left side
        parm2 += QPointF(textWidth, 0);
        *textStart = parm1;
    } else {
        parm2 += QPointF(-textWidth, 0);
        *textStart = parm2;
    }

    QPainterPath path;
    path.moveTo(start);
    path.lineTo(parm1);
    path.lineTo(parm2);

    return path;
}

#include "moc_piesliceitem_p.cpp"

QT_CHARTS_END_NAMESPACE