• 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 <tdeversion.h>
24#include <tqdir.h>
25#include <kdebug.h>
26#include <tdelocale.h>
27#include <kprocess.h>
28#include <tqpainter.h>
29#include <tqapplication.h>
30
31
32ProcessListBoxItem::ProcessListBoxItem(const TQString &s, Type type)
33 : TQListBoxText(s), t(type)
34{
35 TQString clean = s;
36 clean.replace( TQChar('\t'), TQString(" ") );
37 clean.replace( TQChar('\n'), TQString() );
38 clean.replace( TQChar('\r'), TQString() );
39 setText( clean );
40
41 setCustomHighlighting(true);
42}
43
44
45bool ProcessListBoxItem::isCustomItem()
46{
47 return false;
48}
49
50static inline unsigned char normalize(int a)
51{
52 return (a < 0 ? 0 : a > 255 ? 255 : a);
53}
54
55static inline double blend1(double a, double b, double k)
56{
57 return a + (b - a) * k;
58}
59
60TQColor ProcessListBoxItem::blend(const TQColor &c1, const TQColor &c2, double k) const
61{
62 if (k < 0.0) return c1;
63 if (k > 1.0) return c2;
64
65 int r = normalize((int)blend1((double)c1.red(), (double)c2.red(), k));
66 int g = normalize((int)blend1((double)c1.green(), (double)c2.green(), k));
67 int b = normalize((int)blend1((double)c1.blue(), (double)c2.blue(), k));
68
69 return TQColor(tqRgb(r, g, b));
70}
71
72void ProcessListBoxItem::paint(TQPainter *p)
73{
74 TQColor dim, warn, err, back;
75 if (listBox()) {
76 const TQColorGroup& group = listBox()->palette().active();
77 if (isSelected()) {
78 back = group.button();
79 warn = group.buttonText();
80 }
81 else
82 {
83 back = group.base();
84 warn = group.text();
85 }
86 err = group.linkVisited();
87 dim = blend(warn, back);
88 }
89 else
90 {
91 warn = TQt::black;
92 dim = TQt::darkBlue;
93 err = TQt::darkRed;
94 if (isSelected())
95 back = TQt::lightGray;
96 else
97 back = TQt::white;
98 }
99 p->fillRect(p->window(), TQBrush(back));
100 p->setPen((t==Error)? err :
101 (t==Diagnostic)? warn : dim);
102 TQListBoxText::paint(p);
103}
104
105
106ProcessWidget::ProcessWidget(TQWidget *parent, const char *name)
107 : TDEListBox(parent, name)
108{
109 setFocusPolicy(TQWidget::NoFocus);
110
111 // Don't override the palette, as that can mess up styles. Instead, draw
112 // the background ourselves (see ProcessListBoxItem::paint).
113
114
115 childproc = new TDEProcess();
116 childproc->setUseShell(true);
117
118 procLineMaker = new ProcessLineMaker( childproc );
119
120 connect( procLineMaker, TQ_SIGNAL(receivedStdoutLine(const TQCString&)),
121 this, TQ_SLOT(insertStdoutLine(const TQCString&) ));
122 connect( procLineMaker, TQ_SIGNAL(receivedStderrLine(const TQCString&)),
123 this, TQ_SLOT(insertStderrLine(const TQCString&) ));
124 connect( procLineMaker, TQ_SIGNAL(receivedPartialStdoutLine(const TQCString&)),
125 this, TQ_SLOT(addPartialStdoutLine(const TQCString&) ));
126 connect( procLineMaker, TQ_SIGNAL(receivedPartialStderrLine(const TQCString&)),
127 this, TQ_SLOT(addPartialStderrLine(const TQCString&) ));
128
129 connect(childproc, TQ_SIGNAL(processExited(TDEProcess*)),
130 this, TQ_SLOT(slotProcessExited(TDEProcess*) )) ;
131}
132
133
134ProcessWidget::~ProcessWidget()
135{
136 delete childproc;
137 delete procLineMaker;
138}
139
140
141void ProcessWidget::startJob(const TQString &dir, const TQString &command)
142{
143 procLineMaker->clearBuffers();
144 procLineMaker->blockSignals( false );
145
146 clear();
147 insertItem(new ProcessListBoxItem(command, ProcessListBoxItem::Diagnostic));
148 childproc->clearArguments();
149 if (!dir.isNull()) {
150 childproc->setWorkingDirectory( dir );
151 }
152
153 *childproc << command;
154 childproc->start(TDEProcess::OwnGroup, TDEProcess::AllOutput);
155}
156
157
158void ProcessWidget::killJob( int signo )
159{
160 procLineMaker->blockSignals( true );
161
162 childproc->kill( signo );
163}
164
165
166bool ProcessWidget::isRunning()
167{
168 return childproc->isRunning();
169}
170
171
172void ProcessWidget::slotProcessExited(TDEProcess *)
173{
174 procLineMaker->flush();
175 if( !stdoutbuf.isEmpty() )
176 insertStdoutLine("");
177 if( !stderrbuf.isEmpty() )
178 insertStderrLine("");
179 childFinished(childproc->normalExit(), childproc->exitStatus());
180 maybeScrollToBottom();
181 emit processExited(childproc);
182}
183
184
185void ProcessWidget::insertStdoutLine(const TQCString &line)
186{
187 if( !stdoutbuf.isEmpty() )
188 {
189 stdoutbuf += line;
190 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit(stdoutbuf),
191 ProcessListBoxItem::Normal ),
192 lastRowStdout+1 );
193 stdoutbuf.truncate( 0 );
194 }else
195 {
196 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit( line ),
197 ProcessListBoxItem::Normal) );
198 }
199 lastRowStdout = count() - 1;
200 maybeScrollToBottom();
201}
202
203
204void ProcessWidget::insertStderrLine(const TQCString &line)
205{
206 if( !stderrbuf.isEmpty() )
207 {
208 stderrbuf += line;
209 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit( stderrbuf ),
210 ProcessListBoxItem::Error ),
211 lastRowStderr+1 );
212 stderrbuf.truncate( 0 );
213 } else
214 {
215 insertItem( new ProcessListBoxItem( TQString::fromLocal8Bit( line ),
216 ProcessListBoxItem::Error) );
217 }
218 lastRowStderr = count() - 1;
219 maybeScrollToBottom();
220}
221
222
223void ProcessWidget::childFinished(bool normal, int status)
224{
225 TQString s;
226 ProcessListBoxItem::Type t;
227
228 if (normal) {
229 if (status) {
230 s = i18n("*** Exited with status: %1 ***").arg(status);
231 t = ProcessListBoxItem::Error;
232 } else {
233 s = i18n("*** Exited normally ***");
234 t = ProcessListBoxItem::Diagnostic;
235 }
236 } else {
237 if ( childproc->signalled() && childproc->exitSignal() == SIGSEGV )
238 {
239 s = i18n("*** Process aborted. Segmentation fault ***");
240 }
241 else
242 {
243 s = i18n("*** Process aborted ***");
244 }
245 t = ProcessListBoxItem::Error;
246 }
247
248 insertItem(new ProcessListBoxItem(s, t));
249}
250
251
252TQSize ProcessWidget::minimumSizeHint() const
253{
254 // I'm not sure about this, but when I don't use override minimumSizeHint(),
255 // the initial size in clearly too small
256
257 return TQSize( TQListBox::sizeHint().width(),
258 (fontMetrics().lineSpacing()+2)*4 );
259}
260
265void ProcessWidget::maybeScrollToBottom()
266{
267 if ( verticalScrollBar()->value() == verticalScrollBar()->maxValue() )
268 {
269 setBottomItem( count() -1 );
270 }
271}
272
273void ProcessWidget::addPartialStderrLine(const TQCString& linepart)
274{
275 stderrbuf += linepart;
276}
277
278void ProcessWidget::addPartialStdoutLine(const TQCString& linepart)
279{
280 stdoutbuf += linepart;
281}
282
283#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:141
ProcessWidget::insertStdoutLine
virtual void insertStdoutLine(const TQCString &line)
Inserts one line from stdin into the listbox.
Definition processwidget.cpp:185
ProcessWidget::childFinished
virtual void childFinished(bool normal, int status)
This is called when the child process exits.
Definition processwidget.cpp:223
ProcessWidget::insertStderrLine
virtual void insertStderrLine(const TQCString &line)
Inserts one line from stderr into the listbox.
Definition processwidget.cpp:204
ProcessWidget::isRunning
bool isRunning()
Returns whether a process is running in this view.
Definition processwidget.cpp:166
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:265
ProcessWidget::killJob
void killJob(int signo=SIGTERM)
Kills the child processss.
Definition processwidget.cpp:158
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.9.8
This website is maintained by Timothy Pearson.