aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangtools/filterdialog.cpp
blob: 4c59b44b554223dab3dca051c0343c829ee1af73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "filterdialog.h"

#include "clangtoolstr.h"

#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>
#include <utils/treemodel.h>

#include <QApplication>
#include <QDialog>
#include <QDialogButtonBox>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QPushButton>
#include <QTreeView>

namespace ClangTools::Internal {

enum Columns { CheckName, Count };

class CheckItem : public Utils::TreeItem
{
public:
    CheckItem(const Check &check) : check(check) {}

    QVariant data(int column, int role) const override
    {
        if (role != Qt::DisplayRole)
            return {};
        switch (column) {
        case Columns::CheckName: return check.displayName;
        case Columns::Count: return check.count;
        }
        return {};
    }

    Check check;
};

static QItemSelectionModel::SelectionFlags selectionFlags()
{
    return QItemSelectionModel::Select | QItemSelectionModel::Rows;
}

class FilterChecksModel : public Utils::TreeModel<Utils::TreeItem, CheckItem>
{
    Q_OBJECT

public:
    FilterChecksModel(const Checks &checks)
    {
        const Checks sortedChecks = Utils::sorted(checks, [](const Check &lhs, const Check &rhs) {
            return lhs.displayName < rhs.displayName;
        });

        setHeader({Tr::tr("Check"), "#"});
        setRootItem(new Utils::StaticTreeItem(QString()));
        for (const Check &check : sortedChecks)
            m_root->appendChild(new CheckItem(check));
    }
};

FilterDialog::FilterDialog(const Checks &checks, QWidget *parent)
    : QDialog(parent)
{
    resize(400, 400);
    setWindowTitle(Tr::tr("Filter Diagnostics"));

    auto selectAll = new QPushButton(Tr::tr("Select All"));
    auto selectWithFixits = new QPushButton(Tr::tr("Select All with Fixits"));
    auto selectNone = new QPushButton(Tr::tr("Clear Selection"));

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

    m_model = new FilterChecksModel(checks);

    m_view = new QTreeView(this);
    m_view->setModel(m_model);
    m_view->header()->setStretchLastSection(false);
    m_view->header()->setSectionResizeMode(Columns::CheckName, QHeaderView::Stretch);
    m_view->header()->setSectionResizeMode(Columns::Count, QHeaderView::ResizeToContents);
    m_view->setSelectionMode(QAbstractItemView::MultiSelection);
    m_view->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_view->setIndentation(0);

    using namespace Utils::Layouting;

    Column {
        Tr::tr("Select the diagnostics to display."),
        Row { selectAll, selectWithFixits, selectNone, st },
        m_view,
        buttonBox,
    }.attachTo(this);

    connect(m_view->selectionModel(), &QItemSelectionModel::selectionChanged, this, [=] {
        const bool hasSelection = !m_view->selectionModel()->selectedRows().isEmpty();
        buttonBox->button(QDialogButtonBox::Ok)->setEnabled(hasSelection);
    });

    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    // Buttons
    connect(selectNone, &QPushButton::clicked, m_view, &QTreeView::clearSelection);
    connect(selectAll, &QPushButton::clicked, m_view, &QTreeView::selectAll);
    connect(selectWithFixits, &QPushButton::clicked, m_view, [this] {
        m_view->clearSelection();
        m_model->forItemsAtLevel<1>([&](CheckItem *item) {
            if (item->check.hasFixit)
                m_view->selectionModel()->select(item->index(), selectionFlags());
        });
    });
    selectWithFixits->setEnabled(
        Utils::anyOf(checks, [](const Check &c) { return c.hasFixit; }));

    // Select checks that are not filtered out
    m_model->forItemsAtLevel<1>([this](CheckItem *item) {
        if (item->check.isShown)
            m_view->selectionModel()->select(item->index(), selectionFlags());
    });
}

FilterDialog::~FilterDialog() = default;

QSet<QString> FilterDialog::selectedChecks() const
{
    QSet<QString> checks;
    m_model->forItemsAtLevel<1>([&](CheckItem *item) {
        if (m_view->selectionModel()->isSelected(item->index()))
            checks << item->check.name;
    });
    return checks;
}

} // ClangTools::Internal

#include "filterdialog.moc"