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