]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.cpp
2394e1c29f58a841366763af329315ce9d8ccec5
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
1 /**
2  * \file GuiViewSource.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Bo Peng
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiViewSource.h"
16 #include "qt_helpers.h"
17
18 #include <boost/tuple/tuple.hpp>
19
20 #include <QTextCursor>
21 #include <QTextDocument>
22
23
24 namespace lyx {
25 namespace frontend {
26
27 GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
28         :       controller_(controller), document_(new QTextDocument(this)),
29                 highlighter_(new LaTeXHighlighter(document_))
30 {
31         setupUi(this);
32
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()));
39
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);
55 }
56
57
58 void GuiViewSourceDialog::updateView()
59 {
60         if (autoUpdateCB->isChecked())
61                 update(viewFullSourceCB->isChecked());
62
63         int beg, end;
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);
70         QWidget::update();
71 }
72
73
74 void GuiViewSourceDialog::update(bool full_source)
75 {
76         document_->setPlainText(toqstr(controller_.updateContent(full_source)));
77 }
78
79
80
81 /////////////////////////////////////////////////////////////////////
82 //
83 // LaTeXHighlighter
84 //
85 /////////////////////////////////////////////////////////////////////
86
87
88 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
89         : QSyntaxHighlighter(parent)
90 {
91         keywordFormat.setForeground(Qt::darkBlue);
92         keywordFormat.setFontWeight(QFont::Bold);
93         commentFormat.setForeground(Qt::darkGray);
94         mathFormat.setForeground(Qt::red);
95 }
96
97
98 void LaTeXHighlighter::highlightBlock(QString const & text)
99 {
100         // $ $
101         QRegExp exprMath("\\$[^\\$]*\\$");
102         int index = text.indexOf(exprMath);
103         while (index >= 0) {
104                 int length = exprMath.matchedLength();
105                 setFormat(index, length, mathFormat);
106                 index = text.indexOf(exprMath, index + length);
107         }
108         // [ ]
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\\**\\}"
118                 ")");
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\\**\\}"
128                 ")");
129         int startIndex = 0;
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);
137                 int length;
138                 if (endIndex == -1) {
139                         setCurrentBlockState(1);
140                         length = text.length() - startIndex;
141                 } else {
142                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
143                 }
144                 setFormat(startIndex, length, mathFormat);
145                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
146         }
147         // \whatever
148         QRegExp exprKeyword("\\\\[A-Za-z]+");
149         index = text.indexOf(exprKeyword);
150         while (index >= 0) {
151                 int length = exprKeyword.matchedLength();
152                 setFormat(index, length, keywordFormat);
153                 index = text.indexOf(exprKeyword, index + length);
154         }
155         // comment
156         QRegExp exprComment("(^|[^\\\\])%.*$");
157         index = text.indexOf(exprComment);
158         while (index >= 0) {
159                 int const length = exprComment.matchedLength();
160                 setFormat(index, length, commentFormat);
161                 index = text.indexOf(exprComment, index + length);
162         }
163 }
164
165
166 } // namespace frontend
167 } // namespace lyx
168
169 #include "GuiViewSource_moc.cpp"