]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiDocument.cpp
Fix readability
[lyx.git] / src / frontends / qt / GuiDocument.cpp
index d1937a1adeb32fe39399f22a324a71e91108970e..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>
@@ -86,9 +85,8 @@
 #include <vector>
 
 
-// a style sheet for buttons
-// this is for example used for the background color setting button
-static inline QString colorButtonStyleSheet(QColor const & bgColor)
+// a style sheet for color frame widgets
+static inline QString colorFrameStyleSheet(QColor const & bgColor)
 {
        if (bgColor.isValid()) {
                QString rc = QLatin1String("background-color:");
@@ -325,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
@@ -473,15 +474,17 @@ 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);
+       // Install event filter on find line edit to capture return/enter key
+       findLE->installEventFilter(this);
        connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed()));
        connect(findLE, SIGNAL(textEdited(const QString &)), this, SLOT(checkFindButton()));
        connect(findButtonPB, SIGNAL(clicked()), this, SLOT(findText()));
@@ -500,6 +503,31 @@ 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();
+                               // 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);
+}
+
+
+
 void PreambleModule::checkFindButton()
 {
        findButtonPB->setEnabled(!findLE->text().isEmpty());
@@ -575,7 +603,8 @@ void PreambleModule::editExternal() {
                docstring const s = tempfilename.fileContents("UTF-8");
                preambleTE->document()->setPlainText(toqstr(s));
                tempfile_.reset();
-               editPB->setText(qt_("&Edit"));
+               editPB->setText(qt_("&Edit Externally"));
+               editPB->setIcon(QIcon());
                changed();
                return;
        }
@@ -592,6 +621,9 @@ void PreambleModule::editExternal() {
        preambleTE->setReadOnly(true);
        theFormats().edit(*current_id_, tempfilename, format);
        editPB->setText(qt_("&End Edit"));
+       QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+                         : getPixmap("images/", "emblem-shellescape", "svgz,png"));
+       editPB->setIcon(warn);
        changed();
 }
 
@@ -751,7 +783,8 @@ void LocalLayout::editExternal() {
                docstring const s = tempfilename.fileContents("UTF-8");
                locallayoutTE->document()->setPlainText(toqstr(s));
                tempfile_.reset();
-               editPB->setText(qt_("&Edit"));
+               editPB->setText(qt_("&Edit Externally"));
+               editPB->setIcon(QIcon());
                changed();
                return;
        }
@@ -768,6 +801,9 @@ void LocalLayout::editExternal() {
        locallayoutTE->setReadOnly(true);
        theFormats().edit(*current_id_, tempfilename, format);
        editPB->setText(qt_("&End Edit"));
+       QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+                         : getPixmap("images/", "emblem-shellescape", "svgz,png"));
+       editPB->setIcon(warn);
        validatePB->setEnabled(false);
        hideConvert();
        changed();
@@ -855,9 +891,9 @@ GuiDocument::GuiDocument(GuiView & lv)
        textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
                textLayoutModule->lspacingLE));
        textLayoutModule->indentLE->setValidator(new LengthValidator(
-               textLayoutModule->indentLE));
+               textLayoutModule->indentLE, false));
        textLayoutModule->skipLE->setValidator(new LengthValidator(
-               textLayoutModule->skipLE));
+               textLayoutModule->skipLE, false));
 
        textLayoutModule->indentCO->addItem(qt_("Default"), toqstr("default"));
        textLayoutModule->indentCO->addItem(qt_("Custom"), toqstr("custom"));
@@ -876,8 +912,10 @@ GuiDocument::GuiDocument(GuiView & lv)
        textLayoutModule->lspacingCO->insertItem(
                Spacing::Other, qt_("Custom"));
        // initialize the length validator
-       bc().addCheckedLineEdit(textLayoutModule->indentLE);
-       bc().addCheckedLineEdit(textLayoutModule->skipLE);
+       bc().addCheckedLineEdit(textLayoutModule->indentLE, textLayoutModule->indentRB);
+       bc().addCheckedLineEditPanel(textLayoutModule->indentLE, docPS, N_("Text Layout"));
+       bc().addCheckedLineEdit(textLayoutModule->skipLE, textLayoutModule->skipRB);
+       bc().addCheckedLineEditPanel(textLayoutModule->skipLE, docPS, N_("Text Layout"));
 
        textLayoutModule->tableStyleCO->addItem(qt_("Default"), toqstr("default"));
        getTableStyles();
@@ -937,7 +975,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        outputModule->synccustomCB->addItem("\\synctex=-1");
        outputModule->synccustomCB->addItem("\\usepackage[active]{srcltx}");
 
