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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 338 - (hide annotations) (download)
Wed Jan 12 10:43:37 2005 UTC (19 years, 2 months ago) by capela
File size: 7370 byte(s)
~Messages limit optimization with Qt::LogText format (QTextEdit).

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

  ViewVC Help
Powered by ViewVC