2 * \file GuiViewSource.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 "GuiViewSource.h"
16 #include "qt_helpers.h"
18 #include <boost/tuple/tuple.hpp>
20 #include <QTextCursor>
21 #include <QTextDocument>
27 GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
28 : controller_(controller), document_(new QTextDocument(this)),
29 highlighter_(new LaTeXHighlighter(document_))
33 connect(viewFullSourceCB, SIGNAL(clicked()),
34 this, SLOT(update()));
35 connect(autoUpdateCB, SIGNAL(toggled(bool)),
36 updatePB, SLOT(setDisabled(bool)));
37 connect(updatePB, SIGNAL(clicked()),
38 this, SLOT(update()));
40 // setting a document at this point trigger an assertion in Qt
41 // so we disable the signals here:
42 document()->blockSignals(true);
43 viewSourceTV->setDocument(document());
44 document()->blockSignals(false);
45 viewSourceTV->setReadOnly(true);
46 ///dialog_->viewSourceTV->setAcceptRichText(false);
47 // this is personal. I think source code should be in fixed-size font
48 QFont font(toqstr(theApp()->typewriterFontName()));
49 font.setKerning(false);
50 font.setFixedPitch(true);
51 font.setStyleHint(QFont::TypeWriter);
52 viewSourceTV->setFont(font);
53 // again, personal taste
54 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
58 void GuiViewSourceDialog::updateView()
60 if (autoUpdateCB->isChecked())
61 update(viewFullSourceCB->isChecked());
64 boost::tie(beg, end) = controller_.getRows();
65 QTextCursor c = QTextCursor(viewSourceTV->document());
66 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
67 c.select(QTextCursor::BlockUnderCursor);
68 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
69 viewSourceTV->setTextCursor(c);
74 void GuiViewSourceDialog::update(bool full_source)
76 document_->setPlainText(toqstr(controller_.updateContent(full_source)));
81 /////////////////////////////////////////////////////////////////////
85 /////////////////////////////////////////////////////////////////////
88 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
89 : QSyntaxHighlighter(parent)
91 keywordFormat.setForeground(Qt::darkBlue);
92 keywordFormat.setFontWeight(QFont::Bold);
93 commentFormat.setForeground(Qt::darkGray);
94 mathFormat.setForeground(Qt::red);
98 void LaTeXHighlighter::highlightBlock(QString const & text)
101 QRegExp exprMath("\\$[^\\$]*\\$");
102 int index = text.indexOf(exprMath);
104 int length = exprMath.matchedLength();
105 setFormat(index, length, mathFormat);
106 index = text.indexOf(exprMath, index + length);
109 QRegExp exprStartDispMath("(\\\\\\[|"
110 "\\\\begin\\{equation\\**\\}|"
111 "\\\\begin\\{eqnarray\\**\\}|"
112 "\\\\begin\\{align(ed|at)*\\**\\}|"
113 "\\\\begin\\{flalign\\**\\}|"
114 "\\\\begin\\{gather\\**\\}|"
115 "\\\\begin\\{multline\\**\\}|"
116 "\\\\begin\\{array\\**\\}|"
117 "\\\\begin\\{cases\\**\\}"
119 QRegExp exprEndDispMath("(\\\\\\]|"
120 "\\\\end\\{equation\\**\\}|"
121 "\\\\end\\{eqnarray\\**\\}|"
122 "\\\\end\\{align(ed|at)*\\**\\}|"
123 "\\\\end\\{flalign\\**\\}|"
124 "\\\\end\\{gather\\**\\}|"
125 "\\\\end\\{multline\\**\\}|"
126 "\\\\end\\{array\\**\\}|"
127 "\\\\end\\{cases\\**\\}"
130 // if previous block was in 'disp math'
131 // start search from 0 (for end disp math)
132 // otherwise, start search from 'begin disp math'
133 if (previousBlockState() != 1)
134 startIndex = text.indexOf(exprStartDispMath);
135 while (startIndex >= 0) {
136 int endIndex = text.indexOf(exprEndDispMath, startIndex);
138 if (endIndex == -1) {
139 setCurrentBlockState(1);
140 length = text.length() - startIndex;
142 length = endIndex - startIndex + exprEndDispMath.matchedLength();
144 setFormat(startIndex, length, mathFormat);
145 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
148 QRegExp exprKeyword("\\\\[A-Za-z]+");
149 index = text.indexOf(exprKeyword);
151 int length = exprKeyword.matchedLength();
152 setFormat(index, length, keywordFormat);
153 index = text.indexOf(exprKeyword, index + length);
156 QRegExp exprComment("(^|[^\\\\])%.*$");
157 index = text.indexOf(exprComment);
159 int const length = exprComment.matchedLength();
160 setFormat(index, length, commentFormat);
161 index = text.indexOf(exprComment, index + length);
166 } // namespace frontend
169 #include "GuiViewSource_moc.cpp"