aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/waitforstopdialog.cpp
blob: d1e304a3a1429a53abde67da94bde33e03a510b8 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "waitforstopdialog.h"

#include "projectexplorertr.h"
#include "runcontrol.h"

#include <utils/algorithm.h>

#include <QVBoxLayout>
#include <QTimer>
#include <QLabel>
#include <QPushButton>

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

WaitForStopDialog::WaitForStopDialog(const QList<ProjectExplorer::RunControl *> &runControls)
    : m_runControls(runControls)
{
    setWindowTitle(Tr::tr("Waiting for Applications to Stop"));

    auto layout = new QVBoxLayout();
    setLayout(layout);

    m_progressLabel = new QLabel;
    layout->addWidget(m_progressLabel);

    auto cancelButton = new QPushButton(Tr::tr("Cancel"));
    connect(cancelButton, &QPushButton::clicked,
            this, &QDialog::close);
    layout->addWidget(cancelButton);

    updateProgressText();

    for (const RunControl *rc : runControls)
        connect(rc, &RunControl::stopped, this, [this, rc] { runControlFinished(rc); });

    m_timer.start();
}

bool WaitForStopDialog::canceled()
{
    return !m_runControls.isEmpty();
}

void WaitForStopDialog::updateProgressText()
{
    QString text = Tr::tr("Waiting for applications to stop.") + QLatin1String("\n\n");
    QStringList names = Utils::transform(m_runControls, &RunControl::displayName);
    text += names.join(QLatin1Char('\n'));
    m_progressLabel->setText(text);
}

void WaitForStopDialog::runControlFinished(const RunControl *runControl)
{
    m_runControls.removeOne(runControl);
    if (m_runControls.isEmpty()) {
        if (m_timer.elapsed() < 1000)
            QTimer::singleShot(1000 - m_timer.elapsed(), this, &QDialog::close);
        else
            QDialog::close();
    } else {
        updateProgressText();
    }
}