3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "QViewSource.h"
15 #include "QViewSourceDialog.h"
16 #include "qt_helpers.h"
19 #include "controllers/ControlViewSource.h"
24 #include <QPushButton>
29 typedef QController<ControlViewSource, QView<QViewSourceDialog> > base_class;
32 QViewSource::QViewSource(Dialog & parent)
33 : base_class(parent, "")
37 latexHighlighter::latexHighlighter(QTextDocument * parent) :
38 QSyntaxHighlighter(parent)
40 keywordFormat.setForeground(Qt::darkBlue);
41 keywordFormat.setFontWeight(QFont::Bold);
42 commentFormat.setForeground(Qt::gray);
43 mathFormat.setForeground(Qt::red);
47 void latexHighlighter::highlightBlock(QString const & text)
50 QRegExp exprComment("^%.*$");
51 int index = text.indexOf(exprComment);
53 int length = exprComment.matchedLength();
54 setFormat(index, length, commentFormat);
55 index = text.indexOf(exprComment, index + length);
58 QRegExp exprMath("\\$[^\\$]*\\$");
59 index = text.indexOf(exprMath);
61 int length = exprMath.matchedLength();
62 setFormat(index, length, mathFormat);
63 index = text.indexOf(exprMath, index + length);
66 QRegExp exprDispMath("\\[[^\\]]*\\]");
67 index = text.indexOf(exprDispMath);
69 int length = exprDispMath.matchedLength();
70 setFormat(index, length, mathFormat);
71 index = text.indexOf(exprDispMath, index + length);
74 QRegExp exprKeyword("\\\\[A-Za-z]+");
75 index = text.indexOf(exprKeyword);
77 int length = exprKeyword.matchedLength();
78 setFormat(index, length, keywordFormat);
79 index = text.indexOf(exprKeyword, index + length);
84 void QViewSource::build_dialog()
86 dialog_.reset(new QViewSourceDialog(this));
87 // set syntex highlighting
88 highlighter = new latexHighlighter(dialog_->viewSourceTV->document());
90 dialog_->viewSourceTV->setReadOnly(true);
91 dialog_->viewSourceTV->setTextFormat(Qt::PlainText);
92 // this is personal. I think source code should be in fixed-size font
93 QFont font(toqstr(lyx_gui::typewriter_font_name()));
94 font.setFixedPitch(true);
95 font.setStyleHint(QFont::TypeWriter);
96 dialog_->viewSourceTV->setFont(font);
97 // again, personal taste
98 dialog_->viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
102 void QViewSource::update_contents()
104 setTitle(controller().title());
105 dialog_->viewSourceTV->setText(toqstr(controller().updateContent()));
109 } // namespace frontend
112 #include "QViewSource_moc.cpp"