2 * \file QViewSource.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "QViewSource.h"
16 #include "qt_helpers.h"
18 #include <QTextDocument>
23 /////////////////////////////////////////////////////////////////////
27 /////////////////////////////////////////////////////////////////////
29 QViewSourceDialog::QViewSourceDialog(QViewSource * form)
34 connect(viewFullSourceCB, SIGNAL(clicked()),
35 this, SLOT(update()));
36 connect(autoUpdateCB, SIGNAL(toggled(bool)),
37 updatePB, SLOT(setDisabled(bool)));
38 connect(updatePB, SIGNAL(clicked()),
39 this, SLOT(update()));
41 // setting a document at this point trigger an assertion in Qt
42 // so we disable the signals here:
43 form_->document()->blockSignals(true);
44 viewSourceTV->setDocument(form_->document());
45 form_->document()->blockSignals(false);
46 viewSourceTV->setReadOnly(true);
47 ///dialog_->viewSourceTV->setAcceptRichText(false);
48 // this is personal. I think source code should be in fixed-size font
49 QFont font(toqstr(theApp()->typewriterFontName()));
50 font.setKerning(false);
51 font.setFixedPitch(true);
52 font.setStyleHint(QFont::TypeWriter);
53 viewSourceTV->setFont(font);
54 // again, personal taste
55 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
59 void QViewSourceDialog::update()
61 if (autoUpdateCB->isChecked())
62 form_->update(viewFullSourceCB->isChecked());
68 /////////////////////////////////////////////////////////////////////
72 /////////////////////////////////////////////////////////////////////
75 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
76 : QSyntaxHighlighter(parent)
78 keywordFormat.setForeground(Qt::darkBlue);
79 keywordFormat.setFontWeight(QFont::Bold);
80 commentFormat.setForeground(Qt::darkGray);
81 mathFormat.setForeground(Qt::red);
85 void LaTeXHighlighter::highlightBlock(QString const & text)
88 QRegExp exprMath("\\$[^\\$]*\\$");
89 int index = text.indexOf(exprMath);
91 int length = exprMath.matchedLength();
92 setFormat(index, length, mathFormat);
93 index = text.indexOf(exprMath, index + length);
96 QRegExp exprStartDispMath("(\\\\\\[|"
97 "\\\\begin\\{equation\\**\\}|"
98 "\\\\begin\\{eqnarray\\**\\}|"
99 "\\\\begin\\{align(ed|at)*\\**\\}|"
100 "\\\\begin\\{flalign\\**\\}|"
101 "\\\\begin\\{gather\\**\\}|"
102 "\\\\begin\\{multline\\**\\}|"
103 "\\\\begin\\{array\\**\\}|"
104 "\\\\begin\\{cases\\**\\}"
106 QRegExp exprEndDispMath("(\\\\\\]|"
107 "\\\\end\\{equation\\**\\}|"
108 "\\\\end\\{eqnarray\\**\\}|"
109 "\\\\end\\{align(ed|at)*\\**\\}|"
110 "\\\\end\\{flalign\\**\\}|"
111 "\\\\end\\{gather\\**\\}|"
112 "\\\\end\\{multline\\**\\}|"
113 "\\\\end\\{array\\**\\}|"
114 "\\\\end\\{cases\\**\\}"
117 // if previous block was in 'disp math'
118 // start search from 0 (for end disp math)
119 // otherwise, start search from 'begin disp math'
120 if (previousBlockState() != 1)
121 startIndex = text.indexOf(exprStartDispMath);
122 while (startIndex >= 0) {
123 int endIndex = text.indexOf(exprEndDispMath, startIndex);
125 if (endIndex == -1) {
126 setCurrentBlockState(1);
127 length = text.length() - startIndex;
129 length = endIndex - startIndex + exprEndDispMath.matchedLength();
131 setFormat(startIndex, length, mathFormat);
132 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
135 QRegExp exprKeyword("\\\\[A-Za-z]+");
136 index = text.indexOf(exprKeyword);
138 int length = exprKeyword.matchedLength();
139 setFormat(index, length, keywordFormat);
140 index = text.indexOf(exprKeyword, index + length);
143 QRegExp exprComment("(^|[^\\\\])%.*$");
144 index = text.indexOf(exprComment);
146 int const length = exprComment.matchedLength();
147 setFormat(index, length, commentFormat);
148 index = text.indexOf(exprComment, index + length);
153 QViewSource::QViewSource(Dialog & parent)
154 : ControlViewSource(parent)
156 document_ = new QTextDocument(this);
157 highlighter_ = new LaTeXHighlighter(document_);
161 /////////////////////////////////////////////////////////////////////
165 /////////////////////////////////////////////////////////////////////
167 void QViewSource::update(bool full_source)
169 document_->setPlainText(toqstr(updateContent(full_source)));
173 } // namespace frontend
176 #include "QViewSource_moc.cpp"