| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 or (at your option) any later version | 
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by | 
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 | 
| 22 | ** included in the packaging of this file. Please review the following | 
| 23 | ** information to ensure the GNU General Public License requirements will | 
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 25 | ** | 
| 26 | ** $QT_END_LICENSE$ | 
| 27 | ** | 
| 28 | ****************************************************************************/ | 
| 29 |  | 
| 30 | #include <QtCharts/QHPieModelMapper> | 
| 31 |  | 
| 32 | QT_CHARTS_BEGIN_NAMESPACE | 
| 33 |  | 
| 34 | /*! | 
| 35 |     \class QHPieModelMapper | 
| 36 |     \inmodule QtCharts | 
| 37 |     \brief The QHPieModelMapper is a horizontal model mapper for pie series. | 
| 38 |  | 
| 39 |     Model mappers enable using a data model derived from the QAbstractItemModel class | 
| 40 |     as a data source for a chart. A horizontal model mapper is used to create a connection | 
| 41 |     between a data model and QPieSeries, so that each column in the data model defines a | 
| 42 |     pie slice and each row maps to the label or the value of the pie slice. | 
| 43 |  | 
| 44 |     Both model and pie series properties can be used to manipulate the data. The model | 
| 45 |     mapper keeps the pie series and the data model in sync. | 
| 46 | */ | 
| 47 | /*! | 
| 48 |     \qmltype HPieModelMapper | 
| 49 |     \instantiates QHPieModelMapper | 
| 50 |     \inqmlmodule QtCharts | 
| 51 |  | 
| 52 |     \brief Horizontal model mapper for pie series. | 
| 53 |  | 
| 54 |     Model mappers enable using a data model derived from the QAbstractItemModel class | 
| 55 |     as a data source for a chart. A horizontal model mapper is used to create a connection | 
| 56 |     between a data model and PieSeries, so that each column in the data model defines a | 
| 57 |     pie slice and each row maps to the label or the value of the pie slice. | 
| 58 |  | 
| 59 |     Both model and pie series properties can be used to manipulate the data. The model | 
| 60 |     mapper keeps the pie series and the data model in sync. | 
| 61 |  | 
| 62 |     The following QML example creates a pie series with four slices (assuming the model has | 
| 63 |     at least five columns). Each slice gets a label from row 1 and a value from row 2. | 
| 64 |     \code | 
| 65 |         HPieModelMapper { | 
| 66 |             series: pieSeries | 
| 67 |             model: customModel | 
| 68 |             labelsRow: 1 | 
| 69 |             valuesRow: 2 | 
| 70 |             firstColumn: 1 | 
| 71 |             columnCount: 4 | 
| 72 |         } | 
| 73 |     \endcode | 
| 74 | */ | 
| 75 |  | 
| 76 | /*! | 
| 77 |     \property QHPieModelMapper::series | 
| 78 |     \brief The pie series that is used by the mapper. | 
| 79 |  | 
| 80 |     All the data in the series is discarded when it is set to the mapper. | 
| 81 |     When a new series is specified, the old series is disconnected (but it preserves its data). | 
| 82 | */ | 
| 83 | /*! | 
| 84 |     \qmlproperty PieSeries HPieModelMapper::series | 
| 85 |     The PieSeries object that is used by the mapper. If you define the mapper element as a child for a | 
| 86 |     PieSeries, leave this property undefined. All the data in the series is discarded when it is set to the mapper. | 
| 87 |     When a new series is specified, the old series is disconnected (but it preserves its data). | 
| 88 | */ | 
| 89 |  | 
| 90 | /*! | 
| 91 |     \property QHPieModelMapper::model | 
| 92 |     \brief The model that is used by the mapper. | 
| 93 | */ | 
| 94 | /*! | 
| 95 |     \qmlproperty SomeModel HPieModelMapper::model | 
| 96 |     The QAbstractItemModel based model that is used by the mapper. You need to implement the model | 
| 97 |     and expose it to QML. | 
| 98 |  | 
| 99 |     \note The model has to support adding and removing rows or columns and modifying | 
| 100 |     the data in the cells. | 
| 101 | */ | 
| 102 |  | 
| 103 | /*! | 
| 104 |     \property QHPieModelMapper::valuesRow | 
| 105 |     \brief The row of the model that is kept in sync with the values of the pie's slices. | 
| 106 |  | 
| 107 |     The default value is -1 (invalid mapping). | 
| 108 | */ | 
| 109 | /*! | 
| 110 |     \qmlproperty int HPieModelMapper::valuesRow | 
| 111 |     The row of the model that is kept in sync with the values of the pie's slices. | 
| 112 |     The default value is -1 (invalid mapping). | 
| 113 | */ | 
| 114 |  | 
| 115 | /*! | 
| 116 |     \property QHPieModelMapper::labelsRow | 
| 117 |     \brief The row of the model that is kept in sync with the labels of the pie's slices. | 
| 118 |  | 
| 119 |     The default value is -1 (invalid mapping). | 
| 120 | */ | 
| 121 | /*! | 
| 122 |     \qmlproperty int HPieModelMapper::labelsRow | 
| 123 |     The row of the model that is kept in sync with the labels of the pie's slices. | 
| 124 |     The default value is -1 (invalid mapping). | 
| 125 | */ | 
| 126 |  | 
| 127 | /*! | 
| 128 |     \property QHPieModelMapper::firstColumn | 
| 129 |     \brief The column of the model that contains the first slice value. | 
| 130 |  | 
| 131 |     The minimum and default value is 0. | 
| 132 | */ | 
| 133 | /*! | 
| 134 |     \qmlproperty int HPieModelMapper::firstColumn | 
| 135 |     The column of the model that contains the first slice value. | 
| 136 |     The default value is 0. | 
| 137 | */ | 
| 138 |  | 
| 139 | /*! | 
| 140 |     \property QHPieModelMapper::columnCount | 
| 141 |     \brief The number of columns of the model that are mapped as the data for the pie series. | 
| 142 |  | 
| 143 |     The minimum and default value is -1 (number limited to the number of columns in the model). | 
| 144 | */ | 
| 145 | /*! | 
| 146 |     \qmlproperty int HPieModelMapper::columnCount | 
| 147 |     The number of columns of the model that are mapped as the data for the pie series. | 
| 148 |     The default value is -1 (number limited by the number of columns in the model). | 
| 149 | */ | 
| 150 |  | 
| 151 | /*! | 
| 152 |     \fn void QHPieModelMapper::seriesReplaced() | 
| 153 |     This signal is emitted when the series that the mapper is connected to changes. | 
| 154 | */ | 
| 155 |  | 
| 156 | /*! | 
| 157 |     \fn void QHPieModelMapper::modelReplaced() | 
| 158 |     This signal is emitted when the model that the mapper is connected to changes. | 
| 159 | */ | 
| 160 |  | 
| 161 | /*! | 
| 162 |     \fn void QHPieModelMapper::valuesRowChanged() | 
| 163 |     This signal is emitted when the values row changes. | 
| 164 | */ | 
| 165 |  | 
| 166 | /*! | 
| 167 |     \fn void QHPieModelMapper::labelsRowChanged() | 
| 168 |     This signal is emitted when the labels row changes. | 
| 169 | */ | 
| 170 |  | 
| 171 | /*! | 
| 172 |     \fn void QHPieModelMapper::firstColumnChanged() | 
| 173 |     This signal is emitted when the first column changes. | 
| 174 | */ | 
| 175 |  | 
| 176 | /*! | 
| 177 |     \fn void QHPieModelMapper::columnCountChanged() | 
| 178 |     This signal is emitted when the number of columns changes. | 
| 179 | */ | 
| 180 |  | 
| 181 | /*! | 
| 182 |     Constructs a mapper object that is a child of \a parent. | 
| 183 | */ | 
| 184 | QHPieModelMapper::QHPieModelMapper(QObject *parent) : | 
| 185 |     QPieModelMapper(parent) | 
| 186 | { | 
| 187 |     setOrientation(Qt::Horizontal); | 
| 188 | } | 
| 189 |  | 
| 190 | QAbstractItemModel *QHPieModelMapper::model() const | 
| 191 | { | 
| 192 |     return QPieModelMapper::model(); | 
| 193 | } | 
| 194 |  | 
| 195 | void QHPieModelMapper::setModel(QAbstractItemModel *model) | 
| 196 | { | 
| 197 |     if (model != QPieModelMapper::model()) { | 
| 198 |         QPieModelMapper::setModel(model); | 
| 199 |         emit modelReplaced(); | 
| 200 |     } | 
| 201 | } | 
| 202 |  | 
| 203 | QPieSeries *QHPieModelMapper::series() const | 
| 204 | { | 
| 205 |     return QPieModelMapper::series(); | 
| 206 | } | 
| 207 |  | 
| 208 | void QHPieModelMapper::setSeries(QPieSeries *series) | 
| 209 | { | 
| 210 |     if (series != QPieModelMapper::series()) { | 
| 211 |         QPieModelMapper::setSeries(series); | 
| 212 |         emit seriesReplaced(); | 
| 213 |     } | 
| 214 | } | 
| 215 |  | 
| 216 | /*! | 
| 217 |     Returns the row of the model that is kept in sync with the values of the pie's slices. | 
| 218 | */ | 
| 219 | int QHPieModelMapper::valuesRow() const | 
| 220 | { | 
| 221 |     return valuesSection(); | 
| 222 | } | 
| 223 |  | 
| 224 | /*! | 
| 225 |     Sets the model row that is kept in sync with the pie slices' values to \a valuesRow. | 
| 226 | */ | 
| 227 | void QHPieModelMapper::setValuesRow(int valuesRow) | 
| 228 | { | 
| 229 |     if (valuesRow != valuesSection()) { | 
| 230 |         setValuesSection(valuesRow); | 
| 231 |         emit valuesRowChanged(); | 
| 232 |     } | 
| 233 | } | 
| 234 |  | 
| 235 | /*! | 
| 236 |     Returns the row of the model that is kept in sync with the labels of the pie's slices. | 
| 237 | */ | 
| 238 | int QHPieModelMapper::labelsRow() const | 
| 239 | { | 
| 240 |     return labelsSection(); | 
| 241 | } | 
| 242 |  | 
| 243 | /*! | 
| 244 |     Sets the model row that is kept in sync with the pie slices' labels to \a labelsRow. | 
| 245 | */ | 
| 246 | void QHPieModelMapper::setLabelsRow(int labelsRow) | 
| 247 | { | 
| 248 |     if (labelsRow != labelsSection()) { | 
| 249 |         setLabelsSection(labelsRow); | 
| 250 |         emit labelsRowChanged(); | 
| 251 |     } | 
| 252 | } | 
| 253 |  | 
| 254 | int QHPieModelMapper::firstColumn() const | 
| 255 | { | 
| 256 |     return first(); | 
| 257 | } | 
| 258 |  | 
| 259 | void QHPieModelMapper::setFirstColumn(int firstColumn) | 
| 260 | { | 
| 261 |     if (firstColumn != first()) { | 
| 262 |         setFirst(firstColumn); | 
| 263 |         emit firstColumnChanged(); | 
| 264 |     } | 
| 265 | } | 
| 266 |  | 
| 267 | int QHPieModelMapper::columnCount() const | 
| 268 | { | 
| 269 |     return count(); | 
| 270 | } | 
| 271 |  | 
| 272 | void QHPieModelMapper::setColumnCount(int columnCount) | 
| 273 | { | 
| 274 |     if (columnCount != count()) { | 
| 275 |         setCount(columnCount); | 
| 276 |         emit columnCountChanged(); | 
| 277 |     } | 
| 278 | } | 
| 279 |  | 
| 280 | QT_CHARTS_END_NAMESPACE | 
| 281 |  | 
| 282 | #include "moc_qhpiemodelmapper.cpp" | 
| 283 |  |