File indexing completed on 2022-11-29 20:24:01

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 "configurecustomsettingwidget.h"
0009 #include "categorytypecombobox.h"
0010 #include "kdebugsettingsutil.h"
0011 #include <KLineEdit>
0012 #include <KLocalizedString>
0013 #include <QCheckBox>
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QVBoxLayout>
0017 
0018 ConfigureCustomSettingWidget::ConfigureCustomSettingWidget(QWidget *parent)
0019     : QWidget(parent)
0020     , mCategoryLineEdit(new KLineEdit(this))
0021     , mEnableCategory(new QCheckBox(i18n("Enable"), this))
0022     , mCategoryType(new CategoryTypeComboBox(true, this))
0023 {
0024     auto vbox = new QVBoxLayout(this);
0025 
0026     auto lab = new QLabel(i18n("Category:"), this);
0027     lab->setObjectName(QStringLiteral("category_label"));
0028     mCategoryLineEdit->setTrapReturnKey(true);
0029     mCategoryLineEdit->setClearButtonEnabled(true);
0030     mCategoryLineEdit->setObjectName(QStringLiteral("category_lineedit"));
0031     connect(mCategoryLineEdit, &KLineEdit::textChanged, this, &ConfigureCustomSettingWidget::slotTextChanged);
0032     auto categoryLayout = new QHBoxLayout;
0033     vbox->addLayout(categoryLayout);
0034     categoryLayout->addWidget(lab);
0035     categoryLayout->addWidget(mCategoryLineEdit);
0036 
0037     lab = new QLabel(i18n("Type:"), this);
0038     lab->setObjectName(QStringLiteral("categorytype_label"));
0039     categoryLayout->addWidget(lab);
0040 
0041     mCategoryType->setObjectName(QStringLiteral("categorytype_combobox"));
0042     categoryLayout->addWidget(mCategoryType);
0043 
0044     mEnableCategory->setObjectName(QStringLiteral("enable_category"));
0045     categoryLayout->addWidget(mEnableCategory);
0046 }
0047 
0048 ConfigureCustomSettingWidget::~ConfigureCustomSettingWidget() = default;
0049 
0050 void ConfigureCustomSettingWidget::slotTextChanged(const QString &text)
0051 {
0052     const bool state = !text.trimmed().isEmpty();
0053     Q_EMIT enableButton(state);
0054 }
0055 
0056 void ConfigureCustomSettingWidget::setRule(const QString &rule)
0057 {
0058     const KDebugSettingsUtil::LineLoggingQtCategory cat = KDebugSettingsUtil::parseLineLoggingQtCategory(rule);
0059     mCategoryLineEdit->setText(cat.logName);
0060     mEnableCategory->setChecked(cat.enabled);
0061     switch (cat.type) {
0062     case KDebugSettingsUtil::LineLoggingQtCategory::Unknown:
0063         break;
0064     case KDebugSettingsUtil::LineLoggingQtCategory::Info:
0065         mCategoryType->setType(LoggingCategory::Info);
0066         break;
0067     case KDebugSettingsUtil::LineLoggingQtCategory::Warning:
0068         mCategoryType->setType(LoggingCategory::Warning);
0069         break;
0070     case KDebugSettingsUtil::LineLoggingQtCategory::Debug:
0071         mCategoryType->setType(LoggingCategory::Debug);
0072         break;
0073     case KDebugSettingsUtil::LineLoggingQtCategory::Critical:
0074         mCategoryType->setType(LoggingCategory::Critical);
0075         break;
0076     case KDebugSettingsUtil::LineLoggingQtCategory::All:
0077         mCategoryType->setType(LoggingCategory::All);
0078         break;
0079     }
0080 }
0081 
0082 QString ConfigureCustomSettingWidget::rule() const
0083 {
0084     QString ruleStr = mCategoryLineEdit->text().trimmed();
0085     if (!ruleStr.isEmpty()) {
0086         switch (mCategoryType->type()) {
0087         case LoggingCategory::All:
0088             break;
0089         case LoggingCategory::Info:
0090             ruleStr += QStringLiteral(".info");
0091             break;
0092         case LoggingCategory::Warning:
0093             ruleStr += QStringLiteral(".warning");
0094             break;
0095         case LoggingCategory::Debug:
0096             ruleStr += QStringLiteral(".debug");
0097             break;
0098         case LoggingCategory::Critical:
0099             ruleStr += QStringLiteral(".critical");
0100             break;
0101         case LoggingCategory::Off:
0102         case LoggingCategory::Undefined:
0103             break;
0104         }
0105 
0106         if (mEnableCategory->isChecked()) {
0107             ruleStr += QStringLiteral("=true");
0108         } else {
0109             ruleStr += QStringLiteral("=false");
0110         }
0111     }
0112     return ruleStr;
0113 }