~fboucault/qtcreator-plugin-ubuntu/newarch_arm64

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
/*
 * Copyright 2014-2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 2.1.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Benjamin Zeller <benjamin.zeller@canonical.com>
 */
#include "processoutputdialog.h"
#include "ui_processoutputdialog.h"

#include <texteditor/fontsettings.h>
#include <coreplugin/icore.h>

#include <QPushButton>
#include <QDebug>


namespace Ubuntu {
namespace Internal {

const char PROCESS_ERROR_EXIT_MESSAGE[] = "Task exited with errors, please check the output";
const char PROCESS_SUCCESS_EXIT_MESSAGE[] = "Task exited with no errors";

ProcessOutputDialog::ProcessOutputDialog(QWidget *parent)
    : QDialog(parent)
    ,m_ui(new Ui::ProcessOutputDialog)
{
    m_ui->setupUi(this);

    QFont f(TextEditor::FontSettings::defaultFixedFontFamily());
    f.setStyleHint(QFont::TypeWriter);
    m_ui->output->setFont(f);

    connect(m_ui->checkBox, &QCheckBox::toggled, [this](bool checked){
        m_ui->output->setVisible(checked);
        if(checked)
            adjustSize();
        else
            resize(minimumSize());
    });

    m_ui->checkBox->setChecked(false);

    m_process = new Utils::QtcProcess(this);
    connect(m_process,SIGNAL(readyReadStandardOutput()),this,SLOT(on_processReadyReadStandardOutput()));
    connect(m_process,SIGNAL(readyReadStandardError()),this,SLOT(on_processReadyReadStandardError()));
    connect(m_process,SIGNAL(finished(int)),this,SLOT(on_processFinished(int)));

    //make sure the progressbar is not animating just yet
    m_ui->progressBar->setRange(0, 1);
}

ProcessOutputDialog::~ProcessOutputDialog()
{
    delete m_ui;
}

void ProcessOutputDialog::setParameters(const QList<ProjectExplorer::ProcessParameters> &params)
{
    m_tasks = params;
}

int ProcessOutputDialog::lastExitCode() const
{
    return m_exitCode;
}

int ProcessOutputDialog::runProcessModal(const ProjectExplorer::ProcessParameters &params, QWidget *parent)
{
    return runProcessModal(QList<ProjectExplorer::ProcessParameters>()<<params,parent);
}

int ProcessOutputDialog::runProcessModal(const QList<ProjectExplorer::ProcessParameters> &params, QWidget *parent)
{
    ProcessOutputDialog dlg( parent ? parent : Core::ICore::mainWindow());
    dlg.setParameters(params);
    QMetaObject::invokeMethod(&dlg,"runTasks",Qt::QueuedConnection);
    dlg.exec();

    return dlg.m_exitCode;
}

void ProcessOutputDialog::runTasks( )
{
    m_hadErrors = false;
    disableCloseButton(true);
    nextTask();
}

void ProcessOutputDialog::done(int code)
{
    QDialog::done(code);
}

void ProcessOutputDialog::disableCloseButton(const bool &disabled)
{
    QPushButton* bt = m_ui->buttonBox->button(QDialogButtonBox::Close);
    if(bt) bt->setDisabled(disabled);
}

void ProcessOutputDialog::nextTask()
{
    if(m_tasks.length() <= 0)
        return;


    m_ui->progressBar->setRange(0,0);

    ProjectExplorer::ProcessParameters params = m_tasks.takeFirst();
    params.resolveAll();
    m_process->setCommand(params.command(),params.arguments());
    m_process->setEnvironment(params.environment());
    m_process->setWorkingDirectory(params.workingDirectory());
    m_process->start();
}

void ProcessOutputDialog::on_processFinished(int exitCode)
{
    if (exitCode != 0) {
        m_hadErrors = true;
        on_processReadyReadStandardError(tr("---%0---").arg(QLatin1String(PROCESS_ERROR_EXIT_MESSAGE)));
    } else {
        on_processReadyReadStandardOutput(tr("---%0---").arg(QLatin1String(PROCESS_SUCCESS_EXIT_MESSAGE)));
    }

    if(m_tasks.length() > 0) {
        nextTask();
        return;
    }

    m_ui->progressBar->setRange(0,1);
    m_ui->progressBar->setValue(1);
    disableCloseButton(false);
    m_exitCode = exitCode;

    if (m_hadErrors) {
        m_ui->label->setText(tr("There were errors while executing the tasks, please check the details."));
        m_ui->checkBox->setChecked(true);
    } else {
        m_ui->label->setText(tr("All tasks finished, check the details for more information"));
    }
}

void ProcessOutputDialog::on_processReadyReadStandardOutput(const QString txt)
{
    QString outText = QString::fromLocal8Bit("<div>");

    if(txt.isEmpty())
        outText.append(QString::fromLocal8Bit(m_process->readAllStandardOutput()));
    else
        outText.append(txt);

    outText.append(QString::fromLocal8Bit("</div>"));
    m_ui->output->append(outText);
}

void ProcessOutputDialog::on_processReadyReadStandardError(const QString txt)
{
    QString outText = QString::fromLocal8Bit("<div style=\"color:red; font-weight: bold;\">");

    if(txt.isEmpty())
        outText.append(QString::fromLocal8Bit(m_process->readAllStandardError()));
    else
        outText.append(txt);

    outText.append(QString::fromLocal8Bit("</div>"));
    m_ui->output->append(outText);
}

} // namespace Internal
} // namespace Ubuntu