Warning, file /utilities/kdebugsettings/src/customdebugsettingspage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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