Pimcommon

selectmulticollectionwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "selectmulticollectionwidget.h"
8#include "folderdialog/checkedcollectionwidget.h"
9
10#include <Akonadi/CollectionFilterProxyModel>
11#include <Akonadi/EntityTreeModel>
12
13#include <KCheckableProxyModel>
14
15#include <QTreeView>
16#include <QVBoxLayout>
17
18using namespace PimCommon;
19SelectMultiCollectionWidget::SelectMultiCollectionWidget(const QString &mimetype, QWidget *parent)
20 : QWidget(parent)
21{
22 initialize(mimetype);
23}
24
25SelectMultiCollectionWidget::SelectMultiCollectionWidget(const QString &mimetype, const QList<Akonadi::Collection::Id> &selectedCollection, QWidget *parent)
26 : QWidget(parent)
27 , mListCollection(selectedCollection)
28{
29 initialize(mimetype);
30}
31
32SelectMultiCollectionWidget::~SelectMultiCollectionWidget() = default;
33
34void SelectMultiCollectionWidget::initialize(const QString &mimetype)
35{
36 auto vbox = new QVBoxLayout(this);
37 vbox->setContentsMargins({});
38
39 mCheckedCollectionWidget = new PimCommon::CheckedCollectionWidget(mimetype);
40
41 connect(mCheckedCollectionWidget, &PimCommon::CheckedCollectionWidget::collectionAdded, this, &SelectMultiCollectionWidget::slotCollectionsTreeFetched);
42 connect(mCheckedCollectionWidget, &PimCommon::CheckedCollectionWidget::collectionRemoved, this, &SelectMultiCollectionWidget::slotCollectionsTreeFetched);
43
44 connect(mCheckedCollectionWidget->entityTreeModel(),
46 this,
47 &SelectMultiCollectionWidget::slotCollectionsTreeFetched);
48 vbox->addWidget(mCheckedCollectionWidget);
49}
50
51void SelectMultiCollectionWidget::updateStatus(const QModelIndex &parent)
52{
53 const int nbCol = mCheckedCollectionWidget->checkableProxy()->rowCount(parent);
54 for (int i = 0; i < nbCol; ++i) {
55 const QModelIndex child = mCheckedCollectionWidget->checkableProxy()->index(i, 0, parent);
56
57 const auto col = mCheckedCollectionWidget->checkableProxy()->data(child, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
58
59 if (mListCollection.contains(col.id())) {
60 mCheckedCollectionWidget->checkableProxy()->setData(child, Qt::Checked, Qt::CheckStateRole);
61 }
62 updateStatus(child);
63 }
64}
65
66void SelectMultiCollectionWidget::slotCollectionsTreeFetched()
67{
68 if (!mListCollection.isEmpty()) {
69 updateStatus(QModelIndex());
70 }
71 mCheckedCollectionWidget->folderTreeView()->expandAll();
72}
73
74QList<Akonadi::Collection> SelectMultiCollectionWidget::selectedCollection(const QModelIndex &parent) const
75{
77
78 const int nbCol = mCheckedCollectionWidget->checkableProxy()->rowCount(parent);
79 for (int i = 0; i < nbCol; ++i) {
80 const QModelIndex child = mCheckedCollectionWidget->checkableProxy()->index(i, 0, parent);
81
82 const auto col = mCheckedCollectionWidget->checkableProxy()->data(child, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
83
84 if (mCheckedCollectionWidget->checkableProxy()->data(child, Qt::CheckStateRole).toInt()) {
85 lst << col;
86 }
87 lst << selectedCollection(child);
88 }
89 return lst;
90}
91
92#include "moc_selectmulticollectionwidget.cpp"
void collectionTreeFetched(const Akonadi::Collection::List &collections)
The CheckedCollectionWidget class.
void initialize(StandardShortcut id)
folderdialogacltab.h
virtual QVariant data(const QModelIndex &proxyIndex, int role) const const override
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual int rowCount(const QModelIndex &parent) const const override
bool contains(const AT &value) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
CheckStateRole
void expandAll()
int toInt(bool *ok) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.