]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiDocument.cpp
Fix readability
[lyx.git] / src / frontends / qt / GuiDocument.cpp
index 60f9f844b4aa66a640099d3cba1d9183ebc3f866..b16e8923bc8ef36a7d3f336f32e21f4889a6802a 100644 (file)
@@ -19,8 +19,8 @@
 #include "GuiApplication.h"
 #include "GuiBranches.h"
 #include "GuiIndices.h"
+#include "GuiView.h"
 #include "GuiSelectionManager.h"
-#include "LaTeXHighlighter.h"
 #include "Validator.h"
 
 #include "LayoutFile.h"
@@ -72,7 +72,6 @@
 #include <QAbstractItemModel>
 #include <QButtonGroup>
 #include <QColor>
-#include <QColorDialog>
 #include <QCloseEvent>
 #include <QDirIterator>
 #include <QFontDatabase>
@@ -324,6 +323,9 @@ void ModuleSelectionManager::updateAddPB()
        }
 
        QModelIndex const & idx = availableLV->selectionModel()->currentIndex();
+       
+       if (!idx.isValid())
+               return;
 
        if (getAvailableModel()->itemFromIndex(idx)->hasChildren()) {
                // This is a category header
@@ -472,12 +474,12 @@ void ModuleSelectionManager::updateDelPB()
 /////////////////////////////////////////////////////////////////////
 
 PreambleModule::PreambleModule(QWidget * parent)
-       : UiWidget<Ui::PreambleUi>(parent), current_id_(nullptr)
+       : UiWidget<Ui::PreambleUi>(parent), current_id_(nullptr),
+         highlighter_(new LaTeXHighlighter(preambleTE->document(), true))
 {
        // This is not a memory leak. The object will be destroyed
        // with this.
        // @ is letter in the LyX user preamble
-       (void) new LaTeXHighlighter(preambleTE->document(), true);
        preambleTE->setFont(guiApp->typewriterSystemFont());
        preambleTE->setWordWrapMode(QTextOption::NoWrap);
        setFocusProxy(preambleTE);
@@ -504,14 +506,22 @@ PreambleModule::PreambleModule(QWidget * parent)
 bool PreambleModule::eventFilter(QObject * sender, QEvent * event)
 {
        if (sender == findLE) {
-               if (event->type()==QEvent::KeyPress) {
-               QKeyEvent * key = static_cast<QKeyEvent *>(event);
-               if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
-                   findText();
+               if (event->type() == QEvent::KeyPress) {
+                       QKeyEvent * key = static_cast<QKeyEvent *>(event);
+                       if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
+                               findText();
                                // Return true to filter out the event
                                return true;
-               }
-           }
+                       }
+               }
+       }
+       if (event->type() == QEvent::ApplicationPaletteChange) {
+               // mode switch: colors need to be updated
+               // and the highlighting redone
+               if (highlighter_) {
+                       highlighter_->setupColors();
+                       highlighter_->rehighlight();
+               }
        }
        return QWidget::eventFilter(sender, event);
 }
@@ -611,7 +621,8 @@ void PreambleModule::editExternal() {
        preambleTE->setReadOnly(true);
        theFormats().edit(*current_id_, tempfilename, format);
        editPB->setText(qt_("&End Edit"));
-       QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
+       QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+                         : getPixmap("images/", "emblem-shellescape", "svgz,png"));
        editPB->setIcon(warn);
        changed();
 }
@@ -790,7 +801,8 @@ void LocalLayout::editExternal() {
        locallayoutTE->setReadOnly(true);
        theFormats().edit(*current_id_, tempfilename, format);
        editPB->setText(qt_("&End Edit"));
-       QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
+       QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+                         : getPixmap("images/", "emblem-shellescape", "svgz,png"));
        editPB->setIcon(warn);
        validatePB->setEnabled(false);
        hideConvert();
@@ -1260,7 +1272,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
        // margins
        marginsModule = new UiWidget<Ui::MarginsUi>(this);
