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

Legend:
Removed from v.339  
changed lines
  Added in v.3788

  ViewVC Help
Powered by ViewVC