customdebugsettingspage.cpp 7.62 KB
Newer Older
Laurent Montel's avatar
Improve  
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    SPDX-FileCopyrightText: 2015-2021 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 15
#include <QHBoxLayout>
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
16
#include <QListWidget>
Laurent Montel's avatar
Laurent Montel committed
17
#include <QMenu>
Laurent Montel's avatar
Laurent Montel committed
18
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
19 20
#include <QPushButton>
#include <QVBoxLayout>
Laurent Montel's avatar
Improve  
Laurent Montel committed
21 22 23

CustomDebugSettingsPage::CustomDebugSettingsPage(QWidget *parent)
    : QWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
24 25 26 27
    , 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
28
{
29
    auto mainLayout = new QVBoxLayout(this);
30

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

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

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

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

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

    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
68
    connect(mRemoveRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotRemoveRules);
Laurent Montel's avatar
Laurent Montel committed
69
    updateButtons();
Laurent Montel's avatar
Improve  
Laurent Montel committed
70 71 72 73 74 75
}

CustomDebugSettingsPage::~CustomDebugSettingsPage()
{
}

Laurent Montel's avatar
Laurent Montel committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
void CustomDebugSettingsPage::slotCustomContextMenu(const QPoint &pos)
{
    QMenu menu(this);
    const int selectedItemCount{mListWidget->selectedItems().count()};
    menu.addAction(i18n("Add Rule..."), this, &CustomDebugSettingsPage::slotAddRule);
    if (selectedItemCount == 1) {
        menu.addAction(QIcon::fromTheme(QStringLiteral("edit")), i18n("Edit Rule"), this, &CustomDebugSettingsPage::slotEditRule);
    }
    if (selectedItemCount > 0) {
        menu.addSeparator();
        menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Rule"), this, &CustomDebugSettingsPage::slotRemoveRules);
    }
    menu.exec(mListWidget->viewport()->mapToGlobal(pos));
}

Laurent Montel's avatar
Laurent Montel committed
91 92
void CustomDebugSettingsPage::updateButtons()
{
Laurent Montel's avatar
Laurent Montel committed
93 94
    mEditRule->setEnabled(mListWidget->selectedItems().count() == 1);
    mRemoveRule->setEnabled(!mListWidget->selectedItems().isEmpty());
Laurent Montel's avatar
Laurent Montel committed
95 96
}

Laurent Montel's avatar
Laurent Montel committed
97
void CustomDebugSettingsPage::fillList(const LoggingCategory::List &list)
98
{
Laurent Montel's avatar
Laurent Montel committed
99
    for (const LoggingCategory &cat : list) {
Laurent Montel's avatar
Laurent Montel committed
100
        QString ruleStr;
Laurent Montel's avatar
Laurent Montel committed
101
        ruleStr = cat.categoryName;
Laurent Montel's avatar
Laurent Montel committed
102
        switch (cat.loggingType) {
Laurent Montel's avatar
Laurent Montel committed
103
        case LoggingCategory::Undefined:
Laurent Montel's avatar
Laurent Montel committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        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
120 121
        }
        if (cat.enabled) {
Laurent Montel's avatar
Laurent Montel committed
122
            ruleStr += QStringLiteral("=true");
Laurent Montel's avatar
Laurent Montel committed
123
        } else {
Laurent Montel's avatar
Laurent Montel committed
124
            ruleStr += QStringLiteral("=false");
Laurent Montel's avatar
Laurent Montel committed
125
        }
Laurent Montel's avatar
Laurent Montel committed
126
        new QListWidgetItem(ruleStr, mListWidget);
Laurent Montel's avatar
Laurent Montel committed
127
    }
128 129
}

