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(LyXView & lv)
28 : GuiDialog(lv, "view-source"),
29 document_(new QTextDocument(this)),
30 highlighter_(new LaTeXHighlighter(document_))
33 setController(new ControlViewSource(*this));
35 // GuiViewBase & gui_view = static_cast<GuiViewBase &>(lyxview_);
36 // *dialog, qvs, &gui_view, _("LaTeX Source"), Qt::BottomDockWidgetArea));
38 connect(viewFullSourceCB, SIGNAL(clicked()),
39 this, SLOT(update()));
40 connect(autoUpdateCB, SIGNAL(toggled(bool)),
41 updatePB, SLOT(setDisabled(bool)));
42 connect(updatePB, SIGNAL(clicked()),
43 this, SLOT(update()));
45 // setting a document at this point trigger an assertion in Qt
46 // so we disable the signals here:
47 document()->blockSignals(true);
48 viewSourceTV->setDocument(document());
49 document()->blockSignals(false);
50 viewSourceTV->setReadOnly(true);
51 ///dialog_->viewSourceTV->setAcceptRichText(false);
52 // this is personal. I think source code should be in fixed-size font
53 QFont font(toqstr(theApp()->typewriterFontName()));
54 font.setKerning(false);
55 font.setFixedPitch(true);
56 font.setStyleHint(QFont::TypeWriter);
57 viewSourceTV->setFont(font);
58 // again, personal taste
59 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
61 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
65 ControlViewSource & GuiViewSourceDialog::controller() const
67 return static_cast<ControlViewSource &>(Dialog::controller());
71 void GuiViewSourceDialog::updateView()
73 if (autoUpdateCB->isChecked())
74 update(viewFullSourceCB->isChecked());
77 boost::tie(beg, end) = controller().getRows();
78 QTextCursor c = QTextCursor(viewSourceTV->document());
79 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
80 c.select(QTextCursor::BlockUnderCursor);
81 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
82 viewSourceTV->setTextCursor(c);
87 void GuiViewSourceDialog::update(bool full_source)
89 document_->setPlainText(toqstr(controller().updateContent(full_source)));
94 /////////////////////////////////////////////////////////////////////
98 /////////////////////////////////////////////////////////////////////
101 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
102 : QSyntaxHighlighter(parent)
104 keywordFormat.setForeground(Qt::darkBlue);
105 keywordFormat.setFontWeight(QFont::Bold);
106 commentFormat.setForeground(Qt::darkGray);
107 mathFormat.setForeground(Qt::red);
111 void LaTeXHighlighter::highlightBlock(QString const & text)
114 QRegExp exprMath("\\$[^\\$]*\\$");
115 int index = text.indexOf(exprMath);
117 int length = exprMath.matchedLength();
118 setFormat(index, length, mathFormat);
119 index = text.indexOf(exprMath, index + length);
122 QRegExp exprStartDispMath("(\\\\\\[|"
123 "\\\\begin\\{equation\\**\\}|"
124 "\\\\begin\\{eqnarray\\**\\}|"
125 "\\\\begin\\{align(ed|at)*\\**\\}|"
126 "\\\\begin\\{flalign\\**\\}|"
127 "\\\\begin\\{gather\\**\\}|"
128 "\\\\begin\\{multline\\**\\}|"
129 "\\\\begin\\{array\\**\\}|"
130 "\\\\begin\\{cases\\**\\}"
132 QRegExp exprEndDispMath("(\\\\\\]|"
133 "\\\\end\\{equation\\**\\}|"
134 "\\\\end\\{eqnarray\\**\\}|"
135 "\\\\end\\{align(ed|at)*\\**\\}|"
136 "\\\\end\\{flalign\\**\\}|"
137 "\\\\end\\{gather\\**\\}|"
138 "\\\\end\\{multline\\**\\}|"
139 "\\\\end\\{array\\**\\}|"
140 "\\\\end\\{cases\\**\\}"
143 // if previous block was in 'disp math'
144 // start search from 0 (for end disp math)
145 // otherwise, start search from 'begin disp math'
146 if (previousBlockState() != 1)
147 startIndex = text.indexOf(exprStartDispMath);
148 while (startIndex >= 0) {
149 int endIndex = text.indexOf(exprEndDispMath, startIndex);
151 if (endIndex == -1) {
152 setCurrentBlockState(1);
153 length = text.length() - startIndex;
155 length = endIndex - startIndex + exprEndDispMath.matchedLength();
157 setFormat(startIndex, length, mathFormat);
158 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
161 QRegExp exprKeyword("\\\\[A-Za-z]+");
162 index = text.indexOf(exprKeyword);
164 int length = exprKeyword.matchedLength();
165 setFormat(index, length, keywordFormat);
166 index = text.indexOf(exprKeyword, index + length);
169 QRegExp exprComment("(^|[^\\\\])%.*$");
170 index = text.indexOf(exprComment);
172 int const length = exprComment.matchedLength();
173 setFormat(index, length, commentFormat);
174 index = text.indexOf(exprComment, index + length);
179 } // namespace frontend
182 #include "GuiViewSource_moc.cpp"