]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.cpp
Fix bug #7906. Check the widgets after creating the dialog.
[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 "Buffer.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Cursor.h"
24 #include "Format.h"
25 #include "Paragraph.h"
26 #include "TexRow.h"
27
28 #include "support/debug.h"
29 #include "support/lassert.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32
33 #include <boost/crc.hpp>
34
35 #include <QSettings>
36 #include <QTextCursor>
37 #include <QTextDocument>
38 #include <QVariant>
39
40 using namespace std;
41
42 namespace lyx {
43 namespace frontend {
44
45 ViewSourceWidget::ViewSourceWidget()
46         :       bv_(0), document_(new QTextDocument(this)),
47                 highlighter_(new LaTeXHighlighter(document_)),
48                 force_getcontent_(true)
49 {
50         setupUi(this);
51
52         connect(contentsCO, SIGNAL(activated(int)),
53                 this, SLOT(contentsChanged()));
54         connect(autoUpdateCB, SIGNAL(toggled(bool)),
55                 updatePB, SLOT(setDisabled(bool)));
56         connect(autoUpdateCB, SIGNAL(toggled(bool)),
57                 this, SLOT(updateView()));
58         connect(updatePB, SIGNAL(clicked()),
59                 this, SLOT(updateView()));
60         connect(outputFormatCO, SIGNAL(activated(int)),
61                 this, SLOT(updateView()));
62
63         // setting a document at this point trigger an assertion in Qt
64         // so we disable the signals here:
65         document_->blockSignals(true);
66         viewSourceTV->setDocument(document_);
67         document_->blockSignals(false);
68         viewSourceTV->setReadOnly(true);
69         ///dialog_->viewSourceTV->setAcceptRichText(false);
70         // this is personal. I think source code should be in fixed-size font
71         QFont font(guiApp->typewriterFontName());
72         font.setKerning(false);
73         font.setFixedPitch(true);
74         font.setStyleHint(QFont::TypeWriter);
75         viewSourceTV->setFont(font);
76         // again, personal taste
77         viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
78 }
79
80
81 static size_t crcCheck(docstring const & s)
82 {
83         boost::crc_32_type crc;
84         crc.process_bytes(&s[0], sizeof(char_type) * s.size());
85         return crc.checksum();
86 }
87
88
89 /** get the source code of selected paragraphs, or the whole document
90         \param fullSource get full source code
91         \return true if the content has changed since last call.
92  */
93 static bool getContent(BufferView const * view, Buffer::OutputWhat output,
94                        QString & qstr, string const format, bool force_getcontent)
95 {
96         // get the *top* level paragraphs that contain the cursor,
97         // or the selected text
98         pit_type par_begin;
99         pit_type par_end;
100
101         if (!view->cursor().selection()) {
102                 par_begin = view->cursor().bottom().pit();
103                 par_end = par_begin;
104         } else {
105                 par_begin = view->cursor().selectionBegin().bottom().pit();
106                 par_end = view->cursor().selectionEnd().bottom().pit();
107         }
108         if (par_begin > par_end)
109                 swap(par_begin, par_end);
110         odocstringstream ostr;
111         view->buffer().getSourceCode(ostr, format, par_begin, par_end + 1, output);
112         docstring s = ostr.str();
113         static size_t crc = 0;
114         size_t newcrc = crcCheck(s);
115         if (newcrc == crc && !force_getcontent)
116                 return false;
117         crc = newcrc;
118         qstr = toqstr(s);
119         return true;
120 }
121
122
123 void ViewSourceWidget::setBufferView(BufferView const * bv)
124 {
125         if (bv_ != bv)
126                 force_getcontent_ = true;
127         bv_ = bv;
128         setEnabled(bv ?  true : false);
129 }
130
131
132 void ViewSourceWidget::contentsChanged()
133 {
134         if (autoUpdateCB->isChecked())
135                 updateView();
136 }
137
138
139 void ViewSourceWidget::updateView()
140 {
141         if (!bv_) {
142                 document_->setPlainText(QString());
143                 setEnabled(false);
144                 return;
145         }
146
147         setEnabled(true);
148
149         string const format = fromqstr(outputFormatCO->itemData(
150                 outputFormatCO->currentIndex()).toString());
151
152         QString content;
153         Buffer::OutputWhat output = Buffer::CurrentParagraph;
154         if (contentsCO->currentIndex() == 1)
155                 output = Buffer::FullSource;
156         else if (contentsCO->currentIndex() == 2)
157                 output = Buffer::OnlyPreamble;
158         else if (contentsCO->currentIndex() == 3)
159                 output = Buffer::OnlyBody;
160
161         if (getContent(bv_, output, content, format, force_getcontent_))
162                 document_->setPlainText(content);
163
164         CursorSlice beg = bv_->cursor().selectionBegin().bottom();
165         CursorSlice end = bv_->cursor().selectionEnd().bottom();
166         int const begrow = bv_->buffer().texrow().
167                 getRowFromIdPos(beg.paragraph().id(), beg.pos());
168         int endrow = bv_->buffer().texrow().
169                 getRowFromIdPos(end.paragraph().id(), end.pos());
170         int const nextendrow = bv_->buffer().texrow().
171                 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
172         if (endrow != nextendrow)
173                 endrow = nextendrow - 1;
174
175         QTextCursor c = QTextCursor(viewSourceTV->document());
176         c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, begrow);
177         c.select(QTextCursor::BlockUnderCursor);
178         c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
179                 endrow - begrow + 1);
180         viewSourceTV->setTextCursor(c);
181 }
182
183
184 void ViewSourceWidget::updateDefaultFormat()
185 {
186         if (!bv_)
187                 return;
188
189         outputFormatCO->blockSignals(true);
190         outputFormatCO->clear();
191         outputFormatCO->addItem(qt_("Default"),
192                                 QVariant(QString("default")));
193         typedef vector<Format const *> Formats;
194         Formats formats = bv_->buffer().params().exportableFormats(true);
195         Formats::const_iterator cit = formats.begin();
196         Formats::const_iterator end = formats.end();
197         for (; cit != end; ++cit)
198                 outputFormatCO->addItem(qt_((*cit)->prettyname()),
199                                 QVariant(toqstr((*cit)->name())));
200         outputFormatCO->blockSignals(false);
201 }
202
203
204 GuiViewSource::GuiViewSource(GuiView & parent,
205                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
206         : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
207 {
208         widget_ = new ViewSourceWidget();
209         setWidget(widget_);
210 }
211
212
213 GuiViewSource::~GuiViewSource()
214 {
215         delete widget_;
216 }
217
218
219 void GuiViewSource::updateView()
220 {
221         if (widget_->autoUpdateCB->isChecked()) {
222                 widget_->setBufferView(bufferview());
223                 widget_->updateView();
224         }
225 }
226
227
228 void GuiViewSource::enableView(bool enable)
229 {
230         widget_->setBufferView(bufferview());
231         widget_->updateDefaultFormat();
232         if (!enable)
233                 // In the opposite case, updateView() will be called anyway.
234                 widget_->updateView();
235 }
236
237
238 bool GuiViewSource::initialiseParams(string const & /*source*/)
239 {
240         setWindowTitle(title());
241         return true;
242 }
243
244
245 QString GuiViewSource::title() const
246 {
247         switch (docType()) {
248                 case LATEX:
249                         return qt_("LaTeX Source");
250                 case DOCBOOK:
251                         return qt_("DocBook Source");
252                 case LITERATE:
253                         return qt_("Literate Source");
254         }
255         LASSERT(false, /**/);
256         return QString();
257 }
258
259
260 void GuiViewSource::saveSession() const
261 {
262         Dialog::saveSession();
263         QSettings settings;
264         // see below
265         // settings.setValue(
266         //      sessionKey() + "/output", widget_->contentsCO->currentIndex());
267         settings.setValue(
268                 sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked());
269 }
270
271
272 void GuiViewSource::restoreSession()
273 {
274         DockView::restoreSession();
275         // FIXME: Full source updating is too slow to be done at startup.
276         //widget_->outputCO-setCurrentIndex(
277         //      settings.value(sessionKey() + "/output", false).toInt());
278         widget_->contentsCO->setCurrentIndex(0);
279         QSettings settings;
280         widget_->autoUpdateCB->setChecked(
281                 settings.value(sessionKey() + "/autoupdate", true).toBool());
282         widget_->updateView();
283 }
284
285
286 Dialog * createGuiViewSource(GuiView & lv)
287 {
288         return new GuiViewSource(lv);
289 }
290
291
292 } // namespace frontend
293 } // namespace lyx
294
295 #include "moc_GuiViewSource.cpp"