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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 920 - (hide annotations) (download)
Sun Sep 24 12:47:51 2006 UTC (17 years, 6 months ago) by capela
File size: 7465 byte(s)
GPL address update.

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

  ViewVC Help
Powered by ViewVC