| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 | |
| 6 | #include "declarativepieseries_p.h" |
| 7 | #include <QtCharts/QPieSlice> |
| 8 | #include <QtCharts/QVPieModelMapper> |
| 9 | #include <QtCharts/QHPieModelMapper> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | DeclarativePieSlice::DeclarativePieSlice(QObject *parent) |
| 14 | : QPieSlice(parent) |
| 15 | { |
| 16 | connect(sender: this, SIGNAL(brushChanged()), receiver: this, SLOT(handleBrushChanged())); |
| 17 | } |
| 18 | |
| 19 | QString DeclarativePieSlice::brushFilename() const |
| 20 | { |
| 21 | return m_brushFilename; |
| 22 | } |
| 23 | |
| 24 | void DeclarativePieSlice::setBrushFilename(const QString &brushFilename) |
| 25 | { |
| 26 | QImage brushImage(brushFilename); |
| 27 | if (QPieSlice::brush().textureImage() != brushImage) { |
| 28 | QBrush brush = QPieSlice::brush(); |
| 29 | brush.setTextureImage(brushImage); |
| 30 | QPieSlice::setBrush(brush); |
| 31 | m_brushFilename = brushFilename; |
| 32 | m_brushImage = brushImage; |
| 33 | emit brushFilenameChanged(brushFilename); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void DeclarativePieSlice::handleBrushChanged() |
| 38 | { |
| 39 | // If the texture image of the brush has changed along the brush |
| 40 | // the brush file name needs to be cleared. |
| 41 | if (!m_brushFilename.isEmpty() && QPieSlice::brush().textureImage() != m_brushImage) { |
| 42 | m_brushFilename.clear(); |
| 43 | emit brushFilenameChanged(brushFilename: QString()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Declarative pie series ========================================================================= |
| 48 | DeclarativePieSeries::DeclarativePieSeries(QQuickItem *parent) : |
| 49 | QPieSeries(parent) |
| 50 | { |
| 51 | connect(sender: this, SIGNAL(added(QList<QPieSlice*>)), receiver: this, SLOT(handleAdded(QList<QPieSlice*>))); |
| 52 | connect(sender: this, SIGNAL(removed(QList<QPieSlice*>)), receiver: this, SLOT(handleRemoved(QList<QPieSlice*>))); |
| 53 | } |
| 54 | |
| 55 | void DeclarativePieSeries::classBegin() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void DeclarativePieSeries::componentComplete() |
| 60 | { |
| 61 | foreach (QObject *child, children()) { |
| 62 | if (qobject_cast<QPieSlice *>(object: child)) { |
| 63 | QPieSeries::append(slice: qobject_cast<QPieSlice *>(object: child)); |
| 64 | } else if (qobject_cast<QVPieModelMapper *>(object: child)) { |
| 65 | QVPieModelMapper *mapper = qobject_cast<QVPieModelMapper *>(object: child); |
| 66 | mapper->setSeries(this); |
| 67 | } else if (qobject_cast<QHPieModelMapper *>(object: child)) { |
| 68 | QHPieModelMapper *mapper = qobject_cast<QHPieModelMapper *>(object: child); |
| 69 | mapper->setSeries(this); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | QQmlListProperty<QObject> DeclarativePieSeries::seriesChildren() |
| 75 | { |
| 76 | return QQmlListProperty<QObject>(this, 0, &DeclarativePieSeries::appendSeriesChildren ,0,0,0); |
| 77 | } |
| 78 | |
| 79 | void DeclarativePieSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element) |
| 80 | { |
| 81 | // Empty implementation; the children are parsed in componentComplete instead |
| 82 | Q_UNUSED(list); |
| 83 | Q_UNUSED(element); |
| 84 | } |
| 85 | |
| 86 | QPieSlice *DeclarativePieSeries::at(int index) |
| 87 | { |
| 88 | QList<QPieSlice *> sliceList = slices(); |
| 89 | if (index >= 0 && index < sliceList.size()) |
| 90 | return sliceList[index]; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | QPieSlice *DeclarativePieSeries::find(QString label) |
| 96 | { |
| 97 | foreach (QPieSlice *slice, slices()) { |
| 98 | if (slice->label() == label) |
| 99 | return slice; |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | DeclarativePieSlice *DeclarativePieSeries::append(QString label, qreal value) |
| 105 | { |
| 106 | DeclarativePieSlice *slice = new DeclarativePieSlice(this); |
| 107 | slice->setLabel(label); |
| 108 | slice->setValue(value); |
| 109 | if (QPieSeries::append(slice)) |
| 110 | return slice; |
| 111 | delete slice; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | bool DeclarativePieSeries::remove(QPieSlice *slice) |
| 116 | { |
| 117 | return QPieSeries::remove(slice); |
| 118 | } |
| 119 | |
| 120 | void DeclarativePieSeries::clear() |
| 121 | { |
| 122 | QPieSeries::clear(); |
| 123 | } |
| 124 | |
| 125 | void DeclarativePieSeries::handleAdded(const QList<QPieSlice *> &slices) |
| 126 | { |
| 127 | for (auto *slice : slices) |
| 128 | emit sliceAdded(slice); |
| 129 | } |
| 130 | |
| 131 | void DeclarativePieSeries::handleRemoved(const QList<QPieSlice *> &slices) |
| 132 | { |
| 133 | for (auto *slice : slices) |
| 134 | emit sliceRemoved(slice); |
| 135 | } |
| 136 | |
| 137 | QT_END_NAMESPACE |
| 138 | |
| 139 | #include "moc_declarativepieseries_p.cpp" |
| 140 | |