20 #ifndef CONSOLE_OUTPUT_HANDLER_H
21 #define CONSOLE_OUTPUT_HANDLER_H
23 #include <QPlainTextEdit>
24 #include <QTextBrowser>
26 #include <QApplication>
32 class ConsoleOutputHandler :
public QObject {
38 ConsoleOutputHandler() {
45 void writeToConsole(QAbstractScrollArea *consoleWidget,
const QString &output,
bool errorOutput) {
50 QTextBrowser *textBrowser =
dynamic_cast<QTextBrowser*
>(consoleWidget);
51 QPlainTextEdit *textEdit =
dynamic_cast<QPlainTextEdit*
>(consoleWidget);
53 QBrush brush(Qt::SolidPattern);
56 brush.setColor(Qt::red);
59 brush.setColor(Qt::black);
63 QTextCharFormat formt;
66 formt = textEdit->textCursor().charFormat();
67 formt.setForeground(brush);
68 textEdit->moveCursor(QTextCursor::End);
69 cursor = textEdit->textCursor();
72 formt = textBrowser->textCursor().charFormat();
73 formt.setForeground(brush);
74 formt.setAnchor(
false);
75 formt.setUnderlineStyle(QTextCharFormat::NoUnderline);
76 formt.setAnchorHref(
"");
77 textBrowser->moveCursor(QTextCursor::End);
78 cursor = textBrowser->textCursor();
81 cursor.insertText(output, formt);
84 QRegExp rx(
"^.*File.*\"(.*)\".*line.*(\\d+).*$");
85 QRegExp rx2(
"^.*File.*\"(.*)\".*line.*(\\d+).*in (.*)$");
86 cursor = textBrowser->document()->find(rx, QTextCursor(textBrowser->document()->begin()));
88 while (!cursor.isNull()) {
89 rx.indexIn(cursor.selectedText());
90 rx2.indexIn(cursor.selectedText());
92 if (rx.cap(1) !=
"<string>" && rx2.cap(3) !=
"tlpimporthook") {
93 formt = cursor.charFormat();
94 formt.setAnchor(
true);
95 formt.setUnderlineStyle(QTextCharFormat::SingleUnderline);
96 formt.setAnchorHref(rx.cap(1) +
":" + rx.cap(2));
97 cursor.setCharFormat(formt);
100 cursor = textBrowser->document()->find(rx, cursor);
103 if (timer.elapsed() >= 50) {
104 QApplication::processEvents();
116 class ConsoleOutputEmitter :
public QObject {
122 ConsoleOutputEmitter() : _consoleWidget(NULL), _outputActivated(true) {}
124 void sendOutputToConsole(
const QString &output,
bool errorOutput) {
125 if (_outputActivated) {
126 emit consoleOutput(_consoleWidget, output, errorOutput);
130 void setConsoleWidget(QAbstractScrollArea *consoleWidget) {
131 _consoleWidget = consoleWidget;
134 QAbstractScrollArea *consoleWidget()
const {
135 return _consoleWidget;
138 void setOutputActivated(
bool outputActivated) {
139 _outputActivated = outputActivated;
144 void consoleOutput(QAbstractScrollArea *consoleWidget,
const QString &output,
bool errorOutput);
148 QAbstractScrollArea *_consoleWidget;
149 bool _outputActivated;
152 class ConsoleInputHandler :
public QObject {
158 ConsoleInputHandler() : _startReadCol(-1), _consoleWidget(NULL), _lineRead(false), _wasReadOnly(false) {}
160 void setConsoleWidget(QAbstractScrollArea *consoleWidget) {
161 _consoleWidget = consoleWidget;
164 QAbstractScrollArea *consoleWidget()
const {
165 return _consoleWidget;
168 void startReadLine() {
169 if (_consoleWidget) {
170 _consoleWidget->installEventFilter(
this);
171 qApp->installEventFilter(
this);
172 _consoleWidget->setFocus();
180 QTextBrowser *textBrowser =
dynamic_cast<QTextBrowser*
>(_consoleWidget);
181 QPlainTextEdit *textEdit =
dynamic_cast<QPlainTextEdit*
>(_consoleWidget);
182 QColor lineColor = QColor(Qt::green).lighter(160);
185 _readPos = textBrowser->textCursor();
186 _wasReadOnly = textBrowser->isReadOnly();
187 textBrowser->setReadOnly(
false);
188 textBrowser->verticalScrollBar()->setValue(textBrowser->verticalScrollBar()->maximum());
191 _readPos = textEdit->textCursor();
192 _wasReadOnly = textEdit->isReadOnly();
193 textEdit->setReadOnly(
false);
196 _startReadCol = _readPos.columnNumber();
197 QTextBlockFormat format = _blockFormat = _readPos.blockFormat();
198 format.setBackground(lineColor);
199 format.setProperty(QTextFormat::FullWidthSelection,
true);
200 _readPos.setBlockFormat(format);
203 bool lineRead()
const {
207 QString line()
const {
211 bool eventFilter(QObject *, QEvent *event) {
212 QTextBrowser *textBrowser =
dynamic_cast<QTextBrowser*
>(_consoleWidget);
213 QPlainTextEdit *textEdit =
dynamic_cast<QPlainTextEdit*
>(_consoleWidget);
214 QTextCursor curCursor;
217 curCursor = textBrowser->textCursor();
220 curCursor = textEdit->textCursor();
223 if (event->type() == QEvent::KeyPress) {
224 QKeyEvent *kev =
static_cast<QKeyEvent *
>(event);
225 int key = kev->key();
227 if ((key == Qt::Key_Enter || key == Qt::Key_Return) && kev->modifiers() == Qt::NoModifier) {
229 _line = _readPos.block().text().mid(_startReadCol);
231 _readPos.insertText(
"\n");
232 _readPos.setBlockFormat(_blockFormat);
235 textBrowser->setReadOnly(_wasReadOnly);
238 textEdit->setReadOnly(_wasReadOnly);
241 _consoleWidget->removeEventFilter(
this);
242 qApp->removeEventFilter(
this);
245 else if (key == Qt::Key_Up || key == Qt::Key_Down) {
248 else if (key == Qt::Key_Left) {
249 if (curCursor.columnNumber() > _startReadCol) {
251 textEdit->moveCursor(QTextCursor::Left);
254 textBrowser->moveCursor(QTextCursor::Left);
260 else if (key == Qt::Key_Right) {
262 textEdit->moveCursor(QTextCursor::Right);
265 textBrowser->moveCursor(QTextCursor::Right);
268 else if (key == Qt::Key_Backspace) {
269 if (curCursor.columnNumber() > _startReadCol) {
270 curCursor.deletePreviousChar();
276 else if (event->type() == QEvent::MouseButtonDblClick ||
event->type() == QEvent::MouseButtonPress ||
277 event->type() == QEvent::MouseButtonRelease) {
286 QTextCursor _readPos;
288 QAbstractScrollArea *_consoleWidget;
292 QTextBlockFormat _blockFormat;