customdebugsettingspage.cpp 8.21 KB
Newer Older
Laurent Montel's avatar
Improve  
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    SPDX-FileCopyrightText: 2015-2022 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Improve  
Laurent Montel committed
3

Laurent Montel's avatar
Laurent Montel committed
4
    SPDX-License-Identifier: LGPL-2.0-or-later
Laurent Montel's avatar
Improve  
Laurent Montel committed
5
6
7
8

*/

#include "customdebugsettingspage.h"
Laurent Montel's avatar
Laurent Montel committed
9
#include "configurecustomsettingdialog.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include "kdebugsettingsutil.h"
Laurent Montel's avatar
Laurent Montel committed
11
#include <KListWidgetSearchLine>
Laurent Montel's avatar
Laurent Montel committed
12
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
13
#include <KMessageBox>
14
#include <QHBoxLayout>
15
#include <QKeyEvent>
16
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
17
#include <QListWidget>
Laurent Montel's avatar
Laurent Montel committed
18
#include <QMenu>
Laurent Montel's avatar
Laurent Montel committed
19
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
20
21
#include <QPushButton>
#include <QVBoxLayout>
Laurent Montel's avatar
Improve  
Laurent Montel committed
22
23
24

CustomDebugSettingsPage::CustomDebugSettingsPage(QWidget *parent)
    : QWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
25
26
27
28
    , mListWidget(new QListWidget(this))
    , mAddRule(new QPushButton(i18n("Add..."), this))
    , mEditRule(new QPushButton(i18n("Edit..."), this))
    , mRemoveRule(new QPushButton(i18n("Remove..."), this))
Laurent Montel's avatar
Improve  
Laurent Montel committed
29
{
30
    auto mainLayout = new QVBoxLayout(this);
31

Laurent Montel's avatar
Laurent Montel committed
32
    auto lab = new QLabel(i18n("Rules:"), this);
33
    lab->setObjectName(QStringLiteral("custom_label"));
Laurent Montel's avatar
Laurent Montel committed
34
    mainLayout->addWidget(lab);
35

36
    auto horizontalLayout = new QHBoxLayout;
37
    horizontalLayout->setContentsMargins(0, 0, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
38
39
    mainLayout->addLayout(horizontalLayout);

40
    auto vbox = new QVBoxLayout;
Laurent Montel's avatar
Laurent Montel committed
41
    horizontalLayout->addLayout(vbox);
Laurent Montel's avatar
Laurent Montel committed
42

43
    mListWidget->setObjectName(QStringLiteral("custom_listwidget"));
44
    mListWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
Laurent Montel's avatar
Laurent Montel committed
45
    mListWidget->setContextMenuPolicy(Qt::CustomContextMenu);
Laurent Montel's avatar
Laurent Montel committed
46
    connect(mListWidget, &QListWidget::itemSelectionChanged, this, &CustomDebugSettingsPage::updateButtons);
47
    connect(mListWidget, &QListWidget::itemDoubleClicked, this, &CustomDebugSettingsPage::slotEditRule);
Laurent Montel's avatar
Laurent Montel committed
48
    connect(mListWidget, &QListWidget::customContextMenuRequested, this, &CustomDebugSettingsPage::slotCustomContextMenu);
49
50
51
52
    mTreeListWidgetSearchLine = new KListWidgetSearchLine(this, mListWidget);
    mTreeListWidgetSearchLine->setObjectName(QStringLiteral("searchline"));
    mTreeListWidgetSearchLine->setPlaceholderText(i18n("Search..."));
    vbox->addWidget(mTreeListWidgetSearchLine);
53
    vbox->addWidget(mListWidget);
Laurent Montel's avatar
Laurent Montel committed
54

55
    auto buttonLayout = new QVBoxLayout;
Laurent Montel's avatar
Laurent Montel committed
56
    horizontalLayout->addLayout(buttonLayout);
Laurent Montel's avatar
Laurent Montel committed
57
58
59
60
61
62
63
64
65
66
67
68

    mAddRule->setObjectName(QStringLiteral("add_rule"));
    buttonLayout->addWidget(mAddRule);
    connect(mAddRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotAddRule);

    mEditRule->setObjectName(QStringLiteral("edit_rule"));
    buttonLayout->addWidget(mEditRule);
    connect(mEditRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotEditRule);

    mRemoveRule->setObjectName(QStringLiteral("remove_rule"));
    buttonLayout->addWidget(mRemoveRule);
    buttonLayout->addStretch();
Laurent Montel's avatar
Laurent Montel committed
69
    connect(mRemoveRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotRemoveRules);
Laurent Montel's avatar
Laurent Montel committed
70
    updateButtons();
71
    mTreeListWidgetSearchLine->installEventFilter(this);
Laurent Montel's avatar
Improve  
Laurent Montel committed
72
73
}

Laurent Montel's avatar
Laurent Montel committed
74
CustomDebugSettingsPage::~CustomDebugSettingsPage() = default;
Laurent Montel's avatar
Improve  
Laurent Montel committed
75

76
77
78
79
80
81
82
83
84
85
86
87
bool CustomDebugSettingsPage::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress && obj == mTreeListWidgetSearchLine) {
        auto key = static_cast<QKeyEvent *>(event);
        if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
            event->accept();
            return true;
        }
    }
    return QWidget::eventFilter(obj, event);
}

