| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtCore/QMetaMethod> |
| 5 | #include "qquickgraphsbarsseries_p.h" |
| 6 | #include "utils_p.h" |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | QQuickGraphsBar3DSeries::QQuickGraphsBar3DSeries(QObject *parent) |
| 11 | : QBar3DSeries(parent) |
| 12 | |
| 13 | { |
| 14 | QObject::connect(sender: this, |
| 15 | signal: &QBar3DSeries::selectedBarChanged, |
| 16 | context: this, |
| 17 | slot: &QQuickGraphsBar3DSeries::selectedBarChanged); |
| 18 | } |
| 19 | |
| 20 | QQuickGraphsBar3DSeries::~QQuickGraphsBar3DSeries() {} |
| 21 | |
| 22 | QQmlListProperty<QObject> QQuickGraphsBar3DSeries::seriesChildren() |
| 23 | { |
| 24 | return QQmlListProperty<QObject>(this, |
| 25 | this, |
| 26 | &QQuickGraphsBar3DSeries::appendSeriesChildren, |
| 27 | 0, |
| 28 | 0, |
| 29 | 0); |
| 30 | } |
| 31 | |
| 32 | void QQuickGraphsBar3DSeries::appendSeriesChildren(QQmlListProperty<QObject> *list, QObject *element) |
| 33 | { |
| 34 | QBarDataProxy *proxy = qobject_cast<QBarDataProxy *>(object: element); |
| 35 | if (proxy) |
| 36 | reinterpret_cast<QQuickGraphsBar3DSeries *>(list->data)->setDataProxy(proxy); |
| 37 | } |
| 38 | |
| 39 | void QQuickGraphsBar3DSeries::setSelectedBar(QPointF position) |
| 40 | { |
| 41 | QBar3DSeries::setSelectedBar(position.toPoint()); |
| 42 | } |
| 43 | |
| 44 | QPointF QQuickGraphsBar3DSeries::selectedBar() const |
| 45 | { |
| 46 | return QPointF(QBar3DSeries::selectedBar()); |
| 47 | } |
| 48 | |
| 49 | QPointF QQuickGraphsBar3DSeries::invalidSelectionPosition() const |
| 50 | { |
| 51 | return QPointF(QBar3DSeries::invalidSelectionPosition()); |
| 52 | } |
| 53 | |
| 54 | void QQuickGraphsBar3DSeries::setBaseGradient(QQuickGradient *gradient) |
| 55 | { |
| 56 | if (m_gradients.m_baseGradient != gradient) { |
| 57 | setGradientHelper(newGradient: gradient, memberGradient: m_gradients.m_baseGradient, type: GradientType::Base); |
| 58 | m_gradients.m_baseGradient = gradient; |
| 59 | Q_EMIT baseGradientChanged(gradient: m_gradients.m_baseGradient); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | QQuickGradient *QQuickGraphsBar3DSeries::baseGradient() const |
| 64 | { |
| 65 | return m_gradients.m_baseGradient; |
| 66 | } |
| 67 | |
| 68 | void QQuickGraphsBar3DSeries::setSingleHighlightGradient(QQuickGradient *gradient) |
| 69 | { |
| 70 | if (m_gradients.m_singleHighlightGradient != gradient) { |
| 71 | setGradientHelper(newGradient: gradient, memberGradient: m_gradients.m_singleHighlightGradient, type: GradientType::Single); |
| 72 | m_gradients.m_singleHighlightGradient = gradient; |
| 73 | Q_EMIT singleHighlightGradientChanged(gradient: m_gradients.m_singleHighlightGradient); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | QQuickGradient *QQuickGraphsBar3DSeries::singleHighlightGradient() const |
| 78 | { |
| 79 | return m_gradients.m_singleHighlightGradient; |
| 80 | } |
| 81 | |
| 82 | void QQuickGraphsBar3DSeries::setMultiHighlightGradient(QQuickGradient *gradient) |
| 83 | { |
| 84 | if (m_gradients.m_multiHighlightGradient != gradient) { |
| 85 | setGradientHelper(newGradient: gradient, memberGradient: m_gradients.m_multiHighlightGradient, type: GradientType::Multi); |
| 86 | m_gradients.m_multiHighlightGradient = gradient; |
| 87 | Q_EMIT multiHighlightGradientChanged(gradient: m_gradients.m_multiHighlightGradient); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | QQuickGradient *QQuickGraphsBar3DSeries::multiHighlightGradient() const |
| 92 | { |
| 93 | return m_gradients.m_multiHighlightGradient; |
| 94 | } |
| 95 | |
| 96 | QQmlListProperty<QQuickGraphsColor> QQuickGraphsBar3DSeries::rowColors() |
| 97 | { |
| 98 | return QQmlListProperty<QQuickGraphsColor>(this, |
| 99 | this, |
| 100 | &QQuickGraphsBar3DSeries::appendRowColorsFunc, |
| 101 | &QQuickGraphsBar3DSeries::countRowColorsFunc, |
| 102 | &QQuickGraphsBar3DSeries::atRowColorsFunc, |
| 103 | &QQuickGraphsBar3DSeries::clearRowColorsFunc); |
| 104 | } |
| 105 | |
| 106 | void QQuickGraphsBar3DSeries::appendRowColorsFunc(QQmlListProperty<QQuickGraphsColor> *list, |
| 107 | QQuickGraphsColor *color) |
| 108 | { |
| 109 | reinterpret_cast<QQuickGraphsBar3DSeries *>(list->data)->addColor(color); |
| 110 | } |
| 111 | |
| 112 | qsizetype QQuickGraphsBar3DSeries::countRowColorsFunc(QQmlListProperty<QQuickGraphsColor> *list) |
| 113 | { |
| 114 | return reinterpret_cast<QQuickGraphsBar3DSeries *>(list->data)->colorList().size(); |
| 115 | } |
| 116 | |
| 117 | QQuickGraphsColor *QQuickGraphsBar3DSeries::atRowColorsFunc( |
| 118 | QQmlListProperty<QQuickGraphsColor> *list, qsizetype index) |
| 119 | { |
| 120 | return reinterpret_cast<QQuickGraphsBar3DSeries *>(list->data)->colorList().at(i: index); |
| 121 | } |
| 122 | |
| 123 | void QQuickGraphsBar3DSeries::clearRowColorsFunc(QQmlListProperty<QQuickGraphsColor> *list) |
| 124 | { |
| 125 | reinterpret_cast<QQuickGraphsBar3DSeries *>(list->data)->clearColors(); |
| 126 | } |
| 127 | |
| 128 | void QQuickGraphsBar3DSeries::handleBaseGradientUpdate() |
| 129 | { |
| 130 | if (!m_gradients.m_baseGradient) |
| 131 | Utils::setSeriesGradient(series: this, gradient: m_gradients.m_baseGradient, type: GradientType::Base); |
| 132 | } |
| 133 | |
| 134 | void QQuickGraphsBar3DSeries::handleSingleHighlightGradientUpdate() |
| 135 | { |
| 136 | if (!m_gradients.m_singleHighlightGradient) |
| 137 | Utils::setSeriesGradient(series: this, gradient: m_gradients.m_singleHighlightGradient, type: GradientType::Single); |
| 138 | } |
| 139 | |
| 140 | void QQuickGraphsBar3DSeries::handleMultiHighlightGradientUpdate() |
| 141 | { |
| 142 | if (!m_gradients.m_multiHighlightGradient) |
| 143 | Utils::setSeriesGradient(series: this, gradient: m_gradients.m_multiHighlightGradient, type: GradientType::Multi); |
| 144 | } |
| 145 | |
| 146 | void QQuickGraphsBar3DSeries::handleRowColorUpdate() |
| 147 | { |
| 148 | qsizetype colorCount = m_rowColors.size(); |
| 149 | int changed = 0; |
| 150 | |
| 151 | QQuickGraphsColor *color = qobject_cast<QQuickGraphsColor *>(object: QObject::sender()); |
| 152 | for (int i = 0; i < colorCount; i++) { |
| 153 | if (color == m_rowColors.at(i)) { |
| 154 | changed = i; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | QList<QColor> list = QBar3DSeries::rowColors(); |
| 159 | list[changed] = m_rowColors.at(i: changed)->color(); |
| 160 | QBar3DSeries::setRowColors(list); |
| 161 | } |
| 162 | |
| 163 | void QQuickGraphsBar3DSeries::addColor(QQuickGraphsColor *color) |
| 164 | { |
| 165 | if (!color) { |
| 166 | qWarning(msg: "Color is invalid, use Color" ); |
| 167 | return; |
| 168 | } |
| 169 | clearDummyColors(); |
| 170 | m_rowColors.append(t: color); |
| 171 | connect(sender: color, |
| 172 | signal: &QQuickGraphsColor::colorChanged, |
| 173 | context: this, |
| 174 | slot: &QQuickGraphsBar3DSeries::handleRowColorUpdate); |
| 175 | QList<QColor> list = QBar3DSeries::rowColors(); |
| 176 | list.append(t: color->color()); |
| 177 | QBar3DSeries::setRowColors(list); |
| 178 | } |
| 179 | |
| 180 | QList<QQuickGraphsColor *> QQuickGraphsBar3DSeries::colorList() |
| 181 | { |
| 182 | if (m_rowColors.isEmpty()) { |
| 183 | m_dummyColors = true; |
| 184 | const QList<QColor> list = QBar3DSeries::rowColors(); |
| 185 | for (const QColor &item : list) { |
| 186 | QQuickGraphsColor *color = new QQuickGraphsColor(this); |
| 187 | color->setColor(item); |
| 188 | m_rowColors.append(t: color); |
| 189 | connect(sender: color, |
| 190 | signal: &QQuickGraphsColor::colorChanged, |
| 191 | context: this, |
| 192 | slot: &QQuickGraphsBar3DSeries::handleRowColorUpdate); |
| 193 | } |
| 194 | } |
| 195 | return m_rowColors; |
| 196 | } |
| 197 | |
| 198 | void QQuickGraphsBar3DSeries::clearColors() |
| 199 | { |
| 200 | clearDummyColors(); |
| 201 | for (const auto color : std::as_const(t&: m_rowColors)) |
| 202 | disconnect(sender: color, signal: 0, receiver: this, member: 0); |
| 203 | |
| 204 | m_rowColors.clear(); |
| 205 | QBar3DSeries::setRowColors(QList<QColor>()); |
| 206 | } |
| 207 | |
| 208 | void QQuickGraphsBar3DSeries::clearDummyColors() |
| 209 | { |
| 210 | if (m_dummyColors) { |
| 211 | qDeleteAll(c: m_rowColors); |
| 212 | m_rowColors.clear(); |
| 213 | m_dummyColors = false; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void QQuickGraphsBar3DSeries::setGradientHelper(QQuickGradient *newGradient, |
| 218 | QQuickGradient *memberGradient, |
| 219 | GradientType type) |
| 220 | { |
| 221 | if (memberGradient) |
| 222 | QObject::disconnect(sender: memberGradient, signal: 0, receiver: this, member: 0); |
| 223 | Utils::setSeriesGradient(series: this, gradient: newGradient, type); |
| 224 | memberGradient = newGradient; |
| 225 | if (memberGradient) { |
| 226 | switch (type) { |
| 227 | case GradientType::Base: |
| 228 | QObject::connect(sender: memberGradient, |
| 229 | signal: &QQuickGradient::updated, |
| 230 | context: this, |
| 231 | slot: &QQuickGraphsBar3DSeries::handleBaseGradientUpdate); |
| 232 | break; |
| 233 | case GradientType::Single: |
| 234 | QObject::connect(sender: memberGradient, |
| 235 | signal: &QQuickGradient::updated, |
| 236 | context: this, |
| 237 | slot: &QQuickGraphsBar3DSeries::handleSingleHighlightGradientUpdate); |
| 238 | break; |
| 239 | case GradientType::Multi: |
| 240 | QObject::connect(sender: memberGradient, |
| 241 | signal: &QQuickGradient::updated, |
| 242 | context: this, |
| 243 | slot: &QQuickGraphsBar3DSeries::handleMultiHighlightGradientUpdate); |
| 244 | break; |
| 245 | default: |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | QT_END_NAMESPACE |
| 252 | |