File indexing completed on 2025-01-05 04:58:21

0001 /*
0002   SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectmulticollectionwidget.h"
0008 #include "folderdialog/checkedcollectionwidget.h"
0009 
0010 #include <Akonadi/CollectionFilterProxyModel>
0011 #include <Akonadi/EntityTreeModel>
0012 
0013 #include <KCheckableProxyModel>
0014 
0015 #include <QTreeView>
0016 #include <QVBoxLayout>
0017 
0018 using namespace PimCommon;
0019 SelectMultiCollectionWidget::SelectMultiCollectionWidget(const QString &mimetype, QWidget *parent)
0020     : QWidget(parent)
0021 {
0022     initialize(mimetype);
0023 }
0024 
0025 SelectMultiCollectionWidget::SelectMultiCollectionWidget(const QString &mimetype, const QList<Akonadi::Collection::Id> &selectedCollection, QWidget *parent)
0026     : QWidget(parent)
0027     , mListCollection(selectedCollection)
0028 {
0029     initialize(mimetype);
0030 }
0031 
0032 SelectMultiCollectionWidget::~SelectMultiCollectionWidget() = default;
0033 
0034 void SelectMultiCollectionWidget::initialize(const QString &mimetype)
0035 {
0036     auto vbox = new QVBoxLayout(this);
0037     vbox->setContentsMargins({});
0038 
0039     mCheckedCollectionWidget = new PimCommon::CheckedCollectionWidget(mimetype);
0040 
0041     connect(mCheckedCollectionWidget, &PimCommon::CheckedCollectionWidget::collectionAdded, this, &SelectMultiCollectionWidget::slotCollectionsTreeFetched);
0042     connect(mCheckedCollectionWidget, &PimCommon::CheckedCollectionWidget::collectionRemoved, this, &SelectMultiCollectionWidget::slotCollectionsTreeFetched);
0043 
0044     connect(mCheckedCollectionWidget->entityTreeModel(),
0045             &Akonadi::EntityTreeModel::collectionTreeFetched,
0046             this,
0047             &SelectMultiCollectionWidget::slotCollectionsTreeFetched);
0048     vbox->addWidget(mCheckedCollectionWidget);
0049 }
0050 
0051 void SelectMultiCollectionWidget::updateStatus(const QModelIndex &parent)
0052 {
0053     const int nbCol = mCheckedCollectionWidget->checkableProxy()->rowCount(parent);
0054     for (int i = 0; i < nbCol; ++i) {
0055         const QModelIndex child = mCheckedCollectionWidget->checkableProxy()->index(i, 0, parent);
0056 
0057         const auto col = mCheckedCollectionWidget->checkableProxy()->data(child, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0058 
0059         if (mListCollection.contains(col.id())) {
0060             mCheckedCollectionWidget->checkableProxy()->setData(child, Qt::Checked, Qt::CheckStateRole);
0061         }
0062         updateStatus(child);
0063     }
0064 }
0065 
0066 void SelectMultiCollectionWidget::slotCollectionsTreeFetched()
0067 {
0068     if (!mListCollection.isEmpty()) {
0069         updateStatus(QModelIndex());
0070     }
0071     mCheckedCollectionWidget->folderTreeView()->expandAll();
0072 }
0073 
0074 QList<Akonadi::Collection> SelectMultiCollectionWidget::selectedCollection(const QModelIndex &parent) const
0075 {
0076     QList<Akonadi::Collection> lst;
0077 
0078     const int nbCol = mCheckedCollectionWidget->checkableProxy()->rowCount(parent);
0079     for (int i = 0; i < nbCol; ++i) {
0080         const QModelIndex child = mCheckedCollectionWidget->checkableProxy()->index(i, 0, parent);
0081 
0082         const auto col = mCheckedCollectionWidget->checkableProxy()->data(child, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0083 
0084         if (mCheckedCollectionWidget->checkableProxy()->data(child, Qt::CheckStateRole).toInt()) {
0085             lst << col;
0086         }
0087         lst << selectedCollection(child);
0088     }
0089     return lst;
0090 }
0091 
0092 #include "moc_selectmulticollectionwidget.cpp"