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 "Application.h"
20 #include <QTextCursor>
21 #include <QTextDocument>
22 #include <boost/tuple/tuple.hpp>
27 GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
28 : controller_(controller), document_(new QTextDocument(this)),
29 highlighter_(new LaTeXHighlighter(document_))
32 setWindowTitle(qt_("LaTeX Source"));
34 connect(viewFullSourceCB, SIGNAL(clicked()),
35 this, SLOT(updateView()));
36 connect(autoUpdateCB, SIGNAL(toggled(bool)),
37 updatePB, SLOT(setDisabled(bool)));
38 connect(updatePB, SIGNAL(clicked()),
39 this, SLOT(updateView()));
41 // setting a document at this point trigger an assertion in Qt
42 // so we disable the signals here:
43 document()->blockSignals(true);
44 viewSourceTV->setDocument(document());
45 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 GuiViewSourceDialog::updateView()
61 if (autoUpdateCB->isChecked())
62 update(viewFullSourceCB->isChecked());
65 boost::tie(beg, end) = controller_.getRows();
66 QTextCursor c = QTextCursor(viewSourceTV->document());
67 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
68 c.select(QTextCursor::BlockUnderCursor);
69 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
70 viewSourceTV->setTextCursor(c);
75 void GuiViewSourceDialog::update(bool full_source)
77 document_->setPlainText(toqstr(controller_.updateContent(full_source)));
82 /////////////////////////////////////////////////////////////////////
86 /////////////////////////////////////////////////////////////////////
89 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
90 : QSyntaxHighlighter(parent)
92 keywordFormat.setForeground(Qt::darkBlue);
93 keywordFormat.setFontWeight(QFont::Bold);
94 commentFormat.setForeground(Qt::darkGray);
95 mathFormat.setForeground(Qt::red);
99 void LaTeXHighlighter::highlightBlock(QString const & text)
102 QRegExp exprMath("\\$[^\\$]*\\$");
103 int index = text.indexOf(exprMath);
105 int length = exprMath.matchedLength();
106 setFormat(index, length, mathFormat);
107 index = text.indexOf(exprMath, index + length);
110 QRegExp exprStartDispMath("(\\\\\\[|"
111 "\\\\begin\\{equation\\**\\}|"
112 "\\\\begin\\{eqnarray\\**\\}|"
113 "\\\\begin\\{align(ed|at)*\\**\\}|"
114 "\\\\begin\\{flalign\\**\\}|"
115 "\\\\begin\\{gather\\**\\}|"
116 "\\\\begin\\{multline\\**\\}|"
117 "\\\\begin\\{array\\**\\}|"
118 "\\\\begin\\{cases\\**\\}"
120 QRegExp exprEndDispMath("(\\\\\\]|"
121 "\\\\end\\{equation\\**\\}|"
122 "\\\\end\\{eqnarray\\**\\}|"
123 "\\\\end\\{align(ed|at)*\\**\\}|"
124 "\\\\end\\{flalign\\**\\}|"
125 "\\\\end\\{gather\\**\\}|"
126 "\\\\end\\{multline\\**\\}|"
127 "\\\\end\\{array\\**\\}|"
128 "\\\\end\\{cases\\**\\}"
131 // if previous block was in 'disp math'
132 // start search from 0 (for end disp math)
133 // otherwise, start search from 'begin disp math'
134 if (previousBlockState() != 1)
135 startIndex = text.indexOf(exprStartDispMath);
136 while (startIndex >= 0) {
137 int endIndex = text.indexOf(exprEndDispMath, startIndex);
139 if (endIndex == -1) {
140 setCurrentBlockState(1);
141 length = text.length() - startIndex;
143 length = endIndex - startIndex + exprEndDispMath.matchedLength();
145 setFormat(startIndex, length, mathFormat);
146 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
149 QRegExp exprKeyword("\\\\[A-Za-z]+");
150 index = text.indexOf(exprKeyword);
152 int length = exprKeyword.matchedLength();
153 setFormat(index, length, keywordFormat);
154 index = text.indexOf(exprKeyword, index + length);
157 QRegExp exprComment("(^|[^\\\\])%.*$");
158 index = text.indexOf(exprComment);
160 int const length = exprComment.matchedLength();
161 setFormat(index, length, commentFormat);
162 index = text.indexOf(exprComment, index + length);
167 } // namespace frontend
170 #include "GuiViewSource_moc.cpp"