aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/squish/squishnavigationwidget.cpp
blob: 7921938c37a758e88dbe332ebb01da6d111f0343 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (C) 2022 The Qt Company Ltd
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "squishnavigationwidget.h"

#include "squishconstants.h"
#include "squishfilehandler.h"
#include "squishmessages.h"
#include "squishsettings.h"
#include "squishtesttreemodel.h"
#include "squishtesttreeview.h"
#include "squishtr.h"
#include "suiteconf.h"

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/find/itemviewfind.h>
#include <coreplugin/icore.h>
#include <coreplugin/inavigationwidgetfactory.h>

#include <utils/algorithm.h>
#include <utils/checkablemessagebox.h>
#include <utils/qtcassert.h>

#include <QHeaderView>
#include <QMenu>
#include <QVBoxLayout>

using namespace Utils;

namespace Squish::Internal {

const int defaultSectionSize = 17;

class SquishNavigationWidget final : public QWidget
{
public:
    SquishNavigationWidget();

    void contextMenuEvent(QContextMenuEvent *event) final;
    static QList<QToolButton *> createToolButtons();

private:
    void onItemActivated(const QModelIndex &idx);
    void onExpanded(const QModelIndex &idx);
    void onCollapsed(const QModelIndex &idx);
    void onRowsInserted(const QModelIndex &parent, int, int);
    void onRowsRemoved(const QModelIndex &parent, int, int);
    void onAddSharedFileTriggered(const QModelIndex &idx);
    void onRemoveSharedFileTriggered(const QModelIndex &idx);
    void onRemoveSharedFolderTriggered(int row, const QModelIndex &parent);
    void onRemoveAllSharedFolderTriggered();
    void onRecordTestCase(const QString &suiteName, const QString &testCase);
    void onNewTestCaseTriggered(const QModelIndex &index);

    SquishTestTreeView *m_view;
    SquishTestTreeModel *m_model; // not owned
    SquishTestTreeSortModel *m_sortModel;
};

SquishNavigationWidget::SquishNavigationWidget()
{
    setWindowTitle(Tr::tr("Squish"));
    m_view = new SquishTestTreeView(this);
    m_model = SquishTestTreeModel::instance();
    m_sortModel = new SquishTestTreeSortModel(m_model, m_model);
    m_sortModel->setDynamicSortFilter(true);
    m_view->setModel(m_sortModel);
    m_view->setSortingEnabled(true);
    m_view->setItemDelegate(new SquishTestTreeItemDelegate(this));
    QHeaderView *header = new QHeaderView(Qt::Horizontal, m_view);
    header->setModel(m_model);
    header->setStretchLastSection(false);
    header->setMinimumSectionSize(16);
    header->setDefaultSectionSize(16);
    header->setSectionResizeMode(0, QHeaderView::Stretch);
    header->setSectionResizeMode(1, QHeaderView::Fixed);
    header->setSectionResizeMode(2, QHeaderView::Fixed);
    m_view->setHeader(header);
    m_view->setHeaderHidden(true);
    m_view->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addWidget(Core::ItemViewFind::createSearchableWrapper(m_view));
    setLayout(layout);

    connect(m_view, &QTreeView::expanded, this, &SquishNavigationWidget::onExpanded);
    connect(m_view, &QTreeView::collapsed, this, &SquishNavigationWidget::onCollapsed);
    connect(m_view, &QTreeView::activated, this, &SquishNavigationWidget::onItemActivated);
    connect(m_model, &QAbstractItemModel::rowsInserted,
            this, &SquishNavigationWidget::onRowsInserted);
    connect(m_model, &QAbstractItemModel::rowsRemoved, this, &SquishNavigationWidget::onRowsRemoved);
    connect(m_view, &SquishTestTreeView::runTestCase,
            SquishFileHandler::instance(), &SquishFileHandler::runTestCase);
    connect(m_view, &SquishTestTreeView::recordTestCase,
            this, &SquishNavigationWidget::onRecordTestCase);
    connect(m_view, &SquishTestTreeView::runTestSuite,
            SquishFileHandler::instance(), &SquishFileHandler::runTestSuite);
    connect(m_view, &SquishTestTreeView::openObjectsMap,
            SquishFileHandler::instance(), &SquishFileHandler::openObjectsMap);
    connect(SquishFileHandler::instance(), &SquishFileHandler::suitesOpened, this, [this] {
        const QModelIndex &suitesIndex = m_view->model()->index(1, 0);
        if (m_view->isExpanded(suitesIndex))
            onExpanded(suitesIndex);
    });
}

void SquishNavigationWidget::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu;

