]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.cpp
d6b141b969b6bced755bba23b96cda1ae095ff5a
[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 "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 ViewSourceWidget::ViewSourceWidget(GuiViewSource & 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 ViewSourceWidget::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 }
81
82
83 void ViewSourceWidget::update(bool full_source)
84 {
85         document_->setPlainText(controller_.getContent(full_source));
86 }
87
88
89 GuiViewSource::GuiViewSource(GuiViewBase & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
90         : DockView(parent, "view-source", area, flags)
91 {
92         widget_ = new ViewSourceWidget(*this);
93         setWidget(widget_);
94         setWindowTitle(widget_->windowTitle());
95 }
96
97
98 GuiViewSource::~GuiViewSource()
99 {
100         delete widget_;
101 }
102
103
104 void GuiViewSource::updateView()
105 {
106         widget_->updateView();
107 }
108
109
110 bool GuiViewSource::initialiseParams(string const & /*source*/)
111 {
112         setWindowTitle(title());
113         return true;
114 }
115
116
117 QString GuiViewSource::getContent(bool fullSource)
118 {
119         // get the *top* level paragraphs that contain the cursor,
120         // or the selected text
121         pit_type par_begin;
122         pit_type par_end;
123
124         BufferView * view = bufferview();
125         if (!view->cursor().selection()) {
126                 par_begin = view->cursor().bottom().pit();
127                 par_end = par_begin;
128         } else {
129                 par_begin = view->cursor().selectionBegin().bottom().pit();
130                 par_end = view->cursor().selectionEnd().bottom().pit();
131         }
132         if (par_begin > par_end)
133                 std::swap(par_begin, par_end);
134         odocstringstream ostr;
135         view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
136         return toqstr(ostr.str());
137 }
138
139
140 std::pair<int, int> GuiViewSource::getRows() const
141 {
142         BufferView const * view = bufferview();
143         CursorSlice beg = view->cursor().selectionBegin().bottom();
144         CursorSlice end = view->cursor().selectionEnd().bottom();
145
146         int begrow = view->buffer().texrow().
147                 getRowFromIdPos(beg.paragraph().id(), beg.pos());
148         int endrow = view->buffer().texrow().
149                 getRowFromIdPos(end.paragraph().id(), end.pos());
150         int nextendrow = view->buffer().texrow().
151                 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
152         return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1));
153 }
154
155
156 QString GuiViewSource::title() const
157 {
158         switch (docType()) {
159                 case LATEX:
160                         return qt_("LaTeX Source");
161                 case DOCBOOK:
162                         return qt_("DocBook Source");
163                 case LITERATE:
164                         return qt_("Literate Source");
165         }
166         BOOST_ASSERT(false);
167         return QString();
168 }
169
170
171 Dialog * createGuiViewSource(LyXView & lv)
172 {
173         return new GuiViewSource(static_cast<GuiViewBase &>(lv));
174 }
175
176
177 } // namespace frontend
178 } // namespace lyx
179
180 #include "GuiViewSource_moc.cpp"