File indexing completed on 2022-09-27 16:31:45

0001 /*
0002     SPDX-FileCopyrightText: 2015-2022 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "customdebugsettingspage.h"
0009 #include "configurecustomsettingdialog.h"
0010 #include "kdebugsettingsutil.h"
0011 #include <KListWidgetSearchLine>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QHBoxLayout>
0015 #include <QKeyEvent>
0016 #include <QLabel>
0017 #include <QListWidget>
0018 #include <QMenu>
0019 #include <QPointer>
0020 #include <QPushButton>
0021 #include <QVBoxLayout>
0022 
0023 CustomDebugSettingsPage::CustomDebugSettingsPage(QWidget *parent)
0024     : QWidget(parent)
0025     , mListWidget(new QListWidget(this))
0026     , mAddRule(new QPushButton(i18n("Add..."), this))
0027     , mEditRule(new QPushButton(i18n("Edit..."), this))
0028     , mRemoveRule(new QPushButton(i18n("Remove..."), this))
0029 {
0030     auto mainLayout = new QVBoxLayout(this);
0031 
0032     auto lab = new QLabel(i18n("Rules:"), this);
0033     lab->setObjectName(QStringLiteral("custom_label"));
0034     mainLayout->addWidget(lab);
0035 
0036     auto horizontalLayout = new QHBoxLayout;
0037     horizontalLayout->setContentsMargins(0, 0, 0, 0);
0038     mainLayout->addLayout(horizontalLayout);
0039 
0040     auto vbox = new QVBoxLayout;
0041     horizontalLayout->addLayout(vbox);
0042 
0043     mListWidget->setObjectName(QStringLiteral("custom_listwidget"));
0044     mListWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
0045     mListWidget->setContextMenuPolicy(Qt::CustomContextMenu);
0046     connect(mListWidget, &QListWidget::itemSelectionChanged, this, &CustomDebugSettingsPage::updateButtons);
0047     connect(mListWidget, &QListWidget::itemDoubleClicked, this, &CustomDebugSettingsPage::slotEditRule);
0048     connect(mListWidget, &QListWidget::customContextMenuRequested, this, &CustomDebugSettingsPage::slotCustomContextMenu);
0049     mTreeListWidgetSearchLine = new KListWidgetSearchLine(this, mListWidget);
0050     mTreeListWidgetSearchLine->setObjectName(QStringLiteral("searchline"));
0051     mTreeListWidgetSearchLine->setPlaceholderText(i18n("Search..."));
0052     vbox->addWidget(mTreeListWidgetSearchLine);
0053     vbox->addWidget(mListWidget);
0054 
0055     auto buttonLayout = new QVBoxLayout;
0056     horizontalLayout->addLayout(buttonLayout);
0057 
0058     mAddRule->setObjectName(QStringLiteral("add_rule"));
0059     buttonLayout->addWidget(mAddRule);
0060     connect(mAddRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotAddRule);
0061 
0062     mEditRule->setObjectName(QStringLiteral("edit_rule"));
0063     buttonLayout->addWidget(mEditRule);
0064     connect(mEditRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotEditRule);
0065 
0066     mRemoveRule->setObjectName(QStringLiteral("remove_rule"));
0067     buttonLayout->addWidget(mRemoveRule);
0068     buttonLayout->addStretch();
0069     connect(mRemoveRule, &QAbstractButton::clicked, this, &CustomDebugSettingsPage::slotRemoveRules);
0070     updateButtons();
0071     mTreeListWidgetSearchLine->installEventFilter(this);
0072 }
0073 
0074 CustomDebugSettingsPage::~CustomDebugSettingsPage() = default;
0075 
0076 bool CustomDebugSettingsPage::eventFilter(QObject *obj, QEvent *event)
0077 {
0078     if (event->type() == QEvent::KeyPress && obj == mTreeListWidgetSearchLine) {
0079         auto key = static_cast<QKeyEvent *>(event);
0080         if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
0081             event->accept();
0082             return true;
0083         }
0084     }
0085     return QWidget::eventFilter(obj, event);
0086 }
0087 
0088 void CustomDebugSettingsPage::slotCustomContextMenu(const QPoint &pos)
0089 {
0090     QMenu menu(this);
0091     const auto selectedItemCount{mListWidget->selectedItems().count()};
0092     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add Rule..."), this, &CustomDebugSettingsPage::slotAddRule);
0093     if (selectedItemCount == 1) {
0094         menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Edit Rule"), this, &CustomDebugSettingsPage::slotEditRule);
0095     }
0096     if (selectedItemCount > 0) {
0097         menu.addSeparator();
0098         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove Rule"), this, &CustomDebugSettingsPage::slotRemoveRules);
0099     }
0100     menu.exec(mListWidget->viewport()->mapToGlobal(pos));
0101 }
0102 
0103 void CustomDebugSettingsPage::updateButtons()
0104 {
0105     mEditRule->setEnabled(mListWidget->selectedItems().count() == 1);
0106     mRemoveRule->setEnabled(!mListWidget->selectedItems().isEmpty());
0107 }
0108 
0109 void CustomDebugSettingsPage::fillList(const LoggingCategory::List &list)
0110 {
0111     for (const LoggingCategory &cat : list) {
0112         QString ruleStr;
0113         ruleStr = cat.categoryName;
0114         switch (cat.loggingType) {
0115         case LoggingCategory::Undefined:
0116         case LoggingCategory::All:
0117             break;
0118         case LoggingCategory::Info:
0119             ruleStr += QStringLiteral(".info");
0120             break;
0121         case LoggingCategory::Warning:
0122             ruleStr += QStringLiteral(".warning");
0123             break;
0124         case LoggingCategory::Debug:
0125             ruleStr += QStringLiteral(".debug");
0126             break;
0127         case LoggingCategory::Critical:
0128             ruleStr += QStringLiteral(".critical");
0129             break;
0130         case LoggingCategory::Off:
0131             break;
0132         }
0133         if (cat.enabled) {
0134             ruleStr += QStringLiteral("=true");
0135         } else {
0136             ruleStr += QStringLiteral("=false");
0137         }
0138         new QListWidgetItem(ruleStr, mListWidget);
0139     }
0140 }
0141 
0142 LoggingCategory::List CustomDebugSettingsPage::rules() const
0143 {
0144     LoggingCategory::List lst;
0145     const int number(mListWidget->count());
0146     for (int i = 0; i < number; ++i) {
0147         const KDebugSettingsUtil::LineLoggingQtCategory cat = KDebugSettingsUtil::parseLineLoggingQtCategory(mListWidget->item(i)->text());
0148         if (cat.isValid()) {
0149             LoggingCategory tmp;
0150             tmp.categoryName = cat.logName;
0151             tmp.enabled = cat.enabled;
0152             switch (cat.type) {
0153             case KDebugSettingsUtil::LineLoggingQtCategory::Unknown:
0154                 break;
0155             case KDebugSettingsUtil::LineLoggingQtCategory::Info:
0156                 tmp.loggingType = LoggingCategory::Info;
0157                 break;
0158             case KDebugSettingsUtil::LineLoggingQtCategory::Warning:
0159                 tmp.loggingType = LoggingCategory::Warning;
0160                 break;
0161             case KDebugSettingsUtil::LineLoggingQtCategory::Debug:
0162                 tmp.loggingType = LoggingCategory::Debug;
0163                 break;
0164             case KDebugSettingsUtil::LineLoggingQtCategory::Critical:
0165                 tmp.loggingType = LoggingCategory::Critical;
0166                 break;
0167             case KDebugSettingsUtil::LineLoggingQtCategory::All:
0168                 tmp.loggingType = LoggingCategory::All;
0169                 break;
0170             }
0171             lst.append(tmp);
0172         }
0173     }
0174     return lst;
0175 }
0176 
0177 void CustomDebugSettingsPage::slotRemoveRules()
0178 {
0179     QList<QListWidgetItem *> lst = mListWidget->selectedItems();
0180     if (lst.isEmpty()) {
0181         return;
0182     }
0183     const QString str = i18np("Do you want to remove this rule?", "Do you want to remove these %1 rules?", lst.count());
0184 
0185     if (KMessageBox::No == KMessageBox::warningYesNo(this, str, i18n("Remove Rule"), KStandardGuiItem::remove())) {
0186         return;
0187     }
0188     for (int i = 0; i < lst.count(); ++i) {
0189         QListWidgetItem *item = lst.at(i);
0190         delete item;
0191     }
0192 }
0193 
0194 void CustomDebugSettingsPage::slotEditRule()
0195 {
0196     if (mListWidget->selectedItems().count() == 1) {
0197         QListWidgetItem *item = mListWidget->selectedItems().at(0);
0198         if (item) {
0199             QPointer<ConfigureCustomSettingDialog> dlg = new ConfigureCustomSettingDialog(this);
0200             dlg->setRule(item->text());
0201             if (dlg->exec()) {
0202                 const QString ruleStr = dlg->rule();
0203                 if (!ruleStr.isEmpty()) {
0204                     item->setText(dlg->rule());
0205                 }
0206             }
0207             delete dlg;
0208         }
0209     }
0210 }
0211 
0212 void CustomDebugSettingsPage::slotAddRule()
0213 {
0214     QPointer<ConfigureCustomSettingDialog> dlg = new ConfigureCustomSettingDialog(this);
0215     if (dlg->exec()) {
0216         const QString ruleStr = dlg->rule();
0217         if (!ruleStr.isEmpty()) {
0218             bool alreadyAdded = false;
0219             const int number(mListWidget->count());
0220             for (int i = 0; i < number; ++i) {
0221                 if (ruleStr == mListWidget->item(i)->text()) {
0222                     alreadyAdded = true;
0223                     break;
0224                 }
0225             }
0226             if (!alreadyAdded) {
0227                 mListWidget->addItem(ruleStr);
0228             }
0229         }
0230     }
0231     delete dlg;
0232 }