configurefilterswidget.cpp 2.29 KB
Newer Older
1
2
3
4
5
6
7
/*
  SPDX-FileCopyrightText: 2021 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "configurefilterswidget.h"
8
#include "core/filtersavedmanager.h"
Laurent Montel's avatar
Laurent Montel committed
9
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
10
#include <QListWidget>
Laurent Montel's avatar
Laurent Montel committed
11
#include <QMenu>
12
13
14
15
#include <QVBoxLayout>
using namespace MessageList::Core;
ConfigureFiltersWidget::ConfigureFiltersWidget(QWidget *parent)
    : QWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
16
    , mListFiltersWidget(new QListWidget(this))
17
18
19
{
    auto mainLayout = new QVBoxLayout(this);
    mainLayout->setObjectName(QStringLiteral("mainLayout"));
Laurent Montel's avatar
Laurent Montel committed
20
    mainLayout->setContentsMargins({});
Laurent Montel's avatar
Laurent Montel committed
21
22
    mListFiltersWidget->setObjectName(QStringLiteral("mListFiltersWidget"));
    mainLayout->addWidget(mListFiltersWidget);
Laurent Montel's avatar
Laurent Montel committed
23
24
    mListFiltersWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(mListFiltersWidget, &QListWidget::customContextMenuRequested, this, &ConfigureFiltersWidget::slotCustomContextMenuRequested);
25
    init();
26
27
28
29
30
}

ConfigureFiltersWidget::~ConfigureFiltersWidget()
{
}
31
32
33

void ConfigureFiltersWidget::init()
{
Laurent Montel's avatar
Laurent Montel committed
34
35
    const QVector<FilterSavedManager::FilterInfo> lstFilters = FilterSavedManager::self()->filterInfos();
    for (const auto &filter : lstFilters) {
Laurent Montel's avatar
Laurent Montel committed
36
37
38
39
        auto item = new FilterListWidgetItem(mListFiltersWidget);
        item->setText(filter.filterName);
        item->setIdentifier(filter.identifier);
        mListFiltersWidget->addItem(item);
Laurent Montel's avatar
Laurent Montel committed
40
    }
41
}
Laurent Montel's avatar
Laurent Montel committed
42
43
44

void ConfigureFiltersWidget::slotCustomContextMenuRequested(const QPoint &pos)
{
Laurent Montel's avatar
Laurent Montel committed
45
    auto item = static_cast<FilterListWidgetItem *>(mListFiltersWidget->itemAt(pos));
Laurent Montel's avatar
Laurent Montel committed
46
47
    if (item) {
        QMenu menu(this);
Laurent Montel's avatar
Laurent Montel committed
48
        const QString identifier = item->identifier();
Laurent Montel's avatar
Laurent Montel committed
49
50
51
52
53
54
55
56
57
        menu.addAction(i18n("Remove"), this, [this, identifier]() {
            removeFilterInfo(identifier);
        });
        menu.exec(QCursor::pos());
    }
}

void ConfigureFiltersWidget::removeFilterInfo(const QString &identifier)
{
Laurent Montel's avatar
Laurent Montel committed
58
59
60
    if (!identifier.isEmpty()) {
        FilterSavedManager::self()->removeFilter(identifier);
    }
Laurent Montel's avatar
Laurent Montel committed
61
}
Laurent Montel's avatar
Laurent Montel committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

FilterListWidgetItem::FilterListWidgetItem(QListWidget *parent)
    : QListWidgetItem(parent)
{
}

FilterListWidgetItem::~FilterListWidgetItem()
{
}

const QString &FilterListWidgetItem::identifier() const
{
    return mIdentifier;
}

void FilterListWidgetItem::setIdentifier(const QString &newIdentifier)
{
    mIdentifier = newIdentifier;
}