]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiDocument.cpp
Don't allow newline characters in preference (#5840).
[lyx.git] / src / frontends / qt4 / GuiDocument.cpp
index 3fa13ebc3adb2edf715d8bbd7178187c3e2462b0..d4c88b506b9c9641485e9d5efbb23907756a8d59 100644 (file)
@@ -537,6 +537,7 @@ LocalLayout::LocalLayout() : current_id_(0), is_valid_(false)
 {
        connect(locallayoutTE, SIGNAL(textChanged()), this, SLOT(textChanged()));
        connect(validatePB, SIGNAL(clicked()), this, SLOT(validatePressed()));
+       connect(convertPB, SIGNAL(clicked()), this, SLOT(convertPressed()));
 }
 
 
@@ -567,8 +568,30 @@ void LocalLayout::textChanged()
        static const QString unknown = qt_("Press button to check validity...");
 
        is_valid_ = false;
-       infoLB->setText(unknown);
+       validLB->setText(unknown);
        validatePB->setEnabled(true);
+       convertPB->setEnabled(false);
+       changed();
+}
+
+
+void LocalLayout::convert() {
+       string const layout =
+               fromqstr(locallayoutTE->document()->toPlainText().trimmed());
+       string const newlayout = TextClass::convert(layout);
+       LYXERR0(newlayout);
+       if (newlayout.empty()) {
+               Alert::error(_("Conversion Failed!"),
+                     _("Failed to convert local layout to current format."));
+       } else {
+               locallayoutTE->setPlainText(toqstr(newlayout));
+       }
+       validate();
+}
+
+
+void LocalLayout::convertPressed() {
+       convert();
        changed();
 }
 
@@ -587,12 +610,25 @@ void LocalLayout::validate() {
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
        if (layout.empty()) {
                is_valid_ = true;
-               infoLB->setText("");
+               validatePB->setEnabled(false);
+               validLB->setText("");
+               convertPB->hide();
+               convertLB->hide();
        } else {
-               is_valid_ = TextClass::validate(layout);
-               infoLB->setText(is_valid_ ? vtext : ivtext);
+               TextClass::ReturnValues const ret = TextClass::validate(layout);
+               is_valid_ = (ret == TextClass::OK) || (ret == TextClass::OK_OLDFORMAT);
+               validatePB->setEnabled(false);
+               validLB->setText(is_valid_ ? vtext : ivtext);
+               if (ret == TextClass::OK_OLDFORMAT) {
+                       convertPB->show();
+                       convertPB->setEnabled(true);
+                       convertLB->setText(qt_("Convert to current format"));
+                       convertLB->show();
+               } else {
+                       convertPB->hide();
+                       convertLB->hide();
+               }
        }
-       validatePB->setEnabled(false);
 }
 
 
@@ -798,6 +834,11 @@ GuiDocument::GuiDocument(GuiView & lv)
                fontModule->fontsDefaultCO->addItem(
                        qt_(GuiDocument::fontfamilies_gui[n]));
 
+       if (!LaTeXFeatures::isAvailable("fontspec"))
+               fontModule->osFontsCB->setToolTip(
+                       qt_("Use OpenType and TrueType fonts directly (requires XeTeX or LuaTeX)\n"
+                           "You need to install the package \"fontspec\" to use this feature"));
+
 
        // page layout
        pageLayoutModule = new UiWidget<Ui::PageLayoutUi>;
@@ -1040,6 +1081,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        numberingModule->tocTW->headerItem()->setText(0, qt_("Example"));
        numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
        numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
+       numberingModule->tocTW->header()->setResizeMode(QHeaderView::ResizeToContents);
 
 
        // biblio
@@ -1066,7 +1108,8 @@ GuiDocument::GuiDocument(GuiView & lv)
        biblioModule->citeStyleCO->addItem(qt_("Author-year"));
        biblioModule->citeStyleCO->addItem(qt_("Numerical"));
        biblioModule->citeStyleCO->setCurrentIndex(0);
