1 |
capela |
94 |
// qsamplerMessages.cpp |
2 |
|
|
// |
3 |
|
|
/**************************************************************************** |
4 |
schoenebeck |
1461 |
Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
capela |
1464 |
Copyright (C) 2007, Christian Schoenebeck |
6 |
capela |
94 |
|
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 |
capela |
920 |
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 |
capela |
94 |
|
21 |
|
|
*****************************************************************************/ |
22 |
|
|
|
23 |
capela |
1465 |
#include "qsamplerAbout.h" |
24 |
capela |
94 |
#include "qsamplerMessages.h" |
25 |
|
|
|
26 |
capela |
1465 |
#include <QSocketNotifier> |
27 |
|
|
#include <QTextEdit> |
28 |
|
|
#include <QTextCursor> |
29 |
|
|
#include <QTextBlock> |
30 |
schoenebeck |
1461 |
#include <QScrollBar> |
31 |
capela |
1465 |
#include <QDateTime> |
32 |
|
|
#include <QIcon> |
33 |
schoenebeck |
1461 |
|
34 |
capela |
115 |
#if !defined(WIN32) |
35 |
|
|
#include <unistd.h> |
36 |
|
|
#endif |
37 |
|
|
|
38 |
capela |
94 |
// The default maximum number of message lines. |
39 |
|
|
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
40 |
|
|
|
41 |
|
|
// Notification pipe descriptors |
42 |
|
|
#define QSAMPLER_MESSAGES_FDNIL -1 |
43 |
|
|
#define QSAMPLER_MESSAGES_FDREAD 0 |
44 |
|
|
#define QSAMPLER_MESSAGES_FDWRITE 1 |
45 |
|
|
|
46 |
|
|
|
47 |
|
|
//------------------------------------------------------------------------- |
48 |
|
|
// qsamplerMessages - Messages log dockable window. |
49 |
|
|
// |
50 |
|
|
|
51 |
|
|
// Constructor. |
52 |
capela |
1465 |
qsamplerMessages::qsamplerMessages ( QWidget *pParent ) |
53 |
|
|
: QDockWidget(pParent) |
54 |
capela |
94 |
{ |
55 |
capela |
1465 |
// Surely a name is crucial (e.g.for storing geometry settings) |
56 |
|
|
QDockWidget::setObjectName("qsamplerMessages"); |
57 |
|
|
|
58 |
capela |
94 |
// 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 |
capela |
1465 |
// Create local text view widget. |
64 |
|
|
m_pTextView = new QTextEdit(this); |
65 |
capela |
94 |
// QFont font(m_pTextView->font()); |
66 |
|
|
// font.setFamily("Fixed"); |
67 |
|
|
// m_pTextView->setFont(font); |
68 |
capela |
1465 |
m_pTextView->setLineWrapMode(QTextEdit::NoWrap); |
69 |
|
|
m_pTextView->setReadOnly(true); |
70 |
|
|
m_pTextView->setUndoRedoEnabled(false); |
71 |
|
|
// m_pTextView->setTextFormat(Qt::LogText); |
72 |
capela |
94 |
|
73 |
capela |
1465 |
// Initialize default message limit. |
74 |
|
|
m_iMessagesLines = 0; |
75 |
|
|
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
76 |
|
|
|
77 |
|
|
// Prepare the dockable window stuff. |
78 |
|
|
QDockWidget::setWidget(m_pTextView); |
79 |
|
|
QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
80 |
|
|
QDockWidget::setAllowedAreas( |
81 |
|
|
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
82 |
capela |
772 |
// Some specialties to this kind of dock window... |
83 |
capela |
1465 |
QDockWidget::setMinimumHeight(120); |
84 |
capela |
94 |
|
85 |
capela |
1465 |
// Finally set the default caption and tooltip. |
86 |
|
|
const QString& sCaption = tr("Messages"); |
87 |
|
|
QDockWidget::setWindowTitle(sCaption); |
88 |
|
|
// QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png")); |
89 |
|
|
QDockWidget::setToolTip(sCaption); |
90 |
capela |
94 |
} |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
// Destructor. |
94 |
|
|
qsamplerMessages::~qsamplerMessages (void) |
95 |
|
|
{ |
96 |
|
|
// No more notifications. |
97 |
|
|
if (m_pStdoutNotifier) |
98 |
|
|
delete m_pStdoutNotifier; |
99 |
|
|
|
100 |
|
|
// No need to delete child widgets, Qt does it all for us. |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
// Own stdout/stderr socket notifier slot. |
105 |
|
|
void qsamplerMessages::stdoutNotify ( int fd ) |
106 |
|
|
{ |
107 |
|
|
#if !defined(WIN32) |
108 |
|
|
char achBuffer[1024]; |
109 |
|
|
int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1); |
110 |
|
|
if (cchBuffer > 0) { |
111 |
|
|
achBuffer[cchBuffer] = (char) 0; |
112 |
|
|
appendStdoutBuffer(achBuffer); |
113 |
|
|
} |
114 |
|
|
#endif |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
// Stdout buffer handler -- now splitted by complete new-lines... |
119 |
|
|
void qsamplerMessages::appendStdoutBuffer ( const QString& s ) |
120 |
|
|
{ |
121 |
capela |
1465 |
m_sStdoutBuffer.append(s); |
122 |
capela |
94 |
|
123 |
capela |
1465 |
int iLength = m_sStdoutBuffer.lastIndexOf('\n') + 1; |
124 |
|
|
if (iLength > 0) { |
125 |
|
|
QString sTemp = m_sStdoutBuffer.left(iLength); |
126 |
|
|
m_sStdoutBuffer.remove(0, iLength); |
127 |
|
|
QStringList list = sTemp.split('\n'); |
128 |
|
|
QStringListIterator iter(list); |
129 |
|
|
while (iter.hasNext()) |
130 |
|
|
appendMessagesText(iter.next()); |
131 |
|
|
} |
132 |
capela |
94 |
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
// Stdout flusher -- show up any unfinished line... |
136 |
|
|
void qsamplerMessages::flushStdoutBuffer (void) |
137 |
|
|
{ |
138 |
|
|
if (!m_sStdoutBuffer.isEmpty()) { |
139 |
|
|
appendMessagesText(m_sStdoutBuffer); |
140 |
|
|
m_sStdoutBuffer.truncate(0); |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
// Stdout capture accessors. |
146 |
|
|
bool qsamplerMessages::isCaptureEnabled (void) |
147 |
|
|
{ |
148 |
|
|
return (bool) (m_pStdoutNotifier != NULL); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
void qsamplerMessages::setCaptureEnabled ( bool bCapture ) |
152 |
|
|
{ |
153 |
|
|
// Flush current buffer. |
154 |
|
|
flushStdoutBuffer(); |
155 |
|
|
|
156 |
|
|
#if !defined(WIN32) |
157 |
capela |
1465 |
// Destroy if already enabled. |
158 |
|
|
if (!bCapture && m_pStdoutNotifier) { |
159 |
|
|
delete m_pStdoutNotifier; |
160 |
|
|
m_pStdoutNotifier = NULL; |
161 |
|
|
// Close the notification pipes. |
162 |
|
|
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
163 |
|
|
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
164 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
165 |
|
|
} |
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 |
|
|
} |
171 |
|
|
// Are we going to make up the capture? |
172 |
|
|
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
173 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
174 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
175 |
|
|
m_pStdoutNotifier = new QSocketNotifier( |
176 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
177 |
|
|
QObject::connect(m_pStdoutNotifier, |
178 |
|
|
SIGNAL(activated(int)), |
179 |
|
|
SLOT(stdoutNotify(int))); |
180 |
|
|
} |
181 |
capela |
94 |
#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 |
capela |
327 |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
207 |
capela |
94 |
} |
208 |
|
|
|
209 |
|
|
|
210 |
|
|
// The main utility methods. |
211 |
|
|
void qsamplerMessages::appendMessages ( const QString& s ) |
212 |
|
|
{ |
213 |
|
|
appendMessagesColor(s, "#999999"); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c ) |
217 |
|
|
{ |
218 |
capela |
1465 |
appendMessagesText("<font color=\"" + c + "\">" |
219 |
|
|
+ QTime::currentTime().toString("hh:mm:ss.zzz") |
220 |
|
|
+ ' ' + s + "</font>"); |
221 |
capela |
94 |
} |
222 |
|
|
|
223 |
|
|
void qsamplerMessages::appendMessagesText ( const QString& s ) |
224 |
|
|
{ |
225 |
|
|
// Check for message line limit... |
226 |
capela |
1465 |
if (m_iMessagesLines > m_iMessagesHigh) { |
227 |
|
|
m_pTextView->setUpdatesEnabled(false); |
228 |
|
|
QTextCursor textCursor(m_pTextView->document()->begin()); |
229 |
|
|
while (m_iMessagesLines > m_iMessagesLimit) { |
230 |
|
|
// Move cursor extending selection |
231 |
|
|
// from start to next line-block... |
232 |
|
|
textCursor.movePosition( |
233 |
|
|
QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
234 |
|
|
m_iMessagesLines--; |
235 |
|
|
} |
236 |
|
|
// Remove the excessive line-blocks... |
237 |
|
|
textCursor.removeSelectedText(); |
238 |
|
|
m_pTextView->setUpdatesEnabled(true); |
239 |
capela |
94 |
} |
240 |
capela |
1465 |
|
241 |
|
|
// Count always as a new line out there... |
242 |
|
|
m_pTextView->append(s); |
243 |
|
|
m_iMessagesLines++; |
244 |
capela |
94 |
} |
245 |
|
|
|
246 |
|
|
|
247 |
capela |
1465 |
// History reset. |
248 |
|
|
void qsamplerMessages::clear (void) |
249 |
capela |
94 |
{ |
250 |
capela |
1465 |
m_iMessagesLines = 0; |
251 |
|
|
m_pTextView->clear(); |
252 |
capela |
94 |
} |
253 |
|
|
|
254 |
|
|
|
255 |
|
|
// end of qsamplerMessages.cpp |