-       outputModule->synccustomCB->setValidator(new NoNewLineValidator(
+       outputModule->synccustomCB->lineEdit()->setValidator(new NoNewLineValidator(
                outputModule->synccustomCB));
 
        connect(outputModule->saveTransientPropertiesCB, SIGNAL(clicked()),
@@ -1184,8 +1222,10 @@ GuiDocument::GuiDocument(GuiView & lv)
        pageLayoutModule->pagestyleCO->addItem(qt_("fancy"));
        bc().addCheckedLineEdit(pageLayoutModule->paperheightLE,
                pageLayoutModule->paperheightL);
+       bc().addCheckedLineEditPanel(pageLayoutModule->paperheightLE, docPS, N_("Page Layout"));
        bc().addCheckedLineEdit(pageLayoutModule->paperwidthLE,
                pageLayoutModule->paperwidthL);
+       bc().addCheckedLineEditPanel(pageLayoutModule->paperwidthLE, docPS, N_("Page Layout"));
 
        QComboBox * cb = pageLayoutModule->papersizeCO;
        cb->addItem(qt_("Default"));
@@ -1232,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()));
@@ -1287,20 +1327,36 @@ GuiDocument::GuiDocument(GuiView & lv)
 
        bc().addCheckedLineEdit(marginsModule->topLE,
                marginsModule->topL);
+       bc().addCheckedLineEditPanel(marginsModule->topLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->bottomLE,
                marginsModule->bottomL);
+       bc().addCheckedLineEditPanel(marginsModule->bottomLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->innerLE,
                marginsModule->innerL);
+       bc().addCheckedLineEditPanel(marginsModule->innerLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->outerLE,
                marginsModule->outerL);
+       bc().addCheckedLineEditPanel(marginsModule->outerLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->headsepLE,
                marginsModule->headsepL);
+       bc().addCheckedLineEditPanel(marginsModule->headsepLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->headheightLE,
                marginsModule->headheightL);
+       bc().addCheckedLineEditPanel(marginsModule->headheightLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->footskipLE,
                marginsModule->footskipL);
+       bc().addCheckedLineEditPanel(marginsModule->footskipLE,
+                                    docPS, N_("Page Margins"));
        bc().addCheckedLineEdit(marginsModule->columnsepLE,
                marginsModule->columnsepL);
+       bc().addCheckedLineEditPanel(marginsModule->columnsepLE,
+                                    docPS, N_("Page Margins"));
 
 
        // color
@@ -1331,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
@@ -1522,14 +1580,14 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(mathsModule->MathNumberingPosCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
 
-       connect(mathsModule->MathIndentCB, SIGNAL(toggled(bool)),
-               this, SLOT(change_adaptor()));
        connect(mathsModule->MathIndentCB, SIGNAL(toggled(bool)),
                this, SLOT(allowMathIndent()));
-       connect(mathsModule->MathIndentCO, SIGNAL(activated(int)),
+       connect(mathsModule->MathIndentCB, SIGNAL(toggled(bool)),
                this, SLOT(change_adaptor()));
        connect(mathsModule->MathIndentCO, SIGNAL(activated(int)),
                this, SLOT(enableMathIndent(int)));
+       connect(mathsModule->MathIndentCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
        connect(mathsModule->MathIndentLE, SIGNAL(textChanged(const QString &)),
                this, SLOT(change_adaptor()));
        connect(mathsModule->MathIndentLengthCO, SIGNAL(activated(int)),
@@ -1539,9 +1597,10 @@ GuiDocument::GuiDocument(GuiView & lv)
        mathsModule->MathIndentCO->addItem(qt_("Default"), toqstr("default"));
        mathsModule->MathIndentCO->addItem(qt_("Custom"), toqstr("custom"));
        mathsModule->MathIndentLE->setValidator(new LengthValidator(
-               mathsModule->MathIndentLE));
+               mathsModule->MathIndentLE, false));
        // initialize the length validator
-       bc().addCheckedLineEdit(mathsModule->MathIndentLE);
+       bc().addCheckedLineEdit(mathsModule->MathIndentLE, mathsModule->MathIndentCB);
+       bc().addCheckedLineEditPanel(mathsModule->MathIndentLE, docPS, N_("Math Options"));
        mathsModule->MathNumberingPosCO->addItem(qt_("Left"));
        mathsModule->MathNumberingPosCO->addItem(qt_("Default"));
        mathsModule->MathNumberingPosCO->addItem(qt_("Right"));
@@ -1574,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));
@@ -1721,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]));
@@ -1776,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);
 }
 
 
@@ -1854,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_;
@@ -1862,6 +1937,7 @@ void GuiDocument::slotApply()
        if (wasclean && only_shellescape_changed)
                buffer().markClean();
        modulesChanged_ = false;
+       isValid();
 }
 
 
@@ -1911,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) {
@@ -1963,16 +2041,16 @@ void GuiDocument::includeonlyClicked(QTreeWidgetItem * item, int)
                return;
 
        string child = fromqstr(item->text(0));
+
        if (child.empty())
                return;
 
-       if (std::find(includeonlys_.begin(),
-                     includeonlys_.end(), child) != includeonlys_.end())
+       if (isChildIncluded(child))
                includeonlys_.remove(child);
        else
                includeonlys_.push_back(child);
 
-       updateIncludeonlys();
+       updateIncludeonlys(false);
        change_adaptor();
 }
 
