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