| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "declarativebars_p.h" |
| 5 | #include <QtCore/QMutexLocker> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | DeclarativeBars::DeclarativeBars(QQuickItem *parent) |
| 10 | : AbstractDeclarative(parent), |
| 11 | m_barsController(0) |
| 12 | { |
| 13 | setAcceptedMouseButtons(Qt::AllButtons); |
| 14 | |
| 15 | // Create the shared component on the main GUI thread. |
| 16 | m_barsController = new Bars3DController(boundingRect().toRect(), new Declarative3DScene); |
| 17 | AbstractDeclarative::setSharedController(m_barsController); |
| 18 | |
| 19 | QObject::connect(sender: m_barsController, signal: &Bars3DController::primarySeriesChanged, |
| 20 | context: this, slot: &DeclarativeBars::primarySeriesChanged); |
| 21 | QObject::connect(sender: m_barsController, signal: &Bars3DController::selectedSeriesChanged, |
| 22 | context: this, slot: &DeclarativeBars::selectedSeriesChanged); |
| 23 | } |
| 24 | |
| 25 | DeclarativeBars::~DeclarativeBars() |
| 26 | { |
| 27 | QMutexLocker locker(m_nodeMutex.data()); |
| 28 | const QMutexLocker locker2(mutex()); |
| 29 | delete m_barsController; |
| 30 | } |
| 31 | |
| 32 | QCategory3DAxis *DeclarativeBars::rowAxis() const |
| 33 | { |
| 34 | return static_cast<QCategory3DAxis *>(m_barsController->axisZ()); |
| 35 | } |
| 36 | |
| 37 | void DeclarativeBars::setRowAxis(QCategory3DAxis *axis) |
| 38 | { |
| 39 | m_barsController->setAxisZ(axis); |
| 40 | } |
| 41 | |
| 42 | QValue3DAxis *DeclarativeBars::valueAxis() const |
| 43 | { |
| 44 | return static_cast<QValue3DAxis *>(m_barsController->axisY()); |
| 45 | } |
| 46 | |
| 47 | void DeclarativeBars::setValueAxis(QValue3DAxis *axis) |
| 48 | { |
| 49 | m_barsController->setAxisY(axis); |
| 50 | } |
| 51 | |
| 52 | QCategory3DAxis *DeclarativeBars::columnAxis() const |
| 53 | { |
| 54 | return static_cast<QCategory3DAxis *>(m_barsController->axisX()); |
| 55 | } |
| 56 | |
| 57 | void DeclarativeBars::setColumnAxis(QCategory3DAxis *axis) |
| 58 | { |
| 59 | m_barsController->setAxisX(axis); |
| 60 | } |
| 61 | |
| 62 | void DeclarativeBars::setMultiSeriesUniform(bool uniform) |
| 63 | { |
| 64 | if (uniform != isMultiSeriesUniform()) { |
| 65 | m_barsController->setMultiSeriesScaling(uniform); |
| 66 | emit multiSeriesUniformChanged(uniform); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | bool DeclarativeBars::isMultiSeriesUniform() const |
| 71 | { |
| 72 | return m_barsController->multiSeriesScaling(); |
| 73 | } |
| 74 | |
| 75 | void DeclarativeBars::setBarThickness(float thicknessRatio) |
| 76 | { |
| 77 | if (thicknessRatio != barThickness()) { |
| 78 | m_barsController->setBarSpecs(thicknessRatio: GLfloat(thicknessRatio), spacing: barSpacing(), |
| 79 | relative: isBarSpacingRelative()); |
| 80 | emit barThicknessChanged(thicknessRatio); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | float DeclarativeBars::barThickness() const |
| 85 | { |
| 86 | return m_barsController->barThickness(); |
| 87 | } |
| 88 | |
| 89 | void DeclarativeBars::setBarSpacing(const QSizeF &spacing) |
| 90 | { |
| 91 | if (spacing != barSpacing()) { |
| 92 | m_barsController->setBarSpecs(thicknessRatio: GLfloat(barThickness()), spacing, relative: isBarSpacingRelative()); |
| 93 | emit barSpacingChanged(spacing); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | QSizeF DeclarativeBars::barSpacing() const |
| 98 | { |
| 99 | return m_barsController->barSpacing(); |
| 100 | } |
| 101 | |
| 102 | void DeclarativeBars::setBarSpacingRelative(bool relative) |
| 103 | { |
| 104 | if (relative != isBarSpacingRelative()) { |
| 105 | m_barsController->setBarSpecs(thicknessRatio: GLfloat(barThickness()), spacing: barSpacing(), relative); |
| 106 | emit barSpacingRelativeChanged(relative); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool DeclarativeBars::isBarSpacingRelative() const |
| 111 | { |
| 112 | return m_barsController->isBarSpecRelative(); |
| 113 | } |
| 114 | |
| 115 | void DeclarativeBars::setBarSeriesMargin(const QSizeF &margin) |
| 116 | { |
| 117 | if (margin != barSeriesMargin()) { |
| 118 | m_barsController->setBarSeriesMargin(margin); |
| 119 | emit barSeriesMarginChanged(margin: barSeriesMargin()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | QSizeF DeclarativeBars::barSeriesMargin() const |
| 124 | { |
| 125 | return m_barsController->barSeriesMargin(); |
| 126 | } |
| 127 | |
| 128 | QBar3DSeries *DeclarativeBars::selectedSeries() const |
| 129 | { |
| 130 | return m_barsController->selectedSeries(); |
| 131 | } |
| 132 | |
| 133 | void DeclarativeBars::setFloorLevel(float level) |
| 134 | { |
| 135 | if (level != floorLevel()) { |
| 136 | m_barsController->setFloorLevel(level); |
| 137 | emit floorLevelChanged(level); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | float DeclarativeBars::floorLevel() const |
| 142 | { |
| 143 | return m_barsController->floorLevel(); |
| 144 | } |
| 145 | |
| 146 | QQmlListProperty<QBar3DSeries> DeclarativeBars::seriesList() |
| 147 | { |
| 148 | return QQmlListProperty<QBar3DSeries>(this, this, |
| 149 | &DeclarativeBars::appendSeriesFunc, |
| 150 | &DeclarativeBars::countSeriesFunc, |
| 151 | &DeclarativeBars::atSeriesFunc, |
| 152 | &DeclarativeBars::clearSeriesFunc); |
| 153 | } |
| 154 | |
| 155 | void DeclarativeBars::appendSeriesFunc(QQmlListProperty<QBar3DSeries> *list, QBar3DSeries *series) |
| 156 | { |
| 157 | reinterpret_cast<DeclarativeBars *>(list->data)->addSeries(series); |
| 158 | } |
| 159 | |
| 160 | qsizetype DeclarativeBars::countSeriesFunc(QQmlListProperty<QBar3DSeries> *list) |
| 161 | { |
| 162 | return reinterpret_cast<DeclarativeBars *>(list->data)->m_barsController->barSeriesList().size(); |
| 163 | } |
| 164 | |
| 165 | QBar3DSeries *DeclarativeBars::atSeriesFunc(QQmlListProperty<QBar3DSeries> *list, qsizetype index) |
| 166 | { |
| 167 | return reinterpret_cast<DeclarativeBars *>(list->data)->m_barsController->barSeriesList().at(i: index); |
| 168 | } |
| 169 | |
| 170 | void DeclarativeBars::clearSeriesFunc(QQmlListProperty<QBar3DSeries> *list) |
| 171 | { |
| 172 | DeclarativeBars *declBars = reinterpret_cast<DeclarativeBars *>(list->data); |
| 173 | QList<QBar3DSeries *> realList = declBars->m_barsController->barSeriesList(); |
| 174 | int count = realList.size(); |
| 175 | for (int i = 0; i < count; i++) |
| 176 | declBars->removeSeries(series: realList.at(i)); |
| 177 | } |
| 178 | |
| 179 | void DeclarativeBars::addSeries(QBar3DSeries *series) |
| 180 | { |
| 181 | m_barsController->addSeries(series); |
| 182 | } |
| 183 | |
| 184 | void DeclarativeBars::removeSeries(QBar3DSeries *series) |
| 185 | { |
| 186 | m_barsController->removeSeries(series); |
| 187 | series->setParent(this); // Reparent as removing will leave series parentless |
| 188 | } |
| 189 | |
| 190 | void DeclarativeBars::insertSeries(int index, QBar3DSeries *series) |
| 191 | { |
| 192 | m_barsController->insertSeries(index, series); |
| 193 | } |
| 194 | |
| 195 | void DeclarativeBars::setPrimarySeries(QBar3DSeries *series) |
| 196 | { |
| 197 | m_barsController->setPrimarySeries(series); |
| 198 | } |
| 199 | |
| 200 | QBar3DSeries *DeclarativeBars::primarySeries() const |
| 201 | { |
| 202 | return m_barsController->primarySeries(); |
| 203 | } |
| 204 | |
| 205 | void DeclarativeBars::handleAxisXChanged(QAbstract3DAxis *axis) |
| 206 | { |
| 207 | emit columnAxisChanged(axis: static_cast<QCategory3DAxis *>(axis)); |
| 208 | } |
| 209 | |
| 210 | void DeclarativeBars::handleAxisYChanged(QAbstract3DAxis *axis) |
| 211 | { |
| 212 | emit valueAxisChanged(axis: static_cast<QValue3DAxis *>(axis)); |
| 213 | } |
| 214 | |
| 215 | void DeclarativeBars::handleAxisZChanged(QAbstract3DAxis *axis) |
| 216 | { |
| 217 | emit rowAxisChanged(axis: static_cast<QCategory3DAxis *>(axis)); |
| 218 | } |
| 219 | |
| 220 | QT_END_NAMESPACE |
| 221 | |