aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/projectsettingswidget.cpp
blob: b6347f52a70fd91be71809f89b4a00550099d31e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 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 "projectsettingswidget.h"

#include "autotestconstants.h"
#include "autotestplugin.h"
#include "autotesttr.h"
#include "testcodeparser.h"
#include "testprojectsettings.h"
#include "testtreemodel.h"

#include <projectexplorer/projectpanelfactory.h>
#include <projectexplorer/projectsettingswidget.h>

#include <utils/algorithm.h>
#include <utils/aspects.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>

#include <QComboBox>
#include <QPushButton>
#include <QTimer>
#include <QTreeWidget>

using namespace ProjectExplorer;

namespace Autotest::Internal {

enum ItemDataRole  {
    BaseIdRole = Qt::UserRole + 1,
    BaseTypeRole
};

class ProjectTestSettingsWidget : public ProjectSettingsWidget
{
public:
    explicit ProjectTestSettingsWidget(Project *project);

private:
    void populateFrameworks(const QHash<Autotest::ITestFramework *, bool> &frameworks,
                            const QHash<Autotest::ITestTool *, bool> &testTools);
    void populatePathFilters(const QStringList &filters);
    void onActiveFrameworkChanged(QTreeWidgetItem *item, int column);
    TestProjectSettings *m_projectSettings;
    QTreeWidget *m_activeFrameworks = nullptr;
    QComboBox *m_runAfterBuild = nullptr;
    Utils::BoolAspect m_applyFilter;
    QTreeWidget *m_pathFilters = nullptr;
    QTimer m_syncTimer;
    int m_syncType = 0;
};

ProjectTestSettingsWidget::ProjectTestSettingsWidget(Project *project)
    : m_projectSettings(projectSettings(project))
{
    setGlobalSettingsId(Constants::AUTOTEST_SETTINGS_ID);

    QWidget *generalWidget;
    m_activeFrameworks = new QTreeWidget;
    m_activeFrameworks->setHeaderHidden(true);
    m_activeFrameworks->setRootIsDecorated(false);
    m_runAfterBuild = new QComboBox;
    m_runAfterBuild->addItem(Tr::tr("None"));
    m_runAfterBuild->addItem(Tr::tr("All"));
    m_runAfterBuild->addItem(Tr::tr("Selected"));
    m_runAfterBuild->setCurrentIndex(int(m_projectSettings->runAfterBuild()));
    m_applyFilter.setToolTip(Tr::tr("Apply path filters before scanning for tests."));
    m_pathFilters = new QTreeWidget;
    m_pathFilters->setHeaderHidden(true);
    m_pathFilters->setRootIsDecorated(false);
    QLabel *filterLabel = new QLabel(Tr::tr("Wildcard expressions for filtering"), this);
    QPushButton *addFilter = new QPushButton(Tr::tr("Add"), this);
    QPushButton *removeFilter = new QPushButton(Tr::tr("Remove"), this);
    removeFilter->setEnabled(false);

    // clang-format off
    using namespace Layouting;
    Column {
        Widget {
            bindTo(&generalWidget),
            Column {
                Row {
                    Group {
                        title(Tr::tr("Active Test Frameworks")),
                        Column { m_activeFrameworks },
                    },
                    st,
                },
                Row {
                    Tr::tr("Automatically run tests after build"),
                    m_runAfterBuild,
                    st,
                },
                noMargin,
            },
        },
        Row { // explicitly outside of the global settings
            Group {
                title(Tr::tr("Limit Files to Path Patterns")),
                groupChecker(m_applyFilter.groupChecker()),
                Column {
                    filterLabel,
                    Row {
                        Column { m_pathFilters },
                        Column { addFilter, removeFilter, st },
                    },
                },
            },
        },
        noMargin,
    }.attachTo(this);
    // clang-format on

    generalWidget->setDisabled(m_projectSettings->useGlobalSettings());

    populateFrameworks(m_projectSettings->activeFrameworks(),
                       m_projectSettings->activeTestTools());

    populatePathFilters(m_projectSettings->pathFilters());
    setUseGlobalSettings(m_projectSettings->useGlobalSettings());
    m_applyFilter.setValue(m_projectSettings->limitToFilters());
    connect(this, &ProjectSettingsWidget::useGlobalSettingsChanged,
            this, [this, generalWidget](bool useGlobalSettings) {
                generalWidget->setEnabled(!useGlobalSettings);
                m_projectSettings->setUseGlobalSettings(useGlobalSettings);
                m_syncTimer.start(3000);
                m_syncType = ITestBase::Framework | ITestBase::Tool;
            });

    connect(m_activeFrameworks, &QTreeWidget::itemChanged,
            this, &ProjectTestSettingsWidget::onActiveFrameworkChanged);
    connect(m_runAfterBuild, &QComboBox::currentIndexChanged, this, [this](int index) {
        m_projectSettings->setRunAfterBuild(RunAfterBuildMode(index));
    });

    auto itemsToStringList = [this] {
        QStringList items;
        const QTreeWidgetItem *rootItem = m_pathFilters->invisibleRootItem();
        for (int i = 0, count = rootItem->childCount(); i < count; ++i) {
            auto expr = rootItem->child(i)->data(0, Qt::DisplayRole).toString();
            items.append(expr);
        }
        return items;
    };
    auto triggerRescan = [] {
        TestCodeParser *parser = TestTreeModel::instance()->parser();
        parser->emitUpdateTestTree();
    };

    connect(&m_applyFilter, &Utils::BoolAspect::changed,
            this, [this, triggerRescan] {
        m_projectSettings->setLimitToFilter(m_applyFilter.value());
        triggerRescan();
    });
    connect(m_pathFilters, &QTreeWidget::itemSelectionChanged,
            this, [this, removeFilter] {
        removeFilter->setEnabled(!m_pathFilters->selectedItems().isEmpty());
    });
    connect(m_pathFilters->model(), &QAbstractItemModel::dataChanged,
            this, [this, itemsToStringList, triggerRescan]
            (const QModelIndex &tl, const QModelIndex &br, const QList<int> &roles) {
        if (!roles.contains(Qt::DisplayRole))
            return;
        if (tl != br)
            return;
        m_projectSettings->setPathFilters(itemsToStringList());
        triggerRescan();
    });
    connect(addFilter, &QPushButton::clicked, this, [this] {
        m_projectSettings->addPathFilter("*");
        populatePathFilters(m_projectSettings->pathFilters());
        const QTreeWidgetItem *root = m_pathFilters->invisibleRootItem();
        QTreeWidgetItem *lastChild =  root->child(root->childCount() - 1);
        const QModelIndex index = m_pathFilters->indexFromItem(lastChild, 0);
        m_pathFilters->edit(index);
    });
    connect(removeFilter, &QPushButton::clicked, this, [this, itemsToStringList, triggerRescan] {
        const QList<QTreeWidgetItem *> selected = m_pathFilters->selectedItems();
        QTC_ASSERT(selected.size() == 1, return);
        m_pathFilters->invisibleRootItem()->removeChild(selected.first());
        delete selected.first();
        m_projectSettings->setPathFilters(itemsToStringList());
        triggerRescan();
    });
    m_syncTimer.setSingleShot(true);
    connect(&m_syncTimer, &QTimer::timeout, this, [this] {
        auto testTreeModel = TestTreeModel::instance();
        if (m_syncType & ITestBase::Framework)
            testTreeModel->synchronizeTestFrameworks();
        if (m_syncType & ITestBase::Tool)
            testTreeModel->synchronizeTestTools();
        m_syncType = ITestBase::None;
    });
}

void ProjectTestSettingsWidget::populateFrameworks(const QHash<ITestFramework *, bool> &frameworks,
                                                   const QHash<ITestTool *, bool> &testTools)
{
    const TestFrameworks sortedFrameworks = Utils::sorted(frameworks.keys(),
                                                          &ITestFramework::priority);

    auto generateItem = [this](ITestBase *frameworkOrTestTool, bool checked) {
        auto item = new QTreeWidgetItem(m_activeFrameworks, {frameworkOrTestTool->displayName()});
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
        item->setCheckState(0, checked ? Qt::Checked : Qt::Unchecked);
        item->setData(0, BaseIdRole, frameworkOrTestTool->id().toSetting());
        item->setData(0, BaseTypeRole, frameworkOrTestTool->type());
    };

    for (ITestFramework *framework : sortedFrameworks)
        generateItem(framework, frameworks.value(framework));

    // FIXME: testTools aren't sorted and we cannot use priority here
    auto end = testTools.cend();
    for (auto it = testTools.cbegin(); it != end; ++it)
        generateItem(it.key(), it.value());
}

void ProjectTestSettingsWidget::populatePathFilters(const QStringList &filters)
{
    m_pathFilters->clear();
    for (const QString &filter : filters) {
        auto item = new QTreeWidgetItem(m_pathFilters, {filter});
        item->setData(0, Qt::ToolTipRole, filter);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
    }
}

void ProjectTestSettingsWidget::onActiveFrameworkChanged(QTreeWidgetItem *item, int column)
{
    auto id = Utils::Id::fromSetting(item->data(column, BaseIdRole));
    int type = item->data(column, BaseTypeRole).toInt();
    if (type == ITestBase::Framework)
        m_projectSettings->activateFramework(id, item->data(0, Qt::CheckStateRole) == Qt::Checked);
    else if (type == ITestBase::Tool)
        m_projectSettings->activateTestTool(id, item->data(0, Qt::CheckStateRole) == Qt::Checked);
    else
        QTC_ASSERT(! "unexpected test base type", return);
    m_syncTimer.start(3000);
    m_syncType |= type;
}

class AutotestProjectPanelFactory final : public ProjectPanelFactory
{
public:
    AutotestProjectPanelFactory()
    {
        setPriority(666);
        //    setIcon();  // TODO ?
        setDisplayName(Tr::tr("Testing"));
        setCreateWidgetFunction([](Project *project) {
            return new ProjectTestSettingsWidget(project);
        });
    }
};

void setupAutotestProjectPanel()
{
    static AutotestProjectPanelFactory theAutotestProjectPanelFactory;
}

} // Autotest::Internal