shellcommandpage.cpp 4.86 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30

31
32
33
34
35
#include "shellcommandpage.h"
#include "shellcommand.h"
#include "outputformatter.h"
#include "qtcassert.h"
#include "theme/theme.h"
36

37
#include <QAbstractButton>
38
#include <QApplication>
39
40
41
#include <QLabel>
#include <QPlainTextEdit>
#include <QVBoxLayout>
42

43
/*!
44
    \class Utils::ShellCommandPage
45

46
47
    \brief The ShellCommandPage implements a page showing the
    progress of a \c ShellCommand.
48

49
    Turns complete when the command succeeds.
50
51
*/

52
namespace Utils {
53

54
55
56
ShellCommandPage::ShellCommandPage(QWidget *parent) :
    WizardPage(parent),
    m_startedStatus(tr("Command started...")),
57
    m_overwriteOutput(false),
58
59
    m_state(Idle)
{
60
    resize(264, 200);
Tobias Hunger's avatar
Tobias Hunger committed
61
    auto verticalLayout = new QVBoxLayout(this);
62
    m_logPlainTextEdit = new QPlainTextEdit;
63
    m_formatter = new Utils::OutputFormatter;
64
    m_logPlainTextEdit->setReadOnly(true);
65
    m_formatter->setPlainTextEdit(m_logPlainTextEdit);
66
67
68
69
70

    verticalLayout->addWidget(m_logPlainTextEdit);

    m_statusLabel = new QLabel;
    verticalLayout->addWidget(m_statusLabel);
71
    setTitle(tr("Run Command"));
72
73
}

74
ShellCommandPage::~ShellCommandPage()
75
{
76
    QTC_ASSERT(m_state != Running, QApplication::restoreOverrideCursor());
77
    delete m_formatter;
78
79
}

80
void ShellCommandPage::setStartedStatus(const QString &startedStatus)
81
82
83
84
{
    m_startedStatus = startedStatus;
}

85
void ShellCommandPage::start(ShellCommand *command)
86
{
87
    if (!command) {
88
        m_logPlainTextEdit->setPlainText(tr("No job running, please abort."));
89
90
91
        return;
    }

92
    QTC_ASSERT(m_state != Running, return);
93
94
    m_command = command;
    command->setProgressiveOutput(true);
95
96
97
    connect(command, &ShellCommand::stdOutText, this, &ShellCommandPage::reportStdOut);
    connect(command, &ShellCommand::stdErrText, this, &ShellCommandPage::reportStdErr);
    connect(command, &ShellCommand::finished, this, &ShellCommandPage::slotFinished);
98
    QApplication::setOverrideCursor(Qt::WaitCursor);
99
    m_logPlainTextEdit->clear();
100
    m_overwriteOutput = false;
101
102
    m_statusLabel->setText(m_startedStatus);
    m_statusLabel->setPalette(QPalette());
103
    m_state = Running;
104
    command->execute();
105
106

    wizard()->button(QWizard::BackButton)->setEnabled(false);
107
108
}

109
void ShellCommandPage::slotFinished(bool ok, int exitCode, const QVariant &)
110
{
111
112
113
114
115
116
117
118
119
    QTC_ASSERT(m_state == Running, return);

    const bool success = (ok && exitCode == 0);
    QString message;
    QPalette palette;

    if (success) {
        m_state = Succeeded;
        message = tr("Succeeded.");
120
        palette.setColor(QPalette::WindowText, creatorTheme()->color(Theme::TextColorNormal).name());
121
    } else {
122
123
        m_state = Failed;
        message = tr("Failed.");
124
        palette.setColor(QPalette::WindowText, creatorTheme()->color(Theme::TextColorError).name());
125
    }
126
127
128
129
130

    m_statusLabel->setText(message);
    m_statusLabel->setPalette(palette);

    QApplication::restoreOverrideCursor();
131
    wizard()->button(QWizard::BackButton)->setEnabled(true);
132
133
134

    if (success)
        emit completeChanged();
135
    emit finished(success);
136
137
}

138
void ShellCommandPage::reportStdOut(const QString &text)
139
{
140
    m_formatter->appendMessage(text, Utils::StdOutFormat);
141
142
}

143
void ShellCommandPage::reportStdErr(const QString &text)
144
{
145
    m_formatter->appendMessage(text, Utils::StdErrFormat);
146
147
}

148
void ShellCommandPage::terminate()
149
{
150
    if (m_command)
Orgad Shaneh's avatar
Orgad Shaneh committed
151
        m_command->cancel();
152
153
}

154
bool ShellCommandPage::isComplete() const
155
156
157
158
{
    return m_state == Succeeded;
}

159
} // namespace Utils