configurefilterswidget.cpp 1.78 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
36
37
    const QVector<FilterSavedManager::FilterInfo> lstFilters = FilterSavedManager::self()->filterInfos();
    for (const auto &filter : lstFilters) {
        mListFiltersWidget->addItem(filter.filterName);
    }
38
39
    // TODO load filter
}
Laurent Montel's avatar
Laurent Montel committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

void ConfigureFiltersWidget::slotCustomContextMenuRequested(const QPoint &pos)
{
    auto item = mListFiltersWidget->itemAt(pos);
    if (item) {
        QMenu menu(this);
        QString identifier;
        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
56
57
58
    if (!identifier.isEmpty()) {
        FilterSavedManager::self()->removeFilter(identifier);
    }
Laurent Montel's avatar
Laurent Montel committed
59
}