Messagelib

configurefilterswidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "configurefilterswidget.h"
8 #include "core/filtersavedmanager.h"
9 #include "filternamedialog.h"
10 #include <KLocalizedString>
11 #include <KMessageBox>
12 #include <QMenu>
13 #include <QPointer>
14 #include <QVBoxLayout>
15 using namespace MessageList::Core;
16 ConfigureFiltersWidget::ConfigureFiltersWidget(QWidget *parent)
17  : QWidget(parent)
18  , mListFiltersWidget(new QListWidget(this))
19 {
20  auto mainLayout = new QVBoxLayout(this);
21  mainLayout->setObjectName(QStringLiteral("mainLayout"));
22  mainLayout->setContentsMargins({});
23  mListFiltersWidget->setObjectName(QStringLiteral("mListFiltersWidget"));
24  mainLayout->addWidget(mListFiltersWidget);
25  mListFiltersWidget->setContextMenuPolicy(Qt::CustomContextMenu);
26  connect(mListFiltersWidget, &QListWidget::customContextMenuRequested, this, &ConfigureFiltersWidget::slotCustomContextMenuRequested);
27  connect(mListFiltersWidget, &QListWidget::itemDoubleClicked, this, &ConfigureFiltersWidget::slotConfigureFilter);
28  init();
29 }
30 
31 ConfigureFiltersWidget::~ConfigureFiltersWidget() = default;
32 
33 void ConfigureFiltersWidget::init()
34 {
35  const QVector<FilterSavedManager::FilterInfo> lstFilters = FilterSavedManager::self()->filterInfos();
36  for (const auto &filter : lstFilters) {
37  auto item = new FilterListWidgetItem(mListFiltersWidget);
38  item->setText(filter.filterName);
39  item->setIdentifier(filter.identifier);
40  item->setIconName(filter.iconName);
41  item->setIcon(QIcon::fromTheme(filter.iconName));
42  mListFiltersWidget->addItem(item);
43  }
44 }
45 
46 void ConfigureFiltersWidget::slotConfigureFilter(QListWidgetItem *widgetItem)
47 {
48  if (widgetItem) {
49  auto item = static_cast<FilterListWidgetItem *>(widgetItem);
50  const QString identifier = item->identifier();
51  QPointer<FilterNameDialog> dlg = new FilterNameDialog(this);
52  dlg->setFilterName(item->text());
53  dlg->setIconName(item->iconName());
54  if (dlg->exec()) {
55  QString newName = dlg->filterName();
56  const QString newIconName = dlg->iconName();
57  newName = newName.trimmed();
58  if (!newName.isEmpty() && ((newName != item->text()) || (newIconName != item->iconName()))) {
59  updateFilterInfo(identifier, newName, newIconName);
60  item->setText(newName);
61  item->setIconName(newIconName);
62  item->setIcon(QIcon::fromTheme(newIconName));
63  }
64  }
65  delete dlg;
66  }
67 }
68 
69 void ConfigureFiltersWidget::slotCustomContextMenuRequested(const QPoint &pos)
70 {
71  auto item = static_cast<FilterListWidgetItem *>(mListFiltersWidget->itemAt(pos));
72  if (item) {
73  QMenu menu(this);
74  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("Configure..."), this, [this, item]() {
75  slotConfigureFilter(item);
76  });
77  menu.addSeparator();
78  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove"), this, [this, item]() {
79  const QString identifier = item->identifier();
80  const int answer = KMessageBox::questionYesNo(this,
81  i18n("Do you want to delete this filter?"),
82  i18n("Remove Filter"),
85  if (answer == KMessageBox::Yes) {
86  removeFilterInfo(identifier);
87  delete item;
88  }
89  });
90  menu.exec(QCursor::pos());
91  }
92 }
93 
94 void ConfigureFiltersWidget::removeFilterInfo(const QString &identifier)
95 {
96  if (!identifier.isEmpty()) {
97  FilterSavedManager::self()->removeFilter(identifier);
98  }
99 }
100 
101 void ConfigureFiltersWidget::updateFilterInfo(const QString &identifier, const QString &newName, const QString &newIconName)
102 {
103  if (!identifier.isEmpty()) {
104  FilterSavedManager::self()->updateFilter(identifier, newName, newIconName);
105  }
106 }
107 
108 FilterListWidgetItem::FilterListWidgetItem(QListWidget *parent)
109  : QListWidgetItem(parent)
110 {
111 }
112 
113 FilterListWidgetItem::~FilterListWidgetItem() = default;
114 
115 const QString &FilterListWidgetItem::identifier() const
116 {
117  return mIdentifier;
118 }
119 
120 void FilterListWidgetItem::setIdentifier(const QString &newIdentifier)
121 {
122  mIdentifier = newIdentifier;
123 }
124 
125 const QString &FilterListWidgetItem::iconName() const
126 {
127  return mIconName;
128 }
129 
130 void FilterListWidgetItem::setIconName(const QString &newIconName)
131 {
132  mIconName = newIconName;
133 }
void customContextMenuRequested(const QPoint &pos)
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
KGuiItem cancel()
CustomContextMenu
KGuiItem del()
bool isEmpty() const const
QString trimmed() const const
QPoint pos() const const
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QCA_EXPORT void init()
QString i18n(const char *text, const TYPE &arg...)
QPoint pos()
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void itemDoubleClicked(QListWidgetItem *item)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.