Laurent Montel's avatar
Laurent Montel committed
88
89
90
void CustomDebugSettingsPage::slotCustomContextMenu(const QPoint &pos)
{
    QMenu menu(this);
Laurent Montel's avatar
Laurent Montel committed
91
    const auto selectedItemCount{mListWidget->selectedItems().count()};
Laurent Montel's avatar
Laurent Montel committed
92
    menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add Rule..."), this, &CustomDebugSettingsPage::slotAddRule);
Laurent Montel's avatar
Laurent Montel committed
93
    if (selectedItemCount == 1) {
Laurent Montel's avatar
Laurent Montel committed
94
        menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Edit Rule"), this, &CustomDebugSettingsPage::slotEditRule);
Laurent Montel's avatar
Laurent Montel committed
95
96
97
    }
    if (selectedItemCount > 0) {
        menu.addSeparator();
Laurent Montel's avatar
Laurent Montel committed
98
        menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove Rule"), this, &CustomDebugSettingsPage::slotRemoveRules);
Laurent Montel's avatar
Laurent Montel committed
99
100
101
102
    }
    menu.exec(mListWidget->viewport()->mapToGlobal(pos));
}

Laurent Montel's avatar
Laurent Montel committed
103
104
void CustomDebugSettingsPage::updateButtons()
{
Laurent Montel's avatar
Laurent Montel committed
105
106
    mEditRule->setEnabled(mListWidget->selectedItems().count() == 1);
    mRemoveRule->setEnabled(!mListWidget->selectedItems().isEmpty());
Laurent Montel's avatar
Laurent Montel committed
107
108
}

Laurent Montel's avatar
Laurent Montel committed
109
void CustomDebugSettingsPage::fillList(const LoggingCategory::List &list)
110
{
Laurent Montel's avatar
Laurent Montel committed
111
    for (const LoggingCategory &cat : list) {
Laurent Montel's avatar
Laurent Montel committed
112
        QString ruleStr;
Laurent Montel's avatar
Laurent Montel committed
113
        ruleStr = cat.categoryName;
Laurent Montel's avatar
Laurent Montel committed
114
        switch (cat.loggingType) {
Laurent Montel's avatar
Laurent Montel committed
115
        case LoggingCategory::Undefined:
Laurent Montel's avatar
Laurent Montel committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        case LoggingCategory::All:
            break;
        case LoggingCategory::Info:
            ruleStr += QStringLiteral(".info");
            break;
        case LoggingCategory::Warning:
            ruleStr += QStringLiteral(".warning");
            break;
        case LoggingCategory::Debug:
            ruleStr += QStringLiteral(".debug");
            break;
        case LoggingCategory::Critical:
            ruleStr += QStringLiteral(".critical");
            break;
        case LoggingCategory::Off:
            break;
Laurent Montel's avatar
Laurent Montel committed
132
133
        }
        if (cat.enabled) {
Laurent Montel's avatar
Laurent Montel committed
134
            ruleStr += QStringLiteral("=true");
Laurent Montel's avatar
Laurent Montel committed
135
        } else {
Laurent Montel's avatar
Laurent Montel committed
136
            ruleStr += QStringLiteral("=false");
Laurent Montel's avatar
Laurent Montel committed
137
        }
Laurent Montel's avatar
Laurent Montel committed
138
        new QListWidgetItem(ruleStr, mListWidget);
Laurent Montel's avatar
Laurent Montel committed
139
    }
140
141
}

