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"
18 #include "frontends/Application.h"
20 #include "controllers/ControlViewSource.h"
25 #include <QPushButton>
30 typedef QController<ControlViewSource, QView<QViewSourceDialog> > base_class;
33 QViewSource::QViewSource(Dialog & parent)
34 : base_class(parent, lyx::docstring())
38 latexHighlighter::latexHighlighter(QTextDocument * parent) :
39 QSyntaxHighlighter(parent)
41 keywordFormat.setForeground(Qt::darkBlue);
42 keywordFormat.setFontWeight(QFont::Bold);
43 commentFormat.setForeground(Qt::gray);
44 mathFormat.setForeground(Qt::red);
48 void latexHighlighter::highlightBlock(QString const & text)
51 QRegExp exprComment("^%.*$");
52 int index = text.indexOf(exprComment);
54 int length = exprComment.matchedLength();
55 setFormat(index, length, commentFormat);
56 index = text.indexOf(exprComment, index + length);
59 QRegExp exprMath("\\$[^\\$]*\\$");
60 index = text.indexOf(exprMath);
62 int length = exprMath.matchedLength();
63 setFormat(index, length, mathFormat);
64 index = text.indexOf(exprMath, index + length);
67 QRegExp exprDispMath("\\[[^\\]]*\\]");
68 index = text.indexOf(exprDispMath);
70 int length = exprDispMath.matchedLength();
71 setFormat(index, length, mathFormat);
72 index = text.indexOf(exprDispMath, index + length);
75 QRegExp exprKeyword("\\\\[A-Za-z]+");
76 index = text.indexOf(exprKeyword);
78 int length = exprKeyword.matchedLength();
79 setFormat(index, length, keywordFormat);
80 index = text.indexOf(exprKeyword, index + length);
85 void QViewSource::build_dialog()
87 dialog_.reset(new QViewSourceDialog(this));
88 // set syntex highlighting
89 highlighter = new latexHighlighter(dialog_->viewSourceTV->document());
91 dialog_->viewSourceTV->setReadOnly(true);
92 ///dialog_->viewSourceTV->setAcceptRichText(false);
93 // this is personal. I think source code should be in fixed-size font
94 QFont font(toqstr(theApp()->typewriterFontName()));
95 font.setKerning(false);
96 font.setFixedPitch(true);
97 font.setStyleHint(QFont::TypeWriter);
98 dialog_->viewSourceTV->setFont(font);
99 // again, personal taste
100 dialog_->viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
104 void QViewSource::update_source()
106 bool fullSource = dialog_->viewFullSourceCB->isChecked();
107 dialog_->viewSourceTV->setPlainText(toqstr(controller().updateContent(fullSource)));
111 void QViewSource::update_contents()
113 setTitle(controller().title());
114 if (dialog_->autoUpdateCB->isChecked())
119 } // namespace frontend
122 #include "QViewSource_moc.cpp"