]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiViewSource.cpp
move LaTeXHighlighte
[features.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 "LaTeXHighlighter.h"
17 #include "qt_helpers.h"
18
19 #include "Application.h"
20 #include "BufferView.h"
21 #include "Buffer.h"
22 #include "Cursor.h"
23 #include "gettext.h"
24 #include "Paragraph.h"
25 #include "TexRow.h"
26
27 #include <QTextCursor>
28 #include <QTextDocument>
29 #include <boost/tuple/tuple.hpp>
30
31 using std::string;
32
33 namespace lyx {
34 namespace frontend {
35
36 GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
37         :       controller_(controller), document_(new QTextDocument(this)),
38                 highlighter_(new LaTeXHighlighter(document_))
39 {
40         setupUi(this);
41         setWindowTitle(qt_("LaTeX Source"));
42
43         connect(viewFullSourceCB, SIGNAL(clicked()),
44                 this, SLOT(updateView()));
45         connect(autoUpdateCB, SIGNAL(toggled(bool)),
46                 updatePB, SLOT(setDisabled(bool)));
47         connect(updatePB, SIGNAL(clicked()),
48                 this, SLOT(updateView()));
49
50         // setting a document at this point trigger an assertion in Qt
51         // so we disable the signals here:
52         document()->blockSignals(true);
53         viewSourceTV->setDocument(document());
54         document()->blockSignals(false);
55         viewSourceTV->setReadOnly(true);
56         ///dialog_->viewSourceTV->setAcceptRichText(false);
57         // this is personal. I think source code should be in fixed-size font
58         QFont font(toqstr(theApp()->typewriterFontName()));
59         font.setKerning(false);
60         font.setFixedPitch(true);
61         font.setStyleHint(QFont::TypeWriter);
62         viewSourceTV->setFont(font);
63         // again, personal taste
64         viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
65 }
66
67
68 void GuiViewSourceDialog::updateView()
69 {
70         if (autoUpdateCB->isChecked())
71                 update(viewFullSourceCB->isChecked());
72
73         int beg, end;
74         boost::tie(beg, end) = controller_.getRows();
75         QTextCursor c = QTextCursor(viewSourceTV->document());
76         c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
77         c.select(QTextCursor::BlockUnderCursor);
78         c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
79         viewSourceTV->setTextCursor(c);
80         QWidget::update();
81 }
82
83
84 void GuiViewSourceDialog::update(bool full_source)
85 {
86         document_->setPlainText(toqstr(controller_.updateContent(full_source)));
87 }
88
89
90 ControlViewSource::ControlViewSource(Dialog & parent)
91         : Controller(parent)
92 {}
93
94
95 bool ControlViewSource::initialiseParams(string const & /*source*/)
96 {
97         return true;
98 }
99
100
101 docstring const ControlViewSource::updateContent(bool fullSource)
102 {
103         // get the *top* level paragraphs that contain the cursor,
104         // or the selected text
105         pit_type par_begin;
106         pit_type par_end;
107
108         BufferView * view = bufferview();
109         if (!view->cursor().selection()) {
110                 par_begin = view->cursor().bottom().pit();
111                 par_end = par_begin;
112         } else {
113                 par_begin = view->cursor().selectionBegin().bottom().pit();
114                 par_end = view->cursor().selectionEnd().bottom().pit();
115         }
116         if (par_begin > par_end)
117                 std::swap(par_begin, par_end);
118         odocstringstream ostr;
119         view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
120         return ostr.str();
121 }
122
123
124 std::pair<int, int> ControlViewSource::getRows() const
125 {
126         BufferView const * view = bufferview();
127         CursorSlice beg = view->cursor().selectionBegin().bottom();
128         CursorSlice end = view->cursor().selectionEnd().bottom();
129
130         int begrow = view->buffer().texrow().
131                 getRowFromIdPos(beg.paragraph().id(), beg.pos());
132         int endrow = view->buffer().texrow().
133                 getRowFromIdPos(end.paragraph().id(), end.pos());
134         int nextendrow = view->buffer().texrow().
135                 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
136         return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1));
137 }
138
139
140 docstring const ControlViewSource::title() const
141 {
142         switch (docType()) {
143                 case LATEX:
144                         return _("LaTeX Source");
145                 case DOCBOOK:
146                         return _("DocBook Source");
147                 case LITERATE:
148                         return _("Literate Source");
149                 default:
150                         BOOST_ASSERT(false);
151                         return docstring();
152         }
153 }
154
155
156 Dialog * createGuiViewSource(LyXView & lv)
157 {
158         return new GuiViewSource(static_cast<GuiViewBase &>(lv));
159 }
160
161
162 } // namespace frontend
163 } // namespace lyx
164
165 #include "GuiViewSource_moc.cpp"