Laurent Montel's avatar
Laurent Montel committed
130
LoggingCategory::List CustomDebugSettingsPage::rules() const
Laurent Montel's avatar
Laurent Montel committed
131
{
Laurent Montel's avatar
Laurent Montel committed
132 133 134
    LoggingCategory::List lst;
    const int number(mListWidget->count());
    for (int i = 0; i < number; ++i) {
135
        const KDebugSettingsUtil::LineLoggingQtCategory cat = KDebugSettingsUtil::parseLineLoggingQtCategory(mListWidget->item(i)->text());
Laurent Montel's avatar
Laurent Montel committed
136
        if (cat.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
137
            LoggingCategory tmp;
Laurent Montel's avatar
Laurent Montel committed
138
            tmp.categoryName = cat.logName;
Laurent Montel's avatar
Laurent Montel committed
139
            tmp.enabled = cat.enabled;
Laurent Montel's avatar
Laurent Montel committed
140
            switch (cat.type) {
Laurent Montel's avatar
Laurent Montel committed
141
            case KDebugSettingsUtil::LineLoggingQtCategory::Unknown:
Laurent Montel's avatar
Laurent Montel committed
142
                break;
Laurent Montel's avatar
Laurent Montel committed
143
            case KDebugSettingsUtil::LineLoggingQtCategory::Info:
Laurent Montel's avatar
Laurent Montel committed
144 145
                tmp.loggingType = LoggingCategory::Info;
                break;
Laurent Montel's avatar
Laurent Montel committed
146
            case KDebugSettingsUtil::LineLoggingQtCategory::Warning:
Laurent Montel's avatar
Laurent Montel committed
147 148
                tmp.loggingType = LoggingCategory::Warning;
                break;
Laurent Montel's avatar
Laurent Montel committed
149
            case KDebugSettingsUtil::LineLoggingQtCategory::Debug:
Laurent Montel's avatar
Laurent Montel committed
150 151
                tmp.loggingType = LoggingCategory::Debug;
                break;
Laurent Montel's avatar
Laurent Montel committed
152
            case KDebugSettingsUtil::LineLoggingQtCategory::Critical:
Laurent Montel's avatar
Laurent Montel committed
153 154
                tmp.loggingType = LoggingCategory::Critical;
                break;
Laurent Montel's avatar
Laurent Montel committed
155
            case KDebugSettingsUtil::LineLoggingQtCategory::All:
Laurent Montel's avatar
Laurent Montel committed
156 157 158 159
                tmp.loggingType = LoggingCategory::All;
                break;
            }
            lst.append(tmp);
Laurent Montel's avatar
Laurent Montel committed
160
        }
Laurent Montel's avatar
Laurent Montel committed
161 162 163 164
    }
    return lst;
}

Laurent Montel's avatar
Laurent Montel committed
165
void CustomDebugSettingsPage::slotRemoveRules()
Laurent Montel's avatar
Laurent Montel committed
166
{
167 168 169 170 171 172 173 174 175 176 177 178
    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;
179
    }
Laurent Montel's avatar
Laurent Montel committed
180 181 182 183
}

void CustomDebugSettingsPage::slotEditRule()
{
Laurent Montel's avatar
Laurent Montel committed
184 185 186 187 188 189 190 191 192 193
    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
194
            }
Laurent Montel's avatar
Laurent Montel committed
195
            delete dlg;
Laurent Montel's avatar
Laurent Montel committed
196 197 198 199 200 201 202 203 204 205
        }
    }
}

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
206
            bool alreadyAdded = false;
Laurent Montel's avatar
Laurent Montel committed
207 208
            const int number(mListWidget->count());
            for (int i = 0; i < number; ++i) {
Laurent Montel's avatar
Laurent Montel committed
209 210 211 212 213
                if (ruleStr == mListWidget->item(i)->text()) {
                    alreadyAdded = true;
                    break;
                }
            }
Laurent Montel's avatar
Laurent Montel committed
214
            if (!alreadyAdded) {
Laurent Montel's avatar
Laurent Montel committed
215
                mListWidget->addItem(ruleStr);
Laurent Montel's avatar
Laurent Montel committed
216
            }
Laurent Montel's avatar
Laurent Montel committed
217 218 219 220
        }
    }
    delete dlg;
}