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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 94 - (show annotations) (download)
Sat May 29 00:21:13 2004 UTC (16 years, 8 months ago) by capela
File size: 7070 byte(s)
Initial alpha release.

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

  ViewVC Help
Powered by ViewVC