processwidget.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "processwidget.h"
00021 #include "processlinemaker.h"
00022
00023 #include <kdeversion.h>
00024 #include <qdir.h>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <kprocess.h>
00028 #include <qpainter.h>
00029 #include <qapplication.h>
00030
00031
00032 ProcessListBoxItem::ProcessListBoxItem(const QString &s, Type type)
00033 : QListBoxText(s), t(type)
00034 {}
00035
00036
00037 bool ProcessListBoxItem::isCustomItem()
00038 {
00039 return false;
00040 }
00041
00042
00043 void ProcessListBoxItem::paint(QPainter *p)
00044 {
00045 p->setPen((t==Error)? Qt::darkRed :
00046 (t==Diagnostic)? Qt::black : Qt::darkBlue);
00047 QListBoxText::paint(p);
00048 }
00049
00050
00051 ProcessWidget::ProcessWidget(QWidget *parent, const char *name)
00052 : KListBox(parent, name)
00053 {
00054 setFocusPolicy(QWidget::NoFocus);
00055 QPalette pal = palette();
00056 pal.setColor(QColorGroup::HighlightedText,
00057 pal.color(QPalette::Normal, QColorGroup::Text));
00058 pal.setColor(QColorGroup::Highlight,
00059 pal.color(QPalette::Normal, QColorGroup::Mid));
00060 setPalette(pal);
00061
00062 #if (KDE_VERSION > 305)
00063 childproc = new KProcess();
00064 childproc->setUseShell(true);
00065 #else
00066 childproc = new KShellProcess();
00067 #endif
00068
00069 procLineMaker = new ProcessLineMaker( childproc );
00070
00071 connect( procLineMaker, SIGNAL(receivedStdoutLine(const QString&)),
00072 this, SLOT(insertStdoutLine(const QString&) ));
00073 connect( procLineMaker, SIGNAL(receivedStderrLine(const QString&)),
00074 this, SLOT(insertStderrLine(const QString&) ));
00075
00076 connect(childproc, SIGNAL(processExited(KProcess*)),
00077 this, SLOT(slotProcessExited(KProcess*) )) ;
00078 }
00079
00080
00081 ProcessWidget::~ProcessWidget()
00082 {
00083 delete childproc;
00084 delete procLineMaker;
00085 }
00086
00087
00088 void ProcessWidget::startJob(const QString &dir, const QString &command)
00089 {
00090 procLineMaker->clearBuffers();
00091 procLineMaker->blockSignals( false );
00092
00093 clear();
00094 insertItem(new ProcessListBoxItem(command, ProcessListBoxItem::Diagnostic));
00095 childproc->clearArguments();
00096 if (!dir.isNull()) {
00097 kdDebug(9000) << "Changing to dir " << dir << endl;
00098 QDir::setCurrent(dir);
00099 }
00100
00101 *childproc << command;
00102 childproc->start(KProcess::NotifyOnExit, KProcess::AllOutput);
00103 }
00104
00105
00106 void ProcessWidget::killJob( int signo )
00107 {
00108 procLineMaker->blockSignals( true );
00109
00110 childproc->kill( signo );
00111 }
00112
00113
00114 bool ProcessWidget::isRunning()
00115 {
00116 return childproc->isRunning();
00117 }
00118
00119
00120 void ProcessWidget::slotProcessExited(KProcess *)
00121 {
00122 childFinished(childproc->normalExit(), childproc->exitStatus());
00123 maybeScrollToBottom();
00124 emit processExited(childproc);
00125 }
00126
00127
00128 void ProcessWidget::insertStdoutLine(const QString &line)
00129 {
00130 insertItem(new ProcessListBoxItem(line.stripWhiteSpace(),
00131 ProcessListBoxItem::Normal));
00132 maybeScrollToBottom();
00133 }
00134
00135
00136 void ProcessWidget::insertStderrLine(const QString &line)
00137 {
00138 insertItem(new ProcessListBoxItem(line.stripWhiteSpace(),
00139 ProcessListBoxItem::Error));
00140 maybeScrollToBottom();
00141 }
00142
00143
00144 void ProcessWidget::childFinished(bool normal, int status)
00145 {
00146 QString s;
00147 ProcessListBoxItem::Type t;
00148
00149 if (normal) {
00150 if (status) {
00151 s = i18n("*** Exited with status: %1 ***").arg(status);
00152 t = ProcessListBoxItem::Error;
00153 } else {
00154 s = i18n("*** Exited normally ***");
00155 t = ProcessListBoxItem::Diagnostic;
00156 }
00157 } else {
00158 s = i18n("*** Process aborted ***");
00159 t = ProcessListBoxItem::Error;
00160 }
00161
00162 insertItem(new ProcessListBoxItem(s, t));
00163 }
00164
00165
00166 QSize ProcessWidget::minimumSizeHint() const
00167 {
00168
00169
00170
00171 return QSize( QListBox::sizeHint().width(),
00172 (fontMetrics().lineSpacing()+2)*4 );
00173 }
00174
00179 void ProcessWidget::maybeScrollToBottom()
00180 {
00181 if ( verticalScrollBar()->value() == verticalScrollBar()->maxValue() )
00182 {
00183 setBottomItem( count() -1 );
00184 }
00185 }
00186
00187 #include "processwidget.moc"
This file is part of the documentation for KDevelop Version 3.1.2.