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