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