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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 338 by capela, Wed Jan 12 10:43:37 2005 UTC revision 1558 by capela, Thu Dec 6 09:35:33 2007 UTC
# Line 1  Line 1 
1  // qsamplerMessages.cpp  // qsamplerMessages.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2005, rncbc aka Rui Nuno Capela. All rights reserved.     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     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# Line 13  Line 14 
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License along
18     along with this program; if not, write to the Free Software     with this program; if not, write to the Free Software Foundation, Inc.,
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20    
21  *****************************************************************************/  *****************************************************************************/
22    
23    #include "qsamplerAbout.h"
24  #include "qsamplerMessages.h"  #include "qsamplerMessages.h"
25    
26  #include <qsocketnotifier.h>  #include <QSocketNotifier>
27  #include <qstringlist.h>  #include <QTextEdit>
28  #include <qtextedit.h>  #include <QTextCursor>
29  #include <qdatetime.h>  #include <QTextBlock>
30  #include <qtooltip.h>  #include <QScrollBar>
31  #include <qpixmap.h>  #include <QDateTime>
32    #include <QIcon>
 #include "config.h"  
33    
34  #if !defined(WIN32)  #if !defined(WIN32)
35  #include <unistd.h>  #include <unistd.h>
36  #endif  #endif
37    
38    
39    namespace QSampler {
40    
41  // The default maximum number of message lines.  // The default maximum number of message lines.
42  #define QSAMPLER_MESSAGES_MAXLINES  1000  #define QSAMPLER_MESSAGES_MAXLINES  1000
43    
# Line 42  Line 46 
46  #define QSAMPLER_MESSAGES_FDREAD    0  #define QSAMPLER_MESSAGES_FDREAD    0
47  #define QSAMPLER_MESSAGES_FDWRITE   1  #define QSAMPLER_MESSAGES_FDWRITE   1
48    
   
49  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
50  // qsamplerMessages - Messages log dockable window.  // QSampler::Messages - Messages log dockable window.
51  //  //
52    
53  // Constructor.  // Constructor.
54  qsamplerMessages::qsamplerMessages ( QWidget *pParent, const char *pszName )  Messages::Messages ( QWidget *pParent )
55      : QDockWindow(pParent, pszName)      : QDockWidget(pParent)
56  {  {
57  #if QT_VERSION >= 0x030200          // Surely a name is crucial (e.g.for storing geometry settings)
58      m_pTextView->setTextFormat(Qt::LogText);          QDockWidget::setObjectName("qsamplerMessages");
 #endif  
     // Initialize default message limit.  
     setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);  
59    
60      // Intialize stdout capture stuff.      // Intialize stdout capture stuff.
61      m_pStdoutNotifier = NULL;      m_pStdoutNotifier = NULL;
62      m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;      m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
63      m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;      m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;
64    
65      // Surely a name is crucial (e.g.for storing geometry settings)          // Create local text view widget.
66      if (pszName == 0)          m_pTextView = new QTextEdit(this);
         QDockWindow::setName("qsamplerMessages");  
   
     // Create local text view widget.  
     m_pTextView = new QTextEdit(this);  
67  //  QFont font(m_pTextView->font());  //  QFont font(m_pTextView->font());
68  //  font.setFamily("Fixed");  //  font.setFamily("Fixed");
69  //  m_pTextView->setFont(font);  //  m_pTextView->setFont(font);
70      m_pTextView->setWordWrap(QTextEdit::NoWrap);          m_pTextView->setLineWrapMode(QTextEdit::NoWrap);
71      m_pTextView->setReadOnly(true);          m_pTextView->setReadOnly(true);
72      m_pTextView->setUndoRedoEnabled(false);          m_pTextView->setUndoRedoEnabled(false);
73    //      m_pTextView->setTextFormat(Qt::LogText);
74      // Prepare the dockable window stuff.  
75      QDockWindow::setWidget(m_pTextView);          // Initialize default message limit.
76      QDockWindow::setOrientation(Qt::Horizontal);          m_iMessagesLines = 0;
77      QDockWindow::setCloseMode(QDockWindow::Always);          setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);
78      QDockWindow::setResizeEnabled(true);  
79            // Prepare the dockable window stuff.
80      // Finally set the default caption and tooltip.          QDockWidget::setWidget(m_pTextView);
81      QString sCaption = tr("Messages");          QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);
82      QToolTip::add(this, sCaption);          QDockWidget::setAllowedAreas(
83      QDockWindow::setCaption(sCaption);                  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.  // Destructor.
96  qsamplerMessages::~qsamplerMessages (void)  Messages::~Messages (void)
97  {  {
98      // No more notifications.      // No more notifications.
99      if (m_pStdoutNotifier)      if (m_pStdoutNotifier)
# Line 99  qsamplerMessages::~qsamplerMessages (voi Line 103  qsamplerMessages::~qsamplerMessages (voi
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.  // Own stdout/stderr socket notifier slot.
114  void qsamplerMessages::stdoutNotify ( int fd )  void Messages::stdoutNotify ( int fd )
115  {  {
116  #if !defined(WIN32)  #if !defined(WIN32)
117      char achBuffer[1024];      char achBuffer[1024];
# Line 114  void qsamplerMessages::stdoutNotify ( in Line 125  void qsamplerMessages::stdoutNotify ( in
125    
126    
127  // Stdout buffer handler -- now splitted by complete new-lines...  // Stdout buffer handler -- now splitted by complete new-lines...
128  void qsamplerMessages::appendStdoutBuffer ( const QString& s )  void Messages::appendStdoutBuffer ( const QString& s )
129  {  {
130      m_sStdoutBuffer.append(s);          m_sStdoutBuffer.append(s);
131    
132      int iLength = m_sStdoutBuffer.findRev('\n') + 1;          int iLength = m_sStdoutBuffer.lastIndexOf('\n');
133      if (iLength > 0) {          if (iLength > 0) {
134          QString sTemp = m_sStdoutBuffer.left(iLength);                  QString sTemp = m_sStdoutBuffer.left(iLength);
135          m_sStdoutBuffer.remove(0, iLength);                  m_sStdoutBuffer.remove(0, iLength + 1);
136          QStringList list = QStringList::split('\n', sTemp, true);                  QStringList list = sTemp.split('\n');
137          for (QStringList::Iterator iter = list.begin(); iter != list.end(); iter++)                  QStringListIterator iter(list);
138              appendMessagesText(*iter);                  while (iter.hasNext())
139      }                          appendMessagesText(iter.next());
140            }
141  }  }
142    
143    
144  // Stdout flusher -- show up any unfinished line...  // Stdout flusher -- show up any unfinished line...
145  void qsamplerMessages::flushStdoutBuffer (void)  void Messages::flushStdoutBuffer (void)
146  {  {
147      if (!m_sStdoutBuffer.isEmpty()) {      if (!m_sStdoutBuffer.isEmpty()) {
148          appendMessagesText(m_sStdoutBuffer);          appendMessagesText(m_sStdoutBuffer);
# Line 140  void qsamplerMessages::flushStdoutBuffer Line 152  void qsamplerMessages::flushStdoutBuffer
152    
153    
154  // Stdout capture accessors.  // Stdout capture accessors.
155  bool qsamplerMessages::isCaptureEnabled (void)  bool Messages::isCaptureEnabled (void)
156  {  {
157      return (bool) (m_pStdoutNotifier != NULL);      return (bool) (m_pStdoutNotifier != NULL);
158  }  }
159    
160  void qsamplerMessages::setCaptureEnabled ( bool bCapture )  void Messages::setCaptureEnabled ( bool bCapture )
161  {  {
162      // Flush current buffer.      // Flush current buffer.
163      flushStdoutBuffer();      flushStdoutBuffer();
164    
165  #if !defined(WIN32)  #if !defined(WIN32)
166      // Destroy if already enabled.          // Destroy if already enabled.
167      if (!bCapture && m_pStdoutNotifier) {          if (!bCapture && m_pStdoutNotifier) {
168          delete m_pStdoutNotifier;                  delete m_pStdoutNotifier;
169          m_pStdoutNotifier = NULL;                  m_pStdoutNotifier = NULL;
170          // Close the notification pipes.                  // Close the notification pipes.
171          if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
172              ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
173              m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;                          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
174          }                  }
175          if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
176              ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
177              m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;                          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
178          }                  }
179      }          }
180      // Are we going to make up the capture?          // Are we going to make up the capture?
181      if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {          if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {
182          ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);
183          ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);
184          m_pStdoutNotifier = new QSocketNotifier(m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this);                  m_pStdoutNotifier = new QSocketNotifier(
185          QObject::connect(m_pStdoutNotifier, SIGNAL(activated(int)), this, SLOT(stdoutNotify(int)));                          m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this);
186      }                  QObject::connect(m_pStdoutNotifier,
187                            SIGNAL(activated(int)),
188                            SLOT(stdoutNotify(int)));
189            }
190  #endif  #endif
191  }  }
192    
193    
194  // Message font accessors.  // Message font accessors.
195  QFont qsamplerMessages::messagesFont (void)  QFont Messages::messagesFont (void)
196  {  {
197      return m_pTextView->font();      return m_pTextView->font();
198  }  }
199    
200  void qsamplerMessages::setMessagesFont( const QFont& font )  void Messages::setMessagesFont( const QFont& font )
201  {  {
202      m_pTextView->setFont(font);      m_pTextView->setFont(font);
203  }  }
204    
205    
206  // Maximum number of message lines accessors.  // Maximum number of message lines accessors.
207  int qsamplerMessages::messagesLimit (void)  int Messages::messagesLimit (void)
208  {  {
209      return m_iMessagesLimit;      return m_iMessagesLimit;
210  }  }
211    
212  void qsamplerMessages::setMessagesLimit ( int iMessagesLimit )  void Messages::setMessagesLimit ( int iMessagesLimit )
213  {  {
214      m_iMessagesLimit = iMessagesLimit;      m_iMessagesLimit = iMessagesLimit;
215      m_iMessagesHigh  = iMessagesLimit + (iMessagesLimit / 3);      m_iMessagesHigh  = iMessagesLimit + (iMessagesLimit / 3);
 #if QT_VERSION >= 0x030200  
         m_pTextView->setMaxLogLines(iMessagesLimit);  
 #endif  
216  }  }
217    
218    
219  // The main utility methods.  // The main utility methods.
220  void qsamplerMessages::appendMessages ( const QString& s )  void Messages::appendMessages ( const QString& s )
221  {  {
222      appendMessagesColor(s, "#999999");      appendMessagesColor(s, "#999999");
223  }  }
224    
225  void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c )  void Messages::appendMessagesColor ( const QString& s, const QString &c )
226  {  {
227      appendMessagesText("<font color=\"" + c + "\">" + QTime::currentTime().toString("hh:mm:ss.zzz") + " " + s + "</font>");          appendMessagesText("<font color=\"" + c + "\">"
228                    + QTime::currentTime().toString("hh:mm:ss.zzz")
229                    + ' ' + s + "</font>");
230  }  }
231    
232  void qsamplerMessages::appendMessagesText ( const QString& s )  void Messages::appendMessagesText ( const QString& s )
233  {  {
 #if QT_VERSION < 0x030200  
234      // Check for message line limit...      // Check for message line limit...
235      if (m_iMessagesLimit > 0) {      if (m_iMessagesLines > m_iMessagesHigh) {
236          int iParagraphs = m_pTextView->paragraphs();                  m_pTextView->setUpdatesEnabled(false);
237          if (iParagraphs > m_iMessagesHigh) {                  QTextCursor textCursor(m_pTextView->document()->begin());
238              m_pTextView->setUpdatesEnabled(false);                  while (m_iMessagesLines > m_iMessagesLimit) {
239              while (iParagraphs > m_iMessagesLimit) {                          // Move cursor extending selection
240                  m_pTextView->removeParagraph(0);                          // from start to next line-block...
241                  iParagraphs--;                          textCursor.movePosition(
242              }                                  QTextCursor::NextBlock, QTextCursor::KeepAnchor);
243              m_pTextView->scrollToBottom();                          m_iMessagesLines--;
244              m_pTextView->setUpdatesEnabled(true);                  }
245          }                  // Remove the excessive line-blocks...
246                    textCursor.removeSelectedText();
247                    m_pTextView->setUpdatesEnabled(true);
248      }      }
249  #endif  
250      m_pTextView->append(s);          // Count always as a new line out there...
251            m_pTextView->append(s);
252            m_iMessagesLines++;
253  }  }
254    
255    
256  // One time scroll to the most recent message.  // History reset.
257  void qsamplerMessages::scrollToBottom (void)  void Messages::clear (void)
258  {  {
259      flushStdoutBuffer();          m_iMessagesLines = 0;
260      m_pTextView->scrollToBottom();          m_pTextView->clear();
261  }  }
262    
263    } // namespace QSampler
264    
265    
266  // end of qsamplerMessages.cpp  // end of qsamplerMessages.cpp

Legend:
Removed from v.338  
changed lines
  Added in v.1558

  ViewVC Help
Powered by ViewVC