]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.cpp
c2f7fb562f752be3ad6f4fb1e9964592a0d5f190
[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 "Application.h"
19
20 #include <QTextCursor>
21 #include <QTextDocument>
22 #include <boost/tuple/tuple.hpp>
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         setWindowTitle(qt_("LaTeX Source"));
33
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()));
40
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);
56 }
57
58
59 void GuiViewSourceDialog::updateView()
60 {
61         if (autoUpdateCB->isChecked())
62                 update(viewFullSourceCB->isChecked());
63
64         int beg, end;
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);
71         QWidget::update();
72 }
73
74
75 void GuiViewSourceDialog::update(bool full_source)
76 {
77         document_->setPlainText(toqstr(controller_.updateContent(full_source)));
78 }
79
80
81
82 /////////////////////////////////////////////////////////////////////
83 //
84 // LaTeXHighlighter
85 //
86 /////////////////////////////////////////////////////////////////////
87
88
89 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
90         : QSyntaxHighlighter(parent)
91 {
92         keywordFormat.setForeground(Qt::darkBlue);
93         keywordFormat.setFontWeight(QFont::Bold);
94         commentFormat.setForeground(Qt::darkGray);
95         mathFormat.setForeground(Qt::red);
96 }
97
98
99 void LaTeXHighlighter::highlightBlock(QString const & text)
100 {
101         // $ $
102         QRegExp exprMath("\\$[^\\$]*\\$");
103         int index = text.indexOf(exprMath);
104         while (index >= 0) {
105                 int length = exprMath.matchedLength();
106                 setFormat(index, length, mathFormat);
107                 index = text.indexOf(exprMath, index + length);
108         }
109         // [ ]
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\\**\\}"
119                 ")");
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\\**\\}"
129                 ")");
130         int startIndex = 0;
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);
138                 int length;
139                 if (endIndex == -1) {
140                         setCurrentBlockState(1);
141                         length = text.length() - startIndex;
142                 } else {
143                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
144                 }
145                 setFormat(startIndex, length, mathFormat);
146                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
147         }
148         // \whatever
149         QRegExp exprKeyword("\\\\[A-Za-z]+");
150         index = text.indexOf(exprKeyword);
151         while (index >= 0) {
152                 int length = exprKeyword.matchedLength();
153                 setFormat(index, length, keywordFormat);
154                 index = text.indexOf(exprKeyword, index + length);
155         }
156         // comment
157         QRegExp exprComment("(^|[^\\\\])%.*$");
158         index = text.indexOf(exprComment);
159         while (index >= 0) {
160                 int const length = exprComment.matchedLength();
161                 setFormat(index, length, commentFormat);
162                 index = text.indexOf(exprComment, index + length);
163         }
164 }
165
166
167 } // namespace frontend
168 } // namespace lyx
169
170 #include "GuiViewSource_moc.cpp"