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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* This file is part of the KDE project
Copyright (C) 1999-2001 Bernd Gehrmann <bernd@kdevelop.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "processwidget.h"
#include "processlinemaker.h"
#include <kdeversion.h>
#include <qdir.h>
#include <kdebug.h>
#include <klocale.h>
#include <kprocess.h>
#include <qpainter.h>
#include <qapplication.h>
ProcessListBoxItem::ProcessListBoxItem(const QString &s, Type type)
: QListBoxText(s), t(type)
{
QString clean = s;
clean.replace( QChar('\t'), QString(" ") );
clean.replace( QChar('\n'), QString() );
clean.replace( QChar('\r'), QString() );
setText( clean );
setCustomHighlighting(true);
}
bool ProcessListBoxItem::isCustomItem()
{
return false;
}
static inline unsigned char normalize(int a)
{
return (a < 0 ? 0 : a > 255 ? 255 : a);
}
static inline double blend1(double a, double b, double k)
{
return a + (b - a) * k;
}
QColor ProcessListBoxItem::blend(const QColor &c1, const QColor &c2, double k) const
{
if (k < 0.0) return c1;
if (k > 1.0) return c2;
int r = normalize((int)blend1((double)c1.red(), (double)c2.red(), k));
int g = normalize((int)blend1((double)c1.green(), (double)c2.green(), k));
int b = normalize((int)blend1((double)c1.blue(), (double)c2.blue(), k));
return QColor(qRgb(r, g, b));
}
void ProcessListBoxItem::paint(QPainter *p)
{
QColor dim, warn, err, back;
if (listBox()) {
const QColorGroup& group = listBox()->palette().active();
if (isSelected()) {
back = group.button();
warn = group.buttonText();
}
else
{
back = group.base();
warn = group.text();
}
err = group.linkVisited();
dim = blend(warn, back);
}
else
{
warn = Qt::black;
dim = Qt::darkBlue;
err = Qt::darkRed;
if (isSelected())
back = Qt::lightGray;
else
back = Qt::white;
}
p->fillRect(p->window(), QBrush(back));
p->setPen((t==Error)? err :
(t==Diagnostic)? warn : dim);
QListBoxText::paint(p);
}
ProcessWidget::ProcessWidget(QWidget *parent, const char *name)
: KListBox(parent, name)
{
setFocusPolicy(QWidget::NoFocus);
// Don't override the palette, as that can mess up styles. Instead, draw
// the background ourselves (see ProcessListBoxItem::paint).
childproc = new KProcess();
childproc->setUseShell(true);
procLineMaker = new ProcessLineMaker( childproc );
connect( procLineMaker, SIGNAL(receivedStdoutLine(const QCString&)),
this, SLOT(insertStdoutLine(const QCString&) ));
connect( procLineMaker, SIGNAL(receivedStderrLine(const QCString&)),
this, SLOT(insertStderrLine(const QCString&) ));
connect( procLineMaker, SIGNAL(receivedPartialStdoutLine(const QCString&)),
this, SLOT(addPartialStdoutLine(const QCString&) ));
connect( procLineMaker, SIGNAL(receivedPartialStderrLine(const QCString&)),
this, SLOT(addPartialStderrLine(const QCString&) ));
connect(childproc, SIGNAL(processExited(KProcess*)),
this, SLOT(slotProcessExited(KProcess*) )) ;
}
ProcessWidget::~ProcessWidget()
{
delete childproc;
delete procLineMaker;
}
void ProcessWidget::startJob(const QString &dir, const QString &command)
{
procLineMaker->clearBuffers();
procLineMaker->blockSignals( false );
clear();
insertItem(new ProcessListBoxItem(command, ProcessListBoxItem::Diagnostic));
childproc->clearArguments();
if (!dir.isNull()) {
childproc->setWorkingDirectory( dir );
}
*childproc << command;
childproc->start(KProcess::OwnGroup, KProcess::AllOutput);
}
void ProcessWidget::killJob( int signo )
{
procLineMaker->blockSignals( true );
childproc->kill( signo );
}
bool ProcessWidget::isRunning()
{
return childproc->isRunning();
}
void ProcessWidget::slotProcessExited(KProcess *)
{
procLineMaker->flush();
if( !stdoutbuf.isEmpty() )
insertStdoutLine("");
if( !stderrbuf.isEmpty() )
insertStderrLine("");
childFinished(childproc->normalExit(), childproc->exitStatus());
maybeScrollToBottom();
emit processExited(childproc);
}
void ProcessWidget::insertStdoutLine(const QCString &line)
{
if( !stdoutbuf.isEmpty() )
{
stdoutbuf += line;
insertItem( new ProcessListBoxItem( QString::fromLocal8Bit(stdoutbuf),
ProcessListBoxItem::Normal ),
lastRowStdout+1 );
stdoutbuf.truncate( 0 );
}else
{
insertItem( new ProcessListBoxItem( QString::fromLocal8Bit( line ),
ProcessListBoxItem::Normal) );
}
lastRowStdout = count() - 1;
maybeScrollToBottom();
}
void ProcessWidget::insertStderrLine(const QCString &line)
{
if( !stderrbuf.isEmpty() )
{
stderrbuf += line;
insertItem( new ProcessListBoxItem( QString::fromLocal8Bit( stderrbuf ),
ProcessListBoxItem::Error ),
lastRowStderr+1 );
stderrbuf.truncate( 0 );
} else
{
insertItem( new ProcessListBoxItem( QString::fromLocal8Bit( line ),
ProcessListBoxItem::Error) );
}
lastRowStderr = count() - 1;
maybeScrollToBottom();
}
void ProcessWidget::childFinished(bool normal, int status)
{
QString s;
ProcessListBoxItem::Type t;
if (normal) {
if (status) {
s = i18n("*** Exited with status: %1 ***").arg(status);
t = ProcessListBoxItem::Error;
} else {
s = i18n("*** Exited normally ***");
t = ProcessListBoxItem::Diagnostic;
}
} else {
if ( childproc->signalled() && childproc->exitSignal() == SIGSEGV )
{
s = i18n("*** Process aborted. Segmentation fault ***");
}
else
{
s = i18n("*** Process aborted ***");
}
t = ProcessListBoxItem::Error;
}
insertItem(new ProcessListBoxItem(s, t));
}
QSize ProcessWidget::minimumSizeHint() const
{
// I'm not sure about this, but when I don't use override minimumSizeHint(),
// the initial size in clearly too small
return QSize( QListBox::sizeHint().width(),
(fontMetrics().lineSpacing()+2)*4 );
}
/** Should be called right after an insertItem(),
will automatic scroll the listbox if it is already at the bottom
to prevent automatic scrolling when the user has scrolled up
*/
void ProcessWidget::maybeScrollToBottom()
{
if ( verticalScrollBar()->value() == verticalScrollBar()->maxValue() )
{
setBottomItem( count() -1 );
}
}
void ProcessWidget::addPartialStderrLine(const QCString& linepart)
{
stderrbuf += linepart;
}
void ProcessWidget::addPartialStdoutLine(const QCString& linepart)
{
stdoutbuf += linepart;
}
#include "processwidget.moc"
|