]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QViewSource.C
enable Font cache only for MacOSX and inline width() for other platform.
[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
18 #include "frontends/Application.h"
19
20 #include "controllers/ControlViewSource.h"
21
22 #include <sstream>
23
24 #include <QTextEdit>
25 #include <QPushButton>
26
27 namespace lyx {
28 namespace frontend {
29
30 typedef QController<ControlViewSource, QView<QViewSourceDialog> > base_class;
31
32
33 QViewSource::QViewSource(Dialog & parent)
34         : base_class(parent, "")
35 {}
36
37
38 latexHighlighter::latexHighlighter(QTextDocument * parent) :
39         QSyntaxHighlighter(parent)
40 {
41         keywordFormat.setForeground(Qt::darkBlue);
42         keywordFormat.setFontWeight(QFont::Bold);
43         commentFormat.setForeground(Qt::gray);
44         mathFormat.setForeground(Qt::red);
45 }
46
47
48 void latexHighlighter::highlightBlock(QString const & text)
49 {
50         // comment
51         QRegExp exprComment("^%.*$");
52         int index = text.indexOf(exprComment);
53         while (index >= 0) {
54                 int length = exprComment.matchedLength();
55                 setFormat(index, length, commentFormat);
56                 index = text.indexOf(exprComment, index + length);
57         }
58         // $ $ 
59         QRegExp exprMath("\\$[^\\$]*\\$");
60         index = text.indexOf(exprMath);
61         while (index >= 0) {
62                 int length = exprMath.matchedLength();
63                 setFormat(index, length, mathFormat);
64                 index = text.indexOf(exprMath, index + length);
65         }
66         // [ ]
67         QRegExp exprDispMath("\\[[^\\]]*\\]");
68         index = text.indexOf(exprDispMath);
69         while (index >= 0) {
70                 int length = exprDispMath.matchedLength();
71                 setFormat(index, length, mathFormat);
72                 index = text.indexOf(exprDispMath, index + length);
73         }
74         // \whatever
75         QRegExp exprKeyword("\\\\[A-Za-z]+");
76         index = text.indexOf(exprKeyword);
77         while (index >= 0) {
78                 int length = exprKeyword.matchedLength();
79                 setFormat(index, length, keywordFormat);
80                 index = text.indexOf(exprKeyword, index + length);
81         }
82 }
83
84
85 void QViewSource::build_dialog()
86 {
87         dialog_.reset(new QViewSourceDialog(this));
88         // set syntex highlighting
89         highlighter = new latexHighlighter(dialog_->viewSourceTV->document());
90         //
91         dialog_->viewSourceTV->setReadOnly(true);
92         ///dialog_->viewSourceTV->setAcceptRichText(false);
93         // this is personal. I think source code should be in fixed-size font
94         QFont font(toqstr(theApp->typewriterFontName()));
95         font.setFixedPitch(true);
96         font.setStyleHint(QFont::TypeWriter);
97         dialog_->viewSourceTV->setFont(font);
98         // again, personal taste
99         dialog_->viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
100 }
101
102  
103 void QViewSource::update_source()
104 {
105         bool fullSource = dialog_->viewFullSourceCB->isChecked();
106         dialog_->viewSourceTV->setPlainText(toqstr(controller().updateContent(fullSource)));
107 }
108
109
110 void QViewSource::update_contents()
111 {
112         setTitle(controller().title());
113         if (dialog_->autoUpdateCB->isChecked())
114                 update_source();
115 }
116
117
118 } // namespace frontend
119 } // namespace lyx
120
121 #include "QViewSource_moc.cpp"