# HG changeset patch # User Jacob Dawid # Date 1302019397 -7200 # Node ID 5a6afd0e5213f74b9430a3fa5bee665b3e29c403 # Parent ff3206f87e4d093d809f1e6f7a83ec786b45605a Octaves output is being highlighted now. diff --git a/gui//Quint.pro b/gui//Quint.pro --- 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 diff --git a/gui//mainwindow.cpp b/gui//mainwindow.cpp --- 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); } diff --git a/gui//octaveterminal.cpp b/gui//octaveterminal.cpp --- 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() { diff --git a/gui//octaveterminal.h b/gui//octaveterminal.h --- a/gui//octaveterminal.h +++ b/gui//octaveterminal.h @@ -10,6 +10,7 @@ #include #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 diff --git a/gui//terminalhighlighter.cpp b/gui//terminalhighlighter.cpp new file mode 100644 --- /dev/null +++ b/gui//terminalhighlighter.cpp @@ -0,0 +1,48 @@ +#include "terminalhighlighter.h" +#include + +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); + } + } +} diff --git a/gui//terminalhighlighter.h b/gui//terminalhighlighter.h new file mode 100644 --- /dev/null +++ b/gui//terminalhighlighter.h @@ -0,0 +1,28 @@ +#ifndef TERMINALHIGHLIGHTER_H +#define TERMINALHIGHLIGHTER_H + +#include +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 highlightingRules; + + QTextCharFormat keywordFormat; + QTextCharFormat quotationFormat; + QTextCharFormat numberFormat; + QTextCharFormat urlFormat; +}; + +#endif // TERMINALHIGHLIGHTER_H