]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiDocument.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiDocument.cpp
index d60385b041dd98d5bda834874fb4264347b5851a..0f143b2cc032e4be23a0e9a96949e2ab5a14a456 100644 (file)
@@ -65,6 +65,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
+#include "support/Package.h"
 #include "support/TempFile.h"
 
 #include "frontends/alert.h"
 #include <QColor>
 #include <QColorDialog>
 #include <QCloseEvent>
+#include <QDirIterator>
 #include <QFontDatabase>
 #include <QHeaderView>
+#include <QPixmap>
 #include <QScrollBar>
 #include <QTextBoundaryFinder>
 #include <QTextCursor>
@@ -153,6 +156,11 @@ QMap<QString, QString> sffonts_;
 QMap<QString, QString> ttfonts_;
 QMap<QString, QString> mathfonts_;
 
+enum EncodingSets {
+       unicode = 0,
+       legacy = 1,
+       custom = 2
+};
 
 } // anonymous namespace
 
@@ -262,12 +270,12 @@ public:
        ///
        ModuleSelectionManager(QObject * parent,
                               QTreeView * availableLV,
-                              QListView * selectedLV,
+                              QTreeView * selectedLV,
                               QPushButton * addPB,
                               QPushButton * delPB,
                               QPushButton * upPB,
                               QPushButton * downPB,
-                              GuiIdListModel * availableModel,
+                              QStandardItemModel * availableModel,
                               GuiIdListModel * selectedModel,
                               GuiDocument const * container)
                : GuiSelectionManager(parent, availableLV, selectedLV, addPB, delPB,
@@ -290,9 +298,9 @@ private:
        ///
        virtual void updateDelPB();
        /// returns availableModel as a GuiIdListModel
-       GuiIdListModel * getAvailableModel()
+       QStandardItemModel * getAvailableModel()
        {
-               return dynamic_cast<GuiIdListModel *>(availableModel);
+               return dynamic_cast<QStandardItemModel *>(availableModel);
        }
        /// returns selectedModel as a GuiIdListModel
        GuiIdListModel * getSelectedModel()
@@ -311,7 +319,7 @@ void ModuleSelectionManager::updateAddPB()
 {
        int const arows = availableModel->rowCount();
        QModelIndexList const avail_sels =
-                       availableLV->selectionModel()->selectedIndexes();
+                       availableLV->selectionModel()->selectedRows(0);
 
        // disable if there aren't any modules (?), if none of them is chosen
        // in the dialog, or if the chosen one is already selected for use.
@@ -321,7 +329,14 @@ void ModuleSelectionManager::updateAddPB()
        }
 
        QModelIndex const & idx = availableLV->selectionModel()->currentIndex();
-       string const modname = getAvailableModel()->getIDString(idx.row());
+
+       if (getAvailableModel()->itemFromIndex(idx)->hasChildren()) {
+               // This is a category header
+               addPB->setEnabled(false);
+               return;
+       }
+
+       string const modname = fromqstr(getAvailableModel()->data(idx, Qt::UserRole).toString());
 
        bool const enable =
                container_->params().layoutModuleCanBeAdded(modname);
@@ -559,7 +574,7 @@ 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"));
                changed();
                return;
        }
@@ -575,7 +590,7 @@ void PreambleModule::editExternal() {
        os.close();
        preambleTE->setReadOnly(true);
        theFormats().edit(*current_id_, tempfilename, format);
-       editPB->setText(qt_("End Edit"));
+       editPB->setText(qt_("&End Edit"));
        changed();
 }
 
@@ -632,8 +647,10 @@ void LocalLayout::hideConvert()
 
 void LocalLayout::textChanged()
 {
-       static const QString message =
-               qt_("Press button to check validity...");
+       // Flashy red bold text
+       static const QString ivpar("<p style=\"color: #c00000; font-weight: bold; text-align:left\">"
+                                  "%1</p>");
+       static const QString message = ivpar.arg(qt_("Validation required!"));
        string const layout =
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
 
@@ -673,9 +690,9 @@ void LocalLayout::convertPressed() {
 
 void LocalLayout::validate() {
        // Bold text
-       static const QString vpar("<p style=\"font-weight: bold;\">%1</p>");
+       static const QString vpar("<p style=\"font-weight: bold; text-align:left\">%1</p>");
        // Flashy red bold text
-       static const QString ivpar("<p style=\"color: #c00000; font-weight: bold; \">"
+       static const QString ivpar("<p style=\"color: #c00000; font-weight: bold; text-align:left\">"
                                   "%1</p>");
        string const layout =
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
@@ -729,7 +746,7 @@ 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"));
                changed();
                return;
        }
@@ -745,7 +762,7 @@ void LocalLayout::editExternal() {
        os.close();
        locallayoutTE->setReadOnly(true);
        theFormats().edit(*current_id_, tempfilename, format);
-       editPB->setText(qt_("End Edit"));
+       editPB->setText(qt_("&End Edit"));
        validatePB->setEnabled(false);
        hideConvert();
        changed();
@@ -826,6 +843,9 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(textLayoutModule->justCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
 
+       connect(textLayoutModule->tableStyleCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+
        textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
                textLayoutModule->lspacingLE));
        textLayoutModule->indentLE->setValidator(new LengthValidator(
@@ -851,6 +871,9 @@ GuiDocument::GuiDocument(GuiView & lv)
        bc().addCheckedLineEdit(textLayoutModule->indentLE);
        bc().addCheckedLineEdit(textLayoutModule->skipLE);
 
+       textLayoutModule->tableStyleCO->addItem(qt_("Default"), toqstr("default"));
+       getTableStyles();
+
 
        // master/child handling
        masterChildModule = new UiWidget<Ui::MasterChildUi>(this);
@@ -913,11 +936,15 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(langModule->languageCO, SIGNAL(activated(int)),
                this, SLOT(languageChanged(int)));
-       connect(langModule->defaultencodingRB, SIGNAL(clicked()),
-               this, SLOT(change_adaptor()));
-       connect(langModule->otherencodingRB, SIGNAL(clicked()),
+       connect(langModule->encodingCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(langModule->encodingCO, SIGNAL(activated(int)),
+               this, SLOT(encodingSwitched(int)));
+       connect(langModule->unicodeEncodingCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(langModule->customEncodingCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(langModule->noInputencCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
        connect(langModule->quoteStyleCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
@@ -939,15 +966,44 @@ GuiDocument::GuiDocument(GuiView & lv)
        langModule->languageCO->setModel(language_model);
        langModule->languageCO->setModelColumn(0);
 
-       // Always put the default encoding in the first position.
-       langModule->encodingCO->addItem(qt_("Language Default (no inputenc)"));
-       QStringList encodinglist;
+       langModule->encodingCO->addItem(qt_("Unicode (utf8)"));
+       langModule->encodingCO->addItem(qt_("Traditional (auto-selected)"));
+       langModule->encodingCO->addItem(qt_("Custom"));
+       langModule->encodingCO->setItemData(EncodingSets::unicode,
+               "Use Unicode (utf8) for the latex source.", Qt::ToolTipRole);
+       langModule->encodingCO->setItemData(EncodingSets::legacy,
+               "Use legacy default encodings depending on text language.", Qt::ToolTipRole);
+       langModule->encodingCO->setItemData(EncodingSets::custom,
+               "Select a custom, document-wide encoding.", Qt::ToolTipRole);
+       
+       QMap<QString,QString> encodingmap_utf8;
+       for (auto const & encvar : encodings) {
+               if (!encvar.unsafe() && !encvar.guiName().empty()
+                       && std::string(encvar.name()).find("utf8") == 0)
+                       encodingmap_utf8.insert(qt_(encvar.guiName()), qt_(encvar.name()));
+       }
+       QMap<QString, QString>::const_iterator it8 = encodingmap_utf8.constBegin();
+       while (it8 != encodingmap_utf8.constEnd()) {
+               langModule->unicodeEncodingCO->addItem(it8.key(), it8.value());
+               ++it8;
+       }
+
+       QMap<QString,QString> encodingmap;
        for (auto const & encvar : encodings) {
-               if (!encvar.unsafe() && !encvar.guiName().empty())
-                       encodinglist.append(qt_(encvar.guiName()));
+               if (!encvar.unsafe() && !encvar.guiName().empty()
+                       && std::string(encvar.name()).find("utf8") != 0)
+                       encodingmap.insert(qt_(encvar.guiName()), qt_(encvar.name()));
        }
-       encodinglist.sort();
-       langModule->encodingCO->addItems(encodinglist);
+       QMap<QString, QString>::const_iterator it = encodingmap.constBegin();
+       while (it != encodingmap.constEnd()) {
+               langModule->customEncodingCO->addItem(it.key(), it.value());
+               ++it;
+       }
+       // equalise the width of encoding selectors
+       langModule->unicodeEncodingCO->setMinimumSize(
+               langModule->customEncodingCO->minimumSizeHint());
+       langModule->noInputencCB->setMinimumSize(
+               langModule->customEncodingCO->minimumSizeHint());
 
        langModule->languagePackageCO->addItem(
                qt_("Default"), toqstr("default"));
@@ -1226,6 +1282,13 @@ GuiDocument::GuiDocument(GuiView & lv)
        numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
        numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
        setSectionResizeMode(numberingModule->tocTW->header(), QHeaderView::ResizeToContents);
+       connect(numberingModule->linenoCB, SIGNAL(toggled(bool)),
+               this, SLOT(linenoToggled(bool)));
+       connect(numberingModule->linenoCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+       connect(numberingModule->linenoLE, SIGNAL(textChanged(QString)),
+               this, SLOT(change_adaptor()));
+
 
        // biblio
        biblioModule = new UiWidget<Ui::BiblioUi>(this);
@@ -1513,6 +1576,9 @@ GuiDocument::GuiDocument(GuiView & lv)
        modulesModule->availableLV->header()->setVisible(false);
        setSectionResizeMode(modulesModule->availableLV->header(), QHeaderView::ResizeToContents);
        modulesModule->availableLV->header()->setStretchLastSection(false);
+       modulesModule->selectedLV->header()->setVisible(false);
+       setSectionResizeMode(modulesModule->selectedLV->header(), QHeaderView::ResizeToContents);
+       modulesModule->selectedLV->header()->setStretchLastSection(false);
        selectionManager =
                new ModuleSelectionManager(this, modulesModule->availableLV,
                                           modulesModule->selectedLV,
@@ -1520,11 +1586,34 @@ GuiDocument::GuiDocument(GuiView & lv)
                                           modulesModule->deletePB,
                                           modulesModule->upPB,
                                           modulesModule->downPB,
-                                          availableModel(), selectedModel(), this);
+                                          availableModel(), selectedModel(), this);
        connect(selectionManager, SIGNAL(updateHook()),
                this, SLOT(updateModuleInfo()));
        connect(selectionManager, SIGNAL(selectionChanged()),
                this, SLOT(modulesChanged()));
+       // The filter bar
+       filter_ = new FancyLineEdit(this);
+       filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
+       filter_->setButtonVisible(FancyLineEdit::Right, true);
+       filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
+       filter_->setAutoHideButton(FancyLineEdit::Right, true);
+       filter_->setPlaceholderText(qt_("All avail. modules"));
+       modulesModule->moduleFilterBarL->addWidget(filter_, 0);
+       modulesModule->findModulesLA->setBuddy(filter_);
+
+       connect(filter_, SIGNAL(rightButtonClicked()),
+               this, SLOT(resetModuleFilter()));
+       connect(filter_, SIGNAL(textEdited(QString)),
+               this, SLOT(moduleFilterChanged(QString)));
+       connect(filter_, SIGNAL(returnPressed()),
+               this, SLOT(moduleFilterPressed()));
+#if (QT_VERSION < 0x050000)
+       connect(filter_, SIGNAL(downPressed()),
+               modulesModule->availableLV, SLOT(setFocus()));
+#else
+       connect(filter_, &FancyLineEdit::downPressed,
+               modulesModule->availableLV, [=](){ focusAndHighlight(modulesModule->availableLV); });
+#endif
 
 
        // PDF support
@@ -1712,6 +1801,64 @@ void GuiDocument::slotButtonBox(QAbstractButton * button)
 }
 
 
+void GuiDocument::filterModules(QString const & str)
+{
+       updateAvailableModules();
+       if (str.isEmpty())
+               return;
+
+       modules_av_model_.clear();
+       list<modInfoStruct> modInfoList = getModuleInfo();
+       // Sort names according to the locale
+       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"));
+
+       int i = 0;
+       for (modInfoStruct const & m : modInfoList) {
+               if (m.name.contains(str, Qt::CaseInsensitive) || contains(m.id, fromqstr(str))) {
+                       QStandardItem * item = new QStandardItem();
+                       item->setData(m.name, Qt::DisplayRole);
+                       item->setData(toqstr(m.id), Qt::UserRole);
+                       item->setData(m.description, Qt::ToolTipRole);
+                       if (m.local)
+                               item->setIcon(user_icon);
+                       else
+                               item->setIcon(system_icon);
+                       modules_av_model_.insertRow(i, item);
+                       ++i;
+               }
+       }
+}
+
+
+void GuiDocument::moduleFilterChanged(const QString & text)
+{
+       if (!text.isEmpty()) {
+               filterModules(filter_->text());
+               return;
+       }
+       filterModules(filter_->text());
+       filter_->setFocus();
+}
+
+
+void GuiDocument::moduleFilterPressed()
+{
+       filterModules(filter_->text());
+}
+
+
+void GuiDocument::resetModuleFilter()
+{
+       filter_->setText(QString());
+       filterModules(filter_->text());
+}
+
+
 void GuiDocument::includeonlyClicked(QTreeWidgetItem * item, int)
 {
        if (item == 0)
@@ -2069,10 +2216,11 @@ void GuiDocument::updateQuoteStyles(bool const set)
 
 void GuiDocument::languageChanged(int i)
 {
-       // some languages only work with polyglossia
+       // some languages only work with Polyglossia
        Language const * lang = lyx::languages.getLanguage(
                fromqstr(langModule->languageCO->itemData(i).toString()));
-       if (lang->babel().empty() && !lang->polyglossia().empty()) {
+       if (lang->babel().empty() && !lang->polyglossia().empty()
+               && lang->requires() != "CJK" && lang->requires() != "japanese") {
                        // If we force to switch fontspec on, store
                        // current state (#8717)
                        if (fontModule->osFontsCB->isEnabled())
@@ -2143,10 +2291,9 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
        fontModule->font_sf_scale = font_sf_scale;
        fontModule->font_tt_scale = font_tt_scale;
 
-       langModule->encodingCO->setEnabled(tex_fonts &&
-               !langModule->defaultencodingRB->isChecked());
-       langModule->defaultencodingRB->setEnabled(tex_fonts);
-       langModule->otherencodingRB->setEnabled(tex_fonts);
+       // non-tex fonts override the "\inputencoding" option with "utf8-plain"
+       langModule->encodingCO->setEnabled(tex_fonts);
+       inputencodingToDialog();
 
        fontModule->fontsDefaultCO->setEnabled(tex_fonts);
        fontModule->fontsDefaultLA->setEnabled(tex_fonts);
@@ -2164,12 +2311,50 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
 }
 
 
+void GuiDocument::encodingSwitched(int i)
+{
+       bool const tex_fonts = !fontModule->osFontsCB->isChecked();
+       langModule->unicodeEncodingCO->setEnabled(tex_fonts);
+       langModule->customEncodingCO->setEnabled(tex_fonts);
+       langModule->noInputencCB->setEnabled(tex_fonts);
+       langModule->unicodeEncodingCO->setVisible(i == EncodingSets::unicode);
+       langModule->noInputencCB->setVisible(i == EncodingSets::legacy);
+       langModule->customEncodingCO->setVisible(i == EncodingSets::custom);
+}
+
+void GuiDocument::inputencodingToDialog()
+{
+       QString inputenc = toqstr(bp_.inputenc);
+       int p;
+       if (fontModule->osFontsCB->isChecked()) { // non-tex fonts require utf8-plain
+               langModule->encodingCO->setCurrentIndex(EncodingSets::unicode);
+               langModule->unicodeEncodingCO->setCurrentIndex(
+                       langModule->unicodeEncodingCO->findData("utf8-plain"));
+       } else if (inputenc.startsWith("utf8")) {
+               langModule->encodingCO->setCurrentIndex(EncodingSets::unicode);
+               p = langModule->unicodeEncodingCO->findData(inputenc);
+               if (p != -1)
+                       langModule->unicodeEncodingCO->setCurrentIndex(p);
+       } else if (inputenc.startsWith("auto")) {
+               langModule->encodingCO->setCurrentIndex(EncodingSets::legacy);
+               langModule->noInputencCB->setChecked(inputenc == "auto-legacy-plain");
+       } else {
+               langModule->encodingCO->setCurrentIndex(EncodingSets::custom);
+               p = langModule->customEncodingCO->findData(inputenc);
+               if (p != -1)
+                       langModule->customEncodingCO->setCurrentIndex(p);
+               else
+                       langModule->encodingCO->setCurrentIndex(EncodingSets::unicode);
+       }
+       encodingSwitched(langModule->encodingCO->currentIndex());
+}
+
+
 void GuiDocument::mathFontChanged(int)
 {
        updateFontOptions();
 }
 
-
 void GuiDocument::fontOsfToggled(bool state)
 {
        if (fontModule->osFontsCB->isChecked())
@@ -2555,7 +2740,7 @@ void GuiDocument::classChanged()
        // according to the new class. Note, however, that, if you use
        // the scroll wheel when sitting on the combo box, we'll load a
        // lot of TextClass objects very quickly....
-       if (!bp_.setBaseClass(classname)) {
+       if (!bp_.setBaseClass(classname, buffer().layoutPos())) {
                Alert::error(_("Error"), _("Unable to set document class."));
                return;
        }
@@ -2814,6 +2999,7 @@ void GuiDocument::modulesToParams(BufferParams & bp)
        int const srows = modules_sel_model_.rowCount();
        for (int i = 0; i < srows; ++i)
                bp.addLayoutModule(modules_sel_model_.getIDString(i));
+       updateSelectedModules();
 
        // update the list of removed modules
        bp.clearRemovedModules();
@@ -2867,18 +3053,28 @@ void GuiDocument::updateModuleInfo()
        //Module description
        bool const focus_on_selected = selectionManager->selectedFocused();
        QAbstractItemView * lv;
-       if (focus_on_selected)
+       bool category = false;
+       if (focus_on_selected) {
                lv = modulesModule->selectedLV;
-       else
+               category = true;
+       } else
                lv = modulesModule->availableLV;
        if (lv->selectionModel()->selectedIndexes().isEmpty()) {
                modulesModule->infoML->document()->clear();
                return;
        }
        QModelIndex const & idx = lv->selectionModel()->currentIndex();
-       GuiIdListModel const & id_model =
-                       focus_on_selected  ? modules_sel_model_ : modules_av_model_;
-       string const modName = id_model.getIDString(idx.row());
+
+       if (!focus_on_selected
+           && modules_av_model_.itemFromIndex(idx)->hasChildren()) {
+               // This is a category header
+               modulesModule->infoML->document()->clear();
+               return;
+       }
+
+       string const modName = focus_on_selected ?
+                               modules_sel_model_.getIDString(idx.row())
+                             : fromqstr(modules_av_model_.data(idx, Qt::UserRole).toString());
        docstring desc = getModuleDescription(modName);
 
        LayoutModuleList const & provmods = bp_.baseClass()->providedModules();
@@ -2888,11 +3084,14 @@ void GuiDocument::updateModuleInfo()
                desc += _("Module provided by document class.");
        }
 
-       docstring cat = getModuleCategory(modName);
-       if (!cat.empty()) {
-               if (!desc.empty())
-                       desc += "\n";
-               desc += bformat(_("Category: %1$s."), cat);
+       if (category) {
+               docstring cat = getModuleCategory(modName);
+               if (!cat.empty()) {
+                       if (!desc.empty())
+                               desc += "\n";
+                       desc += bformat(_("<p><b>Category:</b> %1$s.</p>"),
+                                       translateIfPossible(cat));
+               }
        }
 
        vector<string> pkglist = getPackageList(modName);
@@ -2900,7 +3099,7 @@ void GuiDocument::updateModuleInfo()
        if (!pkgdesc.empty()) {
                if (!desc.empty())
                        desc += "\n";
-               desc += bformat(_("Package(s) required: %1$s."), pkgdesc);
+               desc += bformat(_("<p><b>Package(s) required:</b> %1$s.</p>"), pkgdesc);
        }
 
        pkglist = getRequiredList(modName);
@@ -2909,7 +3108,7 @@ void GuiDocument::updateModuleInfo()
                pkgdesc = formatStrVec(reqdescs, _("or"));
                if (!desc.empty())
                        desc += "\n";
-               desc += bformat(_("Modules required: %1$s."), pkgdesc);
+               desc += bformat(_("<p><b>Modules required:</b> %1$s.</p>"), pkgdesc);
        }
 
        pkglist = getExcludedList(modName);
@@ -2918,16 +3117,20 @@ void GuiDocument::updateModuleInfo()
                pkgdesc = formatStrVec(reqdescs, _( "and"));
                if (!desc.empty())
                        desc += "\n";
-               desc += bformat(_("Modules excluded: %1$s."), pkgdesc);
+               desc += bformat(_("<p><b>Modules excluded:</b> %1$s.</p>"), pkgdesc);
        }
 
+       if (!desc.empty())
+               desc += "\n";
+       desc += bformat(_("<p><b>Filename:</b> <tt>%1$s.module</tt>.</p>"), from_utf8(modName));
+
        if (!isModuleAvailable(modName)) {
                if (!desc.empty())
                        desc += "\n";
-               desc += _("WARNING: Some required packages are unavailable!");
+               desc += _("<p><font color=red><b>WARNING: Some required packages are unavailable!</b></font></p>");
        }
 
-       modulesModule->infoML->document()->setPlainText(toqstr(desc));
+       modulesModule->infoML->document()->setHtml(toqstr(desc));
 }
 
 
@@ -2961,6 +3164,45 @@ void GuiDocument::updateNumbering()
 }
 
 
+void GuiDocument::getTableStyles()
+{
+       // We look for lyx files in the subdirectory dir of
+       //   1) user_lyxdir
+       //   2) build_lyxdir (if not empty)
+       //   3) system_lyxdir
+       // in this order. Files with a given sub-hierarchy will
+       // only be listed once.
+       // We also consider i18n subdirectories and store them separately.
+       QStringList dirs;
+
+       // The three locations to look at.
+       string const user = addPath(package().user_support().absFileName(), "tabletemplates");
+       string const build = addPath(package().build_support().absFileName(), "tabletemplates");
+       string const system = addPath(package().system_support().absFileName(), "tabletemplates");
+
+       dirs << toqstr(user)
+            << toqstr(build)
+            << toqstr(system);
+
+       for (int i = 0; i < dirs.size(); ++i) {
+               QString const dir = dirs.at(i);
+               QDirIterator it(dir, QDir::Files, QDirIterator::Subdirectories);
+               while (it.hasNext()) {
+                       QString fn = QFileInfo(it.next()).fileName();
+                       if (!fn.endsWith(".lyx") || fn.contains("_1x"))
+                               continue;
+                       QString data = fn.left(fn.lastIndexOf(".lyx"));
+                       QString guiname = data;
+                       guiname = toqstr(translateIfPossible(qstring_to_ucs4(guiname.replace('_', ' '))));
+                       QString relpath = toqstr(makeRelPath(qstring_to_ucs4(fn),
+                                                            qstring_to_ucs4(dir)));
+                       if (textLayoutModule->tableStyleCO->findData(data) == -1)
+                               textLayoutModule->tableStyleCO->addItem(guiname, data);
+               }
+       }
+}
+
+
 void GuiDocument::updateDefaultFormat()
 {
        if (!bufferview())
@@ -2971,7 +3213,7 @@ void GuiDocument::updateDefaultFormat()
        int const idx = latexModule->classCO->currentIndex();
        if (idx >= 0) {
                string const classname = fromqstr(latexModule->classCO->getData(idx));
-               param_copy.setBaseClass(classname);
+               param_copy.setBaseClass(classname, buffer().layoutPos());
                param_copy.makeDocumentClass(true);
        }
        outputModule->defaultFormatCO->blockSignals(true);
@@ -3050,34 +3292,27 @@ void GuiDocument::applyView()
        indicesModule->apply(bp_);
 
        // language & quotes
-       if (langModule->defaultencodingRB->isChecked()) {
-               bp_.inputenc = "auto";
-       } else {
-               int i = langModule->encodingCO->currentIndex();
-               if (i == 0)
-                       bp_.inputenc = "default";
-               else {
-                       QString const enc_gui =
-                               langModule->encodingCO->currentText();
-                       Encodings::const_iterator it = encodings.begin();
-                       Encodings::const_iterator const end = encodings.end();
-                       bool found = false;
-                       for (; it != end; ++it) {
-                               if (qt_(it->guiName()) == enc_gui &&
-                                   !it->unsafe()) {
-                                       bp_.inputenc = it->name();
-                                       found = true;
-                                       break;
-                               }
-                       }
-                       if (!found) {
-                               // should not happen
-                               lyxerr << "GuiDocument::apply: Unknown encoding! Resetting to default" << endl;
-                               bp_.inputenc = "default";
-                       }
+       switch (langModule->encodingCO->currentIndex()) {
+               case EncodingSets::unicode: {
+                       bp_.inputenc = fromqstr(langModule->unicodeEncodingCO->itemData(
+                               langModule->unicodeEncodingCO->currentIndex()).toString());
+                       break;
+               }
+               case EncodingSets::legacy: {
+                       bp_.inputenc = "auto-legacy";
+                       if (langModule->noInputencCB->isChecked())
+                               bp_.inputenc = "auto-legacy-plain";
+                       break;
+               }
+               case EncodingSets::custom: {
+                       bp_.inputenc = fromqstr(langModule->customEncodingCO->itemData(
+                               langModule->customEncodingCO->currentIndex()).toString());
+                       break;
                }
+               default:
+                       // this should never happen
+                       bp_.inputenc = "utf8";
        }
-
        bp_.quotes_style = (InsetQuotesParams::QuoteStyle) langModule->quoteStyleCO->itemData(
                langModule->quoteStyleCO->currentIndex()).toInt();
        bp_.dynamic_quotes = langModule->dynamicQuotesCB->isChecked();
@@ -3114,6 +3349,8 @@ void GuiDocument::applyView()
                bp_.tocdepth = numberingModule->tocSL->value();
                bp_.secnumdepth = numberingModule->depthSL->value();
        }
+       bp_.use_lineno = numberingModule->linenoCB->isChecked();
+       bp_.lineno_opts = fromqstr(numberingModule->linenoLE->text());
 
        // bullets
        bp_.user_defined_bullet(0) = bulletsModule->bullet(0);
@@ -3129,7 +3366,7 @@ void GuiDocument::applyView()
        int idx = latexModule->classCO->currentIndex();
        if (idx >= 0) {
                string const classname = fromqstr(latexModule->classCO->getData(idx));
-               bp_.setBaseClass(classname);
+               bp_.setBaseClass(classname, buffer().layoutPos());
        }
 
        // Modules
@@ -3279,6 +3516,8 @@ void GuiDocument::applyView()
                        break;
                }
        }
+       bp_.tablestyle = fromqstr(textLayoutModule->tableStyleCO->itemData(
+                                     textLayoutModule->tableStyleCO->currentIndex()).toString());
 
        bp_.options =
                fromqstr(latexModule->optionsLE->text());
@@ -3566,35 +3805,9 @@ void GuiDocument::paramsToDialog()
                langModule->quoteStyleCO->findData(bp_.quotes_style));
        langModule->dynamicQuotesCB->setChecked(bp_.dynamic_quotes);
 
-       bool default_enc = true;
-       if (bp_.inputenc != "auto") {
-               default_enc = false;
-               if (bp_.inputenc == "default") {
-                       langModule->encodingCO->setCurrentIndex(0);
-               } else {
-                       string enc_gui;
-                       Encodings::const_iterator it = encodings.begin();
-                       Encodings::const_iterator const end = encodings.end();
-                       for (; it != end; ++it) {
-                               if (it->name() == bp_.inputenc &&
-                                   !it->unsafe()) {
-                                       enc_gui = it->guiName();
-                                       break;
-                               }
-                       }
-                       int const i = langModule->encodingCO->findText(
-                                       qt_(enc_gui));
-                       if (i >= 0)
-                               langModule->encodingCO->setCurrentIndex(i);
-                       else
-                               // unknown encoding. Set to default.
-                               default_enc = true;
-               }
-       }
-       langModule->defaultencodingRB->setChecked(default_enc);
-       langModule->otherencodingRB->setChecked(!default_enc);
+       // LaTeX input encoding: set after the fonts (see below)
 
-       int const p = langModule->languagePackageCO->findData(toqstr(bp_.lang_package));
+       int p = langModule->languagePackageCO->findData(toqstr(bp_.lang_package));
        if (p == -1) {
                langModule->languagePackageCO->setCurrentIndex(
                          langModule->languagePackageCO->findData("custom"));
@@ -3644,6 +3857,11 @@ void GuiDocument::paramsToDialog()
                numberingModule->tocTW->clear();
        }
 
+       numberingModule->linenoCB->setChecked(bp_.use_lineno);
+       numberingModule->linenoLE->setEnabled(bp_.use_lineno);
+       numberingModule->linenoLA->setEnabled(bp_.use_lineno);
+       numberingModule->linenoLE->setText(toqstr(bp_.lineno_opts));
+
        // bullets
        bulletsModule->setBullet(0, bp_.user_defined_bullet(0));
        bulletsModule->setBullet(1, bp_.user_defined_bullet(1));
@@ -3731,6 +3949,9 @@ void GuiDocument::paramsToDialog()
                        bp_.spacing().getValueAsString());
        }
        setLSpacing(nitem);
+       int ts = textLayoutModule->tableStyleCO->findData(toqstr(bp_.tablestyle));
+       if (ts != -1)
+               textLayoutModule->tableStyleCO->setCurrentIndex(ts);
 
        if (bp_.paragraph_separation == BufferParams::ParagraphIndentSeparation) {
                textLayoutModule->indentRB->setChecked(true);
@@ -3847,14 +4068,14 @@ void GuiDocument::paramsToDialog()
        if (nn >= 0)
                listingsModule->packageCO->setCurrentIndex(nn);
 
-
        // Fonts
-       // some languages only work with polyglossia/XeTeX
+       // some languages only work with Polyglossia (which requires non-TeX fonts)
        Language const * lang = lyx::languages.getLanguage(
                fromqstr(langModule->languageCO->itemData(
                        langModule->languageCO->currentIndex()).toString()));
        bool const need_fontspec =
-               lang->babel().empty() && !lang->polyglossia().empty();
+               lang->babel().empty() && !lang->polyglossia().empty()
+               && lang->requires() != "CJK" && lang->requires() != "japanese";
        bool const os_fonts_available =
                bp_.baseClass()->outputType() == lyx::LATEX
                && LaTeXFeatures::isAvailable("fontspec");
@@ -3943,6 +4164,10 @@ void GuiDocument::paramsToDialog()
                fontModule->fontencLE->setText(toqstr(bp_.fontenc));
        }
 
+       // LaTeX input encoding
+       // Set after fonts because non-tex fonts override "\inputencoding".
+       inputencodingToDialog();
+
        // Formats
        // This must be set _after_ fonts since updateDefaultFormat()
        // checks osFontsCB settings.
@@ -4085,11 +4310,40 @@ 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"));
        int i = 0;
+       QFont catfont;
+       catfont.setBold(true);
+       QBrush unavbrush;
+       unavbrush.setColor(Qt::gray);
        for (modInfoStruct const & m : modInfoList) {
-               modules_av_model_.insertRow(i, m.name, m.id, m.description);
-               ++i;
+               QStandardItem * item = new QStandardItem();
+               QStandardItem * catItem = new QStandardItem();
+               QString const catname = m.category;
+               QList<QStandardItem *> fcats = modules_av_model_.findItems(catname, Qt::MatchExactly);
+               if (!fcats.empty())
+                       catItem = fcats.first();
+               else {
+                       catItem->setText(catname);
+                       catItem->setFont(catfont);
+                       modules_av_model_.insertRow(i, catItem);
+                       ++i;
+               }
+               item->setEditable(false);
+               catItem->setEditable(false);
+               item->setData(m.name, Qt::DisplayRole);
+               if (m.missingreqs)
+                       item->setForeground(unavbrush);
+               item->setData(toqstr(m.id), Qt::UserRole);
+               item->setData(m.description, Qt::ToolTipRole);
+               if (m.local)
+                       item->setIcon(user_icon);
+               else
+                       item->setIcon(system_icon);
+               catItem->appendRow(item);
        }
+       modules_av_model_.sort(0);
 }
 
 
@@ -4326,7 +4580,7 @@ void GuiDocument::useClassDefaults()
 
        int idx = latexModule->classCO->currentIndex();
        string const classname = fromqstr(latexModule->classCO->getData(idx));
-       if (!bp_.setBaseClass(classname)) {
+       if (!bp_.setBaseClass(classname, buffer().layoutPos())) {
                Alert::error(_("Error"), _("Unable to set document class."));
                return;
        }
@@ -4439,6 +4693,8 @@ GuiDocument::makeModuleInfo(LayoutModuleList const & mods)
                else {
                        m.id = name;
                        m.name = toqstr(name + " (") + qt_("Not Found") + toqstr(")");
+                       m.local = false;
+                       m.missingreqs = true;
                }
                mInfo.push_back(m);
        }
@@ -4486,10 +4742,9 @@ void GuiDocument::dispatchParams()
        // This must come first so that a language change is correctly noticed
        setLanguage();
 
-       // Apply the BufferParams. Note that this will set the base class
-       // and then update the buffer's layout.
-       dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY, &buffer());
-
+       // We need to load the master before we formally update the params,
+       // since otherwise we run updateBuffer, etc, before the child's master
+       // has been set.
        if (!params().master.empty()) {
                FileName const master_file = support::makeAbsPath(params().master,
                           support::onlyPath(buffer().absFileName()));
@@ -4511,6 +4766,10 @@ void GuiDocument::dispatchParams()
                }
        }
 
+       // Apply the BufferParams. Note that this will set the base class
+       // and then update the buffer's layout.
+       dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY, &buffer());
+
        // Generate the colours requested by each new branch.
        BranchList & branchlist = params().branchlist();
        if (!branchlist.empty()) {
@@ -4631,18 +4890,29 @@ GuiDocument::modInfoStruct GuiDocument::modInfo(LyXModule const & mod)
        // change requires a lot of others
        modInfoStruct m;
        m.id = mod.getID();
-       m.name = toqstr(translateIfPossible(from_utf8(mod.getName())));
+       QString const guiname = toqstr(translateIfPossible(from_utf8(mod.getName())));
+       m.missingreqs = !isModuleAvailable(mod.getID());
+       if (m.missingreqs) {
+               m.name = QString(qt_("%1 (missing req.)")).arg(guiname);
+       } else
+               m.name = guiname;
+       m.category = mod.category().empty() ? qt_("Miscellaneous")
+                                           : toqstr(translateIfPossible(from_utf8(mod.category())));
        QString desc = toqstr(translateIfPossible(from_utf8(mod.getDescription())));
        // Find the first sentence of the description
        QTextBoundaryFinder bf(QTextBoundaryFinder::Sentence, desc);
        int pos = bf.toNextBoundary();
        if (pos > 0)
                desc.truncate(pos);
-       QString modulename = QString(qt_("(Module name: %1)")).arg(toqstr(m.id));
-       // Tooltip is the desc followed by the module name
-       m.description = QString("%1<i>%2</i>")
+       m.local = mod.isLocal();
+       QString const mtype = m.local ? qt_("personal module") : qt_("distributed module");
+       QString modulename = QString(qt_("<b>Module name:</b> <i>%1</i> (%2)")).arg(toqstr(m.id)).arg(mtype);
+       // Tooltip is the desc followed by the module name and the type
+       m.description = QString("%1%2")
                .arg(desc.isEmpty() ? QString() : QString("<p>%1</p>").arg(desc),
                     modulename);
+       if (m.missingreqs)
+               m.description += QString("<p>%1</p>").arg(qt_("<b>Note:</b> Some requirements for this module are missing!"));
        return m;
 }
 
@@ -4651,8 +4921,7 @@ void GuiDocument::loadModuleInfo()
 {
        moduleNames_.clear();
        for (LyXModule const & mod : theModuleList)
-               if (mod.category().substr(0, 8) != "Citation")
-                       moduleNames_.push_back(modInfo(mod));
+               moduleNames_.push_back(modInfo(mod));
 }
 
 
@@ -4725,6 +4994,14 @@ void GuiDocument::allPackages(int col)
 }
 
 
+void GuiDocument::linenoToggled(bool on)
+{
+       numberingModule->linenoLE->setEnabled(on);
+       numberingModule->linenoLA->setEnabled(on);
+}
+
+
+
 Dialog * createGuiDocument(GuiView & lv) { return new GuiDocument(lv); }