File indexing completed on 2024-09-22 04:50:04

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 "filterselectiondialog.h"
0008 #include "mailfilter.h"
0009 
0010 #include <KListWidgetSearchLine>
0011 #include <QPushButton>
0012 
0013 #include <KConfigGroup>
0014 #include <KSharedConfig>
0015 #include <KWindowConfig>
0016 #include <QDialogButtonBox>
0017 #include <QListWidget>
0018 #include <QVBoxLayout>
0019 #include <QWindow>
0020 
0021 using namespace MailCommon;
0022 namespace
0023 {
0024 static const char myFilterSelectionDialogName[] = "FilterSelectionDialog";
0025 }
0026 FilterSelectionDialog::FilterSelectionDialog(QWidget *parent)
0027     : QDialog(parent)
0028     , filtersListWidget(new QListWidget(this))
0029     , selectAllButton(new QPushButton(i18n("Select All"), this))
0030     , unselectAllButton(new QPushButton(i18n("Unselect All"), this))
0031 {
0032     setObjectName(QLatin1StringView("filterselection"));
0033     setModal(true);
0034     setWindowTitle(i18nc("@title:window", "Select Filters"));
0035     auto top = new QVBoxLayout(this);
0036 
0037     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0038     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0039     mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0040     mOkButton->setDefault(true);
0041     connect(buttonBox, &QDialogButtonBox::accepted, this, &FilterSelectionDialog::accept);
0042     connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterSelectionDialog::reject);
0043 
0044     auto searchLine = new KListWidgetSearchLine(this, filtersListWidget);
0045     searchLine->setPlaceholderText(i18nc("@info Displayed grayed-out inside the textbox, verb to search", "Search"));
0046 
0047     top->addWidget(searchLine);
0048     top->addWidget(filtersListWidget);
0049     filtersListWidget->setAlternatingRowColors(true);
0050     filtersListWidget->setSortingEnabled(false);
0051     filtersListWidget->setSelectionMode(QAbstractItemView::NoSelection);
0052 
0053     auto const buttonLayout = new QHBoxLayout();
0054     top->addLayout(buttonLayout);
0055     buttonLayout->addWidget(selectAllButton);
0056     buttonLayout->addWidget(unselectAllButton);
0057     top->addWidget(buttonBox);
0058 
0059     connect(selectAllButton, &QPushButton::clicked, this, &FilterSelectionDialog::slotSelectAllButton);
0060     connect(unselectAllButton, &QPushButton::clicked, this, &FilterSelectionDialog::slotUnselectAllButton);
0061 
0062     readConfig();
0063 }
0064 
0065 FilterSelectionDialog::~FilterSelectionDialog()
0066 {
0067     writeConfig();
0068 }
0069 
0070 void FilterSelectionDialog::reject()
0071 {
0072     qDeleteAll(originalFilters);
0073     QDialog::reject();
0074 }
0075 
0076 void FilterSelectionDialog::readConfig()
0077 {
0078     create(); // ensure a window is created
0079     windowHandle()->resize(QSize(300, 350));
0080     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterSelectionDialogName));
0081     KWindowConfig::restoreWindowSize(windowHandle(), group);
0082     resize(windowHandle()->size()); // workaround for QTBUG-40584
0083 }
0084 
0085 void FilterSelectionDialog::writeConfig()
0086 {
0087     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterSelectionDialogName));
0088     KWindowConfig::saveWindowSize(windowHandle(), group);
0089     group.sync();
0090 }
0091 
0092 void FilterSelectionDialog::setFilters(const QList<MailFilter *> &filters)
0093 {
0094     if (filters.isEmpty()) {
0095         mOkButton->setEnabled(false);
0096         return;
0097     }
0098 
0099     originalFilters = filters;
0100     filtersListWidget->clear();
0101 
0102     for (const MailFilter *filter : filters) {
0103         auto item = new QListWidgetItem(filter->name(), filtersListWidget);
0104         item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
0105         item->setCheckState(Qt::Checked);
0106     }
0107 }
0108 
0109 QList<MailFilter *> FilterSelectionDialog::selectedFilters() const
0110 {
0111     QList<MailFilter *> filters;
0112 
0113     const int filterCount = filtersListWidget->count();
0114     for (int i = 0; i < filterCount; ++i) {
0115         const QListWidgetItem *item = filtersListWidget->item(i);
0116         if (item->checkState() == Qt::Checked) {
0117             filters << originalFilters[i];
0118         } else {
0119             delete originalFilters[i];
0120         }
0121     }
0122 
0123     return filters;
0124 }
0125 
0126 void FilterSelectionDialog::slotUnselectAllButton()
0127 {
0128     const int filterCount = filtersListWidget->count();
0129     for (int i = 0; i < filterCount; ++i) {
0130         QListWidgetItem *const item = filtersListWidget->item(i);
0131         item->setCheckState(Qt::Unchecked);
0132     }
0133 }
0134 
0135 void FilterSelectionDialog::slotSelectAllButton()
0136 {
0137     const int filterCount = filtersListWidget->count();
0138     for (int i = 0; i < filterCount; ++i) {
0139         QListWidgetItem *const item = filtersListWidget->item(i);
0140         item->setCheckState(Qt::Checked);
0141     }
0142 }
0143 
0144 #include "moc_filterselectiondialog.cpp"