| 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/QVBarModelMapper> | 
| 31 |  | 
| 32 | QT_CHARTS_BEGIN_NAMESPACE | 
| 33 |  | 
| 34 | /*! | 
| 35 |     \class QVBarModelMapper | 
| 36 |     \inmodule QtCharts | 
| 37 |     \brief The QVBarModelMapper class is a vertical model mapper for bar series. | 
| 38 |  | 
| 39 |     Model mappers enable using a data model derived from the QAbstractItemModel class | 
| 40 |     as a data source for a chart. A vertical model mapper is used to create a connection | 
| 41 |     between a data model and QAbstractBarSeries, so that each column in the data model | 
| 42 |     defines a bar set and each row maps to a category in a bar series. | 
| 43 |  | 
| 44 |     Both model and bar series properties can be used to manipulate the data. The model mapper | 
| 45 |     keeps the bar series and the data model in sync. | 
| 46 |  | 
| 47 |     The model mapper ensures that all the bar sets in the bar series have equal sizes. | 
| 48 |     Therefore, adding or removing a value from a bar set causes the same change to be | 
| 49 |     made in all the bar sets in the bar series. | 
| 50 |  | 
| 51 |     For more information, see \l{BarModelMapper Example}. | 
| 52 |  | 
| 53 |     \sa QHBarModelMapper | 
| 54 | */ | 
| 55 | /*! | 
| 56 |     \qmltype VBarModelMapper | 
| 57 |     \instantiates QVBarModelMapper | 
| 58 |     \inqmlmodule QtCharts | 
| 59 |  | 
| 60 |     \inherits BarModelMapper | 
| 61 |  | 
| 62 |     \brief Vertical model mapper for bar series. | 
| 63 |  | 
| 64 |     The VBarModelMapper type enables using a data model derived from the QAbstractItemModel | 
| 65 |     class as a data source for a chart. A vertical model mapper is used to create a connection | 
| 66 |     between a data model and QAbstractBarSeries, so that each column in the data model | 
| 67 |     defines a bar set and each row maps to a category in a bar series. You need to implement | 
| 68 |     the data model and expose it to QML. | 
| 69 |  | 
| 70 |     Both model and bar series properties can be used to manipulate the data. The model mapper | 
| 71 |     keeps the bar series and the data model in sync. | 
| 72 |  | 
| 73 |     The model mapper ensures that all the bar sets in the bar series have equal sizes. | 
| 74 |     Therefore, adding or removing a value from a bar set causes the same change to be | 
| 75 |     made in all the bar sets in the bar series. | 
| 76 |  | 
| 77 |     The following QML code snippet creates a bar series with three bar sets (assuming the model | 
| 78 |     has at least four columns). Each bar set contains data starting from row 1. The name | 
| 79 |     of a bar set is defined by the column header. | 
| 80 |     \code | 
| 81 |         BarSeries { | 
| 82 |             VBarModelMapper { | 
| 83 |                 model: myCustomModel // QAbstractItemModel derived implementation | 
| 84 |                 firstBarSetColumn: 1 | 
| 85 |                 lastBarSetColumn: 3 | 
| 86 |                 firstRow: 1 | 
| 87 |             } | 
| 88 |         } | 
| 89 |     \endcode | 
| 90 |  | 
| 91 |     \sa HBarModelMapper | 
| 92 | */ | 
| 93 |  | 
| 94 | /*! | 
| 95 |     \property QVBarModelMapper::series | 
| 96 |     \brief The bar series that is used by the mapper. | 
| 97 |  | 
| 98 |     All the data in the series is discarded when it is set to the mapper. | 
| 99 |     When a new series is specified, the old series is disconnected (but it preserves its data). | 
| 100 | */ | 
| 101 | /*! | 
| 102 |     \qmlproperty AbstractBarSeries VBarModelMapper::series | 
| 103 |     The bar series that is used by the mapper. All the data in the series is discarded when it is | 
| 104 |     set to the mapper. When the new series is specified, the old series is disconnected (but it | 
| 105 |     preserves its data). | 
| 106 | */ | 
| 107 |  | 
| 108 | /*! | 
| 109 |     \property QVBarModelMapper::model | 
| 110 |     \brief The data model that is used by the mapper. | 
| 111 | */ | 
| 112 | /*! | 
| 113 |     \qmlproperty SomeModel VBarModelMapper::model | 
| 114 |     The data model that is used by the mapper. You need to implement the model and expose it to QML. | 
| 115 |  | 
| 116 |     \note The model has to support adding and removing rows or columns and modifying | 
| 117 |     the data in the cells. | 
| 118 | */ | 
| 119 |  | 
| 120 | /*! | 
| 121 |     \property QVBarModelMapper::firstBarSetColumn | 
| 122 |     \brief The column of the model that is used as the data source for the first bar set. | 
| 123 |  | 
| 124 |     The default value is -1 (invalid mapping). | 
| 125 | */ | 
| 126 | /*! | 
| 127 |     \qmlproperty int VBarModelMapper::firstBarSetColumn | 
| 128 |     The column of the model that is used as the data source for the first bar set. The default value | 
| 129 |     is -1 (invalid mapping). | 
| 130 | */ | 
| 131 |  | 
| 132 | /*! | 
| 133 |     \property QVBarModelMapper::lastBarSetColumn | 
| 134 |     \brief The column of the model that is used as the data source for the last bar set. | 
| 135 |  | 
| 136 |     The default value is -1 (invalid mapping). | 
| 137 | */ | 
| 138 | /*! | 
| 139 |     \qmlproperty int VBarModelMapper::lastBarSetColumn | 
| 140 |     The column of the model that is used as the data source for the last bar set. The default | 
| 141 |     value is -1 (invalid mapping). | 
| 142 | */ | 
| 143 |  | 
| 144 | /*! | 
| 145 |     \property QVBarModelMapper::firstRow | 
| 146 |     \brief The row of the model that contains the first values of the bar sets in the bar series. | 
| 147 |  | 
| 148 |     The minimum and default value is 0. | 
| 149 | */ | 
| 150 | /*! | 
| 151 |     \qmlproperty int VBarModelMapper::firstRow | 
| 152 |     The row of the model that contains the first values of the bar sets in the bar series. | 
| 153 |     The default value is 0. | 
| 154 | */ | 
| 155 |  | 
| 156 | /*! | 
| 157 |     \property QVBarModelMapper::rowCount | 
| 158 |     \brief The number of rows of the model that are mapped as the data for the bar series. | 
| 159 |  | 
| 160 |     The minimum and default value is -1 (number limited to the number of rows in the model). | 
| 161 | */ | 
| 162 | /*! | 
| 163 |     \qmlproperty int VBarModelMapper::rowCount | 
| 164 |     The number of rows of the model that are mapped as the data for the bar series. The default | 
| 165 |     value is -1 (number limited to the number of rows in the model). | 
| 166 | */ | 
| 167 |  | 
| 168 | /*! | 
| 169 |     \fn void QVBarModelMapper::seriesReplaced() | 
| 170 |  | 
| 171 |     This signal is emitted when the bar series that the mapper is connected to changes. | 
| 172 | */ | 
| 173 |  | 
| 174 | /*! | 
| 175 |     \fn void QVBarModelMapper::modelReplaced() | 
| 176 |  | 
| 177 |     This signal is emitted when the model that the mapper is connected to changes. | 
| 178 | */ | 
| 179 |  | 
| 180 | /*! | 
| 181 |     \fn void QVBarModelMapper::firstBarSetColumnChanged() | 
| 182 |     This signal is emitted when the first bar set column changes. | 
| 183 | */ | 
| 184 |  | 
| 185 | /*! | 
| 186 |     \fn void QVBarModelMapper::lastBarSetColumnChanged() | 
| 187 |     This signal is emitted when the last bar set column changes. | 
| 188 | */ | 
| 189 |  | 
| 190 | /*! | 
| 191 |     \fn void QVBarModelMapper::firstRowChanged() | 
| 192 |     This signal is emitted when the first row changes. | 
| 193 | */ | 
| 194 |  | 
| 195 | /*! | 
| 196 |     \fn void QVBarModelMapper::rowCountChanged() | 
| 197 |     This signal is emitted when the number of rows changes. | 
| 198 | */ | 
| 199 |  | 
| 200 | /*! | 
| 201 |     Constructs a mapper object that is a child of \a parent. | 
| 202 | */ | 
| 203 | QVBarModelMapper::QVBarModelMapper(QObject *parent) : | 
| 204 |     QBarModelMapper(parent) | 
| 205 | { | 
| 206 |     QBarModelMapper::setOrientation(Qt::Vertical); | 
| 207 | } | 
| 208 |  | 
| 209 | QAbstractItemModel *QVBarModelMapper::model() const | 
| 210 | { | 
| 211 |     return QBarModelMapper::model(); | 
| 212 | } | 
| 213 |  | 
| 214 | void QVBarModelMapper::setModel(QAbstractItemModel *model) | 
| 215 | { | 
| 216 |     if (model != QBarModelMapper::model()) { | 
| 217 |         QBarModelMapper::setModel(model); | 
| 218 |         emit modelReplaced(); | 
| 219 |     } | 
| 220 | } | 
| 221 |  | 
| 222 | QAbstractBarSeries *QVBarModelMapper::series() const | 
| 223 | { | 
| 224 |     return QBarModelMapper::series(); | 
| 225 | } | 
| 226 |  | 
| 227 | void QVBarModelMapper::setSeries(QAbstractBarSeries *series) | 
| 228 | { | 
| 229 |     if (series != QBarModelMapper::series()) { | 
| 230 |         QBarModelMapper::setSeries(series); | 
| 231 |         emit seriesReplaced(); | 
| 232 |     } | 
| 233 | } | 
| 234 |  | 
| 235 | int QVBarModelMapper::firstBarSetColumn() const | 
| 236 | { | 
| 237 |     return QBarModelMapper::firstBarSetSection(); | 
| 238 | } | 
| 239 |  | 
| 240 | void QVBarModelMapper::setFirstBarSetColumn(int firstBarSetColumn) | 
| 241 | { | 
| 242 |     if (firstBarSetColumn != firstBarSetSection()) { | 
| 243 |         QBarModelMapper::setFirstBarSetSection(firstBarSetColumn); | 
| 244 |         emit firstBarSetColumnChanged(); | 
| 245 |     } | 
| 246 | } | 
| 247 |  | 
| 248 | int QVBarModelMapper::lastBarSetColumn() const | 
| 249 | { | 
| 250 |     return QBarModelMapper::lastBarSetSection(); | 
| 251 | } | 
| 252 |  | 
| 253 | void QVBarModelMapper::setLastBarSetColumn(int lastBarSetColumn) | 
| 254 | { | 
| 255 |     if (lastBarSetColumn != lastBarSetSection()) { | 
| 256 |         QBarModelMapper::setLastBarSetSection(lastBarSetColumn); | 
| 257 |         emit lastBarSetColumnChanged(); | 
| 258 |     } | 
| 259 | } | 
| 260 |  | 
| 261 | int QVBarModelMapper::firstRow() const | 
| 262 | { | 
| 263 |     return QBarModelMapper::first(); | 
| 264 | } | 
| 265 |  | 
| 266 | void QVBarModelMapper::setFirstRow(int firstRow) | 
| 267 | { | 
| 268 |     if (firstRow != first()) { | 
| 269 |         QBarModelMapper::setFirst(firstRow); | 
| 270 |         emit firstRowChanged(); | 
| 271 |     } | 
| 272 | } | 
| 273 |  | 
| 274 | int QVBarModelMapper::rowCount() const | 
| 275 | { | 
| 276 |     return QBarModelMapper::count(); | 
| 277 | } | 
| 278 |  | 
| 279 | void QVBarModelMapper::setRowCount(int rowCount) | 
| 280 | { | 
| 281 |     if (rowCount != count()) { | 
| 282 |         QBarModelMapper::setCount(rowCount); | 
| 283 |         emit rowCountChanged(); | 
| 284 |     } | 
| 285 | } | 
| 286 |  | 
| 287 | QT_CHARTS_END_NAMESPACE | 
| 288 |  | 
| 289 | #include "moc_qvbarmodelmapper.cpp" | 
| 290 |  |