-       connect(marginsModule->marginCB, SIGNAL(toggled(bool)),
+       connect(marginsModule->marginCB, SIGNAL(clicked(bool)),
                this, SLOT(setCustomMargins(bool)));
        connect(marginsModule->marginCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
@@ -1375,6 +1387,8 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(outputChangesToggled(bool)));
        connect(changesModule->changeBarsCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(&lv, SIGNAL(changeTrackingToggled(bool)),
+               this, SLOT(changeTrackingChanged(bool)));
 
 
        // numbering
@@ -1619,6 +1633,8 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(latexModule->refstyleCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(latexModule->refFormattedCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
 
        latexModule->optionsLE->setValidator(new NoNewLineValidator(
                latexModule->optionsLE));
@@ -1766,8 +1782,11 @@ GuiDocument::GuiDocument(GuiView & lv)
                pdfSupportModule->subjectLE));
        pdfSupportModule->keywordsLE->setValidator(new NoNewLineValidator(
                pdfSupportModule->keywordsLE));
-       (void) new LaTeXHighlighter(pdfSupportModule->optionsTE->document(), true, true);
-       (void) new LaTeXHighlighter(pdfSupportModule->metadataTE->document(), true, true);
+
+       pdf_options_highlighter_ = new LaTeXHighlighter(
+                               pdfSupportModule->optionsTE->document(), true, true);
+       pdf_metadata_highlighter_ = new LaTeXHighlighter(
+                               pdfSupportModule->metadataTE->document(), true, true);
 
        for (int i = 0; backref_opts[i][0]; ++i)
                pdfSupportModule->backrefCO->addItem(qt_(backref_opts_gui[i]));
@@ -1821,9 +1840,12 @@ GuiDocument::GuiDocument(GuiView & lv)
        docPS->addPanel(listingsModule, N_("Listings[[inset]]"));
        docPS->addPanel(bulletsModule, N_("Bullets"));
        docPS->addPanel(branchesModule, N_("Branches"));
-       docPS->addPanel(outputModule, N_("Formats[[output]]"));
+       docPS->addPanel(outputModule, N_("Output"));
        docPS->addPanel(preambleModule, N_("LaTeX Preamble"));
        docPS->setCurrentPanel("Document Class");
+
+       // Filter out (dark/light) mode changes
+       installEventFilter(this);
 }
 
 
@@ -1899,6 +1921,14 @@ void GuiDocument::bookmarksopenChanged(bool state)
 }
 
 
+void GuiDocument::changeTrackingChanged(bool state)
+{
+       // This is triggered if the CT state is toggled outside
+       // the document dialog (e.g., menu).
+       changesModule->trackChangesCB->setChecked(state);
+}
+
+
 void GuiDocument::slotApply()
 {
        bool only_shellescape_changed = !nonModuleChanged_ && !modulesChanged_;
@@ -1957,8 +1987,10 @@ void GuiDocument::filterModules(QString const & str)
                        return 0 < b.name.localeAwareCompare(a.name);
                });
 
-       QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-       QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
+       QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+                              : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+       QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+                                : getPixmap("images/", "lyxfiles-system", "svgz,png"));
 
        int i = 0;
        for (modInfoStruct const & m : modInfoList) {
@@ -2159,30 +2191,6 @@ void GuiDocument::setMargins()
                marginsModule->marginCB->setChecked(!bp_.use_geometry);
                setCustomMargins(!bp_.use_geometry);
        }
-
-       // set some placeholder text that hint on defaults
-       QString const placeholder = marginsModule->marginCB->isChecked() ?
-               qt_("Class defaults") : qt_("Package defaults");
-       // set tooltip depending on gemoetry state
-       QString const tooltip = marginsModule->marginCB->isChecked() ?
-               qt_("If no value is given, the defaults as set by the class are used.")
-               : qt_("If no value is given, the defaults as set by the geometry package or a package/class overriding geometry's defaults are used.");
-       marginsModule->topLE->setPlaceholderText(placeholder);
-       marginsModule->bottomLE->setPlaceholderText(placeholder);
-       marginsModule->innerLE->setPlaceholderText(placeholder);
-       marginsModule->outerLE->setPlaceholderText(placeholder);
-       marginsModule->headheightLE->setPlaceholderText(placeholder);
-       marginsModule->headsepLE->setPlaceholderText(placeholder);
-       marginsModule->footskipLE->setPlaceholderText(placeholder);
-       marginsModule->columnsepLE->setPlaceholderText(placeholder);
-       marginsModule->topLE->setToolTip(tooltip);
-       marginsModule->bottomLE->setToolTip(tooltip);
-       marginsModule->innerLE->setToolTip(tooltip);
-       marginsModule->outerLE->setToolTip(tooltip);
-       marginsModule->headheightLE->setToolTip(tooltip);
-       marginsModule->headsepLE->setToolTip(tooltip);
-       marginsModule->footskipLE->setToolTip(tooltip);
-       marginsModule->columnsepLE->setToolTip(tooltip);
 }
 
 
