2 * \file GuiViewSource.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiApplication.h"
16 #include "GuiViewSource.h"
17 #include "LaTeXHighlighter.h"
18 #include "qt_helpers.h"
20 #include "BufferView.h"
23 #include "Paragraph.h"
26 #include "support/debug.h"
27 #include "support/lassert.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
31 #include <boost/crc.hpp>
34 #include <QTextCursor>
35 #include <QTextDocument>
43 ViewSourceWidget::ViewSourceWidget()
44 : bv_(0), document_(new QTextDocument(this)),
45 highlighter_(new LaTeXHighlighter(document_))
49 connect(viewFullSourceCB, SIGNAL(clicked()),
50 this, SLOT(updateView()));
51 connect(autoUpdateCB, SIGNAL(toggled(bool)),
52 updatePB, SLOT(setDisabled(bool)));
53 connect(updatePB, SIGNAL(clicked()),
54 this, SLOT(updateView()));
56 // setting a document at this point trigger an assertion in Qt
57 // so we disable the signals here:
58 document_->blockSignals(true);
59 viewSourceTV->setDocument(document_);
60 viewSourceTV->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
61 document_->blockSignals(false);
62 viewSourceTV->setReadOnly(true);
63 ///dialog_->viewSourceTV->setAcceptRichText(false);
64 // this is personal. I think source code should be in fixed-size font
65 QFont font(guiApp->typewriterFontName());
66 font.setKerning(false);
67 font.setFixedPitch(true);
68 font.setStyleHint(QFont::TypeWriter);
69 viewSourceTV->setFont(font);
70 // again, personal taste
71 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
75 static size_t crcCheck(docstring const & s)
77 boost::crc_32_type crc;
78 crc.process_bytes(&s[0], sizeof(char_type) * s.size());
79 return crc.checksum();
83 /** get the source code of selected paragraphs, or the whole document
84 \param fullSource get full source code
85 \return true if the content has changed since last call.
87 static bool getContent(BufferView const * view, bool fullSource, QString & qstr)
89 // get the *top* level paragraphs that contain the cursor,
90 // or the selected text
94 if (!view->cursor().selection()) {
95 par_begin = view->cursor().bottom().pit();
98 par_begin = view->cursor().selectionBegin().bottom().pit();
99 par_end = view->cursor().selectionEnd().bottom().pit();
101 if (par_begin > par_end)
102 swap(par_begin, par_end);
103 odocstringstream ostr;
104 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
105 docstring s = ostr.str();
106 static size_t crc = 0;
107 size_t newcrc = crcCheck(s);
116 void ViewSourceWidget::setBufferView(BufferView const * bv)
122 void ViewSourceWidget::updateView()
125 document_->setPlainText(QString());
131 if (getContent(bv_, viewFullSourceCB->isChecked(), content))
132 document_->setPlainText(content);
134 CursorSlice beg = bv_->cursor().selectionBegin().bottom();
135 CursorSlice end = bv_->cursor().selectionEnd().bottom();
136 int const begrow = bv_->buffer().texrow().
137 getRowFromIdPos(beg.paragraph().id(), beg.pos());
138 int endrow = bv_->buffer().texrow().
139 getRowFromIdPos(end.paragraph().id(), end.pos());
140 int const nextendrow = bv_->buffer().texrow().
141 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
142 if (endrow != nextendrow)
143 endrow = nextendrow - 1;
145 QTextCursor c = QTextCursor(viewSourceTV->document());
146 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, begrow);
147 c.select(QTextCursor::BlockUnderCursor);
148 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
149 endrow - begrow + 1);
150 viewSourceTV->setTextCursor(c);
154 GuiViewSource::GuiViewSource(GuiView & parent,
155 Qt::DockWidgetArea area, Qt::WindowFlags flags)
156 : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
158 widget_ = new ViewSourceWidget();
163 GuiViewSource::~GuiViewSource()
169 void GuiViewSource::updateView()
171 if (widget_->autoUpdateCB->isChecked()) {
172 widget_->setBufferView(bufferview());
173 widget_->updateView();
178 void GuiViewSource::enableView(bool enable)
181 // In the opposite case, updateView() will be called anyway.
182 widget_->setBufferView(bufferview());
183 widget_->updateView();
185 widget_->setEnabled(enable);
189 bool GuiViewSource::initialiseParams(string const & /*source*/)
191 setWindowTitle(title());
196 QString GuiViewSource::title() const
200 return qt_("LaTeX Source");
202 return qt_("DocBook Source");
204 return qt_("Literate Source");
206 LASSERT(false, /**/);
211 void GuiViewSource::saveSession() const
213 Dialog::saveSession();
216 sessionKey() + "/fullsource", widget_->viewFullSourceCB->isChecked());
218 sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked());
222 void GuiViewSource::restoreSession()
224 DockView::restoreSession();
225 // FIXME: Full source updating is too slow to be done at startup.
226 //widget_->viewFullSourceCB->setChecked(
227 // settings.value(sessionKey() + "/fullsource", false).toBool());
228 widget_->viewFullSourceCB->setChecked(false);
230 widget_->autoUpdateCB->setChecked(
231 settings.value(sessionKey() + "/autoupdate", true).toBool());
235 Dialog * createGuiViewSource(GuiView & lv)
237 return new GuiViewSource(lv);
241 } // namespace frontend
244 #include "moc_GuiViewSource.cpp"