• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Widgets Library
 

KDevelop Widgets Library

  • lib
  • widgets
processwidget.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1999-2001 Bernd Gehrmann <bernd@kdevelop.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "processwidget.h"
21#include "processlinemaker.h"
22
23#include <tqdir.h>
24#include <kdebug.h>
25#include <tdelocale.h>
26#include <kprocess.h>
27#include <tqpainter.h>
28#include <tqapplication.h>
29
30
31ProcessListBoxItem::ProcessListBoxItem(const TQString &s, Type type)
32 : TQListBoxText(s), t(type)
33{
34 TQString clean = s;
35 clean.replace( TQChar('\t'), TQString(" ") );
36 clean.replace( TQChar('\n'), TQString() );
37 clean.replace( TQChar('\r'), TQString() );
38 setText( clean );
39
40 setCustomHighlighting(true);
41}
42
43
44bool ProcessListBoxItem::isCustomItem()
45{
46 return false;
47}
48
49static inline unsigned char normalize(int a)
50{
51 return (a < 0 ? 0 : a > 255 ? 255 : a);
52}
53
54static inline double blend1(double a, double b, double k)
55{
56 return a + (b - a) * k;
57}
58
59TQColor ProcessListBoxItem::blend(const TQColor &c1, const TQColor &c2, double k) const
60{
61 if (k < 0.0) return c1;
62 if (k > 1.0) return c2;
63
64 int r = normalize((int)blend1((double)c1.red(), (double)c2.red(), k));
65 int g = normalize((int)blend1((double)c1.green(), (double)c2.green(), k));
66 int b = normalize((int)blend1((double)c1.blue(), (double)c2.blue(), k));
67
68 return TQColor(tqRgb(r, g, b));
69}
70
71void ProcessListBoxItem::paint(TQPainter *p)
72{
73 TQColor dim, warn, err, back;
74 if (listBox()) {
75 const TQColorGroup& group = listBox()->palette().active();
76 if (isSelected()) {
77 back = group.button();
78 warn = group.buttonText();
79 }
80 else
81 {
82 back = group.base();
83 warn = group.text();
84 }
85 err = group.linkVisited();
86 dim = blend(warn, back);
87 }
88 else
89 {
90 warn = TQt::black;
91 dim = TQt::darkBlue;
92 err = TQt::darkRed;
93 if (isSelected())
94 back = TQt::lightGray;
95 else
96 back = TQt::white;
97 }
98 p->fillRect(p->window(), TQBrush(back));
99 p->setPen((t==Error)? err :
100 (t==Diagnostic)? warn : dim);
101 TQListBoxText::paint(p);
102}
103
104
105ProcessWidget::ProcessWidget(TQWidget *parent, const char *name)
106 : TDEListBox(parent, name)
107{
108 setFocusPolicy(TQWidget::NoFocus);
109
110 // Don't override the palette, as that can mess up styles. Instead, draw
111 // the background ourselves (see ProcessListBoxItem::paint).
112
113
114 childproc = new TDEProcess();
115 childproc->setUseShell(true);
116
117 procLineMaker = new ProcessLineMaker( childproc );
118
119 connect( procLineMaker, TQ_SIGNAL(receivedStdoutLine(const TQCString&)),
120 this, TQ_SLOT(insertStdoutLine(const TQCString&) ));
121 connect( procLineMaker, TQ_SIGNAL(receivedStderrLine(const TQCString&)),
122 this, TQ_SLOT(insertStderrLine(const TQCString&) ));
123 connect( procLineMaker, TQ_SIGNAL(receivedPartialStdoutLine(const TQCString&)),
124 this, TQ_SLOT(addPartialStdoutLine(const TQCString&) ));
125 connect( procLineMaker, TQ_SIGNAL(receivedPartialStderrLine(const TQCString&)),
126 this, TQ_SLOT(addPartialStderrLine(const TQCString&) ));
127
128 connect(childproc, TQ_SIGNAL(processExited(TDEProcess*)),
129 this, TQ_SLOT(slotProcessExited(TDEProcess*) )) ;
130}
131
132
133ProcessWidget::~ProcessWidget()
134{
135 delete childproc;
136 delete procLineMaker;
137}
138
139
140void ProcessWidget::startJob(const TQString &dir, const TQString &command)
141{
142 procLineMaker->clearBuffers();
143 procLineMaker->blockSignals( false );
144
145 clear();
146 insertItem(new ProcessListBoxItem(command, ProcessListBoxItem::Diagnostic));
147 childproc->clearArguments();
148 if (!dir.isNull()) {
149 childproc->setWorkingDirectory( dir );
150 }
151
152 *childproc << command;
153 childproc->start(TDEProcess::OwnGroup, TDEProcess::AllOutput);
154}
155
156
157void ProcessWidget::killJob( int signo )
158{
159 procLineMaker->blockSignals( true );
160
161 childproc->kill( signo );
162}
163
164
165bool ProcessWidget::isRunning()
166{
167 return childproc->isRunning();
168}
169
170
171void ProcessWidget::slotProcessExited(TDEProcess *)
172{
173 procLineMaker->flush();
174 if( !stdoutbuf.isEmpty() )
175 insertStdoutLine("");
176 if( !stderrbuf.isEmpty() )
177 insertStderrLine("");
178 childFinished(childproc->normalExit(), childproc->exitStatus());
179 maybeScrollToBottom();
180 emit processExited(childproc);
181}
182
183
184void ProcessWidget::insertStdoutLine(const TQCString &line)
185{
186 if( !stdoutbuf.isEmpty() )
187 {
188 stdoutbuf += line;
189 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit(stdoutbuf),
190 ProcessListBoxItem::Normal ),
191 lastRowStdout+1 );
192 stdoutbuf.truncate( 0 );
193 }else
194 {
195 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit( line ),
196 ProcessListBoxItem::Normal) );
197 }
198 lastRowStdout = count() - 1;
199 maybeScrollToBottom();
200}
201
202
203void ProcessWidget::insertStderrLine(const TQCString &line)
204{
205 if( !stderrbuf.isEmpty() )
206 {
207 stderrbuf += line;
208 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit( stderrbuf ),
209 ProcessListBoxItem::Error ),
210 lastRowStderr+1 );
211 stderrbuf.truncate( 0 );
212 } else
213 {
214 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit( line ),
215 ProcessListBoxItem::Error) );
216 }
217 lastRowStderr = count() - 1;
218 maybeScrollToBottom();
219}
220
221
222void ProcessWidget::childFinished(bool normal, int status)
223{
224 TQString s;
225 ProcessListBoxItem::Type t;
226
227 if (normal) {
228 if (status) {
229 s = i18n("*** Exited with status: %1 ***").arg(status);
230 t = ProcessListBoxItem::Error;
231 } else {
232 s = i18n("*** Exited normally ***");
233 t = ProcessListBoxItem::Diagnostic;
234 }
235 } else {
236 if ( childproc->signalled() && childproc->exitSignal() == SIGSEGV )
237 {
238 s = i18n("*** Process aborted. Segmentation fault ***");
239 }
240 else
241 {
242 s = i18n("*** Process aborted ***");
243 }
244 t = ProcessListBoxItem::Error;
245 }
246
247 insertItem(new ProcessListBoxItem(s, t));
248}
249
250
251TQSize ProcessWidget::minimumSizeHint() const
252{
253 // I'm not sure about this, but when I don't use override minimumSizeHint(),
254 // the initial size in clearly too small
255
256 return TQSize( TQListBox::sizeHint().width(),
257 (fontMetrics().lineSpacing()+2)*4 );
258}
259
264void ProcessWidget::maybeScrollToBottom()
265{
266 if ( verticalScrollBar()->value() == verticalScrollBar()->maxValue() )
267 {
268 setBottomItem( count() -1 );
269 }
270}
271
272void ProcessWidget::addPartialStderrLine(const TQCString& linepart)
273{
274 stderrbuf += linepart;
275}
276
277void ProcessWidget::addPartialStdoutLine(const TQCString& linepart)
278{
279 stdoutbuf += linepart;
280}
281
282#include "processwidget.moc"
ProcessLineMaker
Convenience class to catch output of TDEProcess.
Definition processlinemaker.h:36
ProcessListBoxItem
Listbox item for process widgets.
Definition processwidget.h:37
ProcessWidget::startJob
void startJob(const TQString &dir, const TQString &command)
Starts the child process.
Definition processwidget.cpp:140
ProcessWidget::insertStdoutLine
virtual void insertStdoutLine(const TQCString &line)
Inserts one line from stdin into the listbox.
Definition processwidget.cpp:184
ProcessWidget::childFinished
virtual void childFinished(bool normal, int status)
This is called when the child process exits.
Definition processwidget.cpp:222
ProcessWidget::insertStderrLine
virtual void insertStderrLine(const TQCString &line)
Inserts one line from stderr into the listbox.
Definition processwidget.cpp:203
ProcessWidget::isRunning
bool isRunning()
Returns whether a process is running in this view.
Definition processwidget.cpp:165
ProcessWidget::maybeScrollToBottom
void maybeScrollToBottom()
Should be called right after an insertItem(), will automatic scroll the listbox if it is already at t...
Definition processwidget.cpp:264
ProcessWidget::killJob
void killJob(int signo=SIGTERM)
Kills the child processss.
Definition processwidget.cpp:157
processlinemaker.h
Utility objects for process output views.
processwidget.h
Widgets for various output views.

KDevelop Widgets Library

Skip menu "KDevelop Widgets Library"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDevelop Widgets Library

Skip menu "KDevelop Widgets Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Widgets Library by doxygen 1.15.0
This website is maintained by Timothy Pearson.