File indexing completed on 2024-05-12 04:39:21

0001 /*
0002     SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "checkselection.h"
0008 
0009 // plugin
0010 #include "checklistfilterproxysearchline.h"
0011 #include "checklistitemproxystyle.h"
0012 #include "checklistmodel.h"
0013 #include <checkset.h>
0014 #include <debug.h>
0015 // Qt
0016 #include <QEvent>
0017 #include <QVBoxLayout>
0018 #include <QTreeView>
0019 #include <QHeaderView>
0020 #include <QSortFilterProxyModel>
0021 
0022 namespace ClangTidy
0023 {
0024 
0025 CheckSelection::CheckSelection(QWidget* parent)
0026     : QWidget(parent)
0027     , m_checkListModel(new CheckListModel(this))
0028 {
0029     auto* layout = new QVBoxLayout;
0030     layout->setContentsMargins(0, 0, 0, 0);
0031 
0032     auto* checkFilterEdit = new CheckListFilterProxySearchLine(this);
0033     layout->addWidget(checkFilterEdit);
0034 
0035     m_checkListView = new QTreeView(this);
0036     m_checkListView->setAllColumnsShowFocus(true);
0037     m_checkListView->setRootIsDecorated(true);
0038     m_checkListView->setHeaderHidden(true);
0039     m_checkListView->setUniformRowHeights(true);
0040     m_proxyStyle = new CheckListItemProxyStyle;
0041     m_proxyStyle->setParent(this);
0042     m_checkListView->setStyle(m_proxyStyle);
0043     layout->addWidget(m_checkListView);
0044 
0045     setLayout(layout);
0046 
0047     m_checksFilterProxyModel = new QSortFilterProxyModel(this);
0048     m_checksFilterProxyModel->setRecursiveFilteringEnabled(true);
0049     checkFilterEdit->setFilterProxyModel(m_checksFilterProxyModel);
0050     m_checksFilterProxyModel->setSourceModel(m_checkListModel);
0051     m_checksFilterProxyModel->setFilterKeyColumn(CheckListModel::NameColumnId);
0052     m_checksFilterProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
0053 
0054     m_checkListView->setModel(m_checksFilterProxyModel);
0055     auto* header = m_checkListView->header();
0056     header->setStretchLastSection(false);
0057     header->setSectionResizeMode(CheckListModel::NameColumnId, QHeaderView::Stretch);
0058     header->setSectionResizeMode(CheckListModel::CountColumnId, QHeaderView::ResizeToContents);
0059 
0060     connect(m_checkListModel, &CheckListModel::enabledChecksChanged,
0061             this, &CheckSelection::onEnabledChecksChanged);
0062 }
0063 
0064 CheckSelection::~CheckSelection() = default;
0065 
0066 void CheckSelection::setEditable(bool editable)
0067 {
0068     m_checkListModel->setEditable(editable);
0069 }
0070 
0071 void CheckSelection::setCheckSet(const CheckSet* checkSet)
0072 {
0073     m_checkListModel->setCheckSet(checkSet);
0074     expandSubGroupsWithExplicitlyEnabledStates();
0075 }
0076 
0077 void CheckSelection::expandSubGroupsWithExplicitlyEnabledStates()
0078 {
0079     const QModelIndex allChecksIndex = m_checksFilterProxyModel->index(0, 0, QModelIndex());
0080     expandSubGroupsWithExplicitlyEnabledStates(allChecksIndex);
0081 }
0082 
0083 void CheckSelection::expandSubGroupsWithExplicitlyEnabledStates(const QModelIndex& groupIndex)
0084 {
0085     if (groupIndex.data(CheckListModel::HasExplicitEnabledStateRole).toBool()) {
0086         m_checkListView->setExpanded(groupIndex, true);
0087         const int rowCount = m_checksFilterProxyModel->rowCount(groupIndex);
0088         for (int c = 0; c < rowCount; ++c) {
0089             const auto childIndex = m_checksFilterProxyModel->index(c, 0, groupIndex);
0090             if (m_checksFilterProxyModel->hasChildren(childIndex)) {
0091                 expandSubGroupsWithExplicitlyEnabledStates(childIndex);
0092             }
0093         }
0094     }
0095 }
0096 
0097 void CheckSelection::setChecks(const QString& checks)
0098 {
0099     m_checkListModel->setEnabledChecks(checks.split(QLatin1Char(','), Qt::SkipEmptyParts));
0100     expandSubGroupsWithExplicitlyEnabledStates();
0101 }
0102 
0103 QString CheckSelection::checks() const
0104 {
0105     return m_checkListModel->enabledChecks().join(QLatin1Char(','));
0106 }
0107 
0108 
0109 bool CheckSelection::event(QEvent* event)
0110 {
0111     if (event->type() == QEvent::StyleChange) {
0112         // no recursion protection needed as the style is set on the subchild only
0113         m_checkListView->setStyle(nullptr);
0114         delete m_proxyStyle;
0115         m_proxyStyle = new CheckListItemProxyStyle;
0116         m_proxyStyle->setParent(this);
0117         m_checkListView->setStyle(m_proxyStyle);
0118     }
0119 
0120     return QWidget::event(event);
0121 }
0122 
0123 void CheckSelection::onEnabledChecksChanged()
0124 {
0125     emit checksChanged(checks());
0126 }
0127 
0128 }
0129 
0130 #include "moc_checkselection.cpp"