]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QViewSource.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / qt4 / QViewSource.C
1 /**
2  * \file QViewSource.C
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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QViewSource.h"
15 #include "QViewSourceDialog.h"
16 #include "qt_helpers.h"
17 #include "lyx_gui.h"
18
19 #include "controllers/ControlViewSource.h"
20
21 #include <sstream>
22
23 #include <QTextEdit>
24 #include <QPushButton>
25
26 namespace lyx {
27 namespace frontend {
28
29 typedef QController<ControlViewSource, QView<QViewSourceDialog> > base_class;
30
31
32 QViewSource::QViewSource(Dialog & parent)
33         : base_class(parent, "")
34 {}
35
36
37 latexHighlighter::latexHighlighter(QTextDocument * parent) :
38         QSyntaxHighlighter(parent)
39 {
40         keywordFormat.setForeground(Qt::darkBlue);
41         keywordFormat.setFontWeight(QFont::Bold);
42         commentFormat.setForeground(Qt::gray);
43         mathFormat.setForeground(Qt::red);
44 }
45
46
47 void latexHighlighter::highlightBlock(QString const & text)
48 {
49         // comment
50         QRegExp exprComment("^%.*$");
51         int index = text.indexOf(exprComment);
52         while (index >= 0) {
53                 int length = exprComment.matchedLength();
54                 setFormat(index, length, commentFormat);
55                 index = text.indexOf(exprComment, index + length);
56         }
57         // $ $ 
58         QRegExp exprMath("\\$[^\\$]*\\$");
59         index = text.indexOf(exprMath);
60         while (index >= 0) {
61                 int length = exprMath.matchedLength();
62                 setFormat(index, length, mathFormat);
63                 index = text.indexOf(exprMath, index + length);
64         }
65         // [ ]
66         QRegExp exprDispMath("\\[[^\\]]*\\]");
67         index = text.indexOf(exprDispMath);
68         while (index >= 0) {
69                 int length = exprDispMath.matchedLength();
70                 setFormat(index, length, mathFormat);
71                 index = text.indexOf(exprDispMath, index + length);
72         }
73         // \whatever
74         QRegExp exprKeyword("\\\\[A-Za-z]+");
75         index = text.indexOf(exprKeyword);
76         while (index >= 0) {
77                 int length = exprKeyword.matchedLength();
78                 setFormat(index, length, keywordFormat);
79                 index = text.indexOf(exprKeyword, index + length);
80         }
81 }
82
83
84 void QViewSource::build_dialog()
85 {
86         dialog_.reset(new QViewSourceDialog(this));
87         // set syntex highlighting
88         highlighter = new latexHighlighter(dialog_->viewSourceTV->document());
89         //
90         dialog_->viewSourceTV->setReadOnly(true);
91         dialog_->viewSourceTV->setTextFormat(Qt::PlainText);
92         // this is personal. I think source code should be in fixed-size font
93         QFont font(toqstr(lyx_gui::typewriter_font_name()));
94         font.setFixedPitch(true);
95         font.setStyleHint(QFont::TypeWriter);
96         dialog_->viewSourceTV->setFont(font);
97         // again, personal taste
98         dialog_->viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
99 }
100
101
102 void QViewSource::update_contents()
103 {
104         setTitle(controller().title());
105         dialog_->viewSourceTV->setText(toqstr(controller().updateContent()));
106 }
107
108
109 } // namespace frontend
110 } // namespace lyx