]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QViewSource.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / QViewSource.cpp
1 /**
2  * \file QViewSource.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 "QViewSource.h"
16 #include "qt_helpers.h"
17
18 #include <QTextDocument>
19
20 namespace lyx {
21 namespace frontend {
22
23 /////////////////////////////////////////////////////////////////////
24 //
25 // QViewSourceDialog
26 //
27 /////////////////////////////////////////////////////////////////////
28
29 QViewSourceDialog::QViewSourceDialog(QViewSource * form)
30         : form_(form)
31 {
32         setupUi(this);
33
34         connect(viewFullSourceCB, SIGNAL(clicked()),
35                 this, SLOT(update()));
36         connect(autoUpdateCB, SIGNAL(toggled(bool)),
37                 updatePB, SLOT(setDisabled(bool)));
38         connect(updatePB, SIGNAL(clicked()),
39                 this, SLOT(update()));
40
41         // setting a document at this point trigger an assertion in Qt
42         // so we disable the signals here:
43         form_->document()->blockSignals(true);
44         viewSourceTV->setDocument(form_->document());
45         form_->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 QViewSourceDialog::update()
60 {
61         if (autoUpdateCB->isChecked())
62                 form_->update(viewFullSourceCB->isChecked());
63
64         QWidget::update();
65 }
66
67
68 /////////////////////////////////////////////////////////////////////
69 //
70 // LaTeXHighlighter
71 //
72 /////////////////////////////////////////////////////////////////////
73
74
75 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
76         : QSyntaxHighlighter(parent)
77 {
78         keywordFormat.setForeground(Qt::darkBlue);
79         keywordFormat.setFontWeight(QFont::Bold);
80         commentFormat.setForeground(Qt::darkGray);
81         mathFormat.setForeground(Qt::red);
82 }
83
84
85 void LaTeXHighlighter::highlightBlock(QString const & text)
86 {
87         // $ $
88         QRegExp exprMath("\\$[^\\$]*\\$");
89         int index = text.indexOf(exprMath);
90         while (index >= 0) {
91                 int length = exprMath.matchedLength();
92                 setFormat(index, length, mathFormat);
93                 index = text.indexOf(exprMath, index + length);
94         }
95         // [ ]
96         QRegExp exprStartDispMath("(\\\\\\[|"
97                 "\\\\begin\\{equation\\**\\}|"
98                 "\\\\begin\\{eqnarray\\**\\}|"
99                 "\\\\begin\\{align(ed|at)*\\**\\}|"
100                 "\\\\begin\\{flalign\\**\\}|"
101                 "\\\\begin\\{gather\\**\\}|"
102                 "\\\\begin\\{multline\\**\\}|"
103                 "\\\\begin\\{array\\**\\}|"
104                 "\\\\begin\\{cases\\**\\}"
105                 ")");
106         QRegExp exprEndDispMath("(\\\\\\]|"
107                 "\\\\end\\{equation\\**\\}|"
108                 "\\\\end\\{eqnarray\\**\\}|"
109                 "\\\\end\\{align(ed|at)*\\**\\}|"
110                 "\\\\end\\{flalign\\**\\}|"
111                 "\\\\end\\{gather\\**\\}|"
112                 "\\\\end\\{multline\\**\\}|"
113                 "\\\\end\\{array\\**\\}|"
114                 "\\\\end\\{cases\\**\\}"
115                 ")");
116         int startIndex = 0;
117         // if previous block was in 'disp math'
118         // start search from 0 (for end disp math)
119         // otherwise, start search from 'begin disp math'
120         if (previousBlockState() != 1)
121                 startIndex = text.indexOf(exprStartDispMath);
122         while (startIndex >= 0) {
123                 int endIndex = text.indexOf(exprEndDispMath, startIndex);
124                 int length;
125                 if (endIndex == -1) {
126                         setCurrentBlockState(1);
127                         length = text.length() - startIndex;
128                 } else {
129                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
130                 }
131                 setFormat(startIndex, length, mathFormat);
132                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
133         }
134         // \whatever
135         QRegExp exprKeyword("\\\\[A-Za-z]+");
136         index = text.indexOf(exprKeyword);
137         while (index >= 0) {
138                 int length = exprKeyword.matchedLength();
139                 setFormat(index, length, keywordFormat);
140                 index = text.indexOf(exprKeyword, index + length);
141         }
142         // comment
143         QRegExp exprComment("(^|[^\\\\])%.*$");
144         index = text.indexOf(exprComment);
145         while (index >= 0) {
146                 int const length = exprComment.matchedLength();
147                 setFormat(index, length, commentFormat);
148                 index = text.indexOf(exprComment, index + length);
149         }
150 }
151
152
153 QViewSource::QViewSource(Dialog & parent)
154         : ControlViewSource(parent)
155 {
156         document_ = new QTextDocument(this);
157         highlighter_ = new LaTeXHighlighter(document_);
158 }
159
160
161 /////////////////////////////////////////////////////////////////////
162 //
163 // QViewSource
164 //
165 /////////////////////////////////////////////////////////////////////
166
167 void QViewSource::update(bool full_source)
168 {
169         document_->setPlainText(toqstr(updateContent(full_source)));
170 }
171
172
173 } // namespace frontend
174 } // namespace lyx
175
176 #include "QViewSource_moc.cpp"