Messagelib

configurefilterswidget.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
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 <QPainter>
14#include <QPointer>
15#include <QVBoxLayout>
16using namespace MessageList::Core;
17ConfigureFiltersWidget::ConfigureFiltersWidget(QWidget *parent)
18 : QWidget(parent)
19 , mListFiltersWidget(new ConfigureFiltersListWidget(this))
20{
21 auto mainLayout = new QVBoxLayout(this);
22 mainLayout->setObjectName(QLatin1StringView("mainLayout"));
23 mainLayout->setContentsMargins({});
24 mListFiltersWidget->setObjectName(QLatin1StringView("mListFiltersWidget"));
25 mainLayout->addWidget(mListFiltersWidget);
26 mListFiltersWidget->setContextMenuPolicy(Qt::CustomContextMenu);
27 connect(mListFiltersWidget, &QListWidget::customContextMenuRequested, this, &ConfigureFiltersWidget::slotCustomContextMenuRequested);
28 connect(mListFiltersWidget, &QListWidget::itemDoubleClicked, this, &ConfigureFiltersWidget::slotConfigureFilter);
29 init();
30}
31
32ConfigureFiltersWidget::~ConfigureFiltersWidget() = default;
33
34void ConfigureFiltersWidget::init()
35{
36 const QList<FilterSavedManager::FilterInfo> lstFilters = FilterSavedManager::self()->filterInfos();
37 for (const auto &filter : lstFilters) {
38 auto item = new FilterListWidgetItem(mListFiltersWidget);
39 item->setText(filter.filterName);
40 item->setIdentifier(filter.identifier);
41 item->setIconName(filter.iconName);
42 item->setIcon(QIcon::fromTheme(filter.iconName));
43 mListFiltersWidget->addItem(item);
44 }
45}
46
47void ConfigureFiltersWidget::slotConfigureFilter(QListWidgetItem *widgetItem)
48{
49 if (widgetItem) {
50 auto item = static_cast<FilterListWidgetItem *>(widgetItem);
51 const QString identifier = item->identifier();
52 QPointer<FilterNameDialog> dlg = new FilterNameDialog(this);
53 dlg->setFilterName(item->text());
54 dlg->setIconName(item->iconName());
55 if (dlg->exec()) {
56 QString newName = dlg->filterName();
57 const QString newIconName = dlg->iconName();
58 newName = newName.trimmed();
59 if (!newName.isEmpty() && ((newName != item->text()) || (newIconName != item->iconName()))) {
60 updateFilterInfo(identifier, newName, newIconName);
61 item->setText(newName);
62 item->setIconName(newIconName);
63 item->setIcon(QIcon::fromTheme(newIconName));
64 }
65 }
66 delete dlg;
67 }
68}
69
70void ConfigureFiltersWidget::slotCustomContextMenuRequested(const QPoint &pos)
71{
72 auto item = static_cast<FilterListWidgetItem *>(mListFiltersWidget->itemAt(pos));
73 if (item) {
74 QMenu menu(this);
75 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("Configure..."), this, [this, item]() {
76 slotConfigureFilter(item);
77 });
78 menu.addSeparator();
79 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove"), this, [this, item]() {
80 const QString identifier = item->identifier();
81 const int answer = KMessageBox::questionTwoActions(this,
82 i18n("Do you want to delete this filter?"),
83 i18nc("@title:window", "Remove Filter"),
86 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
87 removeFilterInfo(identifier);
88 delete item;
89 }
90 });
91 menu.exec(QCursor::pos());
92 }
93}
94
95void ConfigureFiltersWidget::removeFilterInfo(const QString &identifier)
96{
97 if (!identifier.isEmpty()) {
98 FilterSavedManager::self()->removeFilter(identifier);
99 }
100}
101
102void ConfigureFiltersWidget::updateFilterInfo(const QString &identifier, const QString &newName, const QString &newIconName)
103{
104 if (!identifier.isEmpty()) {
105 FilterSavedManager::self()->updateFilter(identifier, newName, newIconName);
106 }
107}
108
109FilterListWidgetItem::FilterListWidgetItem(QListWidget *parent)
110 : QListWidgetItem(parent)
111{
112}
113
114FilterListWidgetItem::~FilterListWidgetItem() = default;
115
116const QString &FilterListWidgetItem::identifier() const
117{
118 return mIdentifier;
119}
120
121void FilterListWidgetItem::setIdentifier(const QString &newIdentifier)
122{
123 mIdentifier = newIdentifier;
124}
125
126const QString &FilterListWidgetItem::iconName() const
127{
128 return mIconName;
129}
130
131void FilterListWidgetItem::setIconName(const QString &newIconName)
132{
133 mIconName = newIconName;
134}
135
136ConfigureFiltersListWidget::ConfigureFiltersListWidget(QWidget *parent)
137 : QListWidget(parent)
138{
139}
140
141ConfigureFiltersListWidget::~ConfigureFiltersListWidget() = default;
142
143void ConfigureFiltersListWidget::paintEvent(QPaintEvent *event)
144{
145 if (!model() || model()->rowCount() == 0) {
146 QPainter p(viewport());
147
148 QFont font = p.font();
149 font.setItalic(true);
150 p.setFont(font);
151
152 p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter, i18n("No result found"));
153 } else {
155 }
156}
157
158#include "moc_configurefilterswidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem cancel()
KGuiItem del()
The implementation independent part of the MessageList library.
Definition aggregation.h:22
QAbstractItemModel * model() const const
virtual void paintEvent(QPaintEvent *event) override
QWidget * viewport() const const
QPoint pos()
void setItalic(bool enable)
QIcon fromTheme(const QString &name)
void addItem(QListWidgetItem *item)
virtual bool event(QEvent *e) override
QListWidgetItem * itemAt(const QPoint &p) const const
void itemDoubleClicked(QListWidgetItem *item)
bool isEmpty() const const
QString trimmed() const const
AlignCenter
CustomContextMenu
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void customContextMenuRequested(const QPoint &pos)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.