-       
+
+       // NOTE: we do not provide "custom" here for security reasons!
        biblioModule->bibtexCO->clear();
        biblioModule->bibtexCO->addItem(qt_("Default"), QString("default"));
        for (set<string>::const_iterator it = lyxrc.bibtex_alternatives.begin();
@@ -1074,7 +1117,6 @@ GuiDocument::GuiDocument(GuiView & lv)
                QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
                biblioModule->bibtexCO->addItem(command, command);
        }
-       biblioModule->bibtexCO->addItem(qt_("Custom"), QString("custom"));
 
 
        // indices
@@ -1093,6 +1135,8 @@ GuiDocument::GuiDocument(GuiView & lv)
                mathsModule->mhchemCB, SLOT(setDisabled(bool)));
        connect(mathsModule->mathdotsautoCB, SIGNAL(toggled(bool)),
                mathsModule->mathdotsCB, SLOT(setDisabled(bool)));
+       connect(mathsModule->undertildeautoCB, SIGNAL(toggled(bool)),
+               mathsModule->undertildeCB, SLOT(setDisabled(bool)));
        
        connect(mathsModule->amsCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
@@ -1110,6 +1154,10 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(mathsModule->mathdotsautoCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(mathsModule->undertildeCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+       connect(mathsModule->undertildeautoCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
        
 
        // latex class
@@ -1189,6 +1237,8 @@ GuiDocument::GuiDocument(GuiView & lv)
        // Modules
        modulesModule = new UiWidget<Ui::ModulesUi>;
        modulesModule->availableLV->header()->setVisible(false);
+       modulesModule->availableLV->header()->setResizeMode(QHeaderView::ResizeToContents);
+       modulesModule->availableLV->header()->setStretchLastSection(false);
        selectionManager =
                new ModuleSelectionManager(modulesModule->availableLV,
                        modulesModule->selectedLV,
@@ -1500,7 +1550,7 @@ void GuiDocument::changeBackgroundColor()
        // set the button color and text
        colorModule->backgroundPB->setStyleSheet(
                colorButtonStyleSheet(newColor));
-       colorModule->backgroundPB->setText(toqstr("Change..."));
+       colorModule->backgroundPB->setText(qt_("&Change..."));
        // save color
        set_backgroundcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_backgroundcolor = true;
@@ -1510,10 +1560,10 @@ void GuiDocument::changeBackgroundColor()
 
 void GuiDocument::deleteBackgroundColor()
 {
-       // set the button color back to default by setting an epmty StyleSheet
+       // set the button color back to default by setting an empty StyleSheet
        colorModule->backgroundPB->setStyleSheet(QLatin1String(""));
        // change button text
-       colorModule->backgroundPB->setText(toqstr("Default..."));
+       colorModule->backgroundPB->setText(qt_("&Default..."));
        // save default color (white)
        set_backgroundcolor = rgbFromHexName("#ffffff");
        is_backgroundcolor = false;
@@ -1530,7 +1580,7 @@ void GuiDocument::changeFontColor()
        // set the button color and text
        colorModule->fontColorPB->setStyleSheet(
                colorButtonStyleSheet(newColor));
-       colorModule->fontColorPB->setText(toqstr("Change..."));
+       colorModule->fontColorPB->setText(qt_("&Change..."));
        // save color
        set_fontcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_fontcolor = true;
@@ -1540,10 +1590,10 @@ void GuiDocument::changeFontColor()
 
 void GuiDocument::deleteFontColor()
 {
-       // set the button color back to default by setting an epmty StyleSheet
+       // set the button color back to default by setting an empty StyleSheet
        colorModule->fontColorPB->setStyleSheet(QLatin1String(""));
        // change button text
-       colorModule->fontColorPB->setText(toqstr("Default..."));
+       colorModule->fontColorPB->setText(qt_("&Default..."));
        // save default color (black)
        set_fontcolor = rgbFromHexName("#000000");
        is_fontcolor = false;
@@ -1568,11 +1618,10 @@ void GuiDocument::changeNoteFontColor()
 
 void GuiDocument::deleteNoteFontColor()
 {
-       // set the button color back to light gray
+       // set the button color back to pref
+       theApp()->getRgbColor(Color_greyedouttext, set_notefontcolor);
        colorModule->noteFontColorPB->setStyleSheet(
-               colorButtonStyleSheet(QColor(204, 204, 204, 255)));
-       // save light gray as the set color
-       set_notefontcolor = rgbFromHexName("#cccccc");
+               colorButtonStyleSheet(rgb2qcolor(set_notefontcolor)));
        changed();
 }
 
@@ -1594,11 +1643,10 @@ void GuiDocument::changeBoxBackgroundColor()
 
 void GuiDocument::deleteBoxBackgroundColor()
 {
-       // set the button color back to red
+       // set the button color back to pref
+       theApp()->getRgbColor(Color_shadedbg, set_boxbgcolor);
        colorModule->boxBackgroundPB->setStyleSheet(
-               colorButtonStyleSheet(QColor(Qt::red)));
-       // save red as the set color
-       set_boxbgcolor = rgbFromHexName("#ff0000");
+               colorButtonStyleSheet(rgb2qcolor(set_boxbgcolor)));
        changed();
 }
 
@@ -1668,16 +1716,16 @@ void GuiDocument::updateFontlist()
 
        // With XeTeX, we have access to all system fonts, but not the LaTeX fonts
        if (fontModule->osFontsCB->isChecked()) {
-               fontModule->fontsRomanCO->addItem(qt_("Default"));
-               fontModule->fontsSansCO->addItem(qt_("Default"));
-               fontModule->fontsTypewriterCO->addItem(qt_("Default"));
+               fontModule->fontsRomanCO->addItem(qt_("Default"), QString("default"));
+               fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
+               fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
        
                QFontDatabase fontdb;
                QStringList families(fontdb.families());
                for (QStringList::Iterator it = families.begin(); it != families.end(); ++it) {
-                       fontModule->fontsRomanCO->addItem(*it);
-                       fontModule->fontsSansCO->addItem(*it);
-                       fontModule->fontsTypewriterCO->addItem(*it);
+                       fontModule->fontsRomanCO->addItem(*it, *it);
+                       fontModule->fontsSansCO->addItem(*it, *it);
+                       fontModule->fontsTypewriterCO->addItem(*it, *it);
                }
                return;
        }
@@ -1686,19 +1734,19 @@ void GuiDocument::updateFontlist()
                QString font = qt_(tex_fonts_roman_gui[n]);
                if (!isFontAvailable(tex_fonts_roman[n]))
                        font += qt_(" (not installed)");
-               fontModule->fontsRomanCO->addItem(font);
+               fontModule->fontsRomanCO->addItem(font, qt_(tex_fonts_roman[n]));
        }
        for (int n = 0; tex_fonts_sans[n][0]; ++n) {
                QString font = qt_(tex_fonts_sans_gui[n]);
                if (!isFontAvailable(tex_fonts_sans[n]))
                        font += qt_(" (not installed)");
-               fontModule->fontsSansCO->addItem(font);
+               fontModule->fontsSansCO->addItem(font, qt_(tex_fonts_sans[n]));
        }
        for (int n = 0; tex_fonts_monospaced[n][0]; ++n) {
                QString font = qt_(tex_fonts_monospaced_gui[n]);
                if (!isFontAvailable(tex_fonts_monospaced[n]))
                        font += qt_(" (not installed)");
-               fontModule->fontsTypewriterCO->addItem(font);
+               fontModule->fontsTypewriterCO->addItem(font, qt_(tex_fonts_monospaced[n]));
        }
 }
 
@@ -1775,7 +1823,7 @@ void GuiDocument::browseLayout()
        QString const label1 = qt_("Layouts|#o#O");
        QString const dir1 = toqstr(lyxrc.document_path);
        QStringList const filter(qt_("LyX Layout (*.layout)"));
-       QString file = browseRelFile(QString(), bufferFilePath(),
+       QString file = browseRelToParent(QString(), bufferFilePath(),
                qt_("Local layout file"), filter, false,
                label1, dir1);
 
@@ -1831,7 +1879,7 @@ void GuiDocument::browseMaster()
        QString const old = latexModule->childDocLE->text();
        QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
        QStringList const filter(qt_("LyX Files (*.lyx)"));
-       QString file = browseRelFile(old, docpath, title, filter, false,
+       QString file = browseRelToSub(old, docpath, title, filter, false,
                qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
 
        if (!file.isEmpty())
@@ -1904,13 +1952,8 @@ void GuiDocument::languagePackageChanged(int i)
 
 void GuiDocument::bibtexChanged(int n)
 {
-       QString const data =
-               biblioModule->bibtexCO->itemData(n).toString();
-       biblioModule->bibtexOptionsED->setEnabled(data != "default");
-       if (data == "custom")
-               biblioModule->bibtexOptionsLA->setText(qt_("Co&mmand:"));
-       else
-               biblioModule->bibtexOptionsLA->setText(qt_("&Options:"));
+       biblioModule->bibtexOptionsED->setEnabled(
+               biblioModule->bibtexCO->itemData(n).toString() != "default");
        changed();
 }
 
@@ -2104,29 +2147,26 @@ void GuiDocument::updateDefaultFormat()
        if (!bufferview())
                return;
        // make a copy in order to consider unapplied changes
-       Buffer * tmpbuf = buffer().clone();
-       tmpbuf->params().useNonTeXFonts =
-               fontModule->osFontsCB->isChecked();
-       int idx = latexModule->classCO->currentIndex();
+       BufferParams param_copy = buffer().params();
+       param_copy.useNonTeXFonts = fontModule->osFontsCB->isChecked();
+       int const idx = latexModule->classCO->currentIndex();
        if (idx >= 0) {
                string const classname = classes_model_.getIDString(idx);
-               tmpbuf->params().setBaseClass(classname);
-               tmpbuf->params().makeDocumentClass();
+               param_copy.setBaseClass(classname);
+               param_copy.makeDocumentClass();
        }
        outputModule->defaultFormatCO->blockSignals(true);
        outputModule->defaultFormatCO->clear();
        outputModule->defaultFormatCO->addItem(qt_("Default"),
                                QVariant(QString("default")));
        typedef vector<Format const *> Formats;
-       Formats formats = tmpbuf->exportableFormats(true);
+       Formats formats = param_copy.exportableFormats(true);
        Formats::const_iterator cit = formats.begin();
        Formats::const_iterator end = formats.end();
        for (; cit != end; ++cit)
                outputModule->defaultFormatCO->addItem(qt_((*cit)->prettyname()),
                                QVariant(toqstr((*cit)->name())));
        outputModule->defaultFormatCO->blockSignals(false);
-       // delete the copy
-       delete tmpbuf;
 }
 
 
@@ -2171,9 +2211,7 @@ void GuiDocument::applyView()
                        biblioModule->bibtexCO->currentIndex()).toString());
        string const bibtex_options =
                fromqstr(biblioModule->bibtexOptionsED->text());
-       if (bibtex_command == "custom")
-               bp_.bibtex_command = bibtex_options;
-       else if (bibtex_command == "default" || bibtex_options.empty())
+       if (bibtex_command == "default" || bibtex_options.empty())
                bp_.bibtex_command = bibtex_command;
        else
                bp_.bibtex_command = bibtex_command + " " + bibtex_options;
@@ -2311,6 +2349,14 @@ void GuiDocument::applyView()
                else
                        bp_.use_mathdots = BufferParams::package_off;
        }
+       if (mathsModule->undertildeautoCB->isChecked())
+               bp_.use_undertilde = BufferParams::package_auto;
+       else {
+               if (mathsModule->undertildeCB->isChecked())
+                       bp_.use_undertilde = BufferParams::package_on;
+               else
+                       bp_.use_undertilde = BufferParams::package_off;
+       }
        
        // Page Layout
        if (pageLayoutModule->pagestyleCO->currentIndex() == 0)
@@ -2448,34 +2494,17 @@ void GuiDocument::applyView()
        bp_.html_math_img_scale = outputModule->mathimgSB->value();
 
        // fonts
-       if (nontexfonts) {
-               if (fontModule->fontsRomanCO->currentIndex() == 0)
-                       bp_.fonts_roman = "default";
-               else
-                       bp_.fonts_roman =
-                               fromqstr(fontModule->fontsRomanCO->currentText());
-       
-               if (fontModule->fontsSansCO->currentIndex() == 0)
-                       bp_.fonts_sans = "default";
-               else
-                       bp_.fonts_sans =
-                               fromqstr(fontModule->fontsSansCO->currentText());
-       
-               if (fontModule->fontsTypewriterCO->currentIndex() == 0)
-                       bp_.fonts_typewriter = "default";
-               else
-                       bp_.fonts_typewriter =
-                               fromqstr(fontModule->fontsTypewriterCO->currentText());
-       } else {
-               bp_.fonts_roman =
-                       tex_fonts_roman[fontModule->fontsRomanCO->currentIndex()];
-       
-               bp_.fonts_sans =
-                       tex_fonts_sans[fontModule->fontsSansCO->currentIndex()];
-       
-               bp_.fonts_typewriter =
-                       tex_fonts_monospaced[fontModule->fontsTypewriterCO->currentIndex()];
-       }
+       bp_.fonts_roman =
+               fromqstr(fontModule->fontsRomanCO->
+                       itemData(fontModule->fontsRomanCO->currentIndex()).toString());
+
+       bp_.fonts_sans =
+               fromqstr(fontModule->fontsSansCO->
+                       itemData(fontModule->fontsSansCO->currentIndex()).toString());
+
+       bp_.fonts_typewriter =
+               fromqstr(fontModule->fontsTypewriterCO->
+                       itemData(fontModule->fontsTypewriterCO->currentIndex()).toString());
 
        if (fontModule->fontencCO->currentIndex() == 0)
                bp_.fontenc = "global";
@@ -2610,12 +2639,12 @@ void GuiDocument::paramsToDialog()
        if (bpos != -1) {
                biblioModule->bibtexCO->setCurrentIndex(bpos);
                biblioModule->bibtexOptionsED->setText(toqstr(options).trimmed());
-               biblioModule->bibtexOptionsLA->setText(qt_("&Options:"));
        } else {
+               // We reset to default if we do not know the specified compiler
+               // This is for security reasons
                biblioModule->bibtexCO->setCurrentIndex(
-                       biblioModule->bibtexCO->findData(toqstr("custom")));
-               biblioModule->bibtexOptionsED->setText(toqstr(bp_.bibtex_command));
-               biblioModule->bibtexOptionsLA->setText(qt_("&Command:"));
+                       biblioModule->bibtexCO->findData(toqstr("default")));
+               biblioModule->bibtexOptionsED->clear();
        }
        biblioModule->bibtexOptionsED->setEnabled(
                biblioModule->bibtexCO->currentIndex() != 0);
@@ -2741,6 +2770,11 @@ void GuiDocument::paramsToDialog()
        mathsModule->mathdotsautoCB->setChecked(
                bp_.use_mathdots == BufferParams::package_auto);
 
+       mathsModule->undertildeCB->setChecked(
+               bp_.use_undertilde == BufferParams::package_on);
+       mathsModule->undertildeautoCB->setChecked(
+               bp_.use_undertilde == BufferParams::package_auto);
+
        switch (bp_.spacing().getSpace()) {
                case Spacing::Other: nitem = 3; break;
                case Spacing::Double: nitem = 2; break;
@@ -2881,9 +2915,12 @@ void GuiDocument::paramsToDialog()
        if (index == -1)
                index = 0;
        outputModule->defaultFormatCO->setCurrentIndex(index);
-       fontModule->osFontsCB->setEnabled(bp_.baseClass()->outputType() == lyx::LATEX);
+       bool const os_fonts_available =
+               bp_.baseClass()->outputType() == lyx::LATEX
+               && LaTeXFeatures::isAvailable("fontspec");
+       fontModule->osFontsCB->setEnabled(os_fonts_available);
        fontModule->osFontsCB->setChecked(
-               bp_.baseClass()->outputType() == lyx::LATEX && bp_.useNonTeXFonts);
+               os_fonts_available && bp_.useNonTeXFonts);
 
        outputModule->outputsyncCB->setChecked(bp_.output_sync);
        outputModule->synccustomCB->setEditText(toqstr(bp_.output_sync_macro));
@@ -2896,51 +2933,41 @@ void GuiDocument::paramsToDialog()
        updateFontsize(documentClass().opt_fontsize(),
                        bp_.fontsize);
 
-       if (bp_.useNonTeXFonts) {
+       QString font = toqstr(bp_.fonts_roman);
+       int rpos = fontModule->fontsRomanCO->findData(font);
+       if (rpos == -1) {
+               rpos = fontModule->fontsRomanCO->count();
+               fontModule->fontsRomanCO->addItem(font + qt_(" (not installed)"), font);
+       }
+       fontModule->fontsRomanCO->setCurrentIndex(rpos);
+
+       font = toqstr(bp_.fonts_sans);
+       int spos = fontModule->fontsSansCO->findData(font);
+       if (spos == -1) {
+               spos = fontModule->fontsSansCO->count();
+               fontModule->fontsSansCO->addItem(font + qt_(" (not installed)"), font);
+       }
+       fontModule->fontsSansCO->setCurrentIndex(spos);
+
+       font = toqstr(bp_.fonts_typewriter);
+       int tpos = fontModule->fontsTypewriterCO->findData(font);
+       if (tpos == -1) {
+               tpos = fontModule->fontsTypewriterCO->count();
+               fontModule->fontsTypewriterCO->addItem(font + qt_(" (not installed)"), font);
+       }
+       fontModule->fontsTypewriterCO->setCurrentIndex(tpos);
+
+       if (bp_.useNonTeXFonts && os_fonts_available) {
                fontModule->fontencLA->setEnabled(false);
                fontModule->fontencCO->setEnabled(false);
                fontModule->fontencLE->setEnabled(false);
-               for (int i = 0; i < fontModule->fontsRomanCO->count(); ++i) {
-                       if (fontModule->fontsRomanCO->itemText(i) == toqstr(bp_.fonts_roman)) {
-                               fontModule->fontsRomanCO->setCurrentIndex(i);
-                               break;
-                       }
-               }
-               
-               for (int i = 0; i < fontModule->fontsSansCO->count(); ++i) {
-                       if (fontModule->fontsSansCO->itemText(i) == toqstr(bp_.fonts_sans)) {
-                               fontModule->fontsSansCO->setCurrentIndex(i);
-                               break;
-                       }
-               }
-               for (int i = 0; i < fontModule->fontsTypewriterCO->count(); ++i) {
-                       if (fontModule->fontsTypewriterCO->itemText(i) == 
-                               toqstr(bp_.fonts_typewriter)) {
-                               fontModule->fontsTypewriterCO->setCurrentIndex(i);
-                               break;
-                       }
-               }
        } else {
                fontModule->fontencLA->setEnabled(true);
                fontModule->fontencCO->setEnabled(true);
                fontModule->fontencLE->setEnabled(true);
-               int n = findToken(tex_fonts_roman, bp_.fonts_roman);
-               if (n >= 0) {
-                       fontModule->fontsRomanCO->setCurrentIndex(n);
-                       romanChanged(n);
-               }
-       
-               n = findToken(tex_fonts_sans, bp_.fonts_sans);
-               if (n >= 0) {
-                       fontModule->fontsSansCO->setCurrentIndex(n);
-                       sansChanged(n);
-               }
-       
-               n = findToken(tex_fonts_monospaced, bp_.fonts_typewriter);
-               if (n >= 0) {
-                       fontModule->fontsTypewriterCO->setCurrentIndex(n);
-                       ttChanged(n);
-               }
+               romanChanged(rpos);
+               sansChanged(spos);
+               ttChanged(tpos);
        }
 
        if (!bp_.fonts_cjk.empty())