File indexing completed on 2024-11-10 04:40:36

0001 /*
0002   SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "selectionproxymodel.h"
0008 
0009 #include "entitytreemodel.h"
0010 
0011 using namespace Akonadi;
0012 
0013 namespace Akonadi
0014 {
0015 class SelectionProxyModelPrivate
0016 {
0017 public:
0018     explicit SelectionProxyModelPrivate(SelectionProxyModel *selectionProxyModel)
0019         : q_ptr(selectionProxyModel)
0020     {
0021         Q_Q(SelectionProxyModel);
0022         const auto indexes = q->sourceRootIndexes();
0023         for (const auto &rootIndex : indexes) {
0024             rootIndexAdded(rootIndex);
0025         }
0026     }
0027     ~SelectionProxyModelPrivate()
0028     {
0029         Q_Q(SelectionProxyModel);
0030         const auto indexes = q->sourceRootIndexes();
0031         for (const auto &rootIndex : indexes) {
0032             rootIndexAboutToBeRemoved(rootIndex);
0033         }
0034     }
0035 
0036     /**
0037       Increases the refcount of the Collection in @p newRootIndex
0038     */
0039     void rootIndexAdded(const QModelIndex &newRootIndex)
0040     {
0041         Q_Q(SelectionProxyModel);
0042         // newRootIndex is already in the sourceModel.
0043         q->sourceModel()->setData(newRootIndex, QVariant(), EntityTreeModel::CollectionRefRole);
0044         q->sourceModel()->fetchMore(newRootIndex);
0045     }
0046 
0047     /**
0048       Decreases the refcount of the Collection in @p removedRootIndex
0049     */
0050     void rootIndexAboutToBeRemoved(const QModelIndex &removedRootIndex)
0051     {
0052         Q_Q(SelectionProxyModel);
0053         q->sourceModel()->setData(removedRootIndex, QVariant(), EntityTreeModel::CollectionDerefRole);
0054     }
0055 
0056     Q_DECLARE_PUBLIC(SelectionProxyModel)
0057     SelectionProxyModel *const q_ptr;
0058 };
0059 
0060 } // namespace Akonadi
0061 
0062 SelectionProxyModel::SelectionProxyModel(QItemSelectionModel *selectionModel, QObject *parent)
0063     : KSelectionProxyModel(selectionModel, parent)
0064     , d_ptr(new SelectionProxyModelPrivate(this))
0065 {
0066     connect(this, SIGNAL(rootIndexAdded(QModelIndex)), SLOT(rootIndexAdded(QModelIndex))); // clazy:exclude=old-style-connect
0067     connect(this, SIGNAL(rootIndexAboutToBeRemoved(QModelIndex)), SLOT(rootIndexAboutToBeRemoved(QModelIndex))); // clazy:exclude=old-style-connect
0068 }
0069 
0070 SelectionProxyModel::~SelectionProxyModel() = default;
0071 
0072 #include "moc_selectionproxymodel.cpp"