/[svn]/qsampler/trunk/src/qsamplerMessages.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerMessages.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2441 - (show annotations) (download)
Wed Apr 10 09:11:37 2013 UTC (7 years, 1 month ago) by capela
File size: 8501 byte(s)
* Preparations for Qt5 migration. (FIXING)
1 // qsamplerMessages.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2013, 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 "qsamplerAbout.h"
24 #include "qsamplerMessages.h"
25
26 #include <QSocketNotifier>
27
28 #include <QFile>
29 #include <QTextEdit>
30 #include <QTextCursor>
31 #include <QTextStream>
32 #include <QTextBlock>
33 #include <QScrollBar>
34 #include <QDateTime>
35 #include <QIcon>
36
37 #if !defined(WIN32)
38 #include <unistd.h>
39 #endif
40
41
42 namespace QSampler {
43
44 // The default maximum number of message lines.
45 #define QSAMPLER_MESSAGES_MAXLINES 1000
46
47 // Notification pipe descriptors
48 #define QSAMPLER_MESSAGES_FDNIL -1
49 #define QSAMPLER_MESSAGES_FDREAD 0
50 #define QSAMPLER_MESSAGES_FDWRITE 1
51
52 //-------------------------------------------------------------------------
53 // QSampler::Messages - Messages log dockable window.
54 //
55
56 // Constructor.
57 Messages::Messages ( QWidget *pParent )
58 : QDockWidget(pParent)
59 {
60 // Surely a name is crucial (e.g.for storing geometry settings)
61 QDockWidget::setObjectName("qsamplerMessages");
62
63 // Intialize stdout capture stuff.
64 m_pStdoutNotifier = NULL;
65 m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL;
66 m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;
67
68 // Create local text view widget.
69 m_pMessagesTextView = new QTextEdit(this);
70 // QFont font(m_pMessagesTextView->font());
71 // font.setFamily("Fixed");
72 // m_pMessagesTextView->setFont(font);
73 m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap);
74 m_pMessagesTextView->setReadOnly(true);
75 m_pMessagesTextView->setUndoRedoEnabled(false);
76 // m_pMessagesTextView->setTextFormat(Qt::LogText);
77
78 // Initialize default message limit.
79 m_iMessagesLines = 0;
80 setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);
81
82 m_pMessagesLog = NULL;
83
84 // Prepare the dockable window stuff.
85 QDockWidget::setWidget(m_pMessagesTextView);
86 QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);
87 QDockWidget::setAllowedAreas(
88 Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
89 // Some specialties to this kind of dock window...
90 QDockWidget::setMinimumHeight(120);
91
92 // Finally set the default caption and tooltip.
93 const QString& sCaption = tr("Messages");
94 QDockWidget::setWindowTitle(sCaption);
95 // QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png"));
96 QDockWidget::setToolTip(sCaption);
97 }
98
99
100 // Destructor.
101 Messages::~Messages (void)
102 {
103 // Turn off and close logging.
104 setLogging(false);
105
106 // No more notifications.
107 if (m_pStdoutNotifier)
108 delete m_pStdoutNotifier;
109
110 // No need to delete child widgets, Qt does it all for us.
111 }
112
113
114 #if QT_VERSION < 0x040300
115 void Messages::showEvent ( QShowEvent *pEvent )
116 {
117 QDockWidget::showEvent(pEvent);
118 emit visibilityChanged(isVisible());
119 }
120 #endif
121
122 // Own stdout/stderr socket notifier slot.
123 void Messages::stdoutNotify ( int fd )
124 {
125 #if !defined(WIN32)
126 char achBuffer[1024];
127 int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1);
128 if (cchBuffer > 0) {
129 achBuffer[cchBuffer] = (char) 0;
130 appendStdoutBuffer(achBuffer);
131 }
132 #endif
133 }
134
135
136 // Stdout buffer handler -- now splitted by complete new-lines...
137 void Messages::appendStdoutBuffer ( const QString& s )
138 {
139 m_sStdoutBuffer.append(s);
140
141 int iLength = m_sStdoutBuffer.lastIndexOf('\n');
142 if (iLength > 0) {
143 QString sTemp = m_sStdoutBuffer.left(iLength);
144 m_sStdoutBuffer.remove(0, iLength + 1);
145 QStringList list = sTemp.split('\n');
146 QStringListIterator iter(list);
147 while (iter.hasNext())
148 appendMessagesText(iter.next());
149 }
150 }
151
152
153 // Stdout flusher -- show up any unfinished line...
154 void Messages::flushStdoutBuffer (void)
155 {
156 if (!m_sStdoutBuffer.isEmpty()) {
157 appendMessagesText(m_sStdoutBuffer);
158 m_sStdoutBuffer.truncate(0);
159 }
160 }
161
162
163 // Stdout capture accessors.
164 bool Messages::isCaptureEnabled (void)
165 {
166 return (bool) (m_pStdoutNotifier != NULL);
167 }
168
169 void Messages::setCaptureEnabled ( bool bCapture )
170 {
171 // Flush current buffer.
172 flushStdoutBuffer();
173
174 #if !defined(WIN32)
175 // Destroy if already enabled.
176 if (!bCapture && m_pStdoutNotifier) {
177 delete m_pStdoutNotifier;
178 m_pStdoutNotifier = NULL;
179 // Close the notification pipes.
180 if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
181 ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
182 m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL;
183 }
184 if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
185 ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
186 m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL;
187 }
188 }
189 // Are we going to make up the capture?
190 if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {
191 ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);
192 ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);
193 m_pStdoutNotifier = new QSocketNotifier(
194 m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this);
195 QObject::connect(m_pStdoutNotifier,
196 SIGNAL(activated(int)),
197 SLOT(stdoutNotify(int)));
198 }
199 #endif
200 }
201
202
203 // Message font accessors.
204 QFont Messages::messagesFont (void)
205 {
206 return m_pMessagesTextView->font();
207 }
208
209 void Messages::setMessagesFont ( const QFont& font )
210 {
211 m_pMessagesTextView->setFont(font);
212 }
213
214
215 // Maximum number of message lines accessors.
216 int Messages::messagesLimit (void)
217 {
218 return m_iMessagesLimit;
219 }
220
221 void Messages::setMessagesLimit ( int iMessagesLimit )
222 {
223 m_iMessagesLimit = iMessagesLimit;
224 m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3);
225 }
226
227 // Messages logging stuff.
228 bool Messages::isLogging (void) const
229 {
230 return (m_pMessagesLog != NULL);
231 }
232
233 void Messages::setLogging ( bool bEnabled, const QString& sFilename )
234 {
235 if (m_pMessagesLog) {
236 appendMessages(tr("Logging stopped --- %1 ---")
237 .arg(QDateTime::currentDateTime().toString()));
238 m_pMessagesLog->close();
239 delete m_pMessagesLog;
240 m_pMessagesLog = NULL;
241 }
242
243 if (bEnabled) {
244 m_pMessagesLog = new QFile(sFilename);
245 if (m_pMessagesLog->open(QIODevice::Text | QIODevice::Append)) {
246 appendMessages(tr("Logging started --- %1 ---")
247 .arg(QDateTime::currentDateTime().toString()));
248 } else {
249 delete m_pMessagesLog;
250 m_pMessagesLog = NULL;
251 }
252 }
253 }
254
255
256 // Messages log output method.
257 void Messages::appendMessagesLog ( const QString& s )
258 {
259 if (m_pMessagesLog) {
260 QTextStream(m_pMessagesLog) << s << endl;
261 m_pMessagesLog->flush();
262 }
263 }
264
265 // Messages widget output method.
266 void Messages::appendMessagesLine ( const QString& s )
267 {
268 // Check for message line limit...
269 if (m_iMessagesLines > m_iMessagesHigh) {
270 m_pMessagesTextView->setUpdatesEnabled(false);
271 QTextCursor textCursor(m_pMessagesTextView->document()->begin());
272 while (m_iMessagesLines > m_iMessagesLimit) {
273 // Move cursor extending selection
274 // from start to next line-block...
275 textCursor.movePosition(
276 QTextCursor::NextBlock, QTextCursor::KeepAnchor);
277 m_iMessagesLines--;
278 }
279 // Remove the excessive line-blocks...
280 textCursor.removeSelectedText();
281 m_pMessagesTextView->setUpdatesEnabled(true);
282 }
283
284 m_pMessagesTextView->append(s);
285 m_iMessagesLines++;
286 }
287
288
289 // The main utility methods.
290 void Messages::appendMessages ( const QString& s )
291 {
292 appendMessagesColor(s, "#999999");
293 }
294
295 void Messages::appendMessagesColor ( const QString& s, const QString &c )
296 {
297 QString sText = QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s;
298 appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>");
299 appendMessagesLog(sText);
300 }
301
302 void Messages::appendMessagesText ( const QString& s )
303 {
304 appendMessagesLine(s);
305 appendMessagesLog(s);
306 }
307
308
309 // History reset.
310 void Messages::clear (void)
311 {
312 m_iMessagesLines = 0;
313 m_pMessagesTextView->clear();
314 }
315
316 } // namespace QSampler
317
318
319 // end of qsamplerMessages.cpp

  ViewVC Help
Powered by ViewVC