@@ -2210,48 +2218,105 @@ void GuiDocument::setColSep()
 }
 
 
-void GuiDocument::setCustomMargins(bool custom)
-{
-       marginsModule->topL->setEnabled(!custom);
-       marginsModule->topLE->setEnabled(!custom);
-       marginsModule->topUnit->setEnabled(!custom);
+void GuiDocument::setCustomMargins(bool cb_checked)
+{
+       bool const custom_margins = !cb_checked;
+       if (custom_margins) {
+               Length::UNIT const default_unit = Length::defaultUnit();
+               // fill with chached values
+               lengthToWidgets(marginsModule->topLE,
+                               marginsModule->topUnit,
+                               tmp_topmargin_, default_unit);
+               lengthToWidgets(marginsModule->bottomLE,
+                               marginsModule->bottomUnit,
+                               tmp_bottommargin_, default_unit);
+               lengthToWidgets(marginsModule->innerLE,
+                               marginsModule->innerUnit,
+                               tmp_leftmargin_, default_unit);
+               lengthToWidgets(marginsModule->outerLE,
+                               marginsModule->outerUnit,
+                               tmp_rightmargin_, default_unit);
+               lengthToWidgets(marginsModule->headheightLE,
+                               marginsModule->headheightUnit,
+                               tmp_headheight_, default_unit);
+               lengthToWidgets(marginsModule->headsepLE,
+                               marginsModule->headsepUnit,
+                               tmp_headsep_, default_unit);
+               lengthToWidgets(marginsModule->footskipLE,
+                               marginsModule->footskipUnit,
+                               tmp_footskip_, default_unit);
+               lengthToWidgets(marginsModule->columnsepLE,
+                               marginsModule->columnsepUnit,
+                               tmp_columnsep_, default_unit);
+       } else { // default margins
+               // Cache current values
+               tmp_leftmargin_ = widgetsToLength(marginsModule->innerLE,
+                                                 marginsModule->innerUnit);
+               tmp_topmargin_ = widgetsToLength(marginsModule->topLE,
+                                                marginsModule->topUnit);
+               tmp_rightmargin_ = widgetsToLength(marginsModule->outerLE,
+                                                  marginsModule->outerUnit);
+               tmp_bottommargin_ = widgetsToLength(marginsModule->bottomLE,
+                                                   marginsModule->bottomUnit);
+               tmp_headheight_ = widgetsToLength(marginsModule->headheightLE,
+                                                 marginsModule->headheightUnit);
+               tmp_headsep_ = widgetsToLength(marginsModule->headsepLE,
+                                              marginsModule->headsepUnit);
+               tmp_footskip_ = widgetsToLength(marginsModule->footskipLE,
+                                               marginsModule->footskipUnit);
+               tmp_columnsep_ = widgetsToLength(marginsModule->columnsepLE,
+                                                marginsModule->columnsepUnit);
+               // clear widgets
+               marginsModule->topLE->clear();
+               marginsModule->bottomLE->clear();
+               marginsModule->innerLE->clear();
+               marginsModule->outerLE->clear();
+               marginsModule->headheightLE->clear();
+               marginsModule->headsepLE->clear();
+               marginsModule->footskipLE->clear();
+               marginsModule->columnsepLE->clear();
+       }
+
+       marginsModule->topL->setEnabled(custom_margins);
+       marginsModule->topLE->setEnabled(custom_margins);
+       marginsModule->topUnit->setEnabled(custom_margins);
 
-       marginsModule->bottomL->setEnabled(!custom);
-       marginsModule->bottomLE->setEnabled(!custom);
-       marginsModule->bottomUnit->setEnabled(!custom);
+       marginsModule->bottomL->setEnabled(custom_margins);
+       marginsModule->bottomLE->setEnabled(custom_margins);
+       marginsModule->bottomUnit->setEnabled(custom_margins);
 
-       marginsModule->innerL->setEnabled(!custom);
-       marginsModule->innerLE->setEnabled(!custom);
-       marginsModule->innerUnit->setEnabled(!custom);
+       marginsModule->innerL->setEnabled(custom_margins);
+       marginsModule->innerLE->setEnabled(custom_margins);
+       marginsModule->innerUnit->setEnabled(custom_margins);
 
-       marginsModule->outerL->setEnabled(!custom);
-       marginsModule->outerLE->setEnabled(!custom);
-       marginsModule->outerUnit->setEnabled(!custom);
+       marginsModule->outerL->setEnabled(custom_margins);
+       marginsModule->outerLE->setEnabled(custom_margins);
+       marginsModule->outerUnit->setEnabled(custom_margins);
 
-       marginsModule->headheightL->setEnabled(!custom);
-       marginsModule->headheightLE->setEnabled(!custom);
-       marginsModule->headheightUnit->setEnabled(!custom);
+       marginsModule->headheightL->setEnabled(custom_margins);
+       marginsModule->headheightLE->setEnabled(custom_margins);
+       marginsModule->headheightUnit->setEnabled(custom_margins);
 
-       marginsModule->headsepL->setEnabled(!custom);
-       marginsModule->headsepLE->setEnabled(!custom);
-       marginsModule->headsepUnit->setEnabled(!custom);
+       marginsModule->headsepL->setEnabled(custom_margins);
+       marginsModule->headsepLE->setEnabled(custom_margins);
+       marginsModule->headsepUnit->setEnabled(custom_margins);
 
-       marginsModule->footskipL->setEnabled(!custom);
-       marginsModule->footskipLE->setEnabled(!custom);
-       marginsModule->footskipUnit->setEnabled(!custom);
+       marginsModule->footskipL->setEnabled(custom_margins);
+       marginsModule->footskipLE->setEnabled(custom_margins);
+       marginsModule->footskipUnit->setEnabled(custom_margins);
 
-       bool const enableColSep = !custom &&
-                       textLayoutModule->twoColumnCB->checkState() == Qt::Checked;
+       bool const enableColSep = custom_margins &&
+                       textLayoutModule->twoColumnCB->isChecked();
        marginsModule->columnsepL->setEnabled(enableColSep);
        marginsModule->columnsepLE->setEnabled(enableColSep);
        marginsModule->columnsepUnit->setEnabled(enableColSep);
 
        // set some placeholder text that hint on defaults
        QString const placeholder = marginsModule->marginCB->isChecked() ?
-               qt_("Class defaults") : qt_("Package defaults");
+               qt_("Default margins") : qt_("Package defaults");
        // set tooltip depending on gemoetry state
        QString const tooltip = marginsModule->marginCB->isChecked() ?
-               qt_("If no value is given, the defaults as set by the class are used.")
+               qt_("If no value is given, the defaults as set by the class, a package or the preamble are used.")
                : qt_("If no value is given, the defaults as set by the geometry package or a package/class overriding geometry's defaults are used.");
        marginsModule->topLE->setPlaceholderText(placeholder);
        marginsModule->bottomLE->setPlaceholderText(placeholder);
@@ -2269,14 +2334,12 @@ void GuiDocument::setCustomMargins(bool custom)
        marginsModule->headsepLE->setToolTip(tooltip);
        marginsModule->footskipLE->setToolTip(tooltip);
        marginsModule->columnsepLE->setToolTip(tooltip);
-
 }
 
 
 void GuiDocument::changeBackgroundColor()
 {
-       QColor const & newColor = QColorDialog::getColor(
-               rgb2qcolor(set_backgroundcolor), asQWidget());
+       QColor const & newColor = getColor(rgb2qcolor(set_backgroundcolor));
        if (!newColor.isValid())
                return;
        // set the color
@@ -2304,8 +2367,7 @@ void GuiDocument::deleteBackgroundColor()
 
 void GuiDocument::changeFontColor()
 {
-       QColor const & newColor = QColorDialog::getColor(
-               rgb2qcolor(set_fontcolor), asQWidget());
+       QColor const & newColor = getColor(rgb2qcolor(set_fontcolor));
        if (!newColor.isValid())
                return;
        //  set the color
@@ -2333,8 +2395,7 @@ void GuiDocument::deleteFontColor()
 
 void GuiDocument::changeNoteFontColor()
 {
-       QColor const & newColor = QColorDialog::getColor(
-               rgb2qcolor(set_notefontcolor), asQWidget());
+       QColor const & newColor = getColor(rgb2qcolor(set_notefontcolor));
        if (!newColor.isValid())
                return;
        // set the color
@@ -2360,8 +2421,7 @@ void GuiDocument::deleteNoteFontColor()
 
 void GuiDocument::changeBoxBackgroundColor()
 {
-       QColor const & newColor = QColorDialog::getColor(
-               rgb2qcolor(set_boxbgcolor), asQWidget());
+       QColor const & newColor = getColor(rgb2qcolor(set_boxbgcolor));
        if (!newColor.isValid())
                return;
        // set the color
@@ -2411,6 +2471,25 @@ void GuiDocument::updateQuoteStyles(bool const set)
                        langModule->quoteStyleCO->addItem(
                                toqstr(quoteparams.getGuiLabel(qs, langdef)), static_cast<int>(qs));
        }
+       // Use document serif font to assure quotation marks are distinguishable
+       QFont comboFont(toqstr(lyxrc.roman_font_name),
+                       langModule->quoteStyleCO->fontInfo().pointSize() * 1.4, -1, false);
+       QFontMetrics fm(comboFont);
+       // calculate width of the widest item in the set font
+       int qswidth = 0;
+       for (int i = 0; i < langModule->quoteStyleCO->count(); ++i) {
+               langModule->quoteStyleCO->setItemData(i, QVariant(comboFont), Qt::FontRole);
+               QString str = langModule->quoteStyleCO->itemText(i);
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
+               qswidth = max(qswidth, fm.horizontalAdvance(str));
+#else
+               qswidth = max(qswidth, fm.width(str));
+#endif
+       }
+       // add scrollbar width and margin to width
+       qswidth += langModule->quoteStyleCO->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
+       qswidth += langModule->quoteStyleCO->view()->autoScrollMargin();
+       langModule->quoteStyleCO->view()->setMinimumWidth(qswidth);
        if (set && has_default)
                // (re)set to the default style
                langModule->quoteStyleCO->setCurrentIndex(0);
@@ -2474,9 +2553,12 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
        outputModule->defaultFormatCO->setCurrentIndex(index);
 
        // try to restore fonts which were selected two toggles ago
-       fontModule->fontsRomanCO->set(fontModule->font_roman);
-       fontModule->fontsSansCO->set(fontModule->font_sans);
-       fontModule->fontsTypewriterCO->set(fontModule->font_typewriter);
+       if (!fontModule->font_roman.isEmpty())
+               fontModule->fontsRomanCO->set(fontModule->font_roman);
+       if (!fontModule->font_sans.isEmpty())
+               fontModule->fontsSansCO->set(fontModule->font_sans);
+       if (!fontModule->font_typewriter.isEmpty())
+               fontModule->fontsTypewriterCO->set(fontModule->font_typewriter);
        index = fontModule->fontsMathCO->findData(fontModule->font_math);
        if (index != -1)
                fontModule->fontsMathCO->setCurrentIndex(index);
@@ -2515,6 +2597,18 @@ void GuiDocument::encodingSwitched(int i)
        langModule->unicodeEncodingCO->setVisible(i == EncodingSets::unicode);
        langModule->autoEncodingCO->setVisible(i == EncodingSets::legacy);
        langModule->customEncodingCO->setVisible(i == EncodingSets::custom);
+       switch (i) {
+       case EncodingSets::unicode:
+               langModule->encodingVariantLA->setBuddy(langModule->unicodeEncodingCO);
+               break;
+       case EncodingSets::legacy:
+               langModule->encodingVariantLA->setBuddy(langModule->autoEncodingCO);
+               break;
+       case EncodingSets::custom:
+               langModule->encodingVariantLA->setBuddy(langModule->customEncodingCO);
+               break;
+       }
        if (tex_fonts)
                langModule->unicodeEncodingCO->setItemText(1, qt_("Direct (No inputenc)"));
        else
@@ -2589,50 +2683,40 @@ void GuiDocument::fontScToggled(bool state)
 
 void GuiDocument::updateExtraOpts()
 {
-       bool const tex_fonts = !fontModule->osFontsCB->isChecked();
-       QString font;
-       if (tex_fonts)
-               font = fontModule->fontsRomanCO->getData(
-                               fontModule->fontsRomanCO->currentIndex());
+       QString font = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
        bool const rm_opts = providesExtraOpts(font);
-       if (tex_fonts)
-               font = fontModule->fontsSansCO->getData(
-                               fontModule->fontsSansCO->currentIndex());
+       font = fontModule->fontsSansCO->getData(
+                       fontModule->fontsSansCO->currentIndex());
        bool const sf_opts = providesExtraOpts(font);
-       if (tex_fonts)
-               font = fontModule->fontsTypewriterCO->getData(
-                               fontModule->fontsTypewriterCO->currentIndex());
+       font = fontModule->fontsTypewriterCO->getData(
+                       fontModule->fontsTypewriterCO->currentIndex());
        bool const tt_opts = providesExtraOpts(font);
-       fontModule->fontspecRomanLA->setEnabled(!tex_fonts || rm_opts);
-       fontModule->fontspecRomanLE->setEnabled(!tex_fonts || rm_opts);
-       fontModule->fontspecSansLA->setEnabled(!tex_fonts || sf_opts);
-       fontModule->fontspecSansLE->setEnabled(!tex_fonts || sf_opts);
-       fontModule->fontspecTypewriterLA->setEnabled(!tex_fonts || tt_opts);
-       fontModule->fontspecTypewriterLE->setEnabled(!tex_fonts || tt_opts);
+       fontModule->fontspecRomanLA->setEnabled(rm_opts);
+       fontModule->fontspecRomanLE->setEnabled(rm_opts);
+       fontModule->fontspecSansLA->setEnabled(sf_opts);
+       fontModule->fontspecSansLE->setEnabled(sf_opts);
+       fontModule->fontspecTypewriterLA->setEnabled(tt_opts);
+       fontModule->fontspecTypewriterLE->setEnabled(tt_opts);
 }
 
 
 void GuiDocument::updateFontOptions()
 {
-       bool const tex_fonts = !fontModule->osFontsCB->isChecked();
-       QString font;
-       if (tex_fonts)
-               font = fontModule->fontsSansCO->getData(
-                               fontModule->fontsSansCO->currentIndex());
+       QString font = fontModule->fontsSansCO->getData(
+                       fontModule->fontsSansCO->currentIndex());
        bool scalable = providesScale(font);
        fontModule->scaleSansSB->setEnabled(scalable);
        fontModule->scaleSansLA->setEnabled(scalable);
        fontModule->fontSansOsfCB->setEnabled(providesOSF(font));
-       if (tex_fonts)
-               font = fontModule->fontsTypewriterCO->getData(
-                               fontModule->fontsTypewriterCO->currentIndex());
+       font = fontModule->fontsTypewriterCO->getData(
+                       fontModule->fontsTypewriterCO->currentIndex());
        scalable = providesScale(font);
        fontModule->scaleTypewriterSB->setEnabled(scalable);
        fontModule->scaleTypewriterLA->setEnabled(scalable);
        fontModule->fontTypewriterOsfCB->setEnabled(providesOSF(font));
-       if (tex_fonts)
-               font = fontModule->fontsRomanCO->getData(
-                               fontModule->fontsRomanCO->currentIndex());
+       font = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
        fontModule->fontScCB->setEnabled(providesSC(font));
        fontModule->fontOsfCB->setEnabled(providesOSF(font));
        updateExtraOpts();
@@ -2841,20 +2925,18 @@ void GuiDocument::updateMathFonts(QString const & rm)
 
 void GuiDocument::romanChanged(int item)
 {
-       if (fontModule->osFontsCB->isChecked())
-               return;
        QString const font = fontModule->fontsRomanCO->getData(item);
-       fontModule->fontScCB->setEnabled(providesSC(font));
        fontModule->fontOsfCB->setEnabled(providesOSF(font));
        updateExtraOpts();
+       if (fontModule->osFontsCB->isChecked())
+               return;
+       fontModule->fontScCB->setEnabled(providesSC(font));
        updateMathFonts(font);
 }
 
 
 void GuiDocument::sansChanged(int item)
 {
-       if (fontModule->osFontsCB->isChecked())
-               return;
        QString const font = fontModule->fontsSansCO->getData(item);
        bool const scalable = providesScale(font);
        fontModule->scaleSansSB->setEnabled(scalable);
@@ -2866,8 +2948,6 @@ void GuiDocument::sansChanged(int item)
 
 void GuiDocument::ttChanged(int item)
 {
-       if (fontModule->osFontsCB->isChecked())
-               return;
        QString const font = fontModule->fontsTypewriterCO->getData(item);
        bool scalable = providesScale(font);
        fontModule->scaleTypewriterSB->setEnabled(scalable);
@@ -3334,6 +3414,9 @@ void GuiDocument::updateModuleInfo()
        }
        QModelIndex const & idx = lv->selectionModel()->currentIndex();
 
+       if (!idx.isValid())
+               return;
+
        if (!focus_on_selected
            && modules_av_model_.itemFromIndex(idx)->hasChildren()) {
                // This is a category header
@@ -3527,6 +3610,7 @@ void GuiDocument::applyView()
        // date
        bp_.suppress_date = latexModule->suppressDateCB->isChecked();
        bp_.use_refstyle  = latexModule->refstyleCB->isChecked();
+       bp_.use_formatted_ref  = latexModule->refFormattedCB->isChecked();
 
        // biblio
        string const engine =
@@ -3962,14 +4046,16 @@ void GuiDocument::applyView()
 
        Ui::MarginsUi const * m = marginsModule;
 
-       bp_.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
-       bp_.topmargin = widgetsToLength(m->topLE, m->topUnit);
-       bp_.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
-       bp_.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
-       bp_.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
-       bp_.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
-       bp_.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
-       bp_.columnsep = widgetsToLength(m->columnsepLE, m->columnsepUnit);
+       if (bp_.use_geometry) {
+               bp_.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
+               bp_.topmargin = widgetsToLength(m->topLE, m->topUnit);
+               bp_.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
+               bp_.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
+               bp_.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
+               bp_.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
+               bp_.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
+               bp_.columnsep = widgetsToLength(m->columnsepLE, m->columnsepUnit);
+       }
 
        // branches
        branchesModule->apply(bp_);
@@ -3999,13 +4085,8 @@ void GuiDocument::applyView()
                pdf.pagemode.clear();
        pdf.quoted_options = pdf.quoted_options_check(
                                fromqstr(pdfSupportModule->optionsTE->toPlainText()));
-#if QT_VERSION < 0x060000
-       bp_.document_metadata = qstring_to_ucs4(pdfSupportModule->metadataTE->toPlainText()
-                                               .trimmed().replace(QRegExp("\n+"), "\n"));
-#else
        bp_.document_metadata = qstring_to_ucs4(pdfSupportModule->metadataTE->toPlainText()
                                                .trimmed().replace(QRegularExpression("\n+"), "\n"));
-#endif
 
        // change tracking
        bp_.track_changes = changesModule->trackChangesCB->isChecked();
@@ -4036,6 +4117,7 @@ void GuiDocument::paramsToDialog()
        // date
        latexModule->suppressDateCB->setChecked(bp_.suppress_date);
        latexModule->refstyleCB->setChecked(bp_.use_refstyle);
+       latexModule->refFormattedCB->setChecked(bp_.use_formatted_ref);
 
        // biblio
        string const cite_engine = bp_.citeEngine();
@@ -4399,7 +4481,7 @@ void GuiDocument::paramsToDialog()
                        bp_.fontsize);
 
        QString font = toqstr(bp_.fontsRoman());
-       bool foundfont = fontModule->fontsRomanCO->set(font);
+       bool foundfont = fontModule->fontsRomanCO->set(font, false);
        if (!foundfont) {
                fontModule->fontsRomanCO->addItemSort(font, font + qt_(" (not installed)"),
                                                      qt_("Uninstalled used fonts"),
@@ -4410,8 +4492,8 @@ void GuiDocument::paramsToDialog()
        fontModule->font_roman = toqstr(bp_.fonts_roman[!bp_.useNonTeXFonts]);
 
        font = toqstr(bp_.fontsSans());
-       foundfont = fontModule->fontsSansCO->set(font);
-       if (!foundfont ) {
+       foundfont = fontModule->fontsSansCO->set(font, false);
+       if (!foundfont) {
                fontModule->fontsSansCO->addItemSort(font, font + qt_(" (not installed)"),
                                                     qt_("Uninstalled used fonts"),
                                                     qt_("This font is not installed and won't be used in output"),
@@ -4421,7 +4503,7 @@ void GuiDocument::paramsToDialog()
        fontModule->font_sans = toqstr(bp_.fonts_sans[!bp_.useNonTeXFonts]);
 
        font = toqstr(bp_.fontsTypewriter());
-       foundfont = fontModule->fontsTypewriterCO->set(font);
+       foundfont = fontModule->fontsTypewriterCO->set(font, false);
        if (!foundfont) {
                fontModule->fontsTypewriterCO->addItemSort(font, font + qt_(" (not installed)"),
                                                           qt_("Uninstalled used fonts"),
@@ -4452,6 +4534,8 @@ void GuiDocument::paramsToDialog()
                sansChanged(fontModule->fontsSansCO->currentIndex());
                ttChanged(fontModule->fontsTypewriterCO->currentIndex());
        }
+       // Handle options enabling
+       updateFontOptions();
 
        if (!bp_.fonts_cjk.empty())
                fontModule->cjkFontLE->setText(
@@ -4561,32 +4645,34 @@ void GuiDocument::paramsToDialog()
        // margins
        Ui::MarginsUi * m = marginsModule;
 
-       setMargins();
+       tmp_leftmargin_ = bp_.leftmargin;
+       tmp_topmargin_ = bp_.topmargin;
+       tmp_rightmargin_ = bp_.rightmargin;
+       tmp_bottommargin_ = bp_.bottommargin;
+       tmp_headheight_ = bp_.headheight;
+       tmp_headsep_ = bp_.headsep;
+       tmp_footskip_ = bp_.footskip;
+       tmp_columnsep_ = bp_.columnsep;
 
        lengthToWidgets(m->topLE, m->topUnit,
                bp_.topmargin, default_unit);
-
        lengthToWidgets(m->bottomLE, m->bottomUnit,
                bp_.bottommargin, default_unit);
-
        lengthToWidgets(m->innerLE, m->innerUnit,
                bp_.leftmargin, default_unit);
-
        lengthToWidgets(m->outerLE, m->outerUnit,
                bp_.rightmargin, default_unit);
-
        lengthToWidgets(m->headheightLE, m->headheightUnit,
                bp_.headheight, default_unit);
-
        lengthToWidgets(m->headsepLE, m->headsepUnit,
                bp_.headsep, default_unit);
-
        lengthToWidgets(m->footskipLE, m->footskipUnit,
                bp_.footskip, default_unit);
-
        lengthToWidgets(m->columnsepLE, m->columnsepUnit,
                bp_.columnsep, default_unit);
 
+       setMargins();
+
        // branches
        updateUnknownBranches();
        branchesModule->update(bp_);
@@ -4627,7 +4713,7 @@ void GuiDocument::paramsToDialog()
                toqstr(pdf.quoted_options));
 
        pdfSupportModule->metadataTE->setPlainText(
-               toqstr(bp_.document_metadata));
+               toqstr(rtrim(bp_.document_metadata, "\n")));
 
        // change tracking
        changesModule->trackChangesCB->setChecked(bp_.track_changes);
@@ -4668,8 +4754,10 @@ void GuiDocument::updateAvailableModules()
        modInfoList.sort([](modInfoStruct const & a, modInfoStruct const & b) {
                        return 0 < b.name.localeAwareCompare(a.name);
                });
-       QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-       QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
+       QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+                              : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+       QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+                                : getPixmap("images/", "lyxfiles-system", "svgz,png"));
        int i = 0;
        QFont catfont;
        catfont.setBold(true);
@@ -5176,7 +5264,7 @@ bool GuiDocument::providesOSF(QString const & font) const
        if (fontModule->osFontsCB->isChecked())
                // FIXME: we should check if the fonts really
                // have OSF support. But how?
-               return true;
+               return font != "default";
        return theLaTeXFonts().getLaTeXFont(
                                qstring_to_ucs4(font)).providesOSF(ot1(),
                                                                   completeFontset(),
@@ -5198,7 +5286,7 @@ bool GuiDocument::providesSC(QString const & font) const
 bool GuiDocument::providesScale(QString const & font) const
 {
        if (fontModule->osFontsCB->isChecked())
-               return true;
+               return font != "default";
        return theLaTeXFonts().getLaTeXFont(
                                qstring_to_ucs4(font)).providesScale(ot1(),
                                                                     completeFontset(),
@@ -5209,7 +5297,7 @@ bool GuiDocument::providesScale(QString const & font) const
 bool GuiDocument::providesExtraOpts(QString const & font) const
 {
        if (fontModule->osFontsCB->isChecked())
-               return true;
+               return font != "default";
        return theLaTeXFonts().getLaTeXFont(
                                qstring_to_ucs4(font)).providesMoreOptions(ot1(),
                                                                     completeFontset(),
@@ -5370,6 +5458,24 @@ void GuiDocument::setOutputSync(bool on)
 }
 
 
+bool GuiDocument::eventFilter(QObject * sender, QEvent * event)
+{
+       if (event->type() == QEvent::ApplicationPaletteChange) {
+               // mode switch: colors need to be updated
+               // and the highlighting redone
+               if (pdf_options_highlighter_) {
+                       pdf_options_highlighter_->setupColors();
+                       pdf_options_highlighter_->rehighlight();
+               }
+               if (pdf_metadata_highlighter_) {
+                       pdf_metadata_highlighter_->setupColors();
+                       pdf_metadata_highlighter_->rehighlight();
+               }
+       }
+       return QWidget::eventFilter(sender, event);
+}
+
+
 } // namespace frontend
 } // namespace lyx