X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiViewSource.cpp;h=a5beae5da691e673c05fa55d77e9ae9274527128;hb=425d092204118ea6c24c28e85fdf03fcf2bb51a4;hp=d1e14c819fe975cb9c5acc462722786e792d2bbc;hpb=9d0ea8aeff32833a90b3fe64df0c5518a9e241be;p=lyx.git diff --git a/src/frontends/qt4/GuiViewSource.cpp b/src/frontends/qt4/GuiViewSource.cpp index d1e14c819f..a5beae5da6 100644 --- a/src/frontends/qt4/GuiViewSource.cpp +++ b/src/frontends/qt4/GuiViewSource.cpp @@ -20,32 +20,38 @@ #include "BufferView.h" #include "Buffer.h" #include "Cursor.h" -#include "support/gettext.h" #include "Paragraph.h" #include "TexRow.h" +#include "support/debug.h" +#include "support/lassert.h" #include "support/docstream.h" +#include "support/gettext.h" + +#include +#include #include #include -#include +#include -using std::string; +using namespace std; namespace lyx { namespace frontend { -ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller) - : controller_(controller), document_(new QTextDocument(this)), +ViewSourceWidget::ViewSourceWidget() + : bv_(0), document_(new QTextDocument(this)), highlighter_(new LaTeXHighlighter(document_)) { setupUi(this); - setWindowTitle(qt_("LaTeX Source")); connect(viewFullSourceCB, SIGNAL(clicked()), this, SLOT(updateView())); connect(autoUpdateCB, SIGNAL(toggled(bool)), updatePB, SLOT(setDisabled(bool))); + connect(autoUpdateCB, SIGNAL(toggled(bool)), + this, SLOT(updateView())); connect(updatePB, SIGNAL(clicked()), this, SLOT(updateView())); @@ -67,91 +73,125 @@ ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller) } -void ViewSourceWidget::updateView() +static size_t crcCheck(docstring const & s) { - if (autoUpdateCB->isChecked()) - update(viewFullSourceCB->isChecked()); - - int beg, end; - boost::tie(beg, end) = controller_.getRows(); - QTextCursor c = QTextCursor(viewSourceTV->document()); - c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg); - c.select(QTextCursor::BlockUnderCursor); - c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1); - viewSourceTV->setTextCursor(c); + boost::crc_32_type crc; + crc.process_bytes(&s[0], sizeof(char_type) * s.size()); + return crc.checksum(); } -void ViewSourceWidget::update(bool full_source) +/** get the source code of selected paragraphs, or the whole document + \param fullSource get full source code + \return true if the content has changed since last call. + */ +static bool getContent(BufferView const * view, bool fullSource, QString & qstr) { - document_->setPlainText(controller_.getContent(full_source)); + // get the *top* level paragraphs that contain the cursor, + // or the selected text + pit_type par_begin; + pit_type par_end; + + if (!view->cursor().selection()) { + par_begin = view->cursor().bottom().pit(); + par_end = par_begin; + } else { + par_begin = view->cursor().selectionBegin().bottom().pit(); + par_end = view->cursor().selectionEnd().bottom().pit(); + } + if (par_begin > par_end) + swap(par_begin, par_end); + odocstringstream ostr; + view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource); + docstring s = ostr.str(); + static size_t crc = 0; + size_t newcrc = crcCheck(s); + if (newcrc == crc) + return false; + crc = newcrc; + qstr = toqstr(s); + return true; } -GuiViewSource::GuiViewSource(GuiView & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags) - : DockView(parent, "view-source", area, flags) +void ViewSourceWidget::setBufferView(BufferView const * bv) { - widget_ = new ViewSourceWidget(*this); - setWidget(widget_); - setWindowTitle(widget_->windowTitle()); + bv_ = bv; + setEnabled(bv ? true : false); } -GuiViewSource::~GuiViewSource() +void ViewSourceWidget::updateView() { - delete widget_; + if (!bv_) { + document_->setPlainText(QString()); + setEnabled(false); + return; + } + + setEnabled(true); + + QString content; + if (getContent(bv_, viewFullSourceCB->isChecked(), content)) + document_->setPlainText(content); + + CursorSlice beg = bv_->cursor().selectionBegin().bottom(); + CursorSlice end = bv_->cursor().selectionEnd().bottom(); + int const begrow = bv_->buffer().texrow(). + getRowFromIdPos(beg.paragraph().id(), beg.pos()); + int endrow = bv_->buffer().texrow(). + getRowFromIdPos(end.paragraph().id(), end.pos()); + int const nextendrow = bv_->buffer().texrow(). + getRowFromIdPos(end.paragraph().id(), end.pos() + 1); + if (endrow != nextendrow) + endrow = nextendrow - 1; + + QTextCursor c = QTextCursor(viewSourceTV->document()); + c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, begrow); + c.select(QTextCursor::BlockUnderCursor); + c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, + endrow - begrow + 1); + viewSourceTV->setTextCursor(c); } -void GuiViewSource::updateView() +GuiViewSource::GuiViewSource(GuiView & parent, + Qt::DockWidgetArea area, Qt::WindowFlags flags) + : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags) { - widget_->updateView(); + widget_ = new ViewSourceWidget(); + setWidget(widget_); } -bool GuiViewSource::initialiseParams(string const & /*source*/) +GuiViewSource::~GuiViewSource() { - setWindowTitle(title()); - return true; + delete widget_; } -QString GuiViewSource::getContent(bool fullSource) +void GuiViewSource::updateView() { - // get the *top* level paragraphs that contain the cursor, - // or the selected text - pit_type par_begin; - pit_type par_end; - - BufferView * view = bufferview(); - if (!view->cursor().selection()) { - par_begin = view->cursor().bottom().pit(); - par_end = par_begin; - } else { - par_begin = view->cursor().selectionBegin().bottom().pit(); - par_end = view->cursor().selectionEnd().bottom().pit(); + if (widget_->autoUpdateCB->isChecked()) { + widget_->setBufferView(bufferview()); + widget_->updateView(); } - if (par_begin > par_end) - std::swap(par_begin, par_end); - odocstringstream ostr; - view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource); - return toqstr(ostr.str()); } -std::pair GuiViewSource::getRows() const +void GuiViewSource::enableView(bool enable) { - BufferView const * view = bufferview(); - CursorSlice beg = view->cursor().selectionBegin().bottom(); - CursorSlice end = view->cursor().selectionEnd().bottom(); + widget_->setBufferView(bufferview()); + if (!enable) + // In the opposite case, updateView() will be called anyway. + widget_->updateView(); +} - int begrow = view->buffer().texrow(). - getRowFromIdPos(beg.paragraph().id(), beg.pos()); - int endrow = view->buffer().texrow(). - getRowFromIdPos(end.paragraph().id(), end.pos()); - int nextendrow = view->buffer().texrow(). - getRowFromIdPos(end.paragraph().id(), end.pos() + 1); - return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1)); + +bool GuiViewSource::initialiseParams(string const & /*source*/) +{ + setWindowTitle(title()); + return true; } @@ -165,18 +205,43 @@ QString GuiViewSource::title() const case LITERATE: return qt_("Literate Source"); } - BOOST_ASSERT(false); + LASSERT(false, /**/); return QString(); } +void GuiViewSource::saveSession() const +{ + Dialog::saveSession(); + QSettings settings; + settings.setValue( + sessionKey() + "/fullsource", widget_->viewFullSourceCB->isChecked()); + settings.setValue( + sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked()); +} + + +void GuiViewSource::restoreSession() +{ + DockView::restoreSession(); + // FIXME: Full source updating is too slow to be done at startup. + //widget_->viewFullSourceCB->setChecked( + // settings.value(sessionKey() + "/fullsource", false).toBool()); + widget_->viewFullSourceCB->setChecked(false); + QSettings settings; + widget_->autoUpdateCB->setChecked( + settings.value(sessionKey() + "/autoupdate", true).toBool()); + widget_->updateView(); +} + + Dialog * createGuiViewSource(GuiView & lv) { - return new GuiViewSource(static_cast(lv)); + return new GuiViewSource(lv); } } // namespace frontend } // namespace lyx -#include "GuiViewSource_moc.cpp" +#include "moc_GuiViewSource.cpp"