Laurent Montel's avatar
Laurent Montel committed
142
LoggingCategory::List CustomDebugSettingsPage::rules() const
Laurent Montel's avatar
Laurent Montel committed
143
{
Laurent Montel's avatar
Laurent Montel committed
144
145
146
    LoggingCategory::List lst;
    const int number(mListWidget->count());
    for (int i = 0; i < number; ++i) {
147
        const KDebugSettingsUtil::LineLoggingQtCategory cat = KDebugSettingsUtil::parseLineLoggingQtCategory(mListWidget->item(i)->text());
Laurent Montel's avatar
Laurent Montel committed
148
        if (cat.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
149
            LoggingCategory tmp;
Laurent Montel's avatar
Laurent Montel committed
150
            tmp.categoryName = cat.logName;
Laurent Montel's avatar
Laurent Montel committed
151
            tmp.enabled = cat.enabled;
Laurent Montel's avatar
Laurent Montel committed
152
            switch (cat.type) {
Laurent Montel's avatar
Laurent Montel committed
153
            case KDebugSettingsUtil::LineLoggingQtCategory::Unknown:
Laurent Montel's avatar
Laurent Montel committed
154
                break;
Laurent Montel's avatar
Laurent Montel committed
155
            case KDebugSettingsUtil::LineLoggingQtCategory::Info:
Laurent Montel's avatar
Laurent Montel committed
156
157
                tmp.loggingType = LoggingCategory::Info;
                break;
Laurent Montel's avatar
Laurent Montel committed
158
            case KDebugSettingsUtil::LineLoggingQtCategory::Warning:
Laurent Montel's avatar
Laurent Montel committed
159
160
                tmp.loggingType = LoggingCategory::Warning;
                break;
Laurent Montel's avatar
Laurent Montel committed
161
            case KDebugSettingsUtil::LineLoggingQtCategory::Debug:
Laurent Montel's avatar
Laurent Montel committed
162
163
                tmp.loggingType = LoggingCategory::Debug;
                break;
Laurent Montel's avatar
Laurent Montel committed
164
            case KDebugSettingsUtil::LineLoggingQtCategory::Critical:
Laurent Montel's avatar
Laurent Montel committed
165
166
                tmp.loggingType = LoggingCategory::Critical;
                break;
Laurent Montel's avatar
Laurent Montel committed
167
            case KDebugSettingsUtil::LineLoggingQtCategory::All:
Laurent Montel's avatar
Laurent Montel committed
168
169
170
171
                tmp.loggingType = LoggingCategory::All;
                break;
            }
            lst.append(tmp);
Laurent Montel's avatar
Laurent Montel committed
172
        }
Laurent Montel's avatar
Laurent Montel committed
173
174
175
176
    }
    return lst;
}

Laurent Montel's avatar
Laurent Montel committed
177
void CustomDebugSettingsPage::slotRemoveRules()
Laurent Montel's avatar
Laurent Montel committed
178
{
179
180
181
182
183
184
185
186
187
188
189
190
    QList<QListWidgetItem *> lst = mListWidget->selectedItems();
    if (lst.isEmpty()) {
        return;
    }
    const QString str = i18np("Do you want to remove this rule?", "Do you want to remove these %1 rules?", lst.count());

    if (KMessageBox::No == KMessageBox::warningYesNo(this, str)) {
        return;
    }
    for (int i = 0; i < lst.count(); ++i) {
        QListWidgetItem *item = lst.at(i);
        delete item;
191
    }
Laurent Montel's avatar
Laurent Montel committed
192
193
194
195
}

void CustomDebugSettingsPage::slotEditRule()
{
Laurent Montel's avatar
Laurent Montel committed
196
197
198
199
200
201
202
203
204
205
    if (mListWidget->selectedItems().count() == 1) {
        QListWidgetItem *item = mListWidget->selectedItems().at(0);
        if (item) {
            QPointer<ConfigureCustomSettingDialog> dlg = new ConfigureCustomSettingDialog(this);
            dlg->setRule(item->text());
            if (dlg->exec()) {
                const QString ruleStr = dlg->rule();
                if (!ruleStr.isEmpty()) {
                    item->setText(dlg->rule());
                }
Laurent Montel's avatar
Laurent Montel committed
206
            }
Laurent Montel's avatar
Laurent Montel committed
207
            delete dlg;
Laurent Montel's avatar
Laurent Montel committed
208
209
210
211
212
213
214
215
216
217
        }
    }
}

void CustomDebugSettingsPage::slotAddRule()
{
    QPointer<ConfigureCustomSettingDialog> dlg = new ConfigureCustomSettingDialog(this);
    if (dlg->exec()) {
        const QString ruleStr = dlg->rule();
        if (!ruleStr.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
218
            bool alreadyAdded = false;
Laurent Montel's avatar
Laurent Montel committed
219
220
            const int number(mListWidget->count());
            for (int i = 0; i < number; ++i) {
Laurent Montel's avatar
Laurent Montel committed
221
222
223
224
225
                if (ruleStr == mListWidget->item(i)->text()) {
                    alreadyAdded = true;
                    break;
                }
            }
Laurent Montel's avatar
Laurent Montel committed
226
            if (!alreadyAdded) {
Laurent Montel's avatar
Laurent Montel committed
227
                mListWidget->addItem(ruleStr);
Laurent Montel's avatar
Laurent Montel committed
228
            }
Laurent Montel's avatar
Laurent Montel committed
229
230
231
232
        }
    }
    delete dlg;
}