]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiViewSource.cpp
Fix the tab ordering of GuiDocument components.
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
index 1bfd7a291922289ec063d616d713185ec2266561..345bfedd44968b3644fa46b477b8304d49db6cc5 100644 (file)
 #include "LaTeXHighlighter.h"
 #include "qt_helpers.h"
 
-#include "BufferView.h"
 #include "Buffer.h"
+#include "BufferParams.h"
+#include "BufferView.h"
 #include "Cursor.h"
+#include "Format.h"
 #include "Paragraph.h"
 #include "TexRow.h"
 
@@ -42,18 +44,21 @@ namespace frontend {
 
 ViewSourceWidget::ViewSourceWidget()
        :       bv_(0), document_(new QTextDocument(this)),
-               highlighter_(new LaTeXHighlighter(document_))
+               highlighter_(new LaTeXHighlighter(document_)),
+               force_getcontent_(true)
 {
        setupUi(this);
 
        connect(viewFullSourceCB, SIGNAL(clicked()),
-               this, SLOT(updateView()));
+               this, SLOT(fullSourceChanged()));
        connect(autoUpdateCB, SIGNAL(toggled(bool)),
                updatePB, SLOT(setDisabled(bool)));
        connect(autoUpdateCB, SIGNAL(toggled(bool)),
                this, SLOT(updateView()));
        connect(updatePB, SIGNAL(clicked()),
                this, SLOT(updateView()));
+       connect(outputFormatCO, SIGNAL(activated(int)),
+               this, SLOT(updateView()));
 
        // setting a document at this point trigger an assertion in Qt
        // so we disable the signals here:
@@ -85,7 +90,8 @@ static size_t crcCheck(docstring const & s)
        \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)
+static bool getContent(BufferView const * view, bool fullSource,
+                      QString & qstr, string const format, bool force_getcontent)
 {
        // get the *top* level paragraphs that contain the cursor,
        // or the selected text
@@ -102,12 +108,11 @@ static bool getContent(BufferView const * view, bool fullSource, QString & qstr)
        if (par_begin > par_end)
                swap(par_begin, par_end);
        odocstringstream ostr;
-       const_cast<BufferView *>(view)->buffer().getSourceCode(
-               ostr, par_begin, par_end + 1, fullSource);
+       view->buffer().getSourceCode(ostr, format, par_begin, par_end + 1, fullSource);
        docstring s = ostr.str();
        static size_t crc = 0;
        size_t newcrc = crcCheck(s);
-       if (newcrc == crc)
+       if (newcrc == crc && !force_getcontent)
                return false;
        crc = newcrc;
        qstr = toqstr(s);
@@ -117,11 +122,20 @@ static bool getContent(BufferView const * view, bool fullSource, QString & qstr)
 
 void ViewSourceWidget::setBufferView(BufferView const * bv)
 {
+       if (bv_ != bv)
+               force_getcontent_ = true;
        bv_ = bv;
        setEnabled(bv ?  true : false);
 }
 
 
+void ViewSourceWidget::fullSourceChanged()
+{
+       if (autoUpdateCB->isChecked())
+               updateView();
+}
+
+
 void ViewSourceWidget::updateView()
 {
        if (!bv_) {
@@ -129,11 +143,15 @@ void ViewSourceWidget::updateView()
                setEnabled(false);
                return;
        }
-       
+
        setEnabled(true);
 
+       string const format = fromqstr(outputFormatCO->itemData(
+               outputFormatCO->currentIndex()).toString());
+
        QString content;
-       if (getContent(bv_, viewFullSourceCB->isChecked(), content))
+       if (getContent(bv_, viewFullSourceCB->isChecked(), content,
+                 format, force_getcontent_))
                document_->setPlainText(content);
 
        CursorSlice beg = bv_->cursor().selectionBegin().bottom();
@@ -156,6 +174,26 @@ void ViewSourceWidget::updateView()
 }
 
 
+void ViewSourceWidget::updateDefaultFormat()
+{
+       if (!bv_)
+               return;
+
+       outputFormatCO->blockSignals(true);
+       outputFormatCO->clear();
+       outputFormatCO->addItem(qt_("Default"),
+                               QVariant(QString("default")));
+       typedef vector<Format const *> Formats;
+       Formats formats = bv_->buffer().params().exportableFormats(true);
+       Formats::const_iterator cit = formats.begin();
+       Formats::const_iterator end = formats.end();
+       for (; cit != end; ++cit)
+               outputFormatCO->addItem(qt_((*cit)->prettyname()),
+                               QVariant(toqstr((*cit)->name())));
+       outputFormatCO->blockSignals(false);
+}
+
+
 GuiViewSource::GuiViewSource(GuiView & parent,
                Qt::DockWidgetArea area, Qt::WindowFlags flags)
        : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
@@ -183,6 +221,7 @@ void GuiViewSource::updateView()
 void GuiViewSource::enableView(bool enable)
 {
        widget_->setBufferView(bufferview());
+       widget_->updateDefaultFormat();
        if (!enable)
                // In the opposite case, updateView() will be called anyway.
                widget_->updateView();