@@ -1996,16 +2074,16 @@ void GuiDocument::setListingsMessage()
        static bool isOK = true;
        QString msg = validateListingsParameters();
        if (msg.isEmpty()) {
+               listingsModule->listingsTB->setTextColor(QColor());
                if (isOK)
                        return;
                isOK = true;
-               // listingsModule->listingsTB->setTextColor("black");
                listingsModule->listingsTB->setPlainText(
                        qt_("Input listings parameters below. "
                            "Enter ? for a list of parameters."));
        } else {
                isOK = false;
-               // listingsModule->listingsTB->setTextColor("red");
+               listingsModule->listingsTB->setTextColor(QColor(255, 0, 0));
                listingsModule->listingsTB->setPlainText(msg);
        }
 }
@@ -2040,6 +2118,8 @@ void GuiDocument::setIndent(int item)
        textLayoutModule->indentLengthCO->setEnabled(enable);
        textLayoutModule->skipLE->setEnabled(false);
        textLayoutModule->skipLengthCO->setEnabled(false);
+       // needed to catch empty custom case
+       bc().refresh();
        isValid();
 }
 
@@ -2060,6 +2140,8 @@ void GuiDocument::setSkip(int item)
        bool const enable = (kind == VSpace::LENGTH);
        textLayoutModule->skipLE->setEnabled(enable);
        textLayoutModule->skipLengthCO->setEnabled(enable);
+       // needed to catch empty custom case
+       bc().refresh();
        isValid();
 }
 
@@ -2091,6 +2173,8 @@ void GuiDocument::enableMathIndent(int item)
        bool const enable = (item == 1);
        mathsModule->MathIndentLE->setEnabled(enable);
        mathsModule->MathIndentLengthCO->setEnabled(enable);
+       // needed to catch empty custom case
+       bc().refresh();
        isValid();
 }
 
@@ -2107,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);
 }
 
 
@@ -2158,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->bottomL->setEnabled(!custom);
-       marginsModule->bottomLE->setEnabled(!custom);
-       marginsModule->bottomUnit->setEnabled(!custom);
+       marginsModule->topL->setEnabled(custom_margins);
+       marginsModule->topLE->setEnabled(custom_margins);
+       marginsModule->topUnit->setEnabled(custom_margins);
 
-       marginsModule->innerL->setEnabled(!custom);
-       marginsModule->innerLE->setEnabled(!custom);
-       marginsModule->innerUnit->setEnabled(!custom);
+       marginsModule->bottomL->setEnabled(custom_margins);
+       marginsModule->bottomLE->setEnabled(custom_margins);
+       marginsModule->bottomUnit->setEnabled(custom_margins);
 
-       marginsModule->outerL->setEnabled(!custom);
-       marginsModule->outerLE->setEnabled(!custom);
-       marginsModule->outerUnit->setEnabled(!custom);
+       marginsModule->innerL->setEnabled(custom_margins);
+       marginsModule->innerLE->setEnabled(custom_margins);
+       marginsModule->innerUnit->setEnabled(custom_margins);
 
-       marginsModule->headheightL->setEnabled(!custom);
-       marginsModule->headheightLE->setEnabled(!custom);
-       marginsModule->headheightUnit->setEnabled(!custom);
+       marginsModule->outerL->setEnabled(custom_margins);
+       marginsModule->outerLE->setEnabled(custom_margins);
+       marginsModule->outerUnit->setEnabled(custom_margins);
 
-       marginsModule->headsepL->setEnabled(!custom);
-       marginsModule->headsepLE->setEnabled(!custom);
-       marginsModule->headsepUnit->setEnabled(!custom);
+       marginsModule->headheightL->setEnabled(custom_margins);
+       marginsModule->headheightLE->setEnabled(custom_margins);
+       marginsModule->headheightUnit->setEnabled(custom_margins);
 
-       marginsModule->footskipL->setEnabled(!custom);
-       marginsModule->footskipLE->setEnabled(!custom);
-       marginsModule->footskipUnit->setEnabled(!custom);
+       marginsModule->headsepL->setEnabled(custom_margins);
+       marginsModule->headsepLE->setEnabled(custom_margins);
+       marginsModule->headsepUnit->setEnabled(custom_margins);
 
-       bool const enableColSep = !custom &&
-                       textLayoutModule->twoColumnCB->checkState() == Qt::Checked;
+       marginsModule->footskipL->setEnabled(custom_margins);
+       marginsModule->footskipLE->setEnabled(custom_margins);
+       marginsModule->footskipUnit->setEnabled(custom_margins);
+
+       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);
@@ -2217,20 +2334,18 @@ 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 button color and text
-       colorModule->backgroundPB->setStyleSheet(
-               colorButtonStyleSheet(newColor));
-       colorModule->backgroundPB->setText(qt_("&Change..."));
+       // set the color
+       colorModule->pageBackgroundCF->setVisible(true);
+       colorModule->pageBackgroundCF->setStyleSheet(
+               colorFrameStyleSheet(newColor));
        // save color
        set_backgroundcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_backgroundcolor = true;
@@ -2240,10 +2355,9 @@ void GuiDocument::changeBackgroundColor()
 
 void GuiDocument::deleteBackgroundColor()
 {
-       // set the button color back to default by setting an empty StyleSheet
-       colorModule->backgroundPB->setStyleSheet(QLatin1String(""));
-       // change button text
-       colorModule->backgroundPB->setText(qt_("&Default..."));
+       // set the color back to default by setting an empty StyleSheet
+       colorModule->pageBackgroundCF->setStyleSheet(QLatin1String(""));
+       colorModule->pageBackgroundCF->setVisible(false);
        // save default color (white)
        set_backgroundcolor = rgbFromHexName("#ffffff");
        is_backgroundcolor = false;
@@ -2253,14 +2367,13 @@ 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 button color and text
-       colorModule->fontColorPB->setStyleSheet(
-               colorButtonStyleSheet(newColor));
-       colorModule->fontColorPB->setText(qt_("&Change..."));
+       //  set the color
+       colorModule->mainTextCF->setVisible(true);
+       colorModule->mainTextCF->setStyleSheet(
+               colorFrameStyleSheet(newColor));
        // save color
        set_fontcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_fontcolor = true;
@@ -2271,9 +2384,8 @@ void GuiDocument::changeFontColor()
 void GuiDocument::deleteFontColor()
 {
        // set the button color back to default by setting an empty StyleSheet
-       colorModule->fontColorPB->setStyleSheet(QLatin1String(""));
-       // change button text
-       colorModule->fontColorPB->setText(qt_("&Default..."));
+       colorModule->mainTextCF->setStyleSheet(QLatin1String(""));
+       colorModule->mainTextCF->setVisible(false);
        // save default color (black)
        set_fontcolor = rgbFromHexName("#000000");
        is_fontcolor = false;
@@ -2283,13 +2395,12 @@ 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 button color
-       colorModule->noteFontColorPB->setStyleSheet(
-               colorButtonStyleSheet(newColor));
+       // set the color
+       colorModule->noteFontCF->setStyleSheet(
+               colorFrameStyleSheet(newColor));
        // save color
        set_notefontcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_notefontcolor = true;
@@ -2299,10 +2410,10 @@ void GuiDocument::changeNoteFontColor()
 
 void GuiDocument::deleteNoteFontColor()
 {
-       // set the button color back to pref
+       // set the color back to pref
        theApp()->getRgbColor(Color_greyedouttext, set_notefontcolor);
-       colorModule->noteFontColorPB->setStyleSheet(
-               colorButtonStyleSheet(rgb2qcolor(set_notefontcolor)));
+       colorModule->noteFontCF->setStyleSheet(
+               colorFrameStyleSheet(rgb2qcolor(set_notefontcolor)));
        is_notefontcolor = false;
        change_adaptor();
 }
@@ -2310,13 +2421,12 @@ 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 button color
-       colorModule->boxBackgroundPB->setStyleSheet(
-               colorButtonStyleSheet(newColor));
+       // set the color
+       colorModule->boxBackgroundCF->setStyleSheet(
+               colorFrameStyleSheet(newColor));
        // save color
        set_boxbgcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_boxbgcolor = true;
@@ -2326,10 +2436,10 @@ void GuiDocument::changeBoxBackgroundColor()
 
 void GuiDocument::deleteBoxBackgroundColor()
 {
-       // set the button color back to pref
+       // set the color back to pref
        theApp()->getRgbColor(Color_shadedbg, set_boxbgcolor);
-       colorModule->boxBackgroundPB->setStyleSheet(
-               colorButtonStyleSheet(rgb2qcolor(set_boxbgcolor)));
+       colorModule->boxBackgroundCF->setStyleSheet(
+               colorFrameStyleSheet(rgb2qcolor(set_boxbgcolor)));
        is_boxbgcolor = false;
        change_adaptor();
 }
@@ -2361,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);
@@ -2400,12 +2529,12 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
 {
        bool const tex_fonts = !nontexfonts;
        // store current fonts
-       QString const font_roman = fontModule->fontsRomanCO->itemData(
-                       fontModule->fontsRomanCO->currentIndex()).toString();
-       QString const font_sans = fontModule->fontsSansCO->itemData(
-                       fontModule->fontsSansCO->currentIndex()).toString();
-       QString const font_typewriter = fontModule->fontsTypewriterCO->itemData(
-                       fontModule->fontsTypewriterCO->currentIndex()).toString();
+       QString const font_roman = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
+       QString const font_sans = fontModule->fontsSansCO->getData(
+                       fontModule->fontsSansCO->currentIndex());
+       QString const font_typewriter = fontModule->fontsTypewriterCO->getData(
+                       fontModule->fontsTypewriterCO->currentIndex());
        QString const font_math = fontModule->fontsMathCO->itemData(
                        fontModule->fontsMathCO->currentIndex()).toString();
        int const font_sf_scale = fontModule->scaleSansSB->value();
@@ -2424,15 +2553,12 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
        outputModule->defaultFormatCO->setCurrentIndex(index);
 
        // try to restore fonts which were selected two toggles ago
-       index = fontModule->fontsRomanCO->findData(fontModule->font_roman);
-       if (index != -1)
-               fontModule->fontsRomanCO->setCurrentIndex(index);
-       index = fontModule->fontsSansCO->findData(fontModule->font_sans);
-       if (index != -1)
-               fontModule->fontsSansCO->setCurrentIndex(index);
-       index = fontModule->fontsTypewriterCO->findData(fontModule->font_typewriter);
-       if (index != -1)
-               fontModule->fontsTypewriterCO->setCurrentIndex(index);
+       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);
@@ -2448,8 +2574,6 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
        langModule->encodingCO->setEnabled(tex_fonts);
        inputencodingToDialog();
 
-       fontModule->fontsDefaultCO->setEnabled(tex_fonts);
-       fontModule->fontsDefaultLA->setEnabled(tex_fonts);
        fontModule->cjkFontLE->setEnabled(tex_fonts);
        fontModule->cjkFontLA->setEnabled(tex_fonts);
 
@@ -2473,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
@@ -2527,8 +2663,8 @@ void GuiDocument::fontOsfToggled(bool state)
 {
        if (fontModule->osFontsCB->isChecked())
                return;
-       QString font = fontModule->fontsRomanCO->itemData(
-                       fontModule->fontsRomanCO->currentIndex()).toString();
+       QString font = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
        if (hasMonolithicExpertSet(font))
                fontModule->fontScCB->setChecked(state);
 }
@@ -2538,8 +2674,8 @@ void GuiDocument::fontScToggled(bool state)
 {
        if (fontModule->osFontsCB->isChecked())
                return;
-       QString font = fontModule->fontsRomanCO->itemData(
-                       fontModule->fontsRomanCO->currentIndex()).toString();
+       QString font = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
        if (hasMonolithicExpertSet(font))
                fontModule->fontOsfCB->setChecked(state);
 }
@@ -2547,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->itemData(
-                               fontModule->fontsRomanCO->currentIndex()).toString();
+       QString font = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
        bool const rm_opts = providesExtraOpts(font);
-       if (tex_fonts)
-               font = fontModule->fontsSansCO->itemData(
-                               fontModule->fontsSansCO->currentIndex()).toString();
+       font = fontModule->fontsSansCO->getData(
+                       fontModule->fontsSansCO->currentIndex());
        bool const sf_opts = providesExtraOpts(font);
-       if (tex_fonts)
-               font = fontModule->fontsTypewriterCO->itemData(
-                               fontModule->fontsTypewriterCO->currentIndex()).toString();
+       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->itemData(
-                               fontModule->fontsSansCO->currentIndex()).toString();
+       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->itemData(
-                               fontModule->fontsTypewriterCO->currentIndex()).toString();
+       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->itemData(
-                               fontModule->fontsRomanCO->currentIndex()).toString();
+       font = fontModule->fontsRomanCO->getData(
+                       fontModule->fontsRomanCO->currentIndex());
        fontModule->fontScCB->setEnabled(providesSC(font));
        fontModule->fontOsfCB->setEnabled(providesOSF(font));
        updateExtraOpts();
@@ -2633,10 +2759,10 @@ bool GuiDocument::ot1() const
 
 bool GuiDocument::completeFontset() const
 {
-       return (fontModule->fontsSansCO->itemData(
-                       fontModule->fontsSansCO->currentIndex()).toString() == "default"
-               && fontModule->fontsSansCO->itemData(
-                       fontModule->fontsTypewriterCO->currentIndex()).toString() == "default");
+       return (fontModule->fontsSansCO->getData(
+                       fontModule->fontsSansCO->currentIndex()) == "default"
+               && fontModule->fontsSansCO->getData(
+                       fontModule->fontsTypewriterCO->currentIndex()) == "default");
 }
 
 
@@ -2688,9 +2814,15 @@ void GuiDocument::updateFontlist()
 
        // With fontspec (XeTeX, LuaTeX), we have access to all system fonts, but not the LaTeX fonts
        if (fontModule->osFontsCB->isChecked()) {
-               fontModule->fontsRomanCO->addItem(qt_("Default"), QString("default"));
-               fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
-               fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
+               fontModule->fontsRomanCO->addItemSort(QString("default"), qt_("Default"),
+                                                     QString(), qt_("Default font (as set by class)"),
+                                                     false, false, false, true, true);
+               fontModule->fontsSansCO->addItemSort(QString("default"), qt_("Default"),
+                                                    QString(), qt_("Default font (as set by class)"),
+                                                    false, false, false, true, true);
+               fontModule->fontsTypewriterCO->addItemSort(QString("default"), qt_("Default"),
+                                                          QString(), qt_("Default font (as set by class)"),
+                                                          false, false, false, true, true);
                QString unimath = qt_("Non-TeX Fonts Default");
                if (!LaTeXFeatures::isAvailable("unicode-math"))
                        unimath += qt_(" (not available)");
@@ -2704,9 +2836,15 @@ void GuiDocument::updateFontlist()
                const QStringList families(fontdb.families());
 #endif
                for (auto const & family : families) {
-                       fontModule->fontsRomanCO->addItem(family, family);
-                       fontModule->fontsSansCO->addItem(family, family);
-                       fontModule->fontsTypewriterCO->addItem(family, family);
+                       fontModule->fontsRomanCO->addItemSort(family, family,
+                                                             QString(), QString(),
+                                                             false, false, false, true, true);
+                       fontModule->fontsSansCO->addItemSort(family, family,
+                                                            QString(), QString(),
+                                                            false, false, false, true, true);
+                       fontModule->fontsTypewriterCO->addItemSort(family, family,
+                                                                  QString(), QString(),
+                                                                  false, false, false, true, true);
                }
                return;
        }
@@ -2714,24 +2852,36 @@ void GuiDocument::updateFontlist()
        if (rmfonts_.empty())
                updateTexFonts();
 
-       fontModule->fontsRomanCO->addItem(qt_("Default"), QString("default"));
+       fontModule->fontsRomanCO->addItemSort(QString("default"), qt_("Default"),
+                                             QString(), qt_("Default font (as set by class)"),
+                                             false, false, false, true, true);
        QMap<QString, QString>::const_iterator rmi = rmfonts_.constBegin();
        while (rmi != rmfonts_.constEnd()) {
-               fontModule->fontsRomanCO->addItem(rmi.key(), rmi.value());
+               fontModule->fontsRomanCO->addItemSort(rmi.value(), rmi.key(),
+                                                     QString(), QString(),
+                                                     false, false, false, true, true);
                ++rmi;
        }
 
-       fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
+       fontModule->fontsSansCO->addItemSort(QString("default"), qt_("Default"),
+                                            QString(), qt_("Default font (as set by class)"),
+                                            false, false, false, true, true);
        QMap<QString, QString>::const_iterator sfi = sffonts_.constBegin();
        while (sfi != sffonts_.constEnd()) {
-               fontModule->fontsSansCO->addItem(sfi.key(), sfi.value());
+               fontModule->fontsSansCO->addItemSort(sfi.value(), sfi.key(),
+                                                    QString(), QString(),
+                                                    false, false, false, true, true);
                ++sfi;
        }
 
-       fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
+       fontModule->fontsTypewriterCO->addItemSort(QString("default"), qt_("Default"),
+                                                  QString(), qt_("Default font (as set by class)"),
+                                                  false, false, false, true, true);
        QMap<QString, QString>::const_iterator tti = ttfonts_.constBegin();
        while (tti != ttfonts_.constEnd()) {
-               fontModule->fontsTypewriterCO->addItem(tti.key(), tti.value());
+               fontModule->fontsTypewriterCO->addItemSort(tti.value(), tti.key(),
+                                                          QString(), QString(),
+                                                          false, false, false, true, true);
                ++tti;
        }
 
@@ -2775,23 +2925,19 @@ void GuiDocument::updateMathFonts(QString const & rm)
 
 void GuiDocument::romanChanged(int item)
 {
+       QString const font = fontModule->fontsRomanCO->getData(item);
+       fontModule->fontOsfCB->setEnabled(providesOSF(font));
+       updateExtraOpts();
        if (fontModule->osFontsCB->isChecked())
                return;
-       QString const font =
-               fontModule->fontsRomanCO->itemData(item).toString();
        fontModule->fontScCB->setEnabled(providesSC(font));
-       fontModule->fontOsfCB->setEnabled(providesOSF(font));
-       updateExtraOpts();
        updateMathFonts(font);
 }
 
 
 void GuiDocument::sansChanged(int item)
 {
-       if (fontModule->osFontsCB->isChecked())
-               return;
-       QString const font =
-               fontModule->fontsSansCO->itemData(item).toString();
+       QString const font = fontModule->fontsSansCO->getData(item);
        bool const scalable = providesScale(font);
        fontModule->scaleSansSB->setEnabled(scalable);
        fontModule->scaleSansLA->setEnabled(scalable);
@@ -2802,10 +2948,7 @@ void GuiDocument::sansChanged(int item)
 
 void GuiDocument::ttChanged(int item)
 {
-       if (fontModule->osFontsCB->isChecked())
-               return;
-       QString const font =
-               fontModule->fontsTypewriterCO->itemData(item).toString();
+       QString const font = fontModule->fontsTypewriterCO->getData(item);
        bool scalable = providesScale(font);
        fontModule->scaleTypewriterSB->setEnabled(scalable);
        fontModule->scaleTypewriterLA->setEnabled(scalable);
@@ -3271,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
@@ -3464,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 =
@@ -3740,6 +3887,7 @@ void GuiDocument::applyView()
 
        // Master/Child
        bp_.clearIncludedChildren();
+       updateIncludeonlys();
        if (masterChildModule->includeonlyRB->isChecked()) {
                list<string>::const_iterator it = includeonlys_.begin();
                for (; it != includeonlys_.end() ; ++it) {
@@ -3818,19 +3966,19 @@ void GuiDocument::applyView()
        // fonts
        bp_.fonts_roman[nontexfonts] =
                fromqstr(fontModule->fontsRomanCO->
-                       itemData(fontModule->fontsRomanCO->currentIndex()).toString());
+                       getData(fontModule->fontsRomanCO->currentIndex()));
        bp_.fonts_roman[!nontexfonts] = fromqstr(fontModule->font_roman);
        bp_.font_roman_opts = fromqstr(fontModule->fontspecRomanLE->text());
 
        bp_.fonts_sans[nontexfonts] =
                fromqstr(fontModule->fontsSansCO->
-                       itemData(fontModule->fontsSansCO->currentIndex()).toString());
+                       getData(fontModule->fontsSansCO->currentIndex()));
        bp_.fonts_sans[!nontexfonts] = fromqstr(fontModule->font_sans);
        bp_.font_sans_opts = fromqstr(fontModule->fontspecSansLE->text());
 
        bp_.fonts_typewriter[nontexfonts] =
                fromqstr(fontModule->fontsTypewriterCO->
-                       itemData(fontModule->fontsTypewriterCO->currentIndex()).toString());
+                       getData(fontModule->fontsTypewriterCO->currentIndex()));
        bp_.fonts_typewriter[!nontexfonts] = fromqstr(fontModule->font_typewriter);
        bp_.font_typewriter_opts = fromqstr(fontModule->fontspecTypewriterLE->text());
 
@@ -3864,11 +4012,8 @@ void GuiDocument::applyView()
        bp_.fonts_sans_osf = fontModule->fontSansOsfCB->isChecked();
        bp_.fonts_typewriter_osf = fontModule->fontTypewriterOsfCB->isChecked();
 
-       if (nontexfonts)
-               bp_.fonts_default_family = "default";
-       else
-               bp_.fonts_default_family = GuiDocument::fontfamilies[
-                       fontModule->fontsDefaultCO->currentIndex()];
+       bp_.fonts_default_family = GuiDocument::fontfamilies[
+               fontModule->fontsDefaultCO->currentIndex()];
 
        if (fontModule->fontsizeCO->currentIndex() == 0)
                bp_.fontsize = "default";
@@ -3901,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_);
@@ -3938,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();
@@ -3975,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();
@@ -4071,26 +4214,30 @@ void GuiDocument::paramsToDialog()
 
        //color
        if (bp_.isfontcolor) {
-               colorModule->fontColorPB->setStyleSheet(
-                       colorButtonStyleSheet(rgb2qcolor(bp_.fontcolor)));
-       }
+               colorModule->mainTextCF->setStyleSheet(
+                       colorFrameStyleSheet(rgb2qcolor(bp_.fontcolor)));
+               colorModule->mainTextCF->setVisible(true);
+       } else
+               colorModule->mainTextCF->setVisible(false);
        set_fontcolor = bp_.fontcolor;
        is_fontcolor = bp_.isfontcolor;
 
-       colorModule->noteFontColorPB->setStyleSheet(
-               colorButtonStyleSheet(rgb2qcolor(bp_.notefontcolor)));
+       colorModule->noteFontCF->setStyleSheet(
+               colorFrameStyleSheet(rgb2qcolor(bp_.notefontcolor)));
        set_notefontcolor = bp_.notefontcolor;
        is_notefontcolor = bp_.isnotefontcolor;
 
        if (bp_.isbackgroundcolor) {
-               colorModule->backgroundPB->setStyleSheet(
-                       colorButtonStyleSheet(rgb2qcolor(bp_.backgroundcolor)));
-       }
+               colorModule->pageBackgroundCF->setStyleSheet(
+                       colorFrameStyleSheet(rgb2qcolor(bp_.backgroundcolor)));
+               colorModule->pageBackgroundCF->setVisible(true);
+       } else
+               colorModule->pageBackgroundCF->setVisible(false);
        set_backgroundcolor = bp_.backgroundcolor;
        is_backgroundcolor = bp_.isbackgroundcolor;
 
-       colorModule->boxBackgroundPB->setStyleSheet(
-               colorButtonStyleSheet(rgb2qcolor(bp_.boxbgcolor)));
+       colorModule->boxBackgroundCF->setStyleSheet(
+               colorFrameStyleSheet(rgb2qcolor(bp_.boxbgcolor)));
        set_boxbgcolor = bp_.boxbgcolor;
        is_boxbgcolor = bp_.isboxbgcolor;
 
@@ -4334,30 +4481,36 @@ void GuiDocument::paramsToDialog()
                        bp_.fontsize);
 
        QString font = toqstr(bp_.fontsRoman());
-       int rpos = fontModule->fontsRomanCO->findData(font);
-       if (rpos == -1) {
-               rpos = fontModule->fontsRomanCO->count();
-               fontModule->fontsRomanCO->addItem(font + qt_(" (not installed)"), font);
+       bool foundfont = fontModule->fontsRomanCO->set(font, false);
+       if (!foundfont) {
+               fontModule->fontsRomanCO->addItemSort(font, font + qt_(" (not installed)"),
+                                                     qt_("Uninstalled used fonts"),
+                                                     qt_("This font is not installed and won't be used in output"),
+                                                     false, false, false, true);
+               fontModule->fontsRomanCO->set(font);
        }
-       fontModule->fontsRomanCO->setCurrentIndex(rpos);
        fontModule->font_roman = toqstr(bp_.fonts_roman[!bp_.useNonTeXFonts]);
 
        font = toqstr(bp_.fontsSans());
-       int spos = fontModule->fontsSansCO->findData(font);
-       if (spos == -1) {
-               spos = fontModule->fontsSansCO->count();
-               fontModule->fontsSansCO->addItem(font + qt_(" (not installed)"), font);
+       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"),
+                                                    false, false, false, true);
+               fontModule->fontsSansCO->set(font);
        }
-       fontModule->fontsSansCO->setCurrentIndex(spos);
        fontModule->font_sans = toqstr(bp_.fonts_sans[!bp_.useNonTeXFonts]);
 
        font = toqstr(bp_.fontsTypewriter());
-       int tpos = fontModule->fontsTypewriterCO->findData(font);
-       if (tpos == -1) {
-               tpos = fontModule->fontsTypewriterCO->count();
-               fontModule->fontsTypewriterCO->addItem(font + qt_(" (not installed)"), font);
+       foundfont = fontModule->fontsTypewriterCO->set(font, false);
+       if (!foundfont) {
+               fontModule->fontsTypewriterCO->addItemSort(font, font + qt_(" (not installed)"),
+                                                          qt_("Uninstalled used fonts"),
+                                                          qt_("This font is not installed and won't be used in output"),
+                                                          false, false, false, true);
+               fontModule->fontsTypewriterCO->set(font);
        }
-       fontModule->fontsTypewriterCO->setCurrentIndex(tpos);
        fontModule->font_typewriter = toqstr(bp_.fonts_typewriter[!bp_.useNonTeXFonts]);
 
        font = toqstr(bp_.fontsMath());
@@ -4377,10 +4530,12 @@ void GuiDocument::paramsToDialog()
                fontModule->fontencLA->setEnabled(true);
                fontModule->fontencCO->setEnabled(true);
                fontModule->fontencLE->setEnabled(true);
-               romanChanged(rpos);
-               sansChanged(spos);
-               ttChanged(tpos);
+               romanChanged(fontModule->fontsRomanCO->currentIndex());
+               sansChanged(fontModule->fontsSansCO->currentIndex());
+               ttChanged(fontModule->fontsTypewriterCO->currentIndex());
        }
+       // Handle options enabling
+       updateFontOptions();
 
        if (!bp_.fonts_cjk.empty())
                fontModule->cjkFontLE->setText(
@@ -4490,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_);
@@ -4556,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);
@@ -4597,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);
@@ -4661,7 +4820,7 @@ void GuiDocument::updateIncludeonlyDisplay()
 }
 
 
-void GuiDocument::updateIncludeonlys()
+void GuiDocument::updateIncludeonlys(bool const cleanup)
 {
        masterChildModule->childrenTW->clear();
        QString const no = qt_("No");
@@ -4687,7 +4846,7 @@ void GuiDocument::updateIncludeonlys()
        }
        // Both if all children are included and if none is included
        // is equal to "include all" (i.e., omit \includeonly).
-       if (!has_unincluded || all_unincluded)
+       if (cleanup && (!has_unincluded || all_unincluded))
                includeonlys_.clear();
 }
 
@@ -4886,39 +5045,15 @@ void GuiDocument::setLayoutComboByIDString(string const & idString)
 
 bool GuiDocument::isValid()
 {
-       return
-               validateListingsParameters().isEmpty() &&
-               !localLayout->editing() &&
-               !preambleModule->editing() &&
-               (
-                       // if we're asking for skips between paragraphs
-                       !textLayoutModule->skipRB->isChecked() ||
-                       // then either we haven't chosen custom
-                       VSpace::VSpaceKind(
-                               textLayoutModule->skipCO->itemData(
-                                       textLayoutModule->skipCO->currentIndex()).toInt())
-                               != VSpace::LENGTH ||
-                       // or else a length has been given
-                       !textLayoutModule->skipLE->text().isEmpty()
-               ) &&
-               (
-                       // if we're asking for indentation
-                       !textLayoutModule->indentRB->isChecked() ||
-                       // then either we haven't chosen custom
-                       (textLayoutModule->indentCO->itemData(
-                               textLayoutModule->indentCO->currentIndex()) != "custom") ||
-                       // or else a length has been given
-                       !textLayoutModule->indentLE->text().isEmpty()
-               ) &&
-               (
-                       // if we're asking for math indentation
-                       !mathsModule->MathIndentCB->isChecked() ||
-                       // then either we haven't chosen custom
-                       (mathsModule->MathIndentCO->itemData(
-                               mathsModule->MathIndentCO->currentIndex()) != "custom") ||
-                       // or else a length has been given
-                       !mathsModule->MathIndentLE->text().isEmpty()
-               );
+       bool const listings_valid = validateListingsParameters().isEmpty();
+       bool const local_layout_valid = !localLayout->editing();
+       bool const preamble_valid = !preambleModule->editing();
+
+       docPS->markPanelValid(N_("Listings[[inset]]"), listings_valid);
+       docPS->markPanelValid(N_("Local Layout"), local_layout_valid && localLayout->isValid());
+       docPS->markPanelValid(N_("LaTeX Preamble"), preamble_valid);
+       
+       return listings_valid && local_layout_valid && preamble_valid;
 }
 
 
@@ -5129,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(),
@@ -5151,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(),
@@ -5162,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(),
@@ -5323,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