1 |
// qsamplerMessages.cpp |
// qsamplerMessages.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2004-2006, rncbc aka Rui Nuno Capela. All rights reserved. |
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 |
This program is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU General Public License |
modify it under the terms of the GNU General Public License |
20 |
|
|
21 |
*****************************************************************************/ |
*****************************************************************************/ |
22 |
|
|
23 |
|
#include "qsamplerAbout.h" |
24 |
#include "qsamplerMessages.h" |
#include "qsamplerMessages.h" |
25 |
|
|
26 |
#include <qsocketnotifier.h> |
#include <QSocketNotifier> |
27 |
#include <qstringlist.h> |
#include <QTextEdit> |
28 |
#include <qtextedit.h> |
#include <QTextCursor> |
29 |
#include <qdatetime.h> |
#include <QTextBlock> |
30 |
#include <qtooltip.h> |
#include <QScrollBar> |
31 |
#include <qpixmap.h> |
#include <QDateTime> |
32 |
|
#include <QIcon> |
|
#include "config.h" |
|
33 |
|
|
34 |
#if !defined(WIN32) |
#if !defined(WIN32) |
35 |
#include <unistd.h> |
#include <unistd.h> |
49 |
// |
// |
50 |
|
|
51 |
// Constructor. |
// Constructor. |
52 |
qsamplerMessages::qsamplerMessages ( QWidget *pParent, const char *pszName ) |
qsamplerMessages::qsamplerMessages ( QWidget *pParent ) |
53 |
: QDockWindow(pParent, pszName) |
: QDockWidget(pParent) |
54 |
{ |
{ |
55 |
|
// Surely a name is crucial (e.g.for storing geometry settings) |
56 |
|
QDockWidget::setObjectName("qsamplerMessages"); |
57 |
|
|
58 |
// Intialize stdout capture stuff. |
// Intialize stdout capture stuff. |
59 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = NULL; |
60 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
61 |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
62 |
|
|
63 |
// Surely a name is crucial (e.g.for storing geometry settings) |
// Create local text view widget. |
64 |
if (pszName == 0) |
m_pTextView = new QTextEdit(this); |
|
QDockWindow::setName("qsamplerMessages"); |
|
|
|
|
|
// Create local text view widget. |
|
|
m_pTextView = new QTextEdit(this); |
|
65 |
// QFont font(m_pTextView->font()); |
// QFont font(m_pTextView->font()); |
66 |
// font.setFamily("Fixed"); |
// font.setFamily("Fixed"); |
67 |
// m_pTextView->setFont(font); |
// m_pTextView->setFont(font); |
68 |
m_pTextView->setWordWrap(QTextEdit::NoWrap); |
m_pTextView->setLineWrapMode(QTextEdit::NoWrap); |
69 |
m_pTextView->setReadOnly(true); |
m_pTextView->setReadOnly(true); |
70 |
m_pTextView->setUndoRedoEnabled(false); |
m_pTextView->setUndoRedoEnabled(false); |
71 |
#if QT_VERSION >= 0x030200 |
// m_pTextView->setTextFormat(Qt::LogText); |
72 |
m_pTextView->setTextFormat(Qt::LogText); |
|
73 |
#endif |
// Initialize default message limit. |
74 |
// Initialize default message limit. |
m_iMessagesLines = 0; |
75 |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
76 |
|
|
77 |
// Prepare the dockable window stuff. |
// Prepare the dockable window stuff. |
78 |
QDockWindow::setWidget(m_pTextView); |
QDockWidget::setWidget(m_pTextView); |
79 |
QDockWindow::setOrientation(Qt::Horizontal); |
QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
80 |
QDockWindow::setCloseMode(QDockWindow::Always); |
QDockWidget::setAllowedAreas( |
81 |
QDockWindow::setResizeEnabled(true); |
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
82 |
// Some specialties to this kind of dock window... |
// Some specialties to this kind of dock window... |
83 |
QDockWindow::setFixedExtentHeight(120); |
QDockWidget::setMinimumHeight(120); |
84 |
|
|
85 |
// Finally set the default caption and tooltip. |
// Finally set the default caption and tooltip. |
86 |
QString sCaption = tr("Messages"); |
const QString& sCaption = tr("Messages"); |
87 |
QToolTip::add(this, sCaption); |
QDockWidget::setWindowTitle(sCaption); |
88 |
QDockWindow::setCaption(sCaption); |
// QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png")); |
89 |
|
QDockWidget::setToolTip(sCaption); |
90 |
} |
} |
91 |
|
|
92 |
|
|
118 |
// Stdout buffer handler -- now splitted by complete new-lines... |
// Stdout buffer handler -- now splitted by complete new-lines... |
119 |
void qsamplerMessages::appendStdoutBuffer ( const QString& s ) |
void qsamplerMessages::appendStdoutBuffer ( const QString& s ) |
120 |
{ |
{ |
121 |
m_sStdoutBuffer.append(s); |
m_sStdoutBuffer.append(s); |
122 |
|
|
123 |
int iLength = m_sStdoutBuffer.findRev('\n') + 1; |
int iLength = m_sStdoutBuffer.lastIndexOf('\n') + 1; |
124 |
if (iLength > 0) { |
if (iLength > 0) { |
125 |
QString sTemp = m_sStdoutBuffer.left(iLength); |
QString sTemp = m_sStdoutBuffer.left(iLength); |
126 |
m_sStdoutBuffer.remove(0, iLength); |
m_sStdoutBuffer.remove(0, iLength); |
127 |
QStringList list = QStringList::split('\n', sTemp, true); |
QStringList list = sTemp.split('\n'); |
128 |
for (QStringList::Iterator iter = list.begin(); iter != list.end(); iter++) |
QStringListIterator iter(list); |
129 |
appendMessagesText(*iter); |
while (iter.hasNext()) |
130 |
} |
appendMessagesText(iter.next()); |
131 |
|
} |
132 |
} |
} |
133 |
|
|
134 |
|
|
154 |
flushStdoutBuffer(); |
flushStdoutBuffer(); |
155 |
|
|
156 |
#if !defined(WIN32) |
#if !defined(WIN32) |
157 |
// Destroy if already enabled. |
// Destroy if already enabled. |
158 |
if (!bCapture && m_pStdoutNotifier) { |
if (!bCapture && m_pStdoutNotifier) { |
159 |
delete m_pStdoutNotifier; |
delete m_pStdoutNotifier; |
160 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = NULL; |
161 |
// Close the notification pipes. |
// Close the notification pipes. |
162 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
163 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
164 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
165 |
} |
} |
166 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
167 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
168 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
169 |
} |
} |
170 |
} |
} |
171 |
// Are we going to make up the capture? |
// Are we going to make up the capture? |
172 |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
173 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
174 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
175 |
m_pStdoutNotifier = new QSocketNotifier(m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
m_pStdoutNotifier = new QSocketNotifier( |
176 |
QObject::connect(m_pStdoutNotifier, SIGNAL(activated(int)), this, SLOT(stdoutNotify(int))); |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
177 |
} |
QObject::connect(m_pStdoutNotifier, |
178 |
|
SIGNAL(activated(int)), |
179 |
|
SLOT(stdoutNotify(int))); |
180 |
|
} |
181 |
#endif |
#endif |
182 |
} |
} |
183 |
|
|
204 |
{ |
{ |
205 |
m_iMessagesLimit = iMessagesLimit; |
m_iMessagesLimit = iMessagesLimit; |
206 |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
|
#if QT_VERSION >= 0x030200 |
|
|
m_pTextView->setMaxLogLines(iMessagesLimit); |
|
|
#endif |
|
207 |
} |
} |
208 |
|
|
209 |
|
|
215 |
|
|
216 |
void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c ) |
void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c ) |
217 |
{ |
{ |
218 |
appendMessagesText("<font color=\"" + c + "\">" + QTime::currentTime().toString("hh:mm:ss.zzz") + " " + s + "</font>"); |
appendMessagesText("<font color=\"" + c + "\">" |
219 |
|
+ QTime::currentTime().toString("hh:mm:ss.zzz") |
220 |
|
+ ' ' + s + "</font>"); |
221 |
} |
} |
222 |
|
|
223 |
void qsamplerMessages::appendMessagesText ( const QString& s ) |
void qsamplerMessages::appendMessagesText ( const QString& s ) |
224 |
{ |
{ |
|
#if QT_VERSION < 0x030200 |
|
225 |
// Check for message line limit... |
// Check for message line limit... |
226 |
if (m_iMessagesLimit > 0) { |
if (m_iMessagesLines > m_iMessagesHigh) { |
227 |
int iParagraphs = m_pTextView->paragraphs(); |
m_pTextView->setUpdatesEnabled(false); |
228 |
if (iParagraphs > m_iMessagesHigh) { |
QTextCursor textCursor(m_pTextView->document()->begin()); |
229 |
m_pTextView->setUpdatesEnabled(false); |
while (m_iMessagesLines > m_iMessagesLimit) { |
230 |
while (iParagraphs > m_iMessagesLimit) { |
// Move cursor extending selection |
231 |
m_pTextView->removeParagraph(0); |
// from start to next line-block... |
232 |
iParagraphs--; |
textCursor.movePosition( |
233 |
} |
QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
234 |
m_pTextView->scrollToBottom(); |
m_iMessagesLines--; |
235 |
m_pTextView->setUpdatesEnabled(true); |
} |
236 |
} |
// Remove the excessive line-blocks... |
237 |
} |
textCursor.removeSelectedText(); |
238 |
#endif |
m_pTextView->setUpdatesEnabled(true); |
239 |
m_pTextView->append(s); |
} |
240 |
|
|
241 |
|
// Count always as a new line out there... |
242 |
|
m_pTextView->append(s); |
243 |
|
m_iMessagesLines++; |
244 |
} |
} |
245 |
|
|
246 |
|
|
247 |
// One time scroll to the most recent message. |
// History reset. |
248 |
void qsamplerMessages::scrollToBottom (void) |
void qsamplerMessages::clear (void) |
249 |
{ |
{ |
250 |
flushStdoutBuffer(); |
m_iMessagesLines = 0; |
251 |
m_pTextView->scrollToBottom(); |
m_pTextView->clear(); |
252 |
} |
} |
253 |
|
|
254 |
|
|