1 |
capela |
94 |
// qsamplerMessages.cpp |
2 |
|
|
// |
3 |
|
|
/**************************************************************************** |
4 |
|
|
Copyright (C) 2003-2004, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or |
7 |
|
|
modify it under the terms of the GNU General Public License |
8 |
|
|
as published by the Free Software Foundation; either version 2 |
9 |
|
|
of the License, or (at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
|
20 |
|
|
*****************************************************************************/ |
21 |
|
|
|
22 |
|
|
#include "qsamplerMessages.h" |
23 |
|
|
|
24 |
|
|
#include <qsocketnotifier.h> |
25 |
|
|
#include <qstringlist.h> |
26 |
|
|
#include <qtextedit.h> |
27 |
|
|
#include <qdatetime.h> |
28 |
|
|
#include <qtooltip.h> |
29 |
|
|
#include <qpixmap.h> |
30 |
|
|
|
31 |
|
|
#include "config.h" |
32 |
|
|
|
33 |
capela |
115 |
#if !defined(WIN32) |
34 |
|
|
#include <unistd.h> |
35 |
|
|
#endif |
36 |
|
|
|
37 |
capela |
94 |
// The default maximum number of message lines. |
38 |
|
|
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
39 |
|
|
|
40 |
|
|
// Notification pipe descriptors |
41 |
|
|
#define QSAMPLER_MESSAGES_FDNIL -1 |
42 |
|
|
#define QSAMPLER_MESSAGES_FDREAD 0 |
43 |
|
|
#define QSAMPLER_MESSAGES_FDWRITE 1 |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
//------------------------------------------------------------------------- |
47 |
|
|
// qsamplerMessages - Messages log dockable window. |
48 |
|
|
// |
49 |
|
|
|
50 |
|
|
// Constructor. |
51 |
|
|
qsamplerMessages::qsamplerMessages ( QWidget *pParent, const char *pszName ) |
52 |
|
|
: QDockWindow(pParent, pszName) |
53 |
|
|
{ |
54 |
|
|
// Initialize default message limit. |
55 |
capela |
327 |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
56 |
capela |
94 |
|
57 |
|
|
// Intialize stdout capture stuff. |
58 |
|
|
m_pStdoutNotifier = NULL; |
59 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
60 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
61 |
|
|
|
62 |
|
|
// Surely a name is crucial (e.g.for storing geometry settings) |
63 |
|
|
if (pszName == 0) |
64 |
|
|
QDockWindow::setName("qsamplerMessages"); |
65 |
|
|
|
66 |
|
|
// Create local text view widget. |
67 |
|
|
m_pTextView = new QTextEdit(this); |
68 |
|
|
// QFont font(m_pTextView->font()); |
69 |
|
|
// font.setFamily("Fixed"); |
70 |
|
|
// m_pTextView->setFont(font); |
71 |
|
|
m_pTextView->setWordWrap(QTextEdit::NoWrap); |
72 |
|
|
m_pTextView->setReadOnly(true); |
73 |
|
|
m_pTextView->setUndoRedoEnabled(false); |
74 |
|
|
|
75 |
|
|
// Prepare the dockable window stuff. |
76 |
|
|
QDockWindow::setWidget(m_pTextView); |
77 |
|
|
QDockWindow::setOrientation(Qt::Horizontal); |
78 |
|
|
QDockWindow::setCloseMode(QDockWindow::Always); |
79 |
|
|
QDockWindow::setResizeEnabled(true); |
80 |
|
|
|
81 |
|
|
// Finally set the default caption and tooltip. |
82 |
|
|
QString sCaption = tr("Messages"); |
83 |
|
|
QToolTip::add(this, sCaption); |
84 |
|
|
QDockWindow::setCaption(sCaption); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
|
88 |
|
|
// Destructor. |
89 |
|
|
qsamplerMessages::~qsamplerMessages (void) |
90 |
|
|
{ |
91 |
|
|
// No more notifications. |
92 |
|
|
if (m_pStdoutNotifier) |
93 |
|
|
delete m_pStdoutNotifier; |
94 |
|
|
|
95 |
|
|
// No need to delete child widgets, Qt does it all for us. |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
// Own stdout/stderr socket notifier slot. |
100 |
|
|
void qsamplerMessages::stdoutNotify ( int fd ) |
101 |
|
|
{ |
102 |
|
|
#if !defined(WIN32) |
103 |
|
|
char achBuffer[1024]; |
104 |
|
|
int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1); |
105 |
|
|
if (cchBuffer > 0) { |
106 |
|
|
achBuffer[cchBuffer] = (char) 0; |
107 |
|
|
appendStdoutBuffer(achBuffer); |
108 |
|
|
} |
109 |
|
|
#endif |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
// Stdout buffer handler -- now splitted by complete new-lines... |
114 |
|
|
void qsamplerMessages::appendStdoutBuffer ( const QString& s ) |
115 |
|
|
{ |
116 |
|
|
m_sStdoutBuffer.append(s); |
117 |
|
|
|
118 |
|
|
int iLength = m_sStdoutBuffer.findRev('\n') + 1; |
119 |
|
|
if (iLength > 0) { |
120 |
|
|
QString sTemp = m_sStdoutBuffer.left(iLength); |
121 |
|
|
m_sStdoutBuffer.remove(0, iLength); |
122 |
|
|
QStringList list = QStringList::split('\n', sTemp, true); |
123 |
|
|
for (QStringList::Iterator iter = list.begin(); iter != list.end(); iter++) |
124 |
|
|
appendMessagesText(*iter); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
// Stdout flusher -- show up any unfinished line... |
130 |
|
|
void qsamplerMessages::flushStdoutBuffer (void) |
131 |
|
|
{ |
132 |
|
|
if (!m_sStdoutBuffer.isEmpty()) { |
133 |
|
|
appendMessagesText(m_sStdoutBuffer); |
134 |
|
|
m_sStdoutBuffer.truncate(0); |
135 |
|
|
} |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
|
139 |
|
|
// Stdout capture accessors. |
140 |
|
|
bool qsamplerMessages::isCaptureEnabled (void) |
141 |
|
|
{ |
142 |
|
|
return (bool) (m_pStdoutNotifier != NULL); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
void qsamplerMessages::setCaptureEnabled ( bool bCapture ) |
146 |
|
|
{ |
147 |
|
|
// Flush current buffer. |
148 |
|
|
flushStdoutBuffer(); |
149 |
|
|
|
150 |
|
|
#if !defined(WIN32) |
151 |
|
|
// Destroy if already enabled. |
152 |
|
|
if (!bCapture && m_pStdoutNotifier) { |
153 |
|
|
delete m_pStdoutNotifier; |
154 |
|
|
m_pStdoutNotifier = NULL; |
155 |
|
|
// Close the notification pipes. |
156 |
|
|
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
157 |
|
|
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
158 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
159 |
|
|
} |
160 |
|
|
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
161 |
|
|
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
162 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
// Are we going to make up the capture? |
166 |
|
|
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
167 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
168 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
169 |
|
|
m_pStdoutNotifier = new QSocketNotifier(m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
170 |
|
|
QObject::connect(m_pStdoutNotifier, SIGNAL(activated(int)), this, SLOT(stdoutNotify(int))); |
171 |
|
|
} |
172 |
|
|
#endif |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
// Message font accessors. |
177 |
|
|
QFont qsamplerMessages::messagesFont (void) |
178 |
|
|
{ |
179 |
|
|
return m_pTextView->font(); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
void qsamplerMessages::setMessagesFont( const QFont& font ) |
183 |
|
|
{ |
184 |
|
|
m_pTextView->setFont(font); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
|
// Maximum number of message lines accessors. |
189 |
|
|
int qsamplerMessages::messagesLimit (void) |
190 |
|
|
{ |
191 |
|
|
return m_iMessagesLimit; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
void qsamplerMessages::setMessagesLimit ( int iMessagesLimit ) |
195 |
|
|
{ |
196 |
|
|
m_iMessagesLimit = iMessagesLimit; |
197 |
capela |
327 |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
198 |
capela |
94 |
} |
199 |
|
|
|
200 |
|
|
|
201 |
|
|
// The main utility methods. |
202 |
|
|
void qsamplerMessages::appendMessages ( const QString& s ) |
203 |
|
|
{ |
204 |
|
|
appendMessagesColor(s, "#999999"); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c ) |
208 |
|
|
{ |
209 |
|
|
appendMessagesText("<font color=\"" + c + "\">" + QTime::currentTime().toString("hh:mm:ss.zzz") + " " + s + "</font>"); |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
void qsamplerMessages::appendMessagesText ( const QString& s ) |
213 |
|
|
{ |
214 |
|
|
// Check for message line limit... |
215 |
|
|
if (m_iMessagesLimit > 0) { |
216 |
|
|
int iParagraphs = m_pTextView->paragraphs(); |
217 |
capela |
327 |
if (iParagraphs > m_iMessagesHigh) { |
218 |
capela |
94 |
m_pTextView->setUpdatesEnabled(false); |
219 |
|
|
while (iParagraphs > m_iMessagesLimit) { |
220 |
|
|
m_pTextView->removeParagraph(0); |
221 |
|
|
iParagraphs--; |
222 |
|
|
} |
223 |
|
|
m_pTextView->scrollToBottom(); |
224 |
|
|
m_pTextView->setUpdatesEnabled(true); |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
m_pTextView->append(s); |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
|
|
// One time scroll to the most recent message. |
232 |
|
|
void qsamplerMessages::scrollToBottom (void) |
233 |
|
|
{ |
234 |
|
|
flushStdoutBuffer(); |
235 |
|
|
m_pTextView->scrollToBottom(); |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
|
239 |
|
|
// end of qsamplerMessages.cpp |