Tulip  4.2.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
ConsoleOutputHandler.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef CONSOLE_OUTPUT_HANDLER_H
21 #define CONSOLE_OUTPUT_HANDLER_H
22 
23 #include <QtGui/QPlainTextEdit>
24 #include <QtGui/QTextBrowser>
25 #include <QtGui/QTextBlock>
26 #include <QtGui/QApplication>
27 #include <QtCore/QTime>
28 
29 class ConsoleOutputHandler : public QObject {
30 
31  Q_OBJECT
32 
33 public:
34 
35  ConsoleOutputHandler() {
36  timer.start();
37  }
38 
39 
40 public slots :
41 
42  void writeToConsole(QAbstractScrollArea *consoleWidget, const QString &output, bool errorOutput) {
43 
44  if (!consoleWidget)
45  return;
46 
47  QTextBrowser *textBrowser = dynamic_cast<QTextBrowser*>(consoleWidget);
48  QPlainTextEdit *textEdit = dynamic_cast<QPlainTextEdit*>(consoleWidget);
49 
50  QBrush brush(Qt::SolidPattern);
51 
52  if (errorOutput) {
53  brush.setColor(Qt::red);
54  }
55  else {
56  brush.setColor(Qt::black);
57  }
58 
59  QTextCursor cursor;
60  QTextCharFormat formt;
61 
62  if (textEdit) {
63  formt = textEdit->textCursor().charFormat();
64  formt.setForeground(brush);
65  textEdit->moveCursor(QTextCursor::End);
66  cursor = textEdit->textCursor();
67  }
68  else {
69  formt = textBrowser->textCursor().charFormat();
70  formt.setForeground(brush);
71  formt.setAnchor(false);
72  formt.setUnderlineStyle(QTextCharFormat::NoUnderline);
73  formt.setAnchorHref("");
74  textBrowser->moveCursor(QTextCursor::End);
75  cursor = textBrowser->textCursor();
76  }
77 
78  cursor.insertText(output, formt);
79 
80  if (textBrowser) {
81  QRegExp rx("^.*File.*\"(.*)\".*line.*(\\d+).*$");
82  QRegExp rx2("^.*File.*\"(.*)\".*line.*(\\d+).*in (.*)$");
83  cursor = textBrowser->document()->find(rx, QTextCursor(textBrowser->document()->begin()));
84 
85  while (!cursor.isNull()) {
86  rx.indexIn(cursor.selectedText());
87  rx2.indexIn(cursor.selectedText());
88 
89  if (rx.cap(1) != "<string>" && rx2.cap(3) != "tlpimporthook") {
90  formt = cursor.charFormat();
91  formt.setAnchor(true);
92  formt.setUnderlineStyle(QTextCharFormat::SingleUnderline);
93  formt.setAnchorHref(rx.cap(1) + ":" + rx.cap(2));
94  cursor.setCharFormat(formt);
95  }
96 
97  cursor = textBrowser->document()->find(rx, cursor);
98  }
99 
100  if (timer.elapsed() >= 50) {
101  QApplication::processEvents();
102  timer.start();
103  }
104  }
105  }
106 
107 private:
108 
109  QTime timer;
110 
111 };
112 
113 class ConsoleOutputEmitter : public QObject {
114 
115  Q_OBJECT
116 
117 public:
118 
119  ConsoleOutputEmitter() : _consoleWidget(NULL), _outputActivated(true) {}
120 
121  void sendOutputToConsole(const QString &output, bool errorOutput) {
122  if (_outputActivated) {
123  emit consoleOutput(_consoleWidget, output, errorOutput);
124  }
125  }
126 
127  void setConsoleWidget(QAbstractScrollArea *consoleWidget) {
128  _consoleWidget = consoleWidget;
129  }
130 
131  QAbstractScrollArea *consoleWidget() const {
132  return _consoleWidget;
133  }
134 
135  void setOutputActivated(bool outputActivated) {
136  _outputActivated = outputActivated;
137  }
138 
139 signals:
140 
141  void consoleOutput(QAbstractScrollArea *consoleWidget, const QString &output, bool errorOutput);
142 
143 private :
144 
145  QAbstractScrollArea *_consoleWidget;
146  bool _outputActivated;
147 };
148 
149 #endif