]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiViewSource.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
index b83082336fdaeddf0aa763069180fe2338ad86d6..a5beae5da691e673c05fa55d77e9ae9274527128 100644 (file)
@@ -28,6 +28,8 @@
 #include "support/docstream.h"
 #include "support/gettext.h"
 
+#include <boost/crc.hpp>
+
 #include <QSettings>
 #include <QTextCursor>
 #include <QTextDocument>
@@ -48,6 +50,8 @@ ViewSourceWidget::ViewSourceWidget()
                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()));
 
@@ -69,10 +73,19 @@ ViewSourceWidget::ViewSourceWidget()
 }
 
 
+static size_t crcCheck(docstring const & s)
+{
+       boost::crc_32_type crc;
+       crc.process_bytes(&s[0], sizeof(char_type) * s.size());
+       return crc.checksum();
+}
+
+
 /** 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 QString getContent(BufferView const * view, bool fullSource)
+static bool getContent(BufferView const * view, bool fullSource, QString & qstr)
 {
        // get the *top* level paragraphs that contain the cursor,
        // or the selected text
@@ -90,13 +103,21 @@ static QString getContent(BufferView const * view, bool fullSource)
                swap(par_begin, par_end);
        odocstringstream ostr;
        view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
-       return toqstr(ostr.str());
+       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;
 }
 
 
 void ViewSourceWidget::setBufferView(BufferView const * bv)
 {
        bv_ = bv;
+       setEnabled(bv ?  true : false);
 }
 
 
@@ -107,8 +128,12 @@ void ViewSourceWidget::updateView()
                setEnabled(false);
                return;
        }
+       
+       setEnabled(true);
 
-       document_->setPlainText(getContent(bv_, viewFullSourceCB->isChecked()));
+       QString content;
+       if (getContent(bv_, viewFullSourceCB->isChecked(), content))
+               document_->setPlainText(content);
 
        CursorSlice beg = bv_->cursor().selectionBegin().bottom();
        CursorSlice end = bv_->cursor().selectionEnd().bottom();
@@ -156,12 +181,10 @@ void GuiViewSource::updateView()
 
 void GuiViewSource::enableView(bool enable)
 {
-       if (!enable) {
+       widget_->setBufferView(bufferview());
+       if (!enable)
                // In the opposite case, updateView() will be called anyway.
-               widget_->setBufferView(bufferview());
                widget_->updateView();
-       }
-       widget_->setEnabled(enable);
 }
 
 
@@ -208,6 +231,7 @@ void GuiViewSource::restoreSession()
        QSettings settings;
        widget_->autoUpdateCB->setChecked(
                settings.value(sessionKey() + "/autoupdate", true).toBool());
+       widget_->updateView();
 }
 
 
@@ -220,4 +244,4 @@ Dialog * createGuiViewSource(GuiView & lv)
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiViewSource_moc.cpp"
+#include "moc_GuiViewSource.cpp"