    // item specific menu entries
    const QModelIndexList list = m_view->selectionModel()->selectedIndexes();
    if (list.size() == SquishTestTreeModel::COLUMN_COUNT) {
        QRect rect(m_view->visualRect(list.first()));
        if (rect.contains(event->pos())) {
            const QModelIndex &idx = list.first();
            const int type = idx.data(TypeRole).toInt();
            switch (type) {
            case SquishTestTreeItem::SquishTestCase: {
                const QString caseName = idx.data(DisplayNameRole).toString();
                const QString suiteName = idx.parent().data(DisplayNameRole).toString();
                QAction *runThisTestCase = new QAction(Tr::tr("Run This Test Case"), &menu);
                menu.addAction(runThisTestCase);
                QAction *deleteTestCase = new QAction(Tr::tr("Delete Test Case"), &menu);
                menu.addAction(deleteTestCase);
                menu.addSeparator();

                connect(runThisTestCase, &QAction::triggered, [suiteName, caseName] {
                    SquishFileHandler::instance()->runTestCase(suiteName, caseName);
                });
                connect(deleteTestCase, &QAction::triggered, [suiteName, caseName] {
                    SquishFileHandler::instance()->deleteTestCase(suiteName, caseName);
                });
                break;
            }
            case SquishTestTreeItem::SquishSuite: {
                const QString suiteName = idx.data(DisplayNameRole).toString();
                QAction *runThisTestSuite = new QAction(Tr::tr("Run This Test Suite"), &menu);
                menu.addAction(runThisTestSuite);
                menu.addSeparator();
                QAction *addNewTestCase = new QAction(Tr::tr("Add New Test Case..."), &menu);
                menu.addAction(addNewTestCase);
                QAction *closeTestSuite = new QAction(Tr::tr("Close Test Suite"), &menu);
                menu.addAction(closeTestSuite);
                menu.addSeparator();

                connect(runThisTestSuite, &QAction::triggered, [suiteName] {
                    SquishFileHandler::instance()->runTestSuite(suiteName);
                });
                connect(addNewTestCase, &QAction::triggered, this, [this, idx] {
                    onNewTestCaseTriggered(idx);
                });

                connect(closeTestSuite, &QAction::triggered, [suiteName] {
                    SquishFileHandler::instance()->closeTestSuite(suiteName);
                });
                break;
            }
            case SquishTestTreeItem::SquishSharedFile: {
                QAction *deleteSharedFile = new QAction(Tr::tr("Delete Shared File"), &menu);
                menu.addAction(deleteSharedFile);
                connect(deleteSharedFile, &QAction::triggered, this, [this, idx] {
                    onRemoveSharedFileTriggered(idx);
                });
                break;
            }
            case SquishTestTreeItem::SquishSharedFolder: {
                QAction *addSharedFile = new QAction(Tr::tr("Add Shared File"), &menu);
                menu.addAction(addSharedFile);
                connect(addSharedFile, &QAction::triggered, this, [this, idx] {
                    onAddSharedFileTriggered(idx);
                });
                // only add the action 'Remove Shared Folder' for top-level shared folders, not
                // to their recursively added sub-folders
                if (idx.parent().data(TypeRole).toInt() == SquishTestTreeItem::Root) {
                    QAction *removeSharedFolder = new QAction(Tr::tr("Remove Shared Folder"), &menu);
                    menu.addAction(removeSharedFolder);
                    menu.addSeparator();
                    connect(removeSharedFolder, &QAction::triggered, this, [this, idx] {
                        onRemoveSharedFolderTriggered(idx.row(), idx.parent());
                    });
                }
                break;
            }
            default:
                break;
            }
        }
    }
    const QModelIndex &foldersIndex = m_view->model()->index(0, 0);
    const QModelIndex &suitesIndex = m_view->model()->index(1, 0);

    // general squish related menu entries
    QAction *openSquishSuites = new QAction(Tr::tr("Open Squish Suites..."), &menu);
    menu.addAction(openSquishSuites);
    QAction *createNewTestSuite = new QAction(Tr::tr("Create New Test Suite..."), &menu);
    menu.addAction(createNewTestSuite);

    connect(createNewTestSuite, &QAction::triggered, this, [] {
        auto command = Core::ActionManager::command(Utils::Id("Wizard.Impl.S.SquishTestSuite"));
        if (command && command->action())
            command->action()->trigger();
        else
            qWarning("Failed to get wizard command. UI changed?");
    });
    connect(openSquishSuites,
            &QAction::triggered,
            SquishFileHandler::instance(),
            &SquishFileHandler::openTestSuites);

    if (m_view->model()->rowCount(suitesIndex) > 0) {
        menu.addSeparator();
        QAction *closeAllSuites = new QAction(Tr::tr("Close All Test Suites"), &menu);
        menu.addAction(closeAllSuites);

        connect(closeAllSuites, &QAction::triggered, this, [] {
            if (SquishMessages::simpleQuestion(Tr::tr("Close All Test Suites"),
                                               Tr::tr("Close all test suites?"
                                                      /*"\nThis will close all related files as well."*/))
                == QMessageBox::Yes)
                SquishFileHandler::instance()->closeAllTestSuites();
        });
    }

    menu.addSeparator();
    QAction *addSharedFolder = new QAction(Tr::tr("Add Shared Folder..."), &menu);
    menu.addAction(addSharedFolder);

    connect(addSharedFolder,
            &QAction::triggered,
            SquishFileHandler::instance(),
            &SquishFileHandler::addSharedFolder);

    if (m_view->model()->rowCount(foldersIndex) > 0) {
        menu.addSeparator();
        QAction *removeAllFolders = new QAction(Tr::tr("Remove All Shared Folders"), &menu);
        menu.addAction(removeAllFolders);

        connect(removeAllFolders,
                &QAction::triggered,
                this,
                &SquishNavigationWidget::onRemoveAllSharedFolderTriggered);
    }

    menu.exec(mapToGlobal(event->pos()));
}

QList<QToolButton *> SquishNavigationWidget::createToolButtons()
{
    QList<QToolButton *> toolButtons;
    return toolButtons;
}

void SquishNavigationWidget::onItemActivated(const QModelIndex &idx)
{
    if (!idx.isValid())
        return;

    SquishTestTreeItem *item = static_cast<SquishTestTreeItem *>(m_sortModel->itemFromIndex(idx));
    switch (item->type()) {
    case SquishTestTreeItem::SquishSharedDataFolder:
    case SquishTestTreeItem::SquishSharedFolder:
    case SquishTestTreeItem::SquishSharedRoot:
        return;
    default:
        break;
    }

    if (item->filePath().exists())
        Core::EditorManager::openEditor(item->filePath());
}

void SquishNavigationWidget::onExpanded(const QModelIndex &idx)
{
    if (idx.data().toString().startsWith(Tr::tr("Test Suites")))
        m_view->header()->setDefaultSectionSize(defaultSectionSize);
}

void SquishNavigationWidget::onCollapsed(const QModelIndex &idx)
{
    if (idx.data().toString().startsWith(Tr::tr("Test Suites")))
        m_view->header()->setDefaultSectionSize(0);
}

void SquishNavigationWidget::onRowsInserted(const QModelIndex &parent, int, int)
{
    if (parent.isValid() && parent.data().toString().startsWith(Tr::tr("Test Suites")))
        if (m_view->isExpanded(parent) && m_model->rowCount(parent))
            m_view->header()->setDefaultSectionSize(defaultSectionSize);
}

void SquishNavigationWidget::onRowsRemoved(const QModelIndex &parent, int, int)
{
    if (parent.isValid() && parent.data().toString().startsWith(Tr::tr("Test Suites")))
        if (m_model->rowCount(parent) == 0)
            m_view->header()->setDefaultSectionSize(0);
}

void SquishNavigationWidget::onAddSharedFileTriggered(const QModelIndex &idx)
{
    const auto folder = FilePath::fromVariant(idx.data(LinkRole));
    QTC_ASSERT(!folder.isEmpty(), return);

    const SquishTestTreeItem *anySuiteItem = m_model->findNonRootItem(
        [](SquishTestTreeItem *it) { return it->type() == SquishTestTreeItem::SquishSuite; });
    QString extension(".js");
    if (anySuiteItem) {
        const SuiteConf conf = SuiteConf::readSuiteConf(anySuiteItem->filePath());
        extension = conf.scriptExtension();
    }

    const QString tmpl("script");
    FilePath scriptFile = folder.pathAppended(tmpl + extension);
    int i = 1;
    while (scriptFile.exists())
        scriptFile = folder.pathAppended(tmpl + QString::number(++i) + extension);
    SquishTestTreeItem *item = new SquishTestTreeItem(scriptFile.fileName(),
                                                      SquishTestTreeItem::SquishSharedFile);
    item->setFilePath(scriptFile);
    item->setParentName(idx.data().toString());
    m_model->addTreeItem(item);

    m_view->expand(idx);
    QModelIndex added = m_model->indexForItem(item);
    QTC_ASSERT(added.isValid(), return);
    m_view->edit(m_sortModel->mapFromSource(added));
}

void SquishNavigationWidget::onRemoveSharedFileTriggered(const QModelIndex &idx)
{
    const auto scriptFile = FilePath::fromVariant(idx.data(LinkRole));
    QTC_ASSERT(!scriptFile.isEmpty(), return);

    const QString detail = Tr::tr("Do you really want to delete \"%1\" permanently?")
            .arg(scriptFile.toUserOutput());
    const QMessageBox::StandardButton pressed
        = CheckableMessageBox::question(Core::ICore::dialogParent(),
                                        Tr::tr("Remove Shared File"),
                                        detail,
                                        Key("RemoveSharedSquishScript"));
    if (pressed != QMessageBox::Yes)
        return;

    const QModelIndex &realIdx = m_sortModel->mapToSource(idx);
    // close document silently if open
    if (Core::IDocument *doc = Core::DocumentModel::documentForFilePath(scriptFile))
        Core::EditorManager::closeDocuments({doc}, false);
    if (scriptFile.removeFile())
        m_model->removeTreeItem(realIdx.row(), realIdx.parent());
    else
        SquishMessages::criticalMessage(Tr::tr("Failed to remove \"%1\"."));
}

void SquishNavigationWidget::onRemoveSharedFolderTriggered(int row, const QModelIndex &parent)
{
    const auto folder = Utils::FilePath::fromVariant(m_sortModel->index(row, 0, parent).data(LinkRole));
    QTC_ASSERT(!folder.isEmpty(), return );

    const QString detail = Tr::tr("Remove \"%1\" from the list of shared folders?")
            .arg(folder.toUserOutput());
    if (SquishMessages::simpleQuestion(Tr::tr("Remove Shared Folder"), detail) != QMessageBox::Yes)
        return;

    const QModelIndex &realIdx = m_sortModel->mapToSource(m_sortModel->index(row, 0, parent));
    if (SquishFileHandler::instance()->removeSharedFolder(folder))
        m_model->removeTreeItem(realIdx.row(), realIdx.parent());
}

void SquishNavigationWidget::onRemoveAllSharedFolderTriggered()
{
    if (SquishMessages::simpleQuestion(Tr::tr("Remove All Shared Folders"),
                                       Tr::tr("Remove all shared folders?")) != QMessageBox::Yes) {
        return;
    }

    SquishFileHandler::instance()->removeAllSharedFolders();
    m_model->removeAllSharedFolders();
}

void SquishNavigationWidget::onRecordTestCase(const QString &suiteName, const QString &testCase)
{
    QMessageBox::StandardButton pressed = CheckableMessageBox::question(
        Core::ICore::dialogParent(),
        Tr::tr("Record Test Case"),
        Tr::tr("Do you want to record over the test case \"%1\"? The existing content will "
               "be overwritten by the recorded script.")
            .arg(testCase),
        Key("RecordWithoutApproval"));
    if (pressed != QMessageBox::Yes)
        return;

    SquishFileHandler::instance()->recordTestCase(suiteName, testCase);
}

void SquishNavigationWidget::onNewTestCaseTriggered(const QModelIndex &index)
{
    if (!settings().squishPath().pathAppended("scriptmodules").exists()) {
        SquishMessages::criticalMessage(Tr::tr("Set up a valid Squish path to be able to create "
                                               "a new test case.\n(Edit > Preferences > Squish)"));
        return;
    }

    SquishTestTreeItem *suiteItem = m_model->itemForIndex(m_sortModel->mapToSource(index));
    QTC_ASSERT(suiteItem, return);

    const QString name = suiteItem->generateTestCaseName();
    SquishTestTreeItem *item = new SquishTestTreeItem(name, SquishTestTreeItem::SquishTestCase);
    item->setParentName(suiteItem->displayName());

    m_model->addTreeItem(item);
    m_view->expand(index);
    QModelIndex added = m_model->indexForItem(item);
    QTC_ASSERT(added.isValid(), return);
    m_view->edit(m_sortModel->mapFromSource(added));
}

class SquishNavigationWidgetFactory final : public Core::INavigationWidgetFactory
{
public:
    SquishNavigationWidgetFactory()
    {
        setDisplayName(Tr::tr("Squish"));
        setId(Squish::Constants::SQUISH_ID);
        setPriority(777);
    }

    Core::NavigationView createWidget() final
    {
        SquishNavigationWidget *squishNavigationWidget = new SquishNavigationWidget;
        Core::NavigationView view;
        view.widget = squishNavigationWidget;
        view.dockToolBarWidgets = squishNavigationWidget->createToolButtons();
        return view;
    }
};

void setupSquishNavigationWidgetFactory()
{
    static SquishNavigationWidgetFactory squishNavigationWidgetFactory;
}

} // Squish::Internal