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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1558 - (hide 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 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 1558
39     namespace QSampler {
40    
41 capela 94 // 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 capela 1558 // QSampler::Messages - Messages log dockable window.
51 capela 94 //
52    
53     // Constructor.
54 capela 1558 Messages::Messages ( QWidget *pParent )
55 capela 1465 : QDockWidget(pParent)
56 capela 94 {
57 capela 1465 // Surely a name is crucial (e.g.for storing geometry settings)
58     QDockWidget::setObjectName("qsamplerMessages");
59    
60 capela 94 // 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 capela 1465 // Create local text view widget.
66     m_pTextView = new QTextEdit(this);
67 capela 94 // QFont font(m_pTextView->font());
68     // font.setFamily("Fixed");
69     // m_pTextView->setFont(font);
70 capela 1465 m_pTextView->setLineWrapMode(QTextEdit::NoWrap);
71     m_pTextView->setReadOnly(true);
72     m_pTextView->setUndoRedoEnabled(false);
73     // m_pTextView->setTextFormat(Qt::LogText);
74 capela 94
75 capela 1465 // 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 capela 772 // Some specialties to this kind of dock window...
85 capela 1465 QDockWidget::setMinimumHeight(120);
86 capela 94
87 capela 1465 // 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 capela 94 }
93    
94    
95     // Destructor.
96 capela 1558 Messages::~Messages (void)
97 capela 94 {
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 capela 1558 void Messages::showEvent (QShowEvent* event)
107 schoenebeck 1470 {
108     QDockWidget::showEvent(event);
109     emit visibilityChanged(isVisible());
110     }
111    
112    
113 capela 94 // Own stdout/stderr socket notifier slot.
114 capela 1558 void Messages::stdoutNotify ( int fd )
115 capela 94 {
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 capela 1558 void Messages::appendStdoutBuffer ( const QString& s )
129 capela 94 {
130 capela 1465 m_sStdoutBuffer.append(s);
131 capela 94
132 schoenebeck 1474 int iLength = m_sStdoutBuffer.lastIndexOf('\n');
133 capela 1465 if (iLength > 0) {
134     QString sTemp = m_sStdoutBuffer.left(iLength);
135 schoenebeck 1474 m_sStdoutBuffer.remove(0, iLength + 1);
136 capela 1465 QStringList list = sTemp.split('\n');
137     QStringListIterator iter(list);
138     while (iter.hasNext())
139     appendMessagesText(iter.next());
140     }
141 capela 94 }
142    
143    
144     // Stdout flusher -- show up any unfinished line...
145 capela 1558 void Messages::flushStdoutBuffer (void)
146 capela 94 {
147     if (!m_sStdoutBuffer.isEmpty()) {
148     appendMessagesText(m_sStdoutBuffer);
149     m_sStdoutBuffer.truncate(0);
150     }
151     }
152    
153    
154     // Stdout capture accessors.
155 capela 1558 bool Messages::isCaptureEnabled (void)
156 capela 94 {
157     return (bool) (m_pStdoutNotifier != NULL);
158     }
159    
160 capela 1558 void Messages::setCaptureEnabled ( bool bCapture )
161 capela 94 {
162     // Flush current buffer.
163     flushStdoutBuffer();
164    
165     #if !defined(WIN32)
166 capela 1465 // 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 capela 94 #endif
191     }
192    
193    
194     // Message font accessors.
195 capela 1558 QFont Messages::messagesFont (void)
196 capela 94 {
197     return m_pTextView->font();
198     }
199    
200 capela 1558 void Messages::setMessagesFont( const QFont& font )
201 capela 94 {
202     m_pTextView->setFont(font);
203     }
204    
205    
206     // Maximum number of message lines accessors.
207 capela 1558 int Messages::messagesLimit (void)
208 capela 94 {
209     return m_iMessagesLimit;
210     }
211    
212 capela 1558 void Messages::setMessagesLimit ( int iMessagesLimit )
213 capela 94 {
214     m_iMessagesLimit = iMessagesLimit;
215 capela 327 m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3);
216 capela 94 }
217    
218    
219     // The main utility methods.
220 capela 1558 void Messages::appendMessages ( const QString& s )
221 capela 94 {
222     appendMessagesColor(s, "#999999");
223     }
224    
225 capela 1558 void Messages::appendMessagesColor ( const QString& s, const QString &c )
226 capela 94 {
227 capela 1465 appendMessagesText("<font color=\"" + c + "\">"
228     + QTime::currentTime().toString("hh:mm:ss.zzz")
229     + ' ' + s + "</font>");
230 capela 94 }
231    
232 capela 1558 void Messages::appendMessagesText ( const QString& s )
233 capela 94 {
234     // Check for message line limit...
235 capela 1465 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 capela 94 }
249 capela 1465
250     // Count always as a new line out there...
251     m_pTextView->append(s);
252     m_iMessagesLines++;
253 capela 94 }
254    
255    
256 capela 1465 // History reset.
257 capela 1558 void Messages::clear (void)
258 capela 94 {
259 capela 1465 m_iMessagesLines = 0;
260     m_pTextView->clear();
261 capela 94 }
262    
263 capela 1558 } // namespace QSampler
264 capela 94
265 capela 1558
266 capela 94 // end of qsamplerMessages.cpp

  ViewVC Help
Powered by ViewVC