Mailcommon

filterselectiondialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2022 Laurent Montel <[email protected]>
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 <QDialogButtonBox>
16 #include <QListWidget>
17 #include <QVBoxLayout>
18 
19 using namespace MailCommon;
20 
21 FilterSelectionDialog::FilterSelectionDialog(QWidget *parent)
22  : QDialog(parent)
23  , filtersListWidget(new QListWidget(this))
24  , selectAllButton(new QPushButton(i18n("Select All"), this))
25  , unselectAllButton(new QPushButton(i18n("Unselect All"), this))
26 {
27  setObjectName(QStringLiteral("filterselection"));
28  setModal(true);
29  setWindowTitle(i18nc("@title:window", "Select Filters"));
30  auto top = new QVBoxLayout(this);
31 
33  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
34  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
35  mOkButton->setDefault(true);
36  connect(buttonBox, &QDialogButtonBox::accepted, this, &FilterSelectionDialog::accept);
37  connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterSelectionDialog::reject);
38 
39  auto searchLine = new KListWidgetSearchLine(this, filtersListWidget);
40  searchLine->setPlaceholderText(i18nc("@info Displayed grayed-out inside the textbox, verb to search", "Search"));
41 
42  top->addWidget(searchLine);
43  top->addWidget(filtersListWidget);
44  filtersListWidget->setAlternatingRowColors(true);
45  filtersListWidget->setSortingEnabled(false);
46  filtersListWidget->setSelectionMode(QAbstractItemView::NoSelection);
47 
48  auto const buttonLayout = new QHBoxLayout();
49  top->addLayout(buttonLayout);
50  buttonLayout->addWidget(selectAllButton);
51  buttonLayout->addWidget(unselectAllButton);
52  top->addWidget(buttonBox);
53 
54  connect(selectAllButton, &QPushButton::clicked, this, &FilterSelectionDialog::slotSelectAllButton);
55  connect(unselectAllButton, &QPushButton::clicked, this, &FilterSelectionDialog::slotUnselectAllButton);
56 
57  readConfig();
58 }
59 
60 FilterSelectionDialog::~FilterSelectionDialog()
61 {
62  writeConfig();
63 }
64 
65 void FilterSelectionDialog::reject()
66 {
67  qDeleteAll(originalFilters);
69 }
70 
71 void FilterSelectionDialog::writeConfig()
72 {
73  KConfigGroup group(KSharedConfig::openStateConfig(), "FilterSelectionDialog");
74  group.writeEntry("Size", size());
75 }
76 
77 void FilterSelectionDialog::readConfig()
78 {
79  KConfigGroup group(KSharedConfig::openStateConfig(), "FilterSelectionDialog");
80  const QSize sizeDialog = group.readEntry("Size", QSize(300, 350));
81  if (sizeDialog.isValid()) {
82  resize(sizeDialog);
83  }
84 }
85 
86 void FilterSelectionDialog::setFilters(const QVector<MailFilter *> &filters)
87 {
88  if (filters.isEmpty()) {
89  mOkButton->setEnabled(false);
90  return;
91  }
92 
93  originalFilters = filters;
94  filtersListWidget->clear();
95 
96  for (const MailFilter *filter : filters) {
97  auto item = new QListWidgetItem(filter->name(), filtersListWidget);
99  item->setCheckState(Qt::Checked);
100  }
101 }
102 
103 QVector<MailFilter *> FilterSelectionDialog::selectedFilters() const
104 {
105  QVector<MailFilter *> filters;
106 
107  const int filterCount = filtersListWidget->count();
108  for (int i = 0; i < filterCount; ++i) {
109  const QListWidgetItem *item = filtersListWidget->item(i);
110  if (item->checkState() == Qt::Checked) {
111  filters << originalFilters[i];
112  } else {
113  delete originalFilters[i];
114  }
115  }
116 
117  return filters;
118 }
119 
120 void FilterSelectionDialog::slotUnselectAllButton()
121 {
122  const int filterCount = filtersListWidget->count();
123  for (int i = 0; i < filterCount; ++i) {
124  QListWidgetItem *const item = filtersListWidget->item(i);
126  }
127 }
128 
129 void FilterSelectionDialog::slotSelectAllButton()
130 {
131  const int filterCount = filtersListWidget->count();
132  for (int i = 0; i < filterCount; ++i) {
133  QListWidgetItem *const item = filtersListWidget->item(i);
134  item->setCheckState(Qt::Checked);
135  }
136 }
bool isEmpty() const const
bool isValid() const const
virtual void reject()
Qt::CheckState checkState() const const
void clicked(bool checked)
void readConfig()
The MailFilter class.
Definition: mailfilter.h:28
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setCheckState(Qt::CheckState state)
ItemIsUserCheckable
void clear()
QString i18n(const char *text, const TYPE &arg...)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
Key_Return
void setObjectName(const QString &name)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int count(const T &value) const const
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.