| 1 | // Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #include "qidentityproxymodel.h" |
| 6 | #include "qidentityproxymodel_p.h" |
| 7 | #include "qitemselectionmodel.h" |
| 8 | #include <private/qabstractproxymodel_p.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QIdentityProxyModelPrivate::~QIdentityProxyModelPrivate() |
| 13 | = default; |
| 14 | |
| 15 | /*! |
| 16 | \since 4.8 |
| 17 | \class QIdentityProxyModel |
| 18 | \inmodule QtCore |
| 19 | \brief The QIdentityProxyModel class proxies its source model unmodified. |
| 20 | |
| 21 | \ingroup model-view |
| 22 | |
| 23 | QIdentityProxyModel can be used to forward the structure of a source model exactly, with no sorting, filtering or other transformation. |
| 24 | This is similar in concept to an identity matrix where A.I = A. |
| 25 | |
| 26 | Because it does no sorting or filtering, this class is most suitable to proxy models which transform the data() of the source model. |
| 27 | For example, a proxy model could be created to define the font used, or the background colour, or the tooltip etc. This removes the |
| 28 | need to implement all data handling in the same class that creates the structure of the model, and can also be used to create |
| 29 | re-usable components. |
| 30 | |
| 31 | This also provides a way to change the data in the case where a source model is supplied by a third party which cannot be modified. |
| 32 | |
| 33 | \snippet code/src_gui_itemviews_qidentityproxymodel.cpp 0 |
| 34 | |
| 35 | \sa QAbstractProxyModel, {Model/View Programming}, QAbstractItemModel |
| 36 | |
| 37 | */ |
| 38 | |
| 39 | /*! |
| 40 | Constructs an identity model with the given \a parent. |
| 41 | */ |
| 42 | QIdentityProxyModel::QIdentityProxyModel(QObject* parent) |
| 43 | : QAbstractProxyModel(*new QIdentityProxyModelPrivate, parent) |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | /*! |
| 49 | \internal |
| 50 | */ |
| 51 | QIdentityProxyModel::QIdentityProxyModel(QIdentityProxyModelPrivate &dd, QObject* parent) |
| 52 | : QAbstractProxyModel(dd, parent) |
| 53 | { |
| 54 | |
| 55 | } |
| 56 | |
| 57 | /*! |
| 58 | Destroys this identity model. |
| 59 | */ |
| 60 | QIdentityProxyModel::~QIdentityProxyModel() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | /*! |
| 65 | \reimp |
| 66 | */ |
| 67 | int QIdentityProxyModel::columnCount(const QModelIndex& parent) const |
| 68 | { |
| 69 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 70 | Q_D(const QIdentityProxyModel); |
| 71 | return d->model->columnCount(parent: mapToSource(proxyIndex: parent)); |
| 72 | } |
| 73 | |
| 74 | /*! |
| 75 | \reimp |
| 76 | */ |
| 77 | bool QIdentityProxyModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) |
| 78 | { |
| 79 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 80 | Q_D(QIdentityProxyModel); |
| 81 | return d->model->dropMimeData(data, action, row, column, parent: mapToSource(proxyIndex: parent)); |
| 82 | } |
| 83 | |
| 84 | /*! |
| 85 | \reimp |
| 86 | */ |
| 87 | QModelIndex QIdentityProxyModel::index(int row, int column, const QModelIndex& parent) const |
| 88 | { |
| 89 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 90 | Q_D(const QIdentityProxyModel); |
| 91 | const QModelIndex sourceParent = mapToSource(proxyIndex: parent); |
| 92 | const QModelIndex sourceIndex = d->model->index(row, column, parent: sourceParent); |
| 93 | return mapFromSource(sourceIndex); |
| 94 | } |
| 95 | |
| 96 | /*! |
| 97 | \reimp |
| 98 | */ |
| 99 | QModelIndex QIdentityProxyModel::sibling(int row, int column, const QModelIndex &idx) const |
| 100 | { |
| 101 | Q_D(const QIdentityProxyModel); |
| 102 | return mapFromSource(sourceIndex: d->model->sibling(row, column, idx: mapToSource(proxyIndex: idx))); |
| 103 | } |
| 104 | |
| 105 | /*! |
| 106 | \reimp |
| 107 | */ |
| 108 | bool QIdentityProxyModel::insertColumns(int column, int count, const QModelIndex& parent) |
| 109 | { |
| 110 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 111 | Q_D(QIdentityProxyModel); |
| 112 | return d->model->insertColumns(column, count, parent: mapToSource(proxyIndex: parent)); |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | \reimp |
| 117 | */ |
| 118 | bool QIdentityProxyModel::insertRows(int row, int count, const QModelIndex& parent) |
| 119 | { |
| 120 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 121 | Q_D(QIdentityProxyModel); |
| 122 | return d->model->insertRows(row, count, parent: mapToSource(proxyIndex: parent)); |
| 123 | } |
| 124 | |
| 125 | /*! |
| 126 | \reimp |
| 127 | */ |
| 128 | QModelIndex QIdentityProxyModel::mapFromSource(const QModelIndex& sourceIndex) const |
| 129 | { |
| 130 | Q_D(const QIdentityProxyModel); |
| 131 | if (!d->model || !sourceIndex.isValid()) |
| 132 | return QModelIndex(); |
| 133 | |
| 134 | Q_ASSERT(sourceIndex.model() == d->model); |
| 135 | return createIndex(arow: sourceIndex.row(), acolumn: sourceIndex.column(), adata: sourceIndex.internalPointer()); |
| 136 | } |
| 137 | |
| 138 | /*! |
| 139 | \reimp |
| 140 | */ |
| 141 | QItemSelection QIdentityProxyModel::mapSelectionFromSource(const QItemSelection& selection) const |
| 142 | { |
| 143 | Q_D(const QIdentityProxyModel); |
| 144 | QItemSelection proxySelection; |
| 145 | |
| 146 | if (!d->model) |
| 147 | return proxySelection; |
| 148 | |
| 149 | QItemSelection::const_iterator it = selection.constBegin(); |
| 150 | const QItemSelection::const_iterator end = selection.constEnd(); |
| 151 | proxySelection.reserve(asize: selection.size()); |
| 152 | for ( ; it != end; ++it) { |
| 153 | Q_ASSERT(it->model() == d->model); |
| 154 | const QItemSelectionRange range(mapFromSource(sourceIndex: it->topLeft()), mapFromSource(sourceIndex: it->bottomRight())); |
| 155 | proxySelection.append(t: range); |
| 156 | } |
| 157 | |
| 158 | return proxySelection; |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | \reimp |
| 163 | */ |
| 164 | QItemSelection QIdentityProxyModel::mapSelectionToSource(const QItemSelection& selection) const |
| 165 | { |
| 166 | Q_D(const QIdentityProxyModel); |
| 167 | QItemSelection sourceSelection; |
| 168 | |
| 169 | if (!d->model) |
| 170 | return sourceSelection; |
| 171 | |
| 172 | QItemSelection::const_iterator it = selection.constBegin(); |
| 173 | const QItemSelection::const_iterator end = selection.constEnd(); |
| 174 | sourceSelection.reserve(asize: selection.size()); |
| 175 | for ( ; it != end; ++it) { |
| 176 | Q_ASSERT(it->model() == this); |
| 177 | const QItemSelectionRange range(mapToSource(proxyIndex: it->topLeft()), mapToSource(proxyIndex: it->bottomRight())); |
| 178 | sourceSelection.append(t: range); |
| 179 | } |
| 180 | |
| 181 | return sourceSelection; |
| 182 | } |
| 183 | |
| 184 | /*! |
| 185 | \reimp |
| 186 | */ |
| 187 | QModelIndex QIdentityProxyModel::mapToSource(const QModelIndex& proxyIndex) const |
| 188 | { |
| 189 | Q_D(const QIdentityProxyModel); |
| 190 | if (!d->model || !proxyIndex.isValid()) |
| 191 | return QModelIndex(); |
| 192 | Q_ASSERT(proxyIndex.model() == this); |
| 193 | return createSourceIndex(row: proxyIndex.row(), col: proxyIndex.column(), internalPtr: proxyIndex.internalPointer()); |
| 194 | } |
| 195 | |
| 196 | /*! |
| 197 | \reimp |
| 198 | */ |
| 199 | QModelIndexList QIdentityProxyModel::match(const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags) const |
| 200 | { |
| 201 | return QAbstractProxyModel::match(start, role, value, hits, flags); |
| 202 | } |
| 203 | |
| 204 | /*! |
| 205 | \reimp |
| 206 | */ |
| 207 | QModelIndex QIdentityProxyModel::parent(const QModelIndex& child) const |
| 208 | { |
| 209 | Q_ASSERT(child.isValid() ? child.model() == this : true); |
| 210 | const QModelIndex sourceIndex = mapToSource(proxyIndex: child); |
| 211 | const QModelIndex sourceParent = sourceIndex.parent(); |
| 212 | return mapFromSource(sourceIndex: sourceParent); |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | \reimp |
| 217 | */ |
| 218 | bool QIdentityProxyModel::removeColumns(int column, int count, const QModelIndex& parent) |
| 219 | { |
| 220 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 221 | Q_D(QIdentityProxyModel); |
| 222 | return d->model->removeColumns(column, count, parent: mapToSource(proxyIndex: parent)); |
| 223 | } |
| 224 | |
| 225 | /*! |
| 226 | \reimp |
| 227 | */ |
| 228 | bool QIdentityProxyModel::removeRows(int row, int count, const QModelIndex& parent) |
| 229 | { |
| 230 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 231 | Q_D(QIdentityProxyModel); |
| 232 | return d->model->removeRows(row, count, parent: mapToSource(proxyIndex: parent)); |
| 233 | } |
| 234 | |
| 235 | /*! |
| 236 | \reimp |
| 237 | \since 5.15 |
| 238 | */ |
| 239 | bool QIdentityProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) |
| 240 | { |
| 241 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true); |
| 242 | Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true); |
| 243 | Q_D(QIdentityProxyModel); |
| 244 | return d->model->moveRows(sourceParent: mapToSource(proxyIndex: sourceParent), sourceRow, count, destinationParent: mapToSource(proxyIndex: destinationParent), destinationChild); |
| 245 | } |
| 246 | |
| 247 | /*! |
| 248 | \reimp |
| 249 | \since 5.15 |
| 250 | */ |
| 251 | bool QIdentityProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) |
| 252 | { |
| 253 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true); |
| 254 | Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true); |
| 255 | Q_D(QIdentityProxyModel); |
| 256 | return d->model->moveColumns(sourceParent: mapToSource(proxyIndex: sourceParent), sourceColumn, count, destinationParent: mapToSource(proxyIndex: destinationParent), destinationChild); |
| 257 | } |
| 258 | |
| 259 | /*! |
| 260 | \reimp |
| 261 | */ |
| 262 | int QIdentityProxyModel::rowCount(const QModelIndex& parent) const |
| 263 | { |
| 264 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
| 265 | Q_D(const QIdentityProxyModel); |
| 266 | return d->model->rowCount(parent: mapToSource(proxyIndex: parent)); |
| 267 | } |
| 268 | |
| 269 | /*! |
| 270 | \reimp |
| 271 | */ |
| 272 | QVariant QIdentityProxyModel::(int section, Qt::Orientation orientation, int role) const |
| 273 | { |
| 274 | Q_D(const QIdentityProxyModel); |
| 275 | return d->model->headerData(section, orientation, role); |
| 276 | } |
| 277 | |
| 278 | /*! |
| 279 | \reimp |
| 280 | */ |
| 281 | void QIdentityProxyModel::setSourceModel(QAbstractItemModel* newSourceModel) |
| 282 | { |
| 283 | Q_D(QIdentityProxyModel); |
| 284 | |
| 285 | if (newSourceModel == d->model) |
| 286 | return; |
| 287 | |
| 288 | beginResetModel(); |
| 289 | |
| 290 | // Call QObject::disconnect() unconditionally, if there is an existing source |
| 291 | // model, it's disconnected, and if there isn't, then calling disconnect() on |
| 292 | // a default-constructed Connection does nothing |
| 293 | for (const auto &c : d->m_sourceModelConnections) |
| 294 | QObject::disconnect(c); |
| 295 | |
| 296 | QAbstractProxyModel::setSourceModel(newSourceModel); |
| 297 | |
| 298 | if (sourceModel()) { |
| 299 | auto *m = sourceModel(); |
| 300 | d->m_sourceModelConnections = { |
| 301 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::rowsAboutToBeInserted, receiverPrivate: d, |
| 302 | slot: &QIdentityProxyModelPrivate::sourceRowsAboutToBeInserted), |
| 303 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::rowsInserted, receiverPrivate: d, |
| 304 | slot: &QIdentityProxyModelPrivate::sourceRowsInserted), |
| 305 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::rowsAboutToBeRemoved, receiverPrivate: d, |
| 306 | slot: &QIdentityProxyModelPrivate::sourceRowsAboutToBeRemoved), |
| 307 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::rowsRemoved, receiverPrivate: d, |
| 308 | slot: &QIdentityProxyModelPrivate::sourceRowsRemoved), |
| 309 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::rowsAboutToBeMoved, receiverPrivate: d, |
| 310 | slot: &QIdentityProxyModelPrivate::sourceRowsAboutToBeMoved), |
| 311 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::rowsMoved, receiverPrivate: d, |
| 312 | slot: &QIdentityProxyModelPrivate::sourceRowsMoved), |
| 313 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::columnsAboutToBeInserted, receiverPrivate: d, |
| 314 | slot: &QIdentityProxyModelPrivate::sourceColumnsAboutToBeInserted), |
| 315 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::columnsInserted, receiverPrivate: d, |
| 316 | slot: &QIdentityProxyModelPrivate::sourceColumnsInserted), |
| 317 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::columnsAboutToBeRemoved, receiverPrivate: d, |
| 318 | slot: &QIdentityProxyModelPrivate::sourceColumnsAboutToBeRemoved), |
| 319 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::columnsRemoved, receiverPrivate: d, |
| 320 | slot: &QIdentityProxyModelPrivate::sourceColumnsRemoved), |
| 321 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::columnsAboutToBeMoved, receiverPrivate: d, |
| 322 | slot: &QIdentityProxyModelPrivate::sourceColumnsAboutToBeMoved), |
| 323 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::columnsMoved, receiverPrivate: d, |
| 324 | slot: &QIdentityProxyModelPrivate::sourceColumnsMoved), |
| 325 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::modelAboutToBeReset, receiverPrivate: d, |
| 326 | slot: &QIdentityProxyModelPrivate::sourceModelAboutToBeReset), |
| 327 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::modelReset, receiverPrivate: d, |
| 328 | slot: &QIdentityProxyModelPrivate::sourceModelReset), |
| 329 | QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::headerDataChanged, receiverPrivate: d, |
| 330 | slot: &QIdentityProxyModelPrivate::sourceHeaderDataChanged), |
| 331 | }; |
| 332 | |
| 333 | if (d->m_handleDataChanges) { |
| 334 | d->m_sourceModelConnections.emplace_back( |
| 335 | args: QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::dataChanged, receiverPrivate: d, |
| 336 | slot: &QIdentityProxyModelPrivate::sourceDataChanged)); |
| 337 | } |
| 338 | if (d->m_handleLayoutChanges) { |
| 339 | d->m_sourceModelConnections.emplace_back( |
| 340 | args: QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::layoutAboutToBeChanged, receiverPrivate: d, |
| 341 | slot: &QIdentityProxyModelPrivate::sourceLayoutAboutToBeChanged)); |
| 342 | d->m_sourceModelConnections.emplace_back( |
| 343 | args: QObjectPrivate::connect(sender: m, signal: &QAbstractItemModel::layoutChanged, receiverPrivate: d, |
| 344 | slot: &QIdentityProxyModelPrivate::sourceLayoutChanged)); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | endResetModel(); |
| 349 | } |
| 350 | |
| 351 | /*! |
| 352 | \since 6.8 |
| 353 | |
| 354 | If \a b is \c true, this proxy model will handle the source model layout |
| 355 | changes (by connecting to \c QAbstractItemModel::layoutAboutToBeChanged |
| 356 | and \c QAbstractItemModel::layoutChanged signals). |
| 357 | |
| 358 | The default is for this proxy model to handle the source model layout |
| 359 | changes. |
| 360 | |
| 361 | In sub-classes of QIdentityProxyModel, it may be useful to set this to |
| 362 | \c false if you need to specially handle the source model layout changes. |
| 363 | |
| 364 | \note Calling this method will only have an effect after calling setSourceModel(). |
| 365 | */ |
| 366 | void QIdentityProxyModel::setHandleSourceLayoutChanges(bool b) |
| 367 | { |
| 368 | d_func()->m_handleLayoutChanges = b; |
| 369 | } |
| 370 | |
| 371 | /*! |
| 372 | \since 6.8 |
| 373 | |
| 374 | Returns \c true if this proxy model handles the source model layout |
| 375 | changes, otherwise returns \c false. |
| 376 | */ |
| 377 | bool QIdentityProxyModel::handleSourceLayoutChanges() const |
| 378 | { |
| 379 | return d_func()->m_handleLayoutChanges; |
| 380 | } |
| 381 | |
| 382 | /*! |
| 383 | \since 6.8 |
| 384 | |
| 385 | If \a b is \c true, this proxy model will handle the source model data |
| 386 | changes (by connecting to \c QAbstractItemModel::dataChanged signal). |
| 387 | |
| 388 | The default is for this proxy model to handle the source model data |
| 389 | changes. |
| 390 | |
| 391 | In sub-classes of QIdentityProxyModel, it may be useful to set this to |
| 392 | \c false if you need to specially handle the source model data changes. |
| 393 | |
| 394 | \note Calling this method will only have an effect after calling setSourceModel(). |
| 395 | */ |
| 396 | void QIdentityProxyModel::setHandleSourceDataChanges(bool b) |
| 397 | { |
| 398 | d_func()->m_handleDataChanges = b; |
| 399 | } |
| 400 | |
| 401 | /*! |
| 402 | \since 6.8 |
| 403 | |
| 404 | Returns \c true if this proxy model handles the source model data |
| 405 | changes, otherwise returns \c false. |
| 406 | */ |
| 407 | bool QIdentityProxyModel::handleSourceDataChanges() const |
| 408 | { |
| 409 | return d_func()->m_handleDataChanges; |
| 410 | } |
| 411 | |
| 412 | void QIdentityProxyModelPrivate::sourceColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end) |
| 413 | { |
| 414 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 415 | Q_Q(QIdentityProxyModel); |
| 416 | q->beginInsertColumns(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
| 417 | } |
| 418 | |
| 419 | void QIdentityProxyModelPrivate::sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent, |
| 420 | int sourceStart, int sourceEnd, |
| 421 | const QModelIndex &destParent, |
| 422 | int dest) |
| 423 | { |
| 424 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
| 425 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
| 426 | Q_Q(QIdentityProxyModel); |
| 427 | q->beginMoveColumns(sourceParent: q->mapFromSource(sourceIndex: sourceParent), sourceFirst: sourceStart, sourceLast: sourceEnd, destinationParent: q->mapFromSource(sourceIndex: destParent), destinationColumn: dest); |
| 428 | } |
| 429 | |
| 430 | void QIdentityProxyModelPrivate::sourceColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end) |
| 431 | { |
| 432 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 433 | Q_Q(QIdentityProxyModel); |
| 434 | q->beginRemoveColumns(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
| 435 | } |
| 436 | |
| 437 | void QIdentityProxyModelPrivate::sourceColumnsInserted(const QModelIndex &parent, int start, int end) |
| 438 | { |
| 439 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 440 | Q_Q(QIdentityProxyModel); |
| 441 | Q_UNUSED(parent); |
| 442 | Q_UNUSED(start); |
| 443 | Q_UNUSED(end); |
| 444 | q->endInsertColumns(); |
| 445 | } |
| 446 | |
| 447 | void QIdentityProxyModelPrivate::sourceColumnsMoved(const QModelIndex &sourceParent, |
| 448 | int sourceStart, int sourceEnd, |
| 449 | const QModelIndex &destParent, int dest) |
| 450 | { |
| 451 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
| 452 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
| 453 | Q_Q(QIdentityProxyModel); |
| 454 | Q_UNUSED(sourceParent); |
| 455 | Q_UNUSED(sourceStart); |
| 456 | Q_UNUSED(sourceEnd); |
| 457 | Q_UNUSED(destParent); |
| 458 | Q_UNUSED(dest); |
| 459 | q->endMoveColumns(); |
| 460 | } |
| 461 | |
| 462 | void QIdentityProxyModelPrivate::sourceColumnsRemoved(const QModelIndex &parent, int start, int end) |
| 463 | { |
| 464 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 465 | Q_Q(QIdentityProxyModel); |
| 466 | Q_UNUSED(parent); |
| 467 | Q_UNUSED(start); |
| 468 | Q_UNUSED(end); |
| 469 | q->endRemoveColumns(); |
| 470 | } |
| 471 | |
| 472 | void QIdentityProxyModelPrivate::sourceDataChanged(const QModelIndex &topLeft, |
| 473 | const QModelIndex &bottomRight, |
| 474 | const QList<int> &roles) |
| 475 | { |
| 476 | Q_ASSERT(topLeft.isValid() ? topLeft.model() == model : true); |
| 477 | Q_ASSERT(bottomRight.isValid() ? bottomRight.model() == model : true); |
| 478 | Q_Q(QIdentityProxyModel); |
| 479 | emit q->dataChanged(topLeft: q->mapFromSource(sourceIndex: topLeft), bottomRight: q->mapFromSource(sourceIndex: bottomRight), roles); |
| 480 | } |
| 481 | |
| 482 | void QIdentityProxyModelPrivate::(Qt::Orientation orientation, int first, |
| 483 | int last) |
| 484 | { |
| 485 | Q_Q(QIdentityProxyModel); |
| 486 | emit q->headerDataChanged(orientation, first, last); |
| 487 | } |
| 488 | |
| 489 | void QIdentityProxyModelPrivate::sourceLayoutAboutToBeChanged( |
| 490 | const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint) |
| 491 | { |
| 492 | Q_Q(QIdentityProxyModel); |
| 493 | |
| 494 | QList<QPersistentModelIndex> parents; |
| 495 | parents.reserve(asize: sourceParents.size()); |
| 496 | for (const QPersistentModelIndex &parent : sourceParents) { |
| 497 | if (!parent.isValid()) { |
| 498 | parents << QPersistentModelIndex(); |
| 499 | continue; |
| 500 | } |
| 501 | const QModelIndex mappedParent = q->mapFromSource(sourceIndex: parent); |
| 502 | Q_ASSERT(mappedParent.isValid()); |
| 503 | parents << mappedParent; |
| 504 | } |
| 505 | |
| 506 | emit q->layoutAboutToBeChanged(parents, hint); |
| 507 | |
| 508 | const auto proxyPersistentIndexes = q->persistentIndexList(); |
| 509 | for (const QModelIndex &proxyPersistentIndex : proxyPersistentIndexes) { |
| 510 | proxyIndexes << proxyPersistentIndex; |
| 511 | Q_ASSERT(proxyPersistentIndex.isValid()); |
| 512 | const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyIndex: proxyPersistentIndex); |
| 513 | Q_ASSERT(srcPersistentIndex.isValid()); |
| 514 | layoutChangePersistentIndexes << srcPersistentIndex; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void QIdentityProxyModelPrivate::sourceLayoutChanged( |
| 519 | const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint) |
| 520 | { |
| 521 | Q_Q(QIdentityProxyModel); |
| 522 | |
| 523 | for (int i = 0; i < proxyIndexes.size(); ++i) { |
| 524 | q->changePersistentIndex(from: proxyIndexes.at(i), to: q->mapFromSource(sourceIndex: layoutChangePersistentIndexes.at(i))); |
| 525 | } |
| 526 | |
| 527 | layoutChangePersistentIndexes.clear(); |
| 528 | proxyIndexes.clear(); |
| 529 | |
| 530 | QList<QPersistentModelIndex> parents; |
| 531 | parents.reserve(asize: sourceParents.size()); |
| 532 | for (const QPersistentModelIndex &parent : sourceParents) { |
| 533 | if (!parent.isValid()) { |
| 534 | parents << QPersistentModelIndex(); |
| 535 | continue; |
| 536 | } |
| 537 | const QModelIndex mappedParent = q->mapFromSource(sourceIndex: parent); |
| 538 | Q_ASSERT(mappedParent.isValid()); |
| 539 | parents << mappedParent; |
| 540 | } |
| 541 | |
| 542 | emit q->layoutChanged(parents, hint); |
| 543 | } |
| 544 | |
| 545 | void QIdentityProxyModelPrivate::sourceModelAboutToBeReset() |
| 546 | { |
| 547 | Q_Q(QIdentityProxyModel); |
| 548 | q->beginResetModel(); |
| 549 | } |
| 550 | |
| 551 | void QIdentityProxyModelPrivate::sourceModelReset() |
| 552 | { |
| 553 | Q_Q(QIdentityProxyModel); |
| 554 | q->endResetModel(); |
| 555 | } |
| 556 | |
| 557 | void QIdentityProxyModelPrivate::sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, |
| 558 | int end) |
| 559 | { |
| 560 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 561 | Q_Q(QIdentityProxyModel); |
| 562 | q->beginInsertRows(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
| 563 | } |
| 564 | |
| 565 | void QIdentityProxyModelPrivate::sourceRowsAboutToBeMoved(const QModelIndex &sourceParent, |
| 566 | int sourceStart, int sourceEnd, |
| 567 | const QModelIndex &destParent, int dest) |
| 568 | { |
| 569 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
| 570 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
| 571 | Q_Q(QIdentityProxyModel); |
| 572 | q->beginMoveRows(sourceParent: q->mapFromSource(sourceIndex: sourceParent), sourceFirst: sourceStart, sourceLast: sourceEnd, destinationParent: q->mapFromSource(sourceIndex: destParent), destinationRow: dest); |
| 573 | } |
| 574 | |
| 575 | void QIdentityProxyModelPrivate::sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, |
| 576 | int end) |
| 577 | { |
| 578 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 579 | Q_Q(QIdentityProxyModel); |
| 580 | q->beginRemoveRows(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
| 581 | } |
| 582 | |
| 583 | void QIdentityProxyModelPrivate::sourceRowsInserted(const QModelIndex &parent, int start, int end) |
| 584 | { |
| 585 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 586 | Q_Q(QIdentityProxyModel); |
| 587 | Q_UNUSED(parent); |
| 588 | Q_UNUSED(start); |
| 589 | Q_UNUSED(end); |
| 590 | q->endInsertRows(); |
| 591 | } |
| 592 | |
| 593 | void QIdentityProxyModelPrivate::sourceRowsMoved(const QModelIndex &sourceParent, int sourceStart, |
| 594 | int sourceEnd, const QModelIndex &destParent, |
| 595 | int dest) |
| 596 | { |
| 597 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
| 598 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
| 599 | Q_Q(QIdentityProxyModel); |
| 600 | Q_UNUSED(sourceParent); |
| 601 | Q_UNUSED(sourceStart); |
| 602 | Q_UNUSED(sourceEnd); |
| 603 | Q_UNUSED(destParent); |
| 604 | Q_UNUSED(dest); |
| 605 | q->endMoveRows(); |
| 606 | } |
| 607 | |
| 608 | void QIdentityProxyModelPrivate::sourceRowsRemoved(const QModelIndex &parent, int start, int end) |
| 609 | { |
| 610 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
| 611 | Q_Q(QIdentityProxyModel); |
| 612 | Q_UNUSED(parent); |
| 613 | Q_UNUSED(start); |
| 614 | Q_UNUSED(end); |
| 615 | q->endRemoveRows(); |
| 616 | } |
| 617 | |
| 618 | QT_END_NAMESPACE |
| 619 | |
| 620 | #include "moc_qidentityproxymodel.cpp" |
| 621 | |