~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/gui/qgsrunprocess.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgsrunprocess.cpp
 
3
 
 
4
 A class that runs an external program
 
5
 
 
6
                             -------------------
 
7
    begin                : Jan 2005
 
8
    copyright            : (C) 2005 by Gavin Macaulay
 
9
    email                : gavin at macaulay dot co dot nz
 
10
 ***************************************************************************/
 
11
 
 
12
/***************************************************************************
 
13
 *                                                                         *
 
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.                                   *
 
18
 *                                                                         *
 
19
 ***************************************************************************/
 
20
/* $Id: qgsrunprocess.cpp 4911 2006-02-28 06:50:37Z g_j_m $ */
 
21
 
 
22
#include "qgsrunprocess.h"
 
23
 
 
24
#include "qgsmessageviewer.h"
 
25
#include <QMessageBox>
 
26
#include <Q3Process>
 
27
 
 
28
QgsRunProcess::QgsRunProcess(const QStringList& args,
 
29
                             bool capture) : mProcess(NULL), mLogViewer(NULL)
 
30
{
 
31
  // Make up a string from the command and arguments that we'll use
 
32
  // for display purposes
 
33
  QString whole_cmd;
 
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());
 
37
 
 
38
  mProcess = new Q3Process;
 
39
  mProcess->setArguments(args);
 
40
 
 
41
  if (capture)
 
42
  {
 
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()));
 
49
  }
 
50
 
 
51
  if (!mProcess->start())
 
52
  {
 
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
 
57
    die();
 
58
  }
 
59
  else if (capture)
 
60
  {
 
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>" );
 
67
    mLogViewer->show();
 
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()));
 
71
  }
 
72
  else
 
73
    // We're not capturing the output from the process, so we don't
 
74
    // need to exist anymore.
 
75
    die();
 
76
}
 
77
 
 
78
QgsRunProcess::~QgsRunProcess() 
 
79
 
80
  delete mProcess; 
 
81
}
 
82
 
 
83
void QgsRunProcess::die()
 
84
{
 
85
  delete this;
 
86
}
 
87
 
 
88
void QgsRunProcess::stdoutAvailable()
 
89
{
 
90
  // Add the new output to the dialog box
 
91
  if (mProcess->canReadLineStdout())
 
92
  {
 
93
    QString line;
 
94
    while ((line = mProcess->readLineStdout()) != QString::null)
 
95
    {
 
96
      mLogViewer->appendMessage(line);
 
97
    }
 
98
  }
 
99
}
 
100
 
 
101
void QgsRunProcess::stderrAvailable()
 
102
{
 
103
  // Add the new output to the dialog box, but colour it red
 
104
  if (mProcess->canReadLineStderr())
 
105
  {
 
106
    QString line;
 
107
    mLogViewer->appendMessage("<font color=red>");
 
108
    while ((line = mProcess->readLineStderr()) != QString::null)
 
109
    {
 
110
      mLogViewer->appendMessage(line);
 
111
    }
 
112
    mLogViewer->appendMessage("</font>");
 
113
  }
 
114
}
 
115
 
 
116
void QgsRunProcess::processExit()
 
117
{
 
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).
 
124
 
 
125
  if (mLogViewer != 0)
 
126
  {
 
127
    mLogViewer->appendMessage( "<b>" + tr("Done") + "</b>" );
 
128
  }
 
129
 
 
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.
 
133
  die();
 
134
}
 
135
 
 
136
void QgsRunProcess::dialogGone()
 
137
{
 
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
 
141
  // go too.
 
142
  die();
 
143
}