File indexing completed on 2024-05-12 05:13:14

0001 /*
0002   SPDX-FileCopyrightText: 2009 KDAB
0003   SPDX-FileContributor: Frank Osterfeld <frank@kdab.net>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0006 */
0007 
0008 #include "collectionselection.h"
0009 #include "utils.h"
0010 
0011 #include <Akonadi/CollectionUtils>
0012 
0013 #include <QItemSelectionModel>
0014 
0015 using namespace CalendarSupport;
0016 
0017 class CalendarSupport::CollectionSelectionPrivate
0018 {
0019 public:
0020     explicit CollectionSelectionPrivate(QItemSelectionModel *model_)
0021         : model(model_)
0022     {
0023     }
0024 
0025     QItemSelectionModel *const model;
0026 };
0027 
0028 CollectionSelection::CollectionSelection(QItemSelectionModel *selectionModel, QObject *parent)
0029     : QObject(parent)
0030     , d(new CollectionSelectionPrivate(selectionModel))
0031 {
0032     connect(selectionModel, &QItemSelectionModel::selectionChanged, this, &CollectionSelection::slotSelectionChanged);
0033 }
0034 
0035 CollectionSelection::~CollectionSelection() = default;
0036 
0037 QItemSelectionModel *CollectionSelection::model() const
0038 {
0039     return d->model;
0040 }
0041 
0042 bool CollectionSelection::hasSelection() const
0043 {
0044     return d->model->hasSelection();
0045 }
0046 
0047 bool CollectionSelection::contains(const Akonadi::Collection &c) const
0048 {
0049     return selectedCollectionIds().contains(c.id());
0050 }
0051 
0052 bool CollectionSelection::contains(Akonadi::Collection::Id id) const
0053 {
0054     return selectedCollectionIds().contains(id);
0055 }
0056 
0057 Akonadi::Collection::List CollectionSelection::selectedCollections() const
0058 {
0059     Akonadi::Collection::List selected;
0060     const QModelIndexList selectedIndexes = d->model->selectedIndexes();
0061     selected.reserve(selectedIndexes.count());
0062     for (const QModelIndex &idx : selectedIndexes) {
0063         selected.append(Akonadi::CollectionUtils::fromIndex(idx));
0064     }
0065     return selected;
0066 }
0067 
0068 QList<Akonadi::Collection::Id> CollectionSelection::selectedCollectionIds() const
0069 {
0070     QList<Akonadi::Collection::Id> selected;
0071     const QModelIndexList selectedIndexes = d->model->selectedIndexes();
0072     selected.reserve(selectedIndexes.count());
0073     for (const QModelIndex &idx : selectedIndexes) {
0074         selected.append(collectionIdFromIndex(idx));
0075     }
0076     return selected;
0077 }
0078 
0079 void CollectionSelection::slotSelectionChanged(const QItemSelection &selectedIndexes, const QItemSelection &deselIndexes)
0080 {
0081     const Akonadi::Collection::List selected = collectionsFromIndexes(selectedIndexes.indexes());
0082     const Akonadi::Collection::List deselected = collectionsFromIndexes(deselIndexes.indexes());
0083 
0084     Q_EMIT selectionChanged(selected, deselected);
0085     for (const Akonadi::Collection &c : deselected) {
0086         Q_EMIT collectionDeselected(c);
0087     }
0088     for (const Akonadi::Collection &c : selected) {
0089         Q_EMIT collectionSelected(c);
0090     }
0091 }
0092 
0093 #include "moc_collectionselection.cpp"