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