Mailcommon

filterselectiondialog.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 "filterselectiondialog.h"
8#include "mailfilter.h"
9
10#include <KListWidgetSearchLine>
11#include <QPushButton>
12
13#include <KConfigGroup>
14#include <KSharedConfig>
15#include <KWindowConfig>
16#include <QDialogButtonBox>
17#include <QListWidget>
18#include <QVBoxLayout>
19#include <QWindow>
20
21using namespace MailCommon;
22namespace
23{
24static const char myFilterSelectionDialogName[] = "FilterSelectionDialog";
25}
26FilterSelectionDialog::FilterSelectionDialog(QWidget *parent)
27 : QDialog(parent)
28 , filtersListWidget(new QListWidget(this))
29 , selectAllButton(new QPushButton(i18nc("@action:button", "Select All"), this))
30 , unselectAllButton(new QPushButton(i18nc("@action:button", "Unselect All"), this))
31{
32 setObjectName(QLatin1StringView("filterselection"));
33 setModal(true);
34 setWindowTitle(i18nc("@title:window", "Select Filters"));
35 auto top = new QVBoxLayout(this);
36
38 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
39 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
40 mOkButton->setDefault(true);
41 connect(buttonBox, &QDialogButtonBox::accepted, this, &FilterSelectionDialog::accept);
42 connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterSelectionDialog::reject);
43
44 auto searchLine = new KListWidgetSearchLine(this, filtersListWidget);
45 searchLine->setPlaceholderText(i18nc("@info Displayed grayed-out inside the textbox, verb to search", "Search"));
46
47 top->addWidget(searchLine);
48 top->addWidget(filtersListWidget);
49 filtersListWidget->setAlternatingRowColors(true);
50 filtersListWidget->setSortingEnabled(false);
51 filtersListWidget->setSelectionMode(QAbstractItemView::NoSelection);
52
53 auto const buttonLayout = new QHBoxLayout();
54 top->addLayout(buttonLayout);
55 buttonLayout->addWidget(selectAllButton);
56 buttonLayout->addWidget(unselectAllButton);
57 top->addWidget(buttonBox);
58
59 connect(selectAllButton, &QPushButton::clicked, this, &FilterSelectionDialog::slotSelectAllButton);
60 connect(unselectAllButton, &QPushButton::clicked, this, &FilterSelectionDialog::slotUnselectAllButton);
61
62 readConfig();
63}
64
65FilterSelectionDialog::~FilterSelectionDialog()
66{
67 writeConfig();
68}
69
70void FilterSelectionDialog::reject()
71{
72 qDeleteAll(originalFilters);
74}
75
76void FilterSelectionDialog::readConfig()
77{
78 create(); // ensure a window is created
79 windowHandle()->resize(QSize(300, 350));
80 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterSelectionDialogName));
82 resize(windowHandle()->size()); // workaround for QTBUG-40584
83}
84
85void FilterSelectionDialog::writeConfig()
86{
87 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterSelectionDialogName));
89 group.sync();
90}
91
92void FilterSelectionDialog::setFilters(const QList<MailFilter *> &filters)
93{
94 if (filters.isEmpty()) {
95 mOkButton->setEnabled(false);
96 return;
97 }
98
99 originalFilters = filters;
100 filtersListWidget->clear();
101
102 for (const MailFilter *filter : filters) {
103 auto item = new QListWidgetItem(filter->name(), filtersListWidget);
105 item->setCheckState(Qt::Checked);
106 }
107}
108
109QList<MailFilter *> FilterSelectionDialog::selectedFilters() const
110{
111 QList<MailFilter *> filters;
112
113 const int filterCount = filtersListWidget->count();
114 for (int i = 0; i < filterCount; ++i) {
115 const QListWidgetItem *item = filtersListWidget->item(i);
116 if (item->checkState() == Qt::Checked) {
117 filters << originalFilters[i];
118 } else {
119 delete originalFilters[i];
120 }
121 }
122
123 return filters;
124}
125
126void FilterSelectionDialog::slotUnselectAllButton()
127{
128 const int filterCount = filtersListWidget->count();
129 for (int i = 0; i < filterCount; ++i) {
130 QListWidgetItem *const item = filtersListWidget->item(i);
132 }
133}
134
135void FilterSelectionDialog::slotSelectAllButton()
136{
137 const int filterCount = filtersListWidget->count();
138 for (int i = 0; i < filterCount; ++i) {
139 QListWidgetItem *const item = filtersListWidget->item(i);
141 }
142}
143
144#include "moc_filterselectiondialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The MailFilter class.
Definition mailfilter.h:29
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
The filter dialog.
void clicked(bool checked)
virtual void reject()
bool isEmpty() const const
void clear()
QListWidgetItem * item(int row) const const
Qt::CheckState checkState() const const
void setCheckState(Qt::CheckState state)
ItemIsUserCheckable
Key_Return
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setEnabled(bool)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.