1 |
// qsamplerMessages.cpp |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2004-2007, 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 along |
17 |
with this program; if not, write to the Free Software Foundation, Inc., |
18 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <QDockWidget> |
32 |
#include <QScrollBar> |
33 |
|
34 |
#include "config.h" |
35 |
|
36 |
#if !defined(WIN32) |
37 |
#include <unistd.h> |
38 |
#endif |
39 |
|
40 |
// The default maximum number of message lines. |
41 |
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
42 |
|
43 |
// Notification pipe descriptors |
44 |
#define QSAMPLER_MESSAGES_FDNIL -1 |
45 |
#define QSAMPLER_MESSAGES_FDREAD 0 |
46 |
#define QSAMPLER_MESSAGES_FDWRITE 1 |
47 |
|
48 |
|
49 |
//------------------------------------------------------------------------- |
50 |
// qsamplerMessages - Messages log dockable window. |
51 |
// |
52 |
|
53 |
// Constructor. |
54 |
qsamplerMessages::qsamplerMessages ( QWidget *pParent, const char *pszName ) |
55 |
: QDockWidget(pszName, pParent) |
56 |
{ |
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 |
QDockWidget::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->setWordWrapMode(QTextOption::NoWrap); |
72 |
m_pTextView->setReadOnly(true); |
73 |
m_pTextView->setUndoRedoEnabled(false); |
74 |
#if QT_VERSION >= 0x030200 |
75 |
m_pTextView->setTextFormat(Qt::LogText); |
76 |
#endif |
77 |
// Initialize default message limit. |
78 |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
79 |
|
80 |
// Prepare the dockable window stuff. |
81 |
QDockWidget::setWidget(m_pTextView); |
82 |
//QDockWidget::setOrientation(Qt::Horizontal); |
83 |
QDockWidget::setFeatures( |
84 |
QDockWidget::DockWidgetClosable |
85 |
); |
86 |
//QDockWidget::setResizeEnabled(true); |
87 |
// Some specialties to this kind of dock window... |
88 |
//QDockWidget::setFixedExtentHeight(120); |
89 |
|
90 |
// Finally set the default caption and tooltip. |
91 |
QString sCaption = tr("Messages"); |
92 |
QToolTip::add(this, sCaption); |
93 |
QDockWidget::setWindowIconText(sCaption); |
94 |
} |
95 |
|
96 |
|
97 |
// Destructor. |
98 |
qsamplerMessages::~qsamplerMessages (void) |
99 |
{ |
100 |
// No more notifications. |
101 |
if (m_pStdoutNotifier) |
102 |
delete m_pStdoutNotifier; |
103 |
|
104 |
// No need to delete child widgets, Qt does it all for us. |
105 |
} |
106 |
|
107 |
|
108 |
// Own stdout/stderr socket notifier slot. |
109 |
void qsamplerMessages::stdoutNotify ( int fd ) |
110 |
{ |
111 |
#if !defined(WIN32) |
112 |
char achBuffer[1024]; |
113 |
int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1); |
114 |
if (cchBuffer > 0) { |
115 |
achBuffer[cchBuffer] = (char) 0; |
116 |
appendStdoutBuffer(achBuffer); |
117 |
} |
118 |
#endif |
119 |
} |
120 |
|
121 |
|
122 |
// Stdout buffer handler -- now splitted by complete new-lines... |
123 |
void qsamplerMessages::appendStdoutBuffer ( const QString& s ) |
124 |
{ |
125 |
m_sStdoutBuffer.append(s); |
126 |
|
127 |
int iLength = m_sStdoutBuffer.findRev('\n') + 1; |
128 |
if (iLength > 0) { |
129 |
QString sTemp = m_sStdoutBuffer.left(iLength); |
130 |
m_sStdoutBuffer.remove(0, iLength); |
131 |
QStringList list = QStringList::split('\n', sTemp, true); |
132 |
for (QStringList::Iterator iter = list.begin(); iter != list.end(); iter++) |
133 |
appendMessagesText(*iter); |
134 |
} |
135 |
} |
136 |
|
137 |
|
138 |
// Stdout flusher -- show up any unfinished line... |
139 |
void qsamplerMessages::flushStdoutBuffer (void) |
140 |
{ |
141 |
if (!m_sStdoutBuffer.isEmpty()) { |
142 |
appendMessagesText(m_sStdoutBuffer); |
143 |
m_sStdoutBuffer.truncate(0); |
144 |
} |
145 |
} |
146 |
|
147 |
|
148 |
// Stdout capture accessors. |
149 |
bool qsamplerMessages::isCaptureEnabled (void) |
150 |
{ |
151 |
return (bool) (m_pStdoutNotifier != NULL); |
152 |
} |
153 |
|
154 |
void qsamplerMessages::setCaptureEnabled ( bool bCapture ) |
155 |
{ |
156 |
// Flush current buffer. |
157 |
flushStdoutBuffer(); |
158 |
|
159 |
#if !defined(WIN32) |
160 |
// Destroy if already enabled. |
161 |
if (!bCapture && m_pStdoutNotifier) { |
162 |
delete m_pStdoutNotifier; |
163 |
m_pStdoutNotifier = NULL; |
164 |
// Close the notification pipes. |
165 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
166 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
167 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
168 |
} |
169 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
170 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
171 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
172 |
} |
173 |
} |
174 |
// Are we going to make up the capture? |
175 |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
176 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
177 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
178 |
m_pStdoutNotifier = new QSocketNotifier(m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
179 |
QObject::connect(m_pStdoutNotifier, SIGNAL(activated(int)), this, SLOT(stdoutNotify(int))); |
180 |
} |
181 |
#endif |
182 |
} |
183 |
|
184 |
|
185 |
// Message font accessors. |
186 |
QFont qsamplerMessages::messagesFont (void) |
187 |
{ |
188 |
return m_pTextView->font(); |
189 |
} |
190 |
|
191 |
void qsamplerMessages::setMessagesFont( const QFont& font ) |
192 |
{ |
193 |
m_pTextView->setFont(font); |
194 |
} |
195 |
|
196 |
|
197 |
// Maximum number of message lines accessors. |
198 |
int qsamplerMessages::messagesLimit (void) |
199 |
{ |
200 |
return m_iMessagesLimit; |
201 |
} |
202 |
|
203 |
void qsamplerMessages::setMessagesLimit ( int iMessagesLimit ) |
204 |
{ |
205 |
m_iMessagesLimit = iMessagesLimit; |
206 |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
207 |
#if QT_VERSION >= 0x030200 |
208 |
//m_pTextView->setMaxLogLines(iMessagesLimit); |
209 |
#endif |
210 |
} |
211 |
|
212 |
|
213 |
// The main utility methods. |
214 |
void qsamplerMessages::appendMessages ( const QString& s ) |
215 |
{ |
216 |
appendMessagesColor(s, "#999999"); |
217 |
} |
218 |
|
219 |
void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c ) |
220 |
{ |
221 |
appendMessagesText("<font color=\"" + c + "\">" + QTime::currentTime().toString("hh:mm:ss.zzz") + " " + s + "</font>"); |
222 |
} |
223 |
|
224 |
void qsamplerMessages::appendMessagesText ( const QString& s ) |
225 |
{ |
226 |
#if QT_VERSION < 0x030200 |
227 |
// Check for message line limit... |
228 |
if (m_iMessagesLimit > 0) { |
229 |
int iParagraphs = m_pTextView->paragraphs(); |
230 |
if (iParagraphs > m_iMessagesHigh) { |
231 |
m_pTextView->setUpdatesEnabled(false); |
232 |
while (iParagraphs > m_iMessagesLimit) { |
233 |
m_pTextView->removeParagraph(0); |
234 |
iParagraphs--; |
235 |
} |
236 |
m_pTextView->scrollToBottom(); |
237 |
m_pTextView->setUpdatesEnabled(true); |
238 |
} |
239 |
} |
240 |
#endif |
241 |
m_pTextView->append(s); |
242 |
} |
243 |
|
244 |
|
245 |
// One time scroll to the most recent message. |
246 |
void qsamplerMessages::scrollToBottom (void) |
247 |
{ |
248 |
flushStdoutBuffer(); |
249 |
//m_pTextView->scrollToBottom(); |
250 |
m_pTextView->verticalScrollBar()->setValue(m_pTextView->verticalScrollBar()->maximum()); |
251 |
} |
252 |
|
253 |
|
254 |
// end of qsamplerMessages.cpp |