changeset 13333:5a6afd0e5213

Octaves output is being highlighted now.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Tue, 05 Apr 2011 18:03:17 +0200
parents ff3206f87e4d
children 677d14fe5d8e
files gui//Quint.pro gui//mainwindow.cpp gui//octaveterminal.cpp gui//octaveterminal.h gui//terminalhighlighter.cpp gui//terminalhighlighter.h
diffstat 6 files changed, 84 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/gui//Quint.pro
+++ b/gui//Quint.pro
@@ -14,9 +14,11 @@
         mainwindow.cpp \
     octaveterminal.cpp \
     clientmanager.cpp \
-    client.cpp
+    client.cpp \
+    terminalhighlighter.cpp
 
 HEADERS  += mainwindow.h \
     octaveterminal.h \
     clientmanager.h \
-    client.h
+    client.h \
+    terminalhighlighter.h
--- a/gui//mainwindow.cpp
+++ b/gui//mainwindow.cpp
@@ -13,8 +13,6 @@
     setCentralWidget(m_mdiArea);
 
     addOctaveTerminal();
-    addOctaveTerminal();
-    addOctaveTerminal();
     loadWebPage("Online Manual", "http://www.gnu.org/software/octave/doc/interpreter/");
     m_mdiArea->setViewMode(QMdiArea::TabbedView);
 }
--- a/gui//octaveterminal.cpp
+++ b/gui//octaveterminal.cpp
@@ -27,6 +27,8 @@
     blockUserInput();
     connect(m_commandLine, SIGNAL(returnPressed()), this, SLOT(sendCommand()));
     connect(showEnvironmentButton, SIGNAL(clicked()), this, SLOT(showEnvironment()));
+
+    m_terminalHighlighter = new TerminalHighlighter(m_octaveOutput->document());
 }
 
 void OctaveTerminal::sendCommand() {
--- a/gui//octaveterminal.h
+++ b/gui//octaveterminal.h
@@ -10,6 +10,7 @@
 #include <QToolBar>
 
 #include "client.h"
+#include "terminalhighlighter.h"
 
 class OctaveTerminal : public QMdiSubWindow {
     Q_OBJECT
@@ -35,6 +36,7 @@
     QTextBrowser *m_octaveOutput;
     QLineEdit *m_commandLine;
     Client *m_client;
+    TerminalHighlighter *m_terminalHighlighter;
 };
 
 #endif // OCTAVETERMINAL_H
new file mode 100644
--- /dev/null
+++ b/gui//terminalhighlighter.cpp
@@ -0,0 +1,48 @@
+#include "terminalhighlighter.h"
+#include <QtGui>
+
+TerminalHighlighter::TerminalHighlighter(QTextDocument *parent)
+    : QSyntaxHighlighter(parent)
+{
+    HighlightingRule rule;
+
+    keywordFormat.setForeground(Qt::darkRed);
+    keywordFormat.setFontWeight(QFont::Bold);
+    QStringList keywordPatterns;
+    keywordPatterns << "\\bOctave\\b" << "\\bGNU\\b";
+
+    foreach (const QString &pattern, keywordPatterns) {
+        rule.pattern = QRegExp(pattern);
+        rule.format = keywordFormat;
+        highlightingRules.append(rule);
+    }
+
+    numberFormat.setForeground(Qt::darkGreen);
+    numberFormat.setFontWeight(QFont::Bold);
+    rule.pattern = QRegExp("\\b[0-9\\.]+\\b");
+    rule.format = numberFormat;
+    highlightingRules.append(rule);
+
+    urlFormat.setForeground(Qt::darkBlue);
+    rule.pattern = QRegExp("\\bhttp://[^\\s]+\\b");
+    rule.format = urlFormat;
+    highlightingRules.append(rule);
+
+    quotationFormat.setForeground(Qt::darkGreen);
+    rule.pattern = QRegExp("\".*\"");
+    rule.format = quotationFormat;
+    highlightingRules.append(rule);
+}
+
+void TerminalHighlighter::highlightBlock(const QString &text)
+{
+    foreach (const HighlightingRule &rule, highlightingRules) {
+        QRegExp expression(rule.pattern);
+        int index = expression.indexIn(text);
+        while (index >= 0) {
+            int length = expression.matchedLength();
+            setFormat(index, length, rule.format);
+            index = expression.indexIn(text, index + length);
+        }
+    }
+}
new file mode 100644
--- /dev/null
+++ b/gui//terminalhighlighter.h
@@ -0,0 +1,28 @@
+#ifndef TERMINALHIGHLIGHTER_H
+#define TERMINALHIGHLIGHTER_H
+
+#include <QSyntaxHighlighter>
+class QTextDocument;
+class TerminalHighlighter : public QSyntaxHighlighter {
+    Q_OBJECT
+
+public:
+    TerminalHighlighter(QTextDocument *parent = 0);
+
+protected:
+    void highlightBlock(const QString &text);
+
+private:
+    struct HighlightingRule {
+        QRegExp pattern;
+        QTextCharFormat format;
+    };
+    QVector<HighlightingRule> highlightingRules;
+
+    QTextCharFormat keywordFormat;
+    QTextCharFormat quotationFormat;
+    QTextCharFormat numberFormat;
+    QTextCharFormat urlFormat;
+};
+
+#endif // TERMINALHIGHLIGHTER_H