]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiViewSource.cpp
On Linux show in crash message box the backtrace
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
index e538090c87b214c66131826056289cff4b8f50c6..b2df874437206bde096e139e8559bd5bab06ac1b 100644 (file)
@@ -17,8 +17,9 @@
 #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"
@@ -31,6 +32,7 @@
 
 #include <boost/crc.hpp>
 
+#include <QBoxLayout>
 #include <QSettings>
 #include <QTextCursor>
 #include <QTextDocument>
@@ -48,16 +50,18 @@ ViewSourceWidget::ViewSourceWidget()
 {
        setupUi(this);
 
-       connect(viewFullSourceCB, SIGNAL(clicked()),
-               this, SLOT(fullSourceChanged()));
+       connect(contentsCO, SIGNAL(activated(int)),
+               this, SLOT(contentsChanged()));
        connect(autoUpdateCB, SIGNAL(toggled(bool)),
                updatePB, SLOT(setDisabled(bool)));
        connect(autoUpdateCB, SIGNAL(toggled(bool)),
                this, SLOT(updateView()));
+       connect(masterPerspectiveCB, SIGNAL(toggled(bool)),
+               this, SLOT(updateView()));
        connect(updatePB, SIGNAL(clicked()),
                this, SLOT(updateView()));
        connect(outputFormatCO, SIGNAL(activated(int)),
-               this, SLOT(updateView()));
+               this, SLOT(setViewFormat()));
 
        // setting a document at this point trigger an assertion in Qt
        // so we disable the signals here:
@@ -89,8 +93,9 @@ 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, string const format, bool force_getcontent)
+static bool getContent(BufferView const * view, Buffer::OutputWhat output,
+                      QString & qstr, string const format, bool force_getcontent,
+                      bool master)
 {
        // get the *top* level paragraphs that contain the cursor,
        // or the selected text
@@ -107,8 +112,12 @@ static bool getContent(BufferView const * view, bool fullSource,
        if (par_begin > par_end)
                swap(par_begin, par_end);
        odocstringstream ostr;
-       view->buffer().getSourceCode(ostr, format, par_begin, par_end + 1, fullSource);
+       view->buffer().getSourceCode(ostr, format, par_begin, par_end + 1,
+                                    output, master);
        docstring s = ostr.str();
+       // FIXME THREAD
+       // Could this be private to this particular dialog? We could have
+       // more than one of these, in different windows.
        static size_t crc = 0;
        size_t newcrc = crcCheck(s);
        if (newcrc == crc && !force_getcontent)
@@ -128,13 +137,21 @@ void ViewSourceWidget::setBufferView(BufferView const * bv)
 }
 
 
-void ViewSourceWidget::fullSourceChanged()
+void ViewSourceWidget::contentsChanged()
 {
        if (autoUpdateCB->isChecked())
                updateView();
 }
 
 
+void ViewSourceWidget::setViewFormat()
+{
+       view_format_ = outputFormatCO->itemData(
+             outputFormatCO->currentIndex()).toString();
+       updateView();
+}
+
+
 void ViewSourceWidget::updateView()
 {
        if (!bv_) {
@@ -145,12 +162,19 @@ void ViewSourceWidget::updateView()
 
        setEnabled(true);
 
-       string const format = fromqstr(outputFormatCO->itemData(
-               outputFormatCO->currentIndex()).toString());
+       string const format = fromqstr(view_format_);
 
        QString content;
-       if (getContent(bv_, viewFullSourceCB->isChecked(), content,
-                 format, force_getcontent_))
+       Buffer::OutputWhat output = Buffer::CurrentParagraph;
+       if (contentsCO->currentIndex() == 1)
+               output = Buffer::FullSource;
+       else if (contentsCO->currentIndex() == 2)
+               output = Buffer::OnlyPreamble;
+       else if (contentsCO->currentIndex() == 3)
+               output = Buffer::OnlyBody;
+
+       if (getContent(bv_, output, content, format,
+                     force_getcontent_, masterPerspectiveCB->isChecked()))
                document_->setPlainText(content);
 
        CursorSlice beg = bv_->cursor().selectionBegin().bottom();
@@ -182,22 +206,49 @@ void ViewSourceWidget::updateDefaultFormat()
        outputFormatCO->clear();
        outputFormatCO->addItem(qt_("Default"),
                                QVariant(QString("default")));
-       typedef vector<Format const *> Formats;
-       Formats formats = bv_->buffer().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())));
+
+       int index = 0;
+       vector<string> tmp = bv_->buffer().params().backends();
+       vector<string>::const_iterator it = tmp.begin();
+       vector<string>::const_iterator en = tmp.end();
+       for (; it != en; ++it) {
+               string const format = *it;
+               Format const * fmt = formats.getFormat(format);
+               if (!fmt) {
+                       LYXERR0("Can't find format for backend " << format << "!");
+                       continue;
+               } 
+
+               QString const pretty = qt_(fmt->prettyname());
+               QString const qformat = toqstr(format);
+               outputFormatCO->addItem(pretty, QVariant(qformat));
+               if (qformat == view_format_)
+                  index = outputFormatCO->count() -1;
+       }
+       outputFormatCO->setCurrentIndex(index);
+
        outputFormatCO->blockSignals(false);
 }
 
 
+void ViewSourceWidget::resizeEvent (QResizeEvent * event)
+{
+       QSize const & formSize = formLayout->sizeHint();
+       // minimize the size of the part that contains the buttons
+       if (width() * formSize.height() < height() * formSize.width()) {
+               layout_->setDirection(QBoxLayout::TopToBottom);
+       } else {
+               layout_->setDirection(QBoxLayout::LeftToRight);
+       }
+       QWidget::resizeEvent(event);
+}
+
+
 GuiViewSource::GuiViewSource(GuiView & parent,
                Qt::DockWidgetArea area, Qt::WindowFlags flags)
        : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
 {
-       widget_ = new ViewSourceWidget();
+       widget_ = new ViewSourceWidget;
        setWidget(widget_);
 }
 
@@ -214,6 +265,7 @@ void GuiViewSource::updateView()
                widget_->setBufferView(bufferview());
                widget_->updateView();
        }
+       widget_->masterPerspectiveCB->setEnabled(buffer().parent());
 }
 
 
@@ -244,7 +296,7 @@ QString GuiViewSource::title() const
                case LITERATE:
                        return qt_("Literate Source");
        }
-       LASSERT(false, /**/);
+       LATTEST(false);
        return QString();
 }
 
@@ -253,8 +305,9 @@ void GuiViewSource::saveSession() const
 {
        Dialog::saveSession();
        QSettings settings;
-       settings.setValue(
-               sessionKey() + "/fullsource", widget_->viewFullSourceCB->isChecked());
+       // see below
+       // settings.setValue(
+       //      sessionKey() + "/output", widget_->contentsCO->currentIndex());
        settings.setValue(
                sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked());
 }
@@ -264,9 +317,9 @@ 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);
+       //widget_->outputCO-setCurrentIndex(
+       //      settings.value(sessionKey() + "/output", false).toInt());
+       widget_->contentsCO->setCurrentIndex(0);
        QSettings settings;
        widget_->autoUpdateCB->setChecked(
                settings.value(sessionKey() + "/autoupdate", true).toBool());