summaryrefslogtreecommitdiff
path: root/src/plugins/squish/squishnavigationwidget.cpp
blob: 1b99c338f16a663343a759031daebd41c5f1e229 (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
// 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 "squishplugin.h"
#include "squishsettings.h"
#include "squishtesttreemodel.h"
#include "squishtesttreeview.h"
#include "squishtr.h"

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

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

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

namespace Squish {
namespace Internal {

const int defaultSectionSize = 17;

SquishNavigationWidget::SquishNavigationWidget(QWidget *parent)
    : QWidget(parent)
{
    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);
    });
}

SquishNavigationWidget::~SquishNavigationWidget() {}

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);
                break;
            }
            case SquishTestTreeItem::SquishSharedFolder: {
                QAction *addSharedFile = new QAction(Tr::tr("Add Shared File"), &menu);
                menu.addAction(addSharedFile);
                // 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::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 = Utils::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),
        Core::ICore::settings(),
        "RecordWithoutApproval");
    if (pressed != QMessageBox::Yes)
        return;

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

void SquishNavigationWidget::onNewTestCaseTriggered(const QModelIndex &index)
{
    auto settings = SquishPlugin::squishSettings();
    QTC_ASSERT(settings, return);

    if (!settings->squishPath.filePath().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));
}

SquishNavigationWidgetFactory::SquishNavigationWidgetFactory()
{
    setDisplayName(Tr::tr("Squish"));
    setId(Squish::Constants::SQUISH_ID);
    setPriority(777);
}

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

} // namespace Internal
} // namespace Squish