]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiDocument.cpp
Kill indexing into the list of layouts.
[lyx.git] / src / frontends / qt4 / GuiDocument.cpp
index 9e3edc0e9e7ccce108aaf74f2c484af58e8a9e24..9d0633586977bb207b5f8869ff61a420f5d827bd 100644 (file)
@@ -42,6 +42,7 @@
 
 #include "insets/InsetListingsParams.h"
 
+#include "support/debug.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
@@ -61,6 +62,7 @@ using namespace std;
 using namespace lyx::support;
 
 
+namespace {
 ///
 template<class Pair>
 vector<typename Pair::second_type> const
@@ -135,7 +137,31 @@ char const * tex_fonts_monospaced_gui[] =
 vector<pair<string, lyx::docstring> > pagestyles;
 
 
+} // anonymous namespace
+
 namespace lyx {
+
+namespace {
+// used when sorting the textclass list.
+class less_textclass_avail_desc
+       : public binary_function<string, string, int>
+{
+public:
+       int operator()(string const & lhs, string const & rhs) const
+       {
+               // Ordering criteria:
+               //   1. Availability of text class
+               //   2. Description (lexicographic)
+               LayoutFile const & tc1 = BaseClassList::get()[lhs];
+               LayoutFile const & tc2 = BaseClassList::get()[rhs];
+               return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
+                       (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
+                        _(tc1.description()) < _(tc2.description()));
+       }
+};
+
+}
+
 namespace frontend {
 
 
@@ -489,7 +515,6 @@ void PreambleModule::closeEvent(QCloseEvent * e)
 /////////////////////////////////////////////////////////////////////
 
 
-
 GuiDocument::GuiDocument(GuiView & lv)
        : GuiDialog(lv, "document", qt_("Document Settings")), current_id_(0)
 {
@@ -876,18 +901,19 @@ GuiDocument::GuiDocument(GuiView & lv)
                latexModule->psdriverCO->addItem(enc);
        }
        // latex classes
-       //FIXME This seems too involved with the kernel. Some of this
-       //should be moved to the kernel---which should perhaps just
-       //give us a list of entries or something of the sort.
-       for (BaseClassList::const_iterator cit = baseclasslist.begin();
-            cit != baseclasslist.end(); ++cit) {
-               if (cit->isTeXClassAvailable()) {
-                       latexModule->classCO->addItem(toqstr(cit->description()));
-               } else {
-                       docstring item =
-                               bformat(_("Unavailable: %1$s"), from_utf8(cit->description()));
-                       latexModule->classCO->addItem(toqstr(item));
-               }
+       latexModule->classCO->setModel(&classes_model_);
+       BaseClassList const & bcl = BaseClassList::get();
+       vector<LayoutFileIndex> classList = bcl.classList();
+       sort(classList.begin(), classList.end(), less_textclass_avail_desc());
+
+       vector<LayoutFileIndex>::const_iterator cit  = classList.begin();
+       vector<LayoutFileIndex>::const_iterator cen = classList.end();
+       for (int i = 0; cit != cen; ++cit, ++i) {
+               LayoutFile const & tc = bcl[*cit];
+               docstring item = (tc.isTeXClassAvailable()) ?
+                       from_utf8(tc.description()) :
+                       bformat(_("Unavailable: %1$s"), from_utf8(tc.description()));
+               classes_model_.insertRow(i, toqstr(item), *cit);
        }
 
        // branches
@@ -957,22 +983,22 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(floatModule, SIGNAL(changed()),
                this, SLOT(change_adaptor()));
 
-       docPS->addPanel(latexModule, _("Document Class"));
-       docPS->addPanel(fontModule, _("Fonts"));
-       docPS->addPanel(textLayoutModule, _("Text Layout"));
-       docPS->addPanel(pageLayoutModule, _("Page Layout"));
-       docPS->addPanel(marginsModule, _("Page Margins"));
-       docPS->addPanel(langModule, _("Language"));
-       docPS->addPanel(numberingModule, _("Numbering & TOC"));
-       docPS->addPanel(biblioModule, _("Bibliography"));
-       docPS->addPanel(pdfSupportModule, _("PDF Properties"));
-       docPS->addPanel(mathsModule, _("Math Options"));
-       docPS->addPanel(floatModule, _("Float Placement"));
-       docPS->addPanel(bulletsModule, _("Bullets"));
-       docPS->addPanel(branchesModule, _("Branches"));
-       docPS->addPanel(embeddedFilesModule, _("Embedded Files"));
-       docPS->addPanel(preambleModule, _("LaTeX Preamble"));
-       docPS->setCurrentPanel(_("Document Class"));
+       docPS->addPanel(latexModule, qt_("Document Class"));
+       docPS->addPanel(fontModule, qt_("Fonts"));
+       docPS->addPanel(textLayoutModule, qt_("Text Layout"));
+       docPS->addPanel(pageLayoutModule, qt_("Page Layout"));
+       docPS->addPanel(marginsModule, qt_("Page Margins"));
+       docPS->addPanel(langModule, qt_("Language"));
+       docPS->addPanel(numberingModule, qt_("Numbering & TOC"));
+       docPS->addPanel(biblioModule, qt_("Bibliography"));
+       docPS->addPanel(pdfSupportModule, qt_("PDF Properties"));
+       docPS->addPanel(mathsModule, qt_("Math Options"));
+       docPS->addPanel(floatModule, qt_("Float Placement"));
+       docPS->addPanel(bulletsModule, qt_("Bullets"));
+       docPS->addPanel(branchesModule, qt_("Branches"));
+       docPS->addPanel(embeddedFilesModule, qt_("Embedded Files"));
+       docPS->addPanel(preambleModule, qt_("LaTeX Preamble"));
+       docPS->setCurrentPanel(qt_("Document Class"));
 // FIXME: hack to work around resizing bug in Qt >= 4.2
 // bug verified with Qt 4.2.{0-3} (JSpitzm)
 #if QT_VERSION >= 0x040200
@@ -983,7 +1009,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 void GuiDocument::showPreamble()
 {
-       docPS->setCurrentPanel(_("LaTeX Preamble"));
+       docPS->setCurrentPanel(qt_("LaTeX Preamble"));
 }
 
 
@@ -1212,12 +1238,32 @@ void GuiDocument::updatePagestyle(string const & items, string const & sel)
 
 void GuiDocument::classChanged()
 {
-       BaseClassIndex const tc = latexModule->classCO->currentIndex();
-       bp_.setBaseClass(tc);
+       int idx = latexModule->classCO->currentIndex();
+       if (idx < 0) 
+               return;
+       string const classname = classes_model_.getIDString(idx);
+       // check if this is a local layout file
+       if (prefixIs(classname, BaseClassList::localPrefix)) {
+               int const ret = Alert::prompt(_("Local layout file"),
+                               _("The layout file you have selected is a local layout\n"
+                                 "file, not one in the system or user directory. Your\n"
+                                 "document may not work with this layout if you do not\n"
+                                 "keep the layout file in the same directory."),
+                                 1, 1, _("&Set Layout"), _("&Cancel"));
+               if (ret == 1) {
+                       // try to reset the layout combo
+                       setLayoutComboByIDString(bp_.baseClassID());
+                       return;
+               }
+       }
+       if (!bp_.setBaseClass(classname)) {
+               Alert::error(_("Error"), _("Unable to set document class."));
+               return;
+       }
        if (lyxrc.auto_reset_options) {
                if (applyPB->isEnabled()) {
                        int const ret = Alert::prompt(_("Unapplied changes"),
-                                       _("Some changes in the dialog were not yet applied."
+                                       _("Some changes in the dialog were not yet applied.\n"
                                        "If you do not apply now, they will be lost after this action."),
                                        1, 1, _("&Apply"), _("&Dismiss"));
                        if (ret == 0)
@@ -1287,7 +1333,7 @@ void GuiDocument::updateModuleInfo()
        }
        QModelIndex const & idx = lv->selectionModel()->currentIndex();
        GuiIdListModel const & idModel = 
-                       focusOnSelected  ? selected_model_ : available_model_;
+                       focusOnSelected  ? modules_sel_model_ : modules_av_model_;
        string const modName = idModel.getIDString(idx.row());
        docstring desc = getModuleDescription(modName);
 
@@ -1364,8 +1410,11 @@ void GuiDocument::updateNumbering()
        QString const no = qt_("No");
        QString const yes = qt_("Yes");
        QTreeWidgetItem * item = 0;
-       for (size_t i = 0; i != tclass.layoutCount(); ++i) {
-               Layout const & lt = *tclass.layout(i);
+
+       DocumentClass::const_iterator lit = tclass.begin();
+       DocumentClass::const_iterator len = tclass.end();
+       for (; lit != len; ++lit) {
+               Layout const & lt = **lit;
                int const toclevel = lt.toclevel;
                if (toclevel != Layout::NOT_IN_TOC && lt.labeltype == LABEL_COUNTER) {
                        item = new QTreeWidgetItem(numberingModule->tocTW);
@@ -1457,14 +1506,18 @@ void GuiDocument::apply(BufferParams & params)
                tex_graphics[latexModule->psdriverCO->currentIndex()];
        
        // text layout
-       params.setBaseClass(latexModule->classCO->currentIndex());
+       int idx = latexModule->classCO->currentIndex();
+       if (idx >= 0) {
+               string const classname = classes_model_.getIDString(idx);
+               params.setBaseClass(classname);
+       }
 
        // Modules
        params.clearLayoutModules();
-       int const srows = selected_model_.rowCount();
+       int const srows = modules_sel_model_.rowCount();
        vector<string> selModList;
        for (int i = 0; i < srows; ++i)
-               params.addLayoutModule(selected_model_.getIDString(i));
+               params.addLayoutModule(modules_sel_model_.getIDString(i));
 
        if (mathsModule->amsautoCB->isChecked()) {
                params.use_amsmath = BufferParams::package_auto;
@@ -1647,7 +1700,8 @@ void GuiDocument::apply(BufferParams & params)
                pdf.pagemode = pdf.pagemode_fullscreen;
        else
                pdf.pagemode.clear();
-       pdf.quoted_options = fromqstr(pdfSupportModule->optionsLE->text());
+       pdf.quoted_options = pdf.quoted_options_check(
+                               fromqstr(pdfSupportModule->optionsLE->text()));
 
        // Embedded files
        // FIXME
@@ -1746,9 +1800,9 @@ void GuiDocument::updateParams(BufferParams const & params)
        langModule->otherencodingRB->setChecked(!default_enc);
 
        // numbering
-       int const min_toclevel = textClass().min_toclevel();
-       int const max_toclevel = textClass().max_toclevel();
-       if (textClass().hasTocLevels()) {
+       int const min_toclevel = documentClass().min_toclevel();
+       int const max_toclevel = documentClass().max_toclevel();
+       if (documentClass().hasTocLevels()) {
                numberingModule->setEnabled(true);
                numberingModule->depthSL->setMinimum(min_toclevel - 1);
                numberingModule->depthSL->setMaximum(max_toclevel);
@@ -1793,9 +1847,10 @@ void GuiDocument::updateParams(BufferParams const & params)
        }
 
        // text layout
-       latexModule->classCO->setCurrentIndex(params.baseClass());
-       
-       updatePagestyle(textClass().opt_pagestyle(),
+       string const & layoutID = params.baseClassID();
+       setLayoutComboByIDString(layoutID);
+
+       updatePagestyle(documentClass().opt_pagestyle(),
                                 params.pagestyle);
 
        textLayoutModule->lspacingCO->setCurrentIndex(nitem);
@@ -1855,7 +1910,7 @@ void GuiDocument::updateParams(BufferParams const & params)
        floatModule->set(params.float_placement);
 
        // Fonts
-       updateFontsize(textClass().opt_fontsize(),
+       updateFontsize(documentClass().opt_fontsize(),
                        params.fontsize);
 
        int n = findToken(tex_fonts_roman, params.fontsRoman);
@@ -1982,12 +2037,12 @@ void GuiDocument::saveDocDefault()
 
 void GuiDocument::updateAvailableModules() 
 {
-       available_model_.clear();
+       modules_av_model_.clear();
        vector<modInfoStruct> const modInfoList = getModuleInfo();
        int const mSize = modInfoList.size();
        for (int i = 0; i < mSize; ++i) {
                modInfoStruct const & modInfo = modInfoList[i];
-               available_model_.insertRow(i, qt_(modInfo.name), modInfo.id);
+               modules_av_model_.insertRow(i, qt_(modInfo.name), modInfo.id);
        }
 }
 
@@ -1995,12 +2050,12 @@ void GuiDocument::updateAvailableModules()
 void GuiDocument::updateSelectedModules() 
 {
        //and selected ones, too
-       selected_model_.clear();
+       modules_sel_model_.clear();
        vector<modInfoStruct> const selModList = getSelectedModules();
        int const sSize = selModList.size();
        for (int i = 0; i < sSize; ++i) {
                modInfoStruct const & modInfo = selModList[i];
-               selected_model_.insertRow(i, qt_(modInfo.name), modInfo.id);
+               modules_sel_model_.insertRow(i, qt_(modInfo.name), modInfo.id);
        }
 }
 
@@ -2041,12 +2096,28 @@ void GuiDocument::useClassDefaults()
                        applyView();
        }
 
-       bp_.setBaseClass(latexModule->classCO->currentIndex());
+       int idx = latexModule->classCO->currentIndex();
+       string const classname = classes_model_.getIDString(idx);
+       if (!bp_.setBaseClass(classname)) {
+               Alert::error(_("Error"), _("Unable to set document class."));
+               return;
+       }
        bp_.useClassDefaults();
        forceUpdate();
 }
 
 
+void GuiDocument::setLayoutComboByIDString(std::string const & idString)
+{
+       int idx = classes_model_.findIDString(idString);
+       if (idx < 0)
+               Alert::warning(_("Can't set layout!"), 
+                       bformat(_("Unable to set layout for ID: %1$s"), from_utf8(idString)));
+       else 
+               latexModule->classCO->setCurrentIndex(idx);
+}
+
+
 bool GuiDocument::isValid()
 {
        return (validate_listings_params().empty() &&
@@ -2111,9 +2182,9 @@ vector<GuiDocument::modInfoStruct> const GuiDocument::getSelectedModules()
 }
 
 
-TextClass const & GuiDocument::textClass() const
+DocumentClass const & GuiDocument::documentClass() const
 {
-       return baseclasslist[bp_.baseClass()];
+       return bp_.documentClass();
 }