Pimcommon

checkedcollectionwidget.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 "checkedcollectionwidget.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include <Akonadi/CollectionFilterProxyModel>
11
12#include <Akonadi/EntityTreeModel>
13#include <Akonadi/Monitor>
14#include <QSortFilterProxyModel>
15
16#include <KCheckableProxyModel>
17#include <KLocalizedString>
18#include <QLineEdit>
19
20#include <QTreeView>
21#include <QVBoxLayout>
22
23using namespace PimCommon;
24
25class PimCommon::CheckedCollectionWidgetPrivate
26{
27public:
28 CheckedCollectionWidgetPrivate() = default;
29
30 QTreeView *mFolderView = nullptr;
31 QItemSelectionModel *mSelectionModel = nullptr;
32 KCheckableProxyModel *mCheckProxy = nullptr;
33 QSortFilterProxyModel *mCollectionFilter = nullptr;
34
35 Akonadi::EntityTreeModel *mEntityTreeModel = nullptr;
36};
37
38CheckedCollectionWidget::CheckedCollectionWidget(const QString &mimetype, QWidget *parent)
39 : QWidget(parent)
40 , d(new PimCommon::CheckedCollectionWidgetPrivate)
41{
42 auto vbox = new QVBoxLayout(this);
43 vbox->setContentsMargins({});
44
45 // Create a new change recorder.
46 auto monitor = new Akonadi::Monitor(this);
47 monitor->setObjectName("CheckedCollectionWidgetMonitor"_L1);
48 monitor->fetchCollection(true);
49 monitor->setAllMonitored(true);
50 monitor->setMimeTypeMonitored(mimetype);
51 connect(monitor, &Akonadi::Monitor::collectionAdded, this, &CheckedCollectionWidget::collectionAdded);
52 connect(monitor, &Akonadi::Monitor::collectionRemoved, this, &CheckedCollectionWidget::collectionRemoved);
53
54 d->mEntityTreeModel = new Akonadi::EntityTreeModel(monitor, this);
55 // Set the model to show only collections, not items.
56 d->mEntityTreeModel->setItemPopulationStrategy(Akonadi::EntityTreeModel::NoItemPopulation);
57
58 auto mimeTypeProxy = new Akonadi::CollectionFilterProxyModel(this);
59 mimeTypeProxy->setExcludeVirtualCollections(true);
60 mimeTypeProxy->addMimeTypeFilters({mimetype});
61 mimeTypeProxy->setSourceModel(d->mEntityTreeModel);
62
63 // Create the Check proxy model.
64 d->mSelectionModel = new QItemSelectionModel(mimeTypeProxy);
65 d->mCheckProxy = new KCheckableProxyModel(this);
66 d->mCheckProxy->setSelectionModel(d->mSelectionModel);
67 d->mCheckProxy->setSourceModel(mimeTypeProxy);
68 d->mCollectionFilter = new QSortFilterProxyModel(this);
69 d->mCollectionFilter->setRecursiveFilteringEnabled(true);
70 d->mCollectionFilter->setSourceModel(d->mCheckProxy);
71 d->mCollectionFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
72
73 auto searchLine = new QLineEdit(this);
74 searchLine->setPlaceholderText(i18nc("@info:placeholder", "Search..."));
75 searchLine->setClearButtonEnabled(true);
76 connect(searchLine, &QLineEdit::textChanged, this, &CheckedCollectionWidget::slotSetCollectionFilter);
77
78 vbox->addWidget(searchLine);
79
80 d->mFolderView = new QTreeView;
82 d->mFolderView->setAlternatingRowColors(true);
83 d->mFolderView->setModel(d->mCollectionFilter);
84
85 vbox->addWidget(d->mFolderView);
86}
87
88CheckedCollectionWidget::~CheckedCollectionWidget() = default;
89
90Akonadi::EntityTreeModel *CheckedCollectionWidget::entityTreeModel() const
91{
92 return d->mEntityTreeModel;
93}
94
95QTreeView *CheckedCollectionWidget::folderTreeView() const
96{
97 return d->mFolderView;
98}
99
100QItemSelectionModel *CheckedCollectionWidget::selectionModel() const
101{
102 return d->mSelectionModel;
103}
104
105KCheckableProxyModel *CheckedCollectionWidget::checkableProxy() const
106{
107 return d->mCheckProxy;
108}
109
110void CheckedCollectionWidget::slotSetCollectionFilter(const QString &filter)
111{
112 d->mCollectionFilter->setFilterWildcard(filter);
113 d->mFolderView->expandAll();
114}
115
116#include "moc_checkedcollectionwidget.cpp"
void collectionRemoved(const Akonadi::Collection &collection)
void collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
folderdialogacltab.h
void setEditTriggers(EditTriggers triggers)
void textChanged(const QString &text)
CaseInsensitive
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.