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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC