| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtCharts/QVPieModelMapper> |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | /*! |
| 9 | \class QVPieModelMapper |
| 10 | \inmodule QtCharts |
| 11 | \brief The QVPieModelMapper is a vertical model mapper for pie series. |
| 12 | |
| 13 | Model mappers enable using a data model derived from the QAbstractItemModel class |
| 14 | as a data source for a chart. A vertical model mapper is used to create a connection |
| 15 | between a data model and QPieSeries, so that each row in the data model defines a |
| 16 | pie slice and each column maps to the label or the value of the pie slice. |
| 17 | |
| 18 | Both model and pie series properties can be used to manipulate the data. The model |
| 19 | mapper keeps the pie series and the data model in sync. |
| 20 | */ |
| 21 | /*! |
| 22 | \qmltype VPieModelMapper |
| 23 | \nativetype QVPieModelMapper |
| 24 | \inqmlmodule QtCharts |
| 25 | |
| 26 | \brief Vertical model mapper for pie series. |
| 27 | |
| 28 | Model mappers enable using a data model derived from the QAbstractItemModel class |
| 29 | as a data source for a chart. A vertical model mapper is used to create a connection |
| 30 | between a data model and PieSeries, so that each row in the data model defines a |
| 31 | pie slice and each column maps to the label or the value of the pie slice. |
| 32 | |
| 33 | Both model and pie series properties can be used to manipulate the data. The model |
| 34 | mapper keeps the pie series and the data model in sync. |
| 35 | |
| 36 | The following QML example creates a pie series with four slices (assuming the model has at |
| 37 | least five rows). Each slice gets a label from column 1 and a value from column 2. |
| 38 | \code |
| 39 | VPieModelMapper { |
| 40 | series: pieSeries |
| 41 | model: customModel |
| 42 | labelsColumn: 1 |
| 43 | valuesColumn: 2 |
| 44 | firstRow: 1 |
| 45 | rowCount: 4 |
| 46 | } |
| 47 | \endcode |
| 48 | */ |
| 49 | |
| 50 | /*! |
| 51 | \property QVPieModelMapper::series |
| 52 | \brief The pie series that is used by the mapper. |
| 53 | |
| 54 | All the data in the series is discarded when it is set to the mapper. |
| 55 | When a new series is specified, the old series is disconnected (but it preserves its data). |
| 56 | */ |
| 57 | /*! |
| 58 | \qmlproperty PieSeries VPieModelMapper::series |
| 59 | The pie series that is used by the mapper. If you define the mapper element as a child for a |
| 60 | PieSeries, leave this property undefined. All the data in the series is discarded when it is set to the mapper. |
| 61 | When new series is specified the old series is disconnected (but it preserves its data). |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | \property QVPieModelMapper::model |
| 66 | \brief The model that is used by the mapper. |
| 67 | */ |
| 68 | /*! |
| 69 | \qmlproperty SomeModel VPieModelMapper::model |
| 70 | The QAbstractItemModel based model that is used by the mapper. You need to implement the model |
| 71 | and expose it to QML. |
| 72 | |
| 73 | \note The model has to support adding and removing rows or columns and modifying |
| 74 | the data in the cells. |
| 75 | */ |
| 76 | |
| 77 | /*! |
| 78 | \property QVPieModelMapper::valuesColumn |
| 79 | \brief The column of the model that is kept in sync with the values of the pie's slices. |
| 80 | |
| 81 | The default value is -1 (invalid mapping). |
| 82 | */ |
| 83 | /*! |
| 84 | \qmlproperty int VPieModelMapper::valuesColumn |
| 85 | The column of the model that is kept in sync with the values of the pie's slices. |
| 86 | The default value is -1 (invalid mapping). |
| 87 | */ |
| 88 | |
| 89 | /*! |
| 90 | \property QVPieModelMapper::labelsColumn |
| 91 | \brief The column of the model that is kept in sync with the labels of the pie's slices. |
| 92 | |
| 93 | The default value is -1 (invalid mapping). |
| 94 | */ |
| 95 | /*! |
| 96 | \qmlproperty int VPieModelMapper::labelsColumn |
| 97 | The column of the model that is kept in sync with the labels of the pie's slices. |
| 98 | The default value is -1 (invalid mapping). |
| 99 | */ |
| 100 | |
| 101 | /*! |
| 102 | \property QVPieModelMapper::firstRow |
| 103 | \brief The row of the model that contains the first slice value. |
| 104 | |
| 105 | The minimum and default value is 0. |
| 106 | */ |
| 107 | /*! |
| 108 | \qmlproperty int VPieModelMapper::firstRow |
| 109 | The row of the model that contains the first slice value. |
| 110 | The default value is 0. |
| 111 | */ |
| 112 | |
| 113 | /*! |
| 114 | \property QVPieModelMapper::rowCount |
| 115 | \brief The number of rows of the model that are mapped as the data for a pie series. |
| 116 | |
| 117 | The minimum and default value is -1 (number limited by the number of rows in the model). |
| 118 | */ |
| 119 | /*! |
| 120 | \qmlproperty int VPieModelMapper::rowCount |
| 121 | The number of rows of the model that are mapped as the data for a pie series. |
| 122 | The default value is -1 (number limited by the number of rows in the model). |
| 123 | */ |
| 124 | |
| 125 | /*! |
| 126 | \fn void QVPieModelMapper::seriesReplaced() |
| 127 | |
| 128 | This signal is emitted when the series that the mapper is connected to changes. |
| 129 | */ |
| 130 | |
| 131 | /*! |
| 132 | \fn void QVPieModelMapper::modelReplaced() |
| 133 | |
| 134 | This signal is emitted when the model that the mapper is connected to changes. |
| 135 | */ |
| 136 | |
| 137 | /*! |
| 138 | \fn void QVPieModelMapper::valuesColumnChanged() |
| 139 | |
| 140 | This signal is emitted when the values column changes. |
| 141 | */ |
| 142 | |
| 143 | /*! |
| 144 | \fn void QVPieModelMapper::labelsColumnChanged() |
| 145 | |
| 146 | This signal is emitted when the labels column changes. |
| 147 | */ |
| 148 | |
| 149 | /*! |
| 150 | \fn void QVPieModelMapper::firstRowChanged() |
| 151 | This signal is emitted when the first row changes. |
| 152 | */ |
| 153 | |
| 154 | /*! |
| 155 | \fn void QVPieModelMapper::rowCountChanged() |
| 156 | This signal is emitted when the number of rows changes. |
| 157 | */ |
| 158 | |
| 159 | /*! |
| 160 | Constructs a mapper object that is a child of \a parent. |
| 161 | */ |
| 162 | QVPieModelMapper::QVPieModelMapper(QObject *parent) : |
| 163 | QPieModelMapper(parent) |
| 164 | { |
| 165 | QPieModelMapper::setOrientation(Qt::Vertical); |
| 166 | } |
| 167 | |
| 168 | QAbstractItemModel *QVPieModelMapper::model() const |
| 169 | { |
| 170 | return QPieModelMapper::model(); |
| 171 | } |
| 172 | |
| 173 | void QVPieModelMapper::setModel(QAbstractItemModel *model) |
| 174 | { |
| 175 | if (model != QPieModelMapper::model()) { |
| 176 | QPieModelMapper::setModel(model); |
| 177 | emit modelReplaced(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | QPieSeries *QVPieModelMapper::series() const |
| 182 | { |
| 183 | return QPieModelMapper::series(); |
| 184 | } |
| 185 | |
| 186 | void QVPieModelMapper::setSeries(QPieSeries *series) |
| 187 | { |
| 188 | if (series != QPieModelMapper::series()) { |
| 189 | QPieModelMapper::setSeries(series); |
| 190 | emit seriesReplaced(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /*! |
| 195 | Returns the column of the model that is kept in sync with the values of the pie's slices. |
| 196 | */ |
| 197 | int QVPieModelMapper::valuesColumn() const |
| 198 | { |
| 199 | return QPieModelMapper::valuesSection(); |
| 200 | } |
| 201 | |
| 202 | /*! |
| 203 | Sets the model column that is kept in sync with the pie slices' values to \a valuesColumn. |
| 204 | */ |
| 205 | void QVPieModelMapper::setValuesColumn(int valuesColumn) |
| 206 | { |
| 207 | if (valuesColumn != valuesSection()) { |
| 208 | QPieModelMapper::setValuesSection(valuesColumn); |
| 209 | emit valuesColumnChanged(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /*! |
| 214 | Returns the column of the model that is kept in sync with the labels of the pie's slices. |
| 215 | */ |
| 216 | int QVPieModelMapper::labelsColumn() const |
| 217 | { |
| 218 | return QPieModelMapper::labelsSection(); |
| 219 | } |
| 220 | |
| 221 | /*! |
| 222 | Sets the model column that is kept in sync with the pies slices' labels to \a labelsColumn. |
| 223 | */ |
| 224 | void QVPieModelMapper::setLabelsColumn(int labelsColumn) |
| 225 | { |
| 226 | if (labelsColumn != labelsSection()) { |
| 227 | QPieModelMapper::setLabelsSection(labelsColumn); |
| 228 | emit labelsColumnChanged(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int QVPieModelMapper::firstRow() const |
| 233 | { |
| 234 | return first(); |
| 235 | } |
| 236 | |
| 237 | void QVPieModelMapper::setFirstRow(int firstRow) |
| 238 | { |
| 239 | if (firstRow != first()) { |
| 240 | setFirst(firstRow); |
| 241 | emit firstRowChanged(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | int QVPieModelMapper::rowCount() const |
| 246 | { |
| 247 | return count(); |
| 248 | } |
| 249 | |
| 250 | void QVPieModelMapper::setRowCount(int rowCount) |
| 251 | { |
| 252 | if (rowCount != count()) { |
| 253 | setCount(rowCount); |
| 254 | emit rowCountChanged(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | QT_END_NAMESPACE |
| 259 | |
| 260 | #include "moc_qvpiemodelmapper.cpp" |
| 261 | |