| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include <QtCharts/QHPieModelMapper> | 
| 5 |  | 
| 6 | QT_BEGIN_NAMESPACE | 
| 7 |  | 
| 8 | /*! | 
| 9 |     \class QHPieModelMapper | 
| 10 |     \inmodule QtCharts | 
| 11 |     \brief The QHPieModelMapper is a horizontal 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 horizontal model mapper is used to create a connection | 
| 15 |     between a data model and QPieSeries, so that each column in the data model defines a | 
| 16 |     pie slice and each row 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 HPieModelMapper | 
| 23 |     \nativetype QHPieModelMapper | 
| 24 |     \inqmlmodule QtCharts | 
| 25 |  | 
| 26 |     \brief Horizontal 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 horizontal model mapper is used to create a connection | 
| 30 |     between a data model and PieSeries, so that each column in the data model defines a | 
| 31 |     pie slice and each row 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 | 
| 37 |     at least five columns). Each slice gets a label from row 1 and a value from row 2. | 
| 38 |     \code | 
| 39 |         HPieModelMapper { | 
| 40 |             series: pieSeries | 
| 41 |             model: customModel | 
| 42 |             labelsRow: 1 | 
| 43 |             valuesRow: 2 | 
| 44 |             firstColumn: 1 | 
| 45 |             columnCount: 4 | 
| 46 |         } | 
| 47 |     \endcode | 
| 48 | */ | 
| 49 |  | 
| 50 | /*! | 
| 51 |     \property QHPieModelMapper::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 HPieModelMapper::series | 
| 59 |     The PieSeries object 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 a new series is specified, the old series is disconnected (but it preserves its data). | 
| 62 | */ | 
| 63 |  | 
| 64 | /*! | 
| 65 |     \property QHPieModelMapper::model | 
| 66 |     \brief The model that is used by the mapper. | 
| 67 | */ | 
| 68 | /*! | 
| 69 |     \qmlproperty SomeModel HPieModelMapper::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 QHPieModelMapper::valuesRow | 
| 79 |     \brief The row 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 HPieModelMapper::valuesRow | 
| 85 |     The row 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 QHPieModelMapper::labelsRow | 
| 91 |     \brief The row 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 HPieModelMapper::labelsRow | 
| 97 |     The row 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 QHPieModelMapper::firstColumn | 
| 103 |     \brief The column of the model that contains the first slice value. | 
| 104 |  | 
| 105 |     The minimum and default value is 0. | 
| 106 | */ | 
| 107 | /*! | 
| 108 |     \qmlproperty int HPieModelMapper::firstColumn | 
| 109 |     The column of the model that contains the first slice value. | 
| 110 |     The default value is 0. | 
| 111 | */ | 
| 112 |  | 
| 113 | /*! | 
| 114 |     \property QHPieModelMapper::columnCount | 
| 115 |     \brief The number of columns of the model that are mapped as the data for the pie series. | 
| 116 |  | 
| 117 |     The minimum and default value is -1 (number limited to the number of columns in the model). | 
| 118 | */ | 
| 119 | /*! | 
| 120 |     \qmlproperty int HPieModelMapper::columnCount | 
| 121 |     The number of columns of the model that are mapped as the data for the pie series. | 
| 122 |     The default value is -1 (number limited by the number of columns in the model). | 
| 123 | */ | 
| 124 |  | 
| 125 | /*! | 
| 126 |     \fn void QHPieModelMapper::seriesReplaced() | 
| 127 |     This signal is emitted when the series that the mapper is connected to changes. | 
| 128 | */ | 
| 129 |  | 
| 130 | /*! | 
| 131 |     \fn void QHPieModelMapper::modelReplaced() | 
| 132 |     This signal is emitted when the model that the mapper is connected to changes. | 
| 133 | */ | 
| 134 |  | 
| 135 | /*! | 
| 136 |     \fn void QHPieModelMapper::valuesRowChanged() | 
| 137 |     This signal is emitted when the values row changes. | 
| 138 | */ | 
| 139 |  | 
| 140 | /*! | 
| 141 |     \fn void QHPieModelMapper::labelsRowChanged() | 
| 142 |     This signal is emitted when the labels row changes. | 
| 143 | */ | 
| 144 |  | 
| 145 | /*! | 
| 146 |     \fn void QHPieModelMapper::firstColumnChanged() | 
| 147 |     This signal is emitted when the first column changes. | 
| 148 | */ | 
| 149 |  | 
| 150 | /*! | 
| 151 |     \fn void QHPieModelMapper::columnCountChanged() | 
| 152 |     This signal is emitted when the number of columns changes. | 
| 153 | */ | 
| 154 |  | 
| 155 | /*! | 
| 156 |     Constructs a mapper object that is a child of \a parent. | 
| 157 | */ | 
| 158 | QHPieModelMapper::QHPieModelMapper(QObject *parent) : | 
| 159 |     QPieModelMapper(parent) | 
| 160 | { | 
| 161 |     setOrientation(Qt::Horizontal); | 
| 162 | } | 
| 163 |  | 
| 164 | QAbstractItemModel *QHPieModelMapper::model() const | 
| 165 | { | 
| 166 |     return QPieModelMapper::model(); | 
| 167 | } | 
| 168 |  | 
| 169 | void QHPieModelMapper::setModel(QAbstractItemModel *model) | 
| 170 | { | 
| 171 |     if (model != QPieModelMapper::model()) { | 
| 172 |         QPieModelMapper::setModel(model); | 
| 173 |         emit modelReplaced(); | 
| 174 |     } | 
| 175 | } | 
| 176 |  | 
| 177 | QPieSeries *QHPieModelMapper::series() const | 
| 178 | { | 
| 179 |     return QPieModelMapper::series(); | 
| 180 | } | 
| 181 |  | 
| 182 | void QHPieModelMapper::setSeries(QPieSeries *series) | 
| 183 | { | 
| 184 |     if (series != QPieModelMapper::series()) { | 
| 185 |         QPieModelMapper::setSeries(series); | 
| 186 |         emit seriesReplaced(); | 
| 187 |     } | 
| 188 | } | 
| 189 |  | 
| 190 | /*! | 
| 191 |     Returns the row of the model that is kept in sync with the values of the pie's slices. | 
| 192 | */ | 
| 193 | int QHPieModelMapper::valuesRow() const | 
| 194 | { | 
| 195 |     return valuesSection(); | 
| 196 | } | 
| 197 |  | 
| 198 | /*! | 
| 199 |     Sets the model row that is kept in sync with the pie slices' values to \a valuesRow. | 
| 200 | */ | 
| 201 | void QHPieModelMapper::setValuesRow(int valuesRow) | 
| 202 | { | 
| 203 |     if (valuesRow != valuesSection()) { | 
| 204 |         setValuesSection(valuesRow); | 
| 205 |         emit valuesRowChanged(); | 
| 206 |     } | 
| 207 | } | 
| 208 |  | 
| 209 | /*! | 
| 210 |     Returns the row of the model that is kept in sync with the labels of the pie's slices. | 
| 211 | */ | 
| 212 | int QHPieModelMapper::labelsRow() const | 
| 213 | { | 
| 214 |     return labelsSection(); | 
| 215 | } | 
| 216 |  | 
| 217 | /*! | 
| 218 |     Sets the model row that is kept in sync with the pie slices' labels to \a labelsRow. | 
| 219 | */ | 
| 220 | void QHPieModelMapper::setLabelsRow(int labelsRow) | 
| 221 | { | 
| 222 |     if (labelsRow != labelsSection()) { | 
| 223 |         setLabelsSection(labelsRow); | 
| 224 |         emit labelsRowChanged(); | 
| 225 |     } | 
| 226 | } | 
| 227 |  | 
| 228 | int QHPieModelMapper::firstColumn() const | 
| 229 | { | 
| 230 |     return first(); | 
| 231 | } | 
| 232 |  | 
| 233 | void QHPieModelMapper::setFirstColumn(int firstColumn) | 
| 234 | { | 
| 235 |     if (firstColumn != first()) { | 
| 236 |         setFirst(firstColumn); | 
| 237 |         emit firstColumnChanged(); | 
| 238 |     } | 
| 239 | } | 
| 240 |  | 
| 241 | int QHPieModelMapper::columnCount() const | 
| 242 | { | 
| 243 |     return count(); | 
| 244 | } | 
| 245 |  | 
| 246 | void QHPieModelMapper::setColumnCount(int columnCount) | 
| 247 | { | 
| 248 |     if (columnCount != count()) { | 
| 249 |         setCount(columnCount); | 
| 250 |         emit columnCountChanged(); | 
| 251 |     } | 
| 252 | } | 
| 253 |  | 
| 254 | QT_END_NAMESPACE | 
| 255 |  | 
| 256 | #include "moc_qhpiemodelmapper.cpp" | 
| 257 |  |