/[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 772 by capela, Tue Sep 13 14:00:22 2005 UTC revision 2669 by capela, Sun Aug 3 16:56:18 2014 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-2014, 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>
 #include <qstringlist.h>  
 #include <qtextedit.h>  
 #include <qdatetime.h>  
 #include <qtooltip.h>  
 #include <qpixmap.h>  
27    
28  #include "config.h"  #include <QFile>
29    #include <QTextEdit>
30    #include <QTextCursor>
31    #include <QTextStream>
32    #include <QTextBlock>
33    #include <QScrollBar>
34    #include <QDateTime>
35    #include <QIcon>
36    
37  #if !defined(WIN32)  #if !defined(WIN32)
38  #include <unistd.h>  #include <unistd.h>
39    #include <fcntl.h>
40  #endif  #endif
41    
42    
43    namespace QSampler {
44    
45  // The default maximum number of message lines.  // The default maximum number of message lines.
46  #define QSAMPLER_MESSAGES_MAXLINES  1000  #define QSAMPLER_MESSAGES_MAXLINES  1000
47    
# Line 44  Line 52 
52    
53    
54  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
55  // qsamplerMessages - Messages log dockable window.  // QSampler::Messages - Messages log dockable window.
56  //  //
57    
58  // Constructor.  // Constructor.
59  qsamplerMessages::qsamplerMessages ( QWidget *pParent, const char *pszName )  Messages::Messages ( QWidget *pParent )
60      : QDockWindow(pParent, pszName)          : QDockWidget(pParent)
61  {  {
62      // Intialize stdout capture stuff.          // Surely a name is crucial (e.g.for storing geometry settings)
63      m_pStdoutNotifier = NULL;          QDockWidget::setObjectName("qsamplerMessages");
     m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;  
     m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;  
   
     // Surely a name is crucial (e.g.for storing geometry settings)  
     if (pszName == 0)  
         QDockWindow::setName("qsamplerMessages");  
   
     // Create local text view widget.  
     m_pTextView = new QTextEdit(this);  
 //  QFont font(m_pTextView->font());  
 //  font.setFamily("Fixed");  
 //  m_pTextView->setFont(font);  
     m_pTextView->setWordWrap(QTextEdit::NoWrap);  
     m_pTextView->setReadOnly(true);  
     m_pTextView->setUndoRedoEnabled(false);  
 #if QT_VERSION >= 0x030200  
     m_pTextView->setTextFormat(Qt::LogText);  
 #endif  
     // Initialize default message limit.  
     setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);  
64    
65      // Prepare the dockable window stuff.          // Intialize stdout capture stuff.
66      QDockWindow::setWidget(m_pTextView);          m_pStdoutNotifier = NULL;
67      QDockWindow::setOrientation(Qt::Horizontal);          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
68      QDockWindow::setCloseMode(QDockWindow::Always);          m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;
69      QDockWindow::setResizeEnabled(true);  
70            // Create local text view widget.
71            m_pMessagesTextView = new QTextEdit(this);
72    //  QFont font(m_pMessagesTextView->font());
73    //  font.setFamily("Fixed");
74    //  m_pMessagesTextView->setFont(font);
75            m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap);
76            m_pMessagesTextView->setReadOnly(true);
77            m_pMessagesTextView->setUndoRedoEnabled(false);
78    //      m_pMessagesTextView->setTextFormat(Qt::LogText);
79    
80            // Initialize default message limit.
81            m_iMessagesLines = 0;
82            setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);
83    
84            m_pMessagesLog = NULL;
85    
86            // Prepare the dockable window stuff.
87            QDockWidget::setWidget(m_pMessagesTextView);
88            QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);
89            QDockWidget::setAllowedAreas(
90                    Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
91          // Some specialties to this kind of dock window...          // Some specialties to this kind of dock window...
92          QDockWindow::setFixedExtentHeight(120);          QDockWidget::setMinimumHeight(120);
93    
94      // Finally set the default caption and tooltip.          // Finally set the default caption and tooltip.
95      QString sCaption = tr("Messages");          const QString& sCaption = tr("Messages");
96      QToolTip::add(this, sCaption);          QDockWidget::setWindowTitle(sCaption);
97      QDockWindow::setCaption(sCaption);  //      QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png"));
98            QDockWidget::setToolTip(sCaption);
99  }  }
100    
101    
102  // Destructor.  // Destructor.
103  qsamplerMessages::~qsamplerMessages (void)  Messages::~Messages (void)
104  {  {
105      // No more notifications.          // Turn off and close logging.
106      if (m_pStdoutNotifier)          setLogging(false);
107          delete m_pStdoutNotifier;  
108            // No more notifications.
109            if (m_pStdoutNotifier)
110                    delete m_pStdoutNotifier;
111    
112      // No need to delete child widgets, Qt does it all for us.          // No need to delete child widgets, Qt does it all for us.
113  }  }
114    
115    
116  // Own stdout/stderr socket notifier slot.  // Own stdout/stderr socket notifier slot.
117  void qsamplerMessages::stdoutNotify ( int fd )  void Messages::stdoutNotify ( int fd )
118  {  {
119  #if !defined(WIN32)  #if !defined(WIN32)
120      char achBuffer[1024];          // Set non-blocking reads, if not already...
121      int  cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1);          const int iFlags = ::fcntl(fd, F_GETFL, 0);
122      if (cchBuffer > 0) {          int iBlock = ((iFlags & O_NONBLOCK) == 0);
123          achBuffer[cchBuffer] = (char) 0;          if (iBlock)
124          appendStdoutBuffer(achBuffer);                  iBlock = ::fcntl(fd, F_SETFL, iFlags | O_NONBLOCK);
125      }          // Read as much as is available...
126            QString sTemp;
127            char achBuffer[1024];
128            const int cchBuffer = sizeof(achBuffer) - 1;
129            int cchRead = ::read(fd, achBuffer, cchBuffer);
130            while (cchRead > 0) {
131                    achBuffer[cchRead] = (char) 0;
132                    sTemp.append(achBuffer);
133                    cchRead = (iBlock ? 0 : ::read(fd, achBuffer, cchBuffer));
134            }
135            // Needs to be non-empty...
136            if (!sTemp.isEmpty())
137                    appendStdoutBuffer(sTemp);
138  #endif  #endif
139  }  }
140    
141    
142  // Stdout buffer handler -- now splitted by complete new-lines...  // Stdout buffer handler -- now splitted by complete new-lines...
143  void qsamplerMessages::appendStdoutBuffer ( const QString& s )  void Messages::appendStdoutBuffer ( const QString& s )
144  {  {
145      m_sStdoutBuffer.append(s);          m_sStdoutBuffer.append(s);
146    
147      int iLength = m_sStdoutBuffer.findRev('\n') + 1;          const int iLength = m_sStdoutBuffer.lastIndexOf('\n');
148      if (iLength > 0) {          if (iLength > 0) {
149          QString sTemp = m_sStdoutBuffer.left(iLength);                  const QString& sTemp = m_sStdoutBuffer.left(iLength);
150          m_sStdoutBuffer.remove(0, iLength);                  m_sStdoutBuffer.remove(0, iLength + 1);
151          QStringList list = QStringList::split('\n', sTemp, true);                  QStringList list = sTemp.split('\n');
152          for (QStringList::Iterator iter = list.begin(); iter != list.end(); iter++)                  QStringListIterator iter(list);
153              appendMessagesText(*iter);                  while (iter.hasNext())
154      }                          appendMessagesText(iter.next());
155            }
156  }  }
157    
158    
159  // Stdout flusher -- show up any unfinished line...  // Stdout flusher -- show up any unfinished line...
160  void qsamplerMessages::flushStdoutBuffer (void)  void Messages::flushStdoutBuffer (void)
161  {  {
162      if (!m_sStdoutBuffer.isEmpty()) {          if (!m_sStdoutBuffer.isEmpty()) {
163          appendMessagesText(m_sStdoutBuffer);                  appendMessagesText(m_sStdoutBuffer);
164          m_sStdoutBuffer.truncate(0);                  m_sStdoutBuffer.clear();
165      }          }
166  }  }
167    
168    
169  // Stdout capture accessors.  // Stdout capture accessors.
170  bool qsamplerMessages::isCaptureEnabled (void)  bool Messages::isCaptureEnabled (void)
171  {  {
172      return (bool) (m_pStdoutNotifier != NULL);          return (m_pStdoutNotifier != NULL);
173  }  }
174    
175  void qsamplerMessages::setCaptureEnabled ( bool bCapture )  void Messages::setCaptureEnabled ( bool bCapture )
176  {  {
177      // Flush current buffer.          // Flush current buffer.
178      flushStdoutBuffer();          flushStdoutBuffer();
179    
180  #if !defined(WIN32)  #if !defined(WIN32)
181      // Destroy if already enabled.          // Destroy if already enabled.
182      if (!bCapture && m_pStdoutNotifier) {          if (!bCapture && m_pStdoutNotifier) {
183          delete m_pStdoutNotifier;                  delete m_pStdoutNotifier;
184          m_pStdoutNotifier = NULL;                  m_pStdoutNotifier = NULL;
185          // Close the notification pipes.                  // Close the notification pipes.
186          if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
187              ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
188              m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;                          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
189          }                  }
190          if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {          }
191              ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);          // Are we going to make up the capture?
192              m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;          if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {
193          }                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);
194      }                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);
195      // Are we going to make up the capture?                  m_pStdoutNotifier = new QSocketNotifier(
196      if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {                          m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this);
197          ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);                  QObject::connect(m_pStdoutNotifier,
198          ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);                          SIGNAL(activated(int)),
199          m_pStdoutNotifier = new QSocketNotifier(m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this);                          SLOT(stdoutNotify(int)));
200          QObject::connect(m_pStdoutNotifier, SIGNAL(activated(int)), this, SLOT(stdoutNotify(int)));          }
     }  
201  #endif  #endif
202  }  }
203    
204    
205  // Message font accessors.  // Message font accessors.
206  QFont qsamplerMessages::messagesFont (void)  QFont Messages::messagesFont (void)
207  {  {
208      return m_pTextView->font();          return m_pMessagesTextView->font();
209  }  }
210    
211  void qsamplerMessages::setMessagesFont( const QFont& font )  void Messages::setMessagesFont ( const QFont& font )
212  {  {
213      m_pTextView->setFont(font);          m_pMessagesTextView->setFont(font);
214  }  }
215    
216    
217  // Maximum number of message lines accessors.  // Maximum number of message lines accessors.
218  int qsamplerMessages::messagesLimit (void)  int Messages::messagesLimit (void)
219  {  {
220      return m_iMessagesLimit;          return m_iMessagesLimit;
221  }  }
222    
223  void qsamplerMessages::setMessagesLimit ( int iMessagesLimit )  void Messages::setMessagesLimit ( int iMessagesLimit )
224  {  {
225      m_iMessagesLimit = iMessagesLimit;          m_iMessagesLimit = iMessagesLimit;
226      m_iMessagesHigh  = iMessagesLimit + (iMessagesLimit / 3);          m_iMessagesHigh  = iMessagesLimit + (iMessagesLimit / 3);
227  #if QT_VERSION >= 0x030200  }
228          m_pTextView->setMaxLogLines(iMessagesLimit);  
229  #endif  // Messages logging stuff.
230    bool Messages::isLogging (void) const
231    {
232            return (m_pMessagesLog != NULL);
233    }
234    
235    void Messages::setLogging ( bool bEnabled, const QString& sFilename )
236    {
237            if (m_pMessagesLog) {
238                    appendMessages(tr("Logging stopped --- %1 ---")
239                            .arg(QDateTime::currentDateTime().toString()));
240                    m_pMessagesLog->close();
241                    delete m_pMessagesLog;
242                    m_pMessagesLog = NULL;
243            }
244    
245            if (bEnabled) {
246                    m_pMessagesLog = new QFile(sFilename);
247                    if (m_pMessagesLog->open(QIODevice::Text | QIODevice::Append)) {
248                            appendMessages(tr("Logging started --- %1 ---")
249                                    .arg(QDateTime::currentDateTime().toString()));
250                    } else {
251                            delete m_pMessagesLog;
252                            m_pMessagesLog = NULL;
253                    }
254            }
255    }
256    
257    
258    // Messages log output method.
259    void Messages::appendMessagesLog ( const QString& s )
260    {
261            if (m_pMessagesLog) {
262                    QTextStream(m_pMessagesLog) << s << endl;
263                    m_pMessagesLog->flush();
264            }
265    }
266    
267    // Messages widget output method.
268    void Messages::appendMessagesLine ( const QString& s )
269    {
270            // Check for message line limit...
271            if (m_iMessagesLines > m_iMessagesHigh) {
272                    m_pMessagesTextView->setUpdatesEnabled(false);
273                    QTextCursor textCursor(m_pMessagesTextView->document()->begin());
274                    while (m_iMessagesLines > m_iMessagesLimit) {
275                            // Move cursor extending selection
276                            // from start to next line-block...
277                            textCursor.movePosition(
278                                    QTextCursor::NextBlock, QTextCursor::KeepAnchor);
279                            m_iMessagesLines--;
280                    }
281                    // Remove the excessive line-blocks...
282                    textCursor.removeSelectedText();
283                    m_pMessagesTextView->setUpdatesEnabled(true);
284            }
285    
286            m_pMessagesTextView->append(s);
287            m_iMessagesLines++;
288  }  }
289    
290    
291  // The main utility methods.  // The main utility methods.
292  void qsamplerMessages::appendMessages ( const QString& s )  void Messages::appendMessages ( const QString& s )
293  {  {
294      appendMessagesColor(s, "#999999");          appendMessagesColor(s, "#999999");
295  }  }
296    
297  void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c )  void Messages::appendMessagesColor ( const QString& s, const QString &c )
298  {  {
299      appendMessagesText("<font color=\"" + c + "\">" + QTime::currentTime().toString("hh:mm:ss.zzz") + " " + s + "</font>");          const QString& sText
300                    = QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s;
301            appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>");
302            appendMessagesLog(sText);
303  }  }
304    
305  void qsamplerMessages::appendMessagesText ( const QString& s )  void Messages::appendMessagesText ( const QString& s )
306  {  {
307  #if QT_VERSION < 0x030200          appendMessagesLine(s);
308      // Check for message line limit...          appendMessagesLog(s);
     if (m_iMessagesLimit > 0) {  
         int iParagraphs = m_pTextView->paragraphs();  
         if (iParagraphs > m_iMessagesHigh) {  
             m_pTextView->setUpdatesEnabled(false);  
             while (iParagraphs > m_iMessagesLimit) {  
                 m_pTextView->removeParagraph(0);  
                 iParagraphs--;  
             }  
             m_pTextView->scrollToBottom();  
             m_pTextView->setUpdatesEnabled(true);  
         }  
     }  
 #endif  
     m_pTextView->append(s);  
309  }  }
310    
311    
312  // One time scroll to the most recent message.  // History reset.
313  void qsamplerMessages::scrollToBottom (void)  void Messages::clear (void)
314  {  {
315      flushStdoutBuffer();          m_iMessagesLines = 0;
316      m_pTextView->scrollToBottom();          m_pMessagesTextView->clear();
317  }  }
318    
319    } // namespace QSampler
320    
321    
322  // end of qsamplerMessages.cpp  // end of qsamplerMessages.cpp

Legend:
Removed from v.772  
changed lines
  Added in v.2669

  ViewVC Help
Powered by ViewVC