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 "kdeapplicationtreelistwidget.h"
0009 #include "categorytypecombobox.h"
0010 #include "kdebugsettingsutil.h"
0011 #include <KLocalizedString>
0012 #include <QHeaderView>
0013 
0014 KDEApplicationTreeListWidgetItem::KDEApplicationTreeListWidgetItem(const QString &category, QTreeWidget *parent)
0015     : QTreeWidgetItem(parent)
0016     , mCategory(category)
0017     , mCategoryTypeCombobox(new CategoryTypeComboBox(false))
0018 {
0019     treeWidget()->setItemWidget(this, CategoryType, mCategoryTypeCombobox);
0020 }
0021 
0022 KDEApplicationTreeListWidgetItem::~KDEApplicationTreeListWidgetItem() = default;
0023 
0024 void KDEApplicationTreeListWidgetItem::restoreToDefault()
0025 {
0026     mCategoryTypeCombobox->restoreToDefault();
0027 }
0028 
0029 void KDEApplicationTreeListWidgetItem::setType(LoggingCategory::LoggingType type)
0030 {
0031     mCategoryTypeCombobox->setType(type);
0032 }
0033 
0034 void KDEApplicationTreeListWidgetItem::setDefaultCategory(LoggingCategory::LoggingType type)
0035 {
0036     mCategoryTypeCombobox->setDefaultCategories(type);
0037 }
0038 
0039 QString KDEApplicationTreeListWidgetItem::logName() const
0040 {
0041     return mCategory;
0042 }
0043 
0044 void KDEApplicationTreeListWidgetItem::setLogName(const QString &category)
0045 {
0046     mCategory = category;
0047 }
0048 
0049 LoggingCategory KDEApplicationTreeListWidgetItem::rule(bool forceSavingAllRules) const
0050 {
0051     LoggingCategory cat;
0052     if (mCategoryTypeCombobox->loggingCategoryIsNotDefault() || forceSavingAllRules) {
0053         cat.enabled = false;
0054         cat.categoryName = mCategory;
0055         cat.loggingType = mCategoryTypeCombobox->type();
0056     }
0057     return cat;
0058 }
0059 
0060 KDEApplicationTreeListWidget::KDEApplicationTreeListWidget(QWidget *parent)
0061     : QTreeWidget(parent)
0062 {
0063     setColumnCount(4);
0064     setRootIsDecorated(false);
0065     setUniformRowHeights(true);
0066     setColumnHidden(KDEApplicationTreeListWidgetItem::LogName, true);
0067     setColumnHidden(KDEApplicationTreeListWidgetItem::IdentifierName, true);
0068     header()->hide();
0069 }
0070 
0071 KDEApplicationTreeListWidget::~KDEApplicationTreeListWidget() = default;
0072 
0073 QList<int> KDEApplicationTreeListWidget::searchColumns() const
0074 {
0075     return {KDEApplicationTreeListWidgetItem::Description, KDEApplicationTreeListWidgetItem::LogName, KDEApplicationTreeListWidgetItem::IdentifierName};
0076 }
0077 
0078 void KDEApplicationTreeListWidget::selectAllDebugCategories()
0079 {
0080     for (int i = 0; i < topLevelItemCount(); ++i) {
0081         auto itemWidget = static_cast<KDEApplicationTreeListWidgetItem *>(topLevelItem(i));
0082         if (!itemWidget->isHidden()) {
0083             itemWidget->setType(LoggingCategory::Debug);
0084         }
0085     }
0086 }
0087 
0088 void KDEApplicationTreeListWidget::deSelectAllDebugCategories()
0089 {
0090     for (int i = 0; i < topLevelItemCount(); ++i) {
0091         auto itemWidget = static_cast<KDEApplicationTreeListWidgetItem *>(topLevelItem(i));
0092         if (!itemWidget->isHidden()) {
0093             itemWidget->setType(LoggingCategory::Info);
0094         }
0095     }
0096 }
0097 
0098 void KDEApplicationTreeListWidget::deSelectAllMessagesCategories()
0099 {
0100     for (int i = 0; i < topLevelItemCount(); ++i) {
0101         auto itemWidget = static_cast<KDEApplicationTreeListWidgetItem *>(topLevelItem(i));
0102         if (!itemWidget->isHidden()) {
0103             itemWidget->setType(LoggingCategory::Off);
0104         }
0105     }
0106 }
0107 
0108 void KDEApplicationTreeListWidget::fillList(const LoggingCategory::List &list)
0109 {
0110     addListItems(list, true);
0111 }
0112 
0113 QString KDEApplicationTreeListWidget::generateToolTip(const LoggingCategory &cat)
0114 {
0115     QString toopTip = i18n("Category name: %1", cat.categoryName);
0116     if (!cat.identifierName.isEmpty()) {
0117         toopTip += QLatin1Char('\n') + i18n("Identifier: %1", cat.identifierName);
0118     }
0119     toopTip += QLatin1Char('\n') + i18n("Default Severity: %1", KDebugSettingsUtil::convertCategoryTypeToString(cat.defaultSeverityType));
0120     return toopTip;
0121 }
0122 
0123 void KDEApplicationTreeListWidget::addListItems(const LoggingCategory::List &list, bool clearList)
0124 {
0125     if (clearList) {
0126         clear();
0127     }
0128     for (const LoggingCategory &cat : list) {
0129         auto item = new KDEApplicationTreeListWidgetItem(cat.categoryName, this);
0130         item->setToolTip(0, generateToolTip(cat));
0131         item->setText(KDEApplicationTreeListWidgetItem::Description, cat.description);
0132         item->setType(cat.loggingType);
0133         item->setDefaultCategory(cat.defaultSeverityType);
0134         item->setText(KDEApplicationTreeListWidgetItem::LogName, cat.categoryName);
0135         item->setText(KDEApplicationTreeListWidgetItem::IdentifierName, cat.identifierName);
0136     }
0137     resizeColumnToContents(KDEApplicationTreeListWidgetItem::Description);
0138 }
0139 
0140 void KDEApplicationTreeListWidget::insertCategories(const LoggingCategory::List &list)
0141 {
0142     addListItems(list, false);
0143 }
0144 
0145 LoggingCategory::List KDEApplicationTreeListWidget::rules(bool forceSavingAllRules) const
0146 {
0147     LoggingCategory::List lst;
0148     for (int i = 0; i < topLevelItemCount(); ++i) {
0149         auto listWidgetItem = static_cast<KDEApplicationTreeListWidgetItem *>(topLevelItem(i));
0150         const LoggingCategory cat = listWidgetItem->rule(forceSavingAllRules);
0151         if (cat.isValid()) {
0152             lst.append(cat);
0153         }
0154     }
0155     return lst;
0156 }
0157 
0158 void KDEApplicationTreeListWidget::restoreToDefault()
0159 {
0160     for (int i = 0; i < topLevelItemCount(); ++i) {
0161         auto itemWidget = static_cast<KDEApplicationTreeListWidgetItem *>(topLevelItem(i));
0162         itemWidget->restoreToDefault();
0163     }
0164 }