File indexing completed on 2024-05-12 05:52:36
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 0006 */ 0007 0008 #include "customdebuglistview.h" 0009 #include "configurecustomsettingdialog.h" 0010 #include "kdebugsettings_debug.h" 0011 #include "kdebugsettingsutil.h" 0012 #include "model/customloggingcategorymodel.h" 0013 #include "model/customloggingcategoryproxymodel.h" 0014 0015 #include <KLocalizedString> 0016 #include <KMessageBox> 0017 0018 #include <QAction> 0019 #include <QMenu> 0020 #include <QPointer> 0021 0022 CustomDebugListView::CustomDebugListView(QWidget *parent) 0023 : QListView(parent) 0024 , mCustomLoggingCategoryProxyModel(new CustomLoggingCategoryProxyModel(this)) 0025 { 0026 mCustomLoggingCategoryProxyModel->setObjectName(QLatin1StringView("mCustomDebugProxyModel")); 0027 setContextMenuPolicy(Qt::CustomContextMenu); 0028 setSelectionMode(QAbstractItemView::ExtendedSelection); 0029 connect(this, &CustomDebugListView::customContextMenuRequested, this, &CustomDebugListView::slotCustomContextMenuRequested); 0030 connect(this, &CustomDebugListView::doubleClicked, this, &CustomDebugListView::slotEditRule); 0031 } 0032 0033 CustomDebugListView::~CustomDebugListView() = default; 0034 0035 void CustomDebugListView::slotCustomContextMenuRequested(const QPoint &pos) 0036 { 0037 const QModelIndex idx = indexAt(pos); 0038 const QModelIndex index = mCustomLoggingCategoryProxyModel->mapToSource(idx); 0039 QMenu menu(this); 0040 const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); 0041 const auto selectedItemCount{selectedIndexes.count()}; 0042 menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add Rule..."), this, &CustomDebugListView::slotAddRule); 0043 if (index.isValid() && selectedItemCount == 1) { 0044 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Edit Rule"), this, [this, index]() { 0045 slotEditRule(index); 0046 }); 0047 } 0048 if (selectedItemCount > 0) { 0049 menu.addSeparator(); 0050 menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18np("Remove Rule", "Remove Rules", selectedItemCount), this, [this, selectedIndexes]() { 0051 slotRemoveRules(selectedIndexes); 0052 }); 0053 } 0054 menu.exec(viewport()->mapToGlobal(pos)); 0055 } 0056 0057 CustomLoggingCategoryModel *CustomDebugListView::loggingCategoryModel() const 0058 { 0059 return mCustomLoggingCategoryModel; 0060 } 0061 0062 void CustomDebugListView::setLoggingCategoryModel(CustomLoggingCategoryModel *newLoggingCategoryModel) 0063 { 0064 if (mCustomLoggingCategoryModel) { 0065 qCWarning(KDEBUGSETTINGS_LOG) << " There is a problem as there already has a model"; 0066 } 0067 mCustomLoggingCategoryModel = newLoggingCategoryModel; 0068 mCustomLoggingCategoryModel->setObjectName(QLatin1StringView("mLoggingCategoryModel")); 0069 0070 mCustomLoggingCategoryProxyModel->setSourceModel(mCustomLoggingCategoryModel); 0071 setModel(mCustomLoggingCategoryProxyModel); 0072 connect(mCustomLoggingCategoryModel, &QAbstractListModel::rowsRemoved, this, &CustomDebugListView::updateButtonsRequested); 0073 connect(mCustomLoggingCategoryModel, &QAbstractListModel::rowsInserted, this, &CustomDebugListView::updateButtonsRequested); 0074 connect(mCustomLoggingCategoryModel, &QAbstractListModel::modelReset, this, &CustomDebugListView::updateButtonsRequested); 0075 } 0076 0077 void CustomDebugListView::slotRemoveRules(const QModelIndexList &selectedIndexes) 0078 { 0079 if (selectedIndexes.isEmpty()) { 0080 return; 0081 } 0082 const auto numberOfRules{selectedIndexes.count()}; 0083 const QString str = i18np("Do you want to remove this rule?", "Do you want to remove these %1 rules?", numberOfRules); 0084 0085 if (KMessageBox::ButtonCode::SecondaryAction 0086 == KMessageBox::warningTwoActions(this, 0087 str, 0088 i18np("Remove Rule", "Remove Rules", numberOfRules), 0089 KStandardGuiItem::remove(), 0090 KStandardGuiItem::cancel())) { 0091 return; 0092 } 0093 LoggingCategory::List categories; 0094 for (const auto &index : selectedIndexes) { 0095 const auto cat = mCustomLoggingCategoryModel->index(index.row()).data(CustomLoggingCategoryModel::CategoryRole).value<LoggingCategory>(); 0096 categories.append(cat); 0097 } 0098 mCustomLoggingCategoryModel->removeCategory(categories); 0099 } 0100 0101 void CustomDebugListView::slotEditRule(const QModelIndex &index) 0102 { 0103 if (!index.isValid()) { 0104 return; 0105 } 0106 const QString rule = mCustomLoggingCategoryModel->index(index.row()).data(CustomLoggingCategoryModel::DisplayRuleRole).toString(); 0107 QPointer<ConfigureCustomSettingDialog> dlg = new ConfigureCustomSettingDialog(this); 0108 dlg->setRule(rule); 0109 if (dlg->exec()) { 0110 const QString ruleStr = dlg->rule(); 0111 if (!ruleStr.isEmpty()) { 0112 const LoggingCategory cat = KDebugSettingsUtil::convertRuleStrToLoggingCategory(ruleStr); 0113 0114 mCustomLoggingCategoryModel->setData(index, QVariant::fromValue(cat), CustomLoggingCategoryModel::CategoryRole); 0115 } 0116 } 0117 delete dlg; 0118 } 0119 0120 void CustomDebugListView::slotAddRule() 0121 { 0122 QPointer<ConfigureCustomSettingDialog> dlg = new ConfigureCustomSettingDialog(this); 0123 if (dlg->exec()) { 0124 const QString ruleStr = dlg->rule(); 0125 const LoggingCategory cat = KDebugSettingsUtil::convertRuleStrToLoggingCategory(ruleStr); 0126 if (mCustomLoggingCategoryModel->addCategory(cat)) { 0127 qCDebug(KDEBUGSETTINGS_LOG) << " categorie already exist"; 0128 } 0129 } 0130 0131 delete dlg; 0132 } 0133 0134 void CustomDebugListView::removeRules() 0135 { 0136 const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); 0137 slotRemoveRules(selectedIndexes); 0138 } 0139 0140 void CustomDebugListView::editRule() 0141 { 0142 slotEditRule(selectionModel()->currentIndex()); 0143 } 0144 0145 void CustomDebugListView::setFilterRuleStr(const QString &str) 0146 { 0147 mCustomLoggingCategoryProxyModel->setFilterText(str); 0148 } 0149 0150 #include "moc_customdebuglistview.cpp"