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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1558 - (show annotations) (download)
Thu Dec 6 09:35:33 2007 UTC (16 years, 4 months ago) by capela
File size: 7325 byte(s)
* Qt4 migration: complete QSampler namespace overhaul.

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

  ViewVC Help
Powered by ViewVC