On Wed, Dec 26, 2007 at 11:18:16AM +0100, Jürgen Spitzmüller wrote:

> Enrico Forestieri wrote:
> > Yes, I got that, but I think that it could be improved.
> 
> Improvements are certainly welcome.

What about the attached?

-- 
Enrico
Index: src/frontends/qt4/QViewSource.cpp
===================================================================
--- src/frontends/qt4/QViewSource.cpp   (revision 22329)
+++ src/frontends/qt4/QViewSource.cpp   (working copy)
@@ -78,8 +78,6 @@ LaTeXHighlighter::LaTeXHighlighter(QText
        keywordFormat.setForeground(Qt::darkBlue);
        keywordFormat.setFontWeight(QFont::Bold);
        commentFormat.setForeground(Qt::darkGray);
-       warningFormat.setForeground(Qt::red);
-       warningFormat.setFontWeight(QFont::Bold);
        mathFormat.setForeground(Qt::red);
 }
 
@@ -157,16 +155,6 @@ void LaTeXHighlighter::highlightBlock(QS
                text.indexOf(exprComment, index + length);
                index = exprComment.pos(1);
        }
-       // <LyX Warning: ...> ... </LyX Warning>
-       QString opening = QRegExp::escape(qt_("<LyX Warning:"));
-       QString closing = QRegExp::escape(qt_("</LyX Warning>"));
-       QRegExp exprWarning(opening + "[^<]*" + closing);
-       index = text.indexOf(exprWarning);
-       while (index >= 0) {
-               int length = exprWarning.matchedLength();
-               setFormat(index, length, warningFormat);
-               index = text.indexOf(exprWarning, index + length);
-       }
 }
 
 
@@ -175,6 +163,8 @@ QViewSource::QViewSource(Dialog & parent
 {
        document_ = new QTextDocument(this);
        highlighter_ = new LaTeXHighlighter(document_);
+       warningFormat.setForeground(Qt::red);
+       warningFormat.setFontWeight(QFont::Bold);
 }
 
 
@@ -187,6 +177,20 @@ QViewSource::QViewSource(Dialog & parent
 void QViewSource::update(bool full_source)
 {
        document_->setPlainText(toqstr(updateContent(full_source)));
+
+       // replace <LyX Warning: msg>text</LyX Warning>
+       // by      <msg: text>
+       QRegExp exprWarning("<LyX Warning: [^<]*</LyX Warning>");
+       QTextCursor qtcur = document_->find(exprWarning);
+       while (!qtcur.isNull()) {
+               QString s = qtcur.selectedText();
+               s.remove(1,13);
+               s.remove(s.indexOf("</LyX Warning>"), 13);
+               s.replace(s.indexOf('>'), 1, ": ");
+               qtcur.removeSelectedText();
+               qtcur.insertText(s, warningFormat);
+               qtcur = document_->find(exprWarning, qtcur);
+       }
 }
 
 
Index: src/frontends/qt4/QViewSource.h
===================================================================
--- src/frontends/qt4/QViewSource.h     (revision 22329)
+++ src/frontends/qt4/QViewSource.h     (working copy)
@@ -40,7 +40,6 @@ private:
        QTextCharFormat commentFormat;
        QTextCharFormat keywordFormat;
        QTextCharFormat mathFormat;
-       QTextCharFormat warningFormat;
 };
 
 
@@ -76,6 +75,8 @@ private:
        QTextDocument * document_;
        /// LaTeX syntax highlighter
        LaTeXHighlighter * highlighter_;
+       ///
+       QTextCharFormat warningFormat;
 };
 
 
Index: src/frontends/qt4/LaTeXHighlighter.cpp
===================================================================
--- src/frontends/qt4/LaTeXHighlighter.cpp      (revision 22329)
+++ src/frontends/qt4/LaTeXHighlighter.cpp      (working copy)
@@ -11,7 +11,6 @@
 #include <config.h>
 
 #include "LaTeXHighlighter.h"
-#include "qt_helpers.h"
 
 #include <QString>
 #include <QTextDocument>
@@ -26,8 +25,6 @@ LaTeXHighlighter::LaTeXHighlighter(QText
        keywordFormat.setFontWeight(QFont::Bold);
        commentFormat.setForeground(Qt::darkGray);
        mathFormat.setForeground(Qt::red);
-       warningFormat.setForeground(Qt::red);
-       warningFormat.setFontWeight(QFont::Bold);
 }
 
 
@@ -104,16 +101,6 @@ void LaTeXHighlighter::highlightBlock(QS
                text.indexOf(exprComment, index + length);
                index = exprComment.pos(1);
        }
-       // <LyX Warning: ...> ... </LyX Warning>
-       QString opening = QRegExp::escape(qt_("<LyX Warning:"));
-       QString closing = QRegExp::escape(qt_("</LyX Warning>"));
-       QRegExp exprWarning(opening + "[^<]*" + closing);
-       index = text.indexOf(exprWarning);
-       while (index >= 0) {
-               int length = exprWarning.matchedLength();
-               setFormat(index, length, warningFormat);
-               index = text.indexOf(exprWarning, index + length);
-       }
 }
 
 } // namespace frontend
Index: src/frontends/qt4/GuiViewSource.cpp
===================================================================
--- src/frontends/qt4/GuiViewSource.cpp (revision 22329)
+++ src/frontends/qt4/GuiViewSource.cpp (working copy)
@@ -63,6 +63,8 @@ ViewSourceWidget::ViewSourceWidget(GuiVi
        viewSourceTV->setFont(font);
        // again, personal taste
        viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
+       warningFormat.setForeground(Qt::red);
+       warningFormat.setFontWeight(QFont::Bold);
 }
 
 
@@ -84,6 +86,20 @@ void ViewSourceWidget::updateView()
 void ViewSourceWidget::update(bool full_source)
 {
        document_->setPlainText(controller_.getContent(full_source));
+
+       // replace <LyX Warning: msg>text</LyX Warning>
+       // by      <msg: text>
+       QRegExp exprWarning("<LyX Warning: [^<]*</LyX Warning>");
+       QTextCursor qtcur = document_->find(exprWarning);
+       while (!qtcur.isNull()) {
+               QString s = qtcur.selectedText();
+               s.remove(1,13);
+               s.remove(s.indexOf("</LyX Warning>"), 13);
+               s.replace(s.indexOf('>'), 1, ": ");
+               qtcur.removeSelectedText();
+               qtcur.insertText(s, warningFormat);
+               qtcur = document_->find(exprWarning, qtcur);
+       }
 }
 
 
Index: src/frontends/qt4/LaTeXHighlighter.h
===================================================================
--- src/frontends/qt4/LaTeXHighlighter.h        (revision 22329)
+++ src/frontends/qt4/LaTeXHighlighter.h        (working copy)
@@ -34,7 +34,6 @@ private:
        QTextCharFormat commentFormat;
        QTextCharFormat keywordFormat;
        QTextCharFormat mathFormat;
-       QTextCharFormat warningFormat;
 };
 
 } // namespace frontend
Index: src/frontends/qt4/GuiViewSource.h
===================================================================
--- src/frontends/qt4/GuiViewSource.h   (revision 22329)
+++ src/frontends/qt4/GuiViewSource.h   (working copy)
@@ -54,6 +54,8 @@ private:
        QTextDocument * document_;
        /// LaTeX syntax highlighter
        LaTeXHighlighter * highlighter_;
+       ///
+       QTextCharFormat warningFormat;
 };
 
 

Reply via email to