1
/***************************************************************************
4
A class that runs an external program
8
copyright : (C) 2005 by Gavin Macaulay
9
email : gavin at macaulay dot co dot nz
10
***************************************************************************/
12
/***************************************************************************
14
* This program is free software; you can redistribute it and/or modify *
15
* it under the terms of the GNU General Public License as published by *
16
* the Free Software Foundation; either version 2 of the License, or *
17
* (at your option) any later version. *
19
***************************************************************************/
20
/* $Id: qgsrunprocess.cpp 4911 2006-02-28 06:50:37Z g_j_m $ */
22
#include "qgsrunprocess.h"
24
#include "qgsmessageviewer.h"
25
#include <QMessageBox>
28
QgsRunProcess::QgsRunProcess(const QStringList& args,
29
bool capture) : mProcess(NULL), mLogViewer(NULL)
31
// Make up a string from the command and arguments that we'll use
32
// for display purposes
34
for (int i = 0; i < args.count(); ++i)
35
whole_cmd += "[" + args[i] + "] ";
36
qDebug("Running command: %s\n", (const char *)whole_cmd.toLocal8Bit().data());
38
mProcess = new Q3Process;
39
mProcess->setArguments(args);
43
connect(mProcess, SIGNAL(readyReadStdout()), this, SLOT(stdoutAvailable()));
44
connect(mProcess, SIGNAL(readyReadStderr()), this, SLOT(stderrAvailable()));
45
// We only care if the process has finished if we are capturing
46
// the output from the process, hence this connect() call is
47
// inside the capture if() statement.
48
connect(mProcess, SIGNAL(processExited()), this, SLOT(processExit()));
51
if (!mProcess->start())
53
QMessageBox::critical(0, tr("Unable to run command"),
54
tr("Unable to run the command") + "\n" + whole_cmd +
55
"\n", QMessageBox::Ok, Qt::NoButton);
56
// Didn't work, so no need to hang around
61
// Create a dialog box to display the output. Use the
62
// QgsMessageViewer dialog, but tweak its behaviour to suit our
63
// needs. It will delete itself when the dialog box is closed.
64
mLogViewer = new QgsMessageViewer(0, Qt::WDestructiveClose);
65
mLogViewer->setCaption(whole_cmd);
66
mLogViewer->setMessageAsHtml( "<b>" + tr("Starting") + " " + whole_cmd + "...</b>" );
68
// Be told when the dialog box is closed (it gets destroyed when
69
// closed because of the Qt flag used when it was created above).
70
connect(mLogViewer, SIGNAL(destroyed()), this, SLOT(dialogGone()));
73
// We're not capturing the output from the process, so we don't
74
// need to exist anymore.
78
QgsRunProcess::~QgsRunProcess()
83
void QgsRunProcess::die()
88
void QgsRunProcess::stdoutAvailable()
90
// Add the new output to the dialog box
91
if (mProcess->canReadLineStdout())
94
while ((line = mProcess->readLineStdout()) != QString::null)
96
mLogViewer->appendMessage(line);
101
void QgsRunProcess::stderrAvailable()
103
// Add the new output to the dialog box, but colour it red
104
if (mProcess->canReadLineStderr())
107
mLogViewer->appendMessage("<font color=red>");
108
while ((line = mProcess->readLineStderr()) != QString::null)
110
mLogViewer->appendMessage(line);
112
mLogViewer->appendMessage("</font>");
116
void QgsRunProcess::processExit()
118
// Because we catch the dialog box going (the dialogGone()
119
// function), and delete this instance, control will only pass to
120
// this function if the dialog box still exists when the process
121
// exits, so it's always safe to use the pointer to the dialog box
122
// (unless it was never created in the first case, which is what the
123
// test against 0 is for).
127
mLogViewer->appendMessage( "<b>" + tr("Done") + "</b>" );
130
// Since the dialog box takes care of deleting itself, and the
131
// process has gone, there's no need for this instance to stay
132
// around, so we disappear too.
136
void QgsRunProcess::dialogGone()
138
// The dialog has gone, so the user is no longer interested in the
139
// output from the process. Since the process will run happily
140
// without the QProcess object, this instance and its data can then