]> git.lyx.org Git - features.git/commitdiff
Fix Unicode use in Format's prettyname
authorGuillaume Munch <gm@lyx.org>
Sun, 28 Aug 2016 00:27:37 +0000 (01:27 +0100)
committerGuillaume Munch <gm@lyx.org>
Sun, 28 Aug 2016 00:30:53 +0000 (01:30 +0100)
The field prettyname can accept Unicode and therefore must be parsed into a
docstring.

Little simplification of the code on the way.

* For other fields, either a validator should be set to prevent non-ascii input
  in the preferences, or they should be transformed into docstring too.

src/Format.cpp
src/Format.h
src/LyXRC.cpp
src/frontends/qt4/GuiDocument.cpp
src/frontends/qt4/GuiPrefs.cpp
src/frontends/qt4/GuiSendto.cpp
src/frontends/qt4/GuiView.cpp
src/frontends/qt4/GuiViewSource.cpp
src/frontends/qt4/Menus.cpp
src/frontends/qt4/Toolbars.cpp

index c94481bb628affcc8902ebdc5a39b4f09b44042c..09fb33bf2827c475c6338ec6fc77f4ec9b304cc4 100644 (file)
@@ -106,16 +106,18 @@ private:
 
 bool Format::formatSorter(Format const * lhs, Format const * rhs)
 {
-       return compare_locale(_(lhs->prettyname()), _(rhs->prettyname())) < 0;
+       return compare_locale(translateIfPossible(lhs->prettyname()),
+                             translateIfPossible(rhs->prettyname())) < 0;
 }
 
 bool operator<(Format const & a, Format const & b)
 {
-       return compare_locale(_(a.prettyname()), _(b.prettyname())) < 0;
+       return compare_locale(translateIfPossible(a.prettyname()),
+                             translateIfPossible(b.prettyname())) < 0;
 }
 
 
-Format::Format(string const & n, string const & e, string const & p,
+Format::Format(string const & n, string const & e, docstring const & p,
               string const & s, string const & v, string const & ed,
               string const & m, int flags)
        : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
@@ -599,13 +601,13 @@ int Formats::getNumber(string const & name) const
 void Formats::add(string const & name)
 {
        if (!getFormat(name))
-               add(name, name, name, string(), string(), string(),
+               add(name, name, from_utf8(name), string(), string(), string(),
                    string(), Format::document);
 }
 
 
 void Formats::add(string const & name, string const & extensions,
-                 string const & prettyname, string const & shortcut,
+                 docstring const & prettyname, string const & shortcut,
                  string const & viewer, string const & editor,
                  string const & mime, int flags)
 {
@@ -803,7 +805,7 @@ docstring const Formats::prettyName(string const & name) const
 {
        Format const * format = getFormat(name);
        if (format)
-               return from_utf8(format->prettyname());
+               return format->prettyname();
        else
                return from_utf8(name);
 }
index 74d2c4ea2651ef4bd7f6f7b44528e19d3845be12..07c081bf71a08b22d45c2bc16c766e2862521f9b 100644 (file)
@@ -42,7 +42,7 @@ public:
                zipped_native = 8
        };
        ///
-       Format(std::string const & n, std::string const & e, std::string const & p,
+       Format(std::string const & n, std::string const & e, docstring const & p,
               std::string const & s, std::string const & v, std::string const & ed,
               std::string const & m, int);
        ///
@@ -68,9 +68,9 @@ public:
        ///
        void setExtensions(std::string const & v);
        ///
-       std::string const prettyname() const { return prettyname_; }
+       docstring const prettyname() const { return prettyname_; }
        ///
-       void setPrettyname(std::string const & v) { prettyname_ = v; }
+       void setPrettyname(docstring const & v) { prettyname_ = v; }
        ///
        std::string const shortcut() const { return shortcut_; }
        ///
@@ -106,7 +106,7 @@ private:
        /// Filename extensions, the first one being the default
        std::vector<std::string> extension_list_;
        /// Name presented to the user. Needs to be unique.
-       trivstring prettyname_;
+       trivdocstring prettyname_;
        /// Keyboard shortcut for the View and Export menu.
        trivstring shortcut_;
        /*!
@@ -176,7 +176,7 @@ public:
        void add(std::string const & name);
        ///
        void add(std::string const & name, std::string const & extensions,
-                std::string const & prettyname, std::string const & shortcut,
+                docstring const & prettyname, std::string const & shortcut,
                 std::string const & viewer, std::string const & editor,
                 std::string const & mime, int flags);
        ///
index c8b69d1ec50b1d49f39124bdc079a261acabfa6c..c3fef830d77e4b98536f885de4e4b1bbbd90983e 100644 (file)
@@ -987,11 +987,12 @@ LyXRC::ReturnValues LyXRC::read(Lexer & lexrc, bool check_format)
                }
                case RC_FILEFORMAT: {
                        bool ok = true;
-                       string format, extensions, prettyname, shortcut;
+                       string format, extensions, shortcut;
+                       docstring prettyname;
                        if (!(lexrc >> format >> extensions))
                                ok = false;
                        if (ok && lexrc.next(true))
-                               prettyname  = lexrc.getString();
+                               prettyname = lexrc.getDocString();
                        else
                                ok = false;
                        if (ok)
@@ -2524,7 +2525,7 @@ void LyXRC::write(ostream & os, bool ignore_system_lyxrc, string const & name) c
                            format->mime() != cit->mime()) {
                                os << "\\format \"" << cit->name() << "\" \""
                                   << cit->extensions() << "\" \""
-                                  << cit->prettyname() << "\" \""
+                                  << to_utf8(cit->prettyname()) << "\" \""
                                   << cit->shortcut() << "\" \""
                                   << escapeCommand(cit->viewer()) << "\" \""
                                   << escapeCommand(cit->editor()) << "\" \"";
index 7d6e865952d26f713cbe1c3fda568084ef32f8a2..c54ded9b5ed7488f29774b37b6bff4fbd4cce834 100644 (file)
@@ -2553,14 +2553,12 @@ void GuiDocument::updateDefaultFormat()
        outputModule->defaultFormatCO->clear();
        outputModule->defaultFormatCO->addItem(qt_("Default"),
                                QVariant(QString("default")));
-       typedef vector<Format const *> Formats;
-       Formats formats = param_copy.exportableFormats(true);
+       vector<Format const *> formats = param_copy.exportableFormats(true);
        sort(formats.begin(), formats.end(), Format::formatSorter);
-       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())));
+       for (Format const * f : formats)
+               outputModule->defaultFormatCO->addItem
+                       (toqstr(translateIfPossible(f->prettyname())),
+                        QVariant(toqstr(f->name())));
        outputModule->defaultFormatCO->blockSignals(false);
 }
 
index 996be4916be1d52e8a4832314141937ead92f3c7..dff16cc83ac633d0eea41c70baee36e6da554a6d 100644 (file)
@@ -1636,20 +1636,22 @@ void PrefConverters::updateRC(LyXRC const & rc)
 
 void PrefConverters::updateGui()
 {
+       QString const pattern("%1 -> %2");
        form_->formats().sort();
        form_->converters().update(form_->formats());
        // save current selection
-       QString current = converterFromCO->currentText()
-               + " -> " + converterToCO->currentText();
+       QString current =
+               pattern
+               .arg(converterFromCO->currentText())
+               .arg(converterToCO->currentText());
 
        converterFromCO->clear();
        converterToCO->clear();
 
-       Formats::const_iterator cit = form_->formats().begin();
-       Formats::const_iterator end = form_->formats().end();
-       for (; cit != end; ++cit) {
-               converterFromCO->addItem(qt_(cit->prettyname()));
-               converterToCO->addItem(qt_(cit->prettyname()));
+       for (Format const & f : form_->formats()) {
+               QString const name = toqstr(translateIfPossible(f.prettyname()));
+               converterFromCO->addItem(name);
+               converterToCO->addItem(name);
        }
 
        // currentRowChanged(int) is also triggered when updating the listwidget
@@ -1657,19 +1659,20 @@ void PrefConverters::updateGui()
        convertersLW->blockSignals(true);
        convertersLW->clear();
 
-       Converters::const_iterator ccit = form_->converters().begin();
-       Converters::const_iterator cend = form_->converters().end();
-       for (; ccit != cend; ++ccit) {
+       for (Converter const & c : form_->converters()) {
                QString const name =
-                       qt_(ccit->From()->prettyname()) + " -> " + qt_(ccit->To()->prettyname());
-               int type = form_->converters().getNumber(ccit->From()->name(), ccit->To()->name());
+                       pattern
+                       .arg(toqstr(translateIfPossible(c.From()->prettyname())))
+                       .arg(toqstr(translateIfPossible(c.To()->prettyname())));
+               int type = form_->converters().getNumber(c.From()->name(),
+                                                        c.To()->name());
                new QListWidgetItem(name, convertersLW, type);
        }
        convertersLW->sortItems(Qt::AscendingOrder);
        convertersLW->blockSignals(false);
 
        // restore selection
-       if (!current.isEmpty()) {
+       if (current != pattern.arg(QString()).arg(QString())) {
                QList<QListWidgetItem *> const item =
                        convertersLW->findItems(current, Qt::MatchExactly);
                if (!item.isEmpty())
@@ -1884,7 +1887,7 @@ public:
 private:
        QString toString(Format const & format) const
        {
-               return qt_(format.prettyname());
+               return toqstr(translateIfPossible(format.prettyname()));
        }
 };
 
@@ -1931,13 +1934,13 @@ PrefFileformats::PrefFileformats(GuiPreferences * form)
 
 namespace {
 
-string const l10n_shortcut(string const & prettyname, string const & shortcut)
+string const l10n_shortcut(docstring const & prettyname, string const & shortcut)
 {
        if (shortcut.empty())
                return string();
 
        string l10n_format =
-               to_utf8(_(prettyname + '|' + shortcut));
+               to_utf8(_(to_utf8(prettyname) + '|' + shortcut));
        return split(l10n_format, '|');
 }
 
@@ -1986,25 +1989,24 @@ void PrefFileformats::updateView()
        defaultFormatCB->clear();
        defaultOTFFormatCB->clear();
        form_->formats().sort();
-       Formats::const_iterator cit = form_->formats().begin();
-       Formats::const_iterator end = form_->formats().end();
-       for (; cit != end; ++cit) {
-               formatsCB->addItem(qt_(cit->prettyname()),
-                               QVariant(form_->formats().getNumber(cit->name())));
-               if (cit->viewer().empty())
+       for (Format const & f : formats) {
+               QString const prettyname = toqstr(translateIfPossible(f.prettyname()));
+               formatsCB->addItem(prettyname,
+                                  QVariant(form_->formats().getNumber(f.name())));
+               if (f.viewer().empty())
                        continue;
-               if (form_->converters().isReachable("xhtml", cit->name())
-                   || form_->converters().isReachable("dviluatex", cit->name())
-                   || form_->converters().isReachable("luatex", cit->name())
-                   || form_->converters().isReachable("xetex", cit->name())) {
-                       defaultFormatCB->addItem(qt_(cit->prettyname()),
-                                       QVariant(toqstr(cit->name())));
-                       defaultOTFFormatCB->addItem(qt_(cit->prettyname()),
-                                       QVariant(toqstr(cit->name())));
-               } else if (form_->converters().isReachable("latex", cit->name())
-                          || form_->converters().isReachable("pdflatex", cit->name()))
-                       defaultFormatCB->addItem(qt_(cit->prettyname()),
-                                       QVariant(toqstr(cit->name())));
+               if (form_->converters().isReachable("xhtml", f.name())
+                   || form_->converters().isReachable("dviluatex", f.name())
+                   || form_->converters().isReachable("luatex", f.name())
+                   || form_->converters().isReachable("xetex", f.name())) {
+                       defaultFormatCB->addItem(prettyname,
+                                       QVariant(toqstr(f.name())));
+                       defaultOTFFormatCB->addItem(prettyname,
+                                       QVariant(toqstr(f.name())));
+               } else if (form_->converters().isReachable("latex", f.name())
+                          || form_->converters().isReachable("pdflatex", f.name()))
+                       defaultFormatCB->addItem(prettyname,
+                                       QVariant(toqstr(f.name())));
        }
 
        // restore selections
@@ -2146,10 +2148,10 @@ void PrefFileformats::on_formatsCB_editTextChanged(const QString &)
 void PrefFileformats::updatePrettyname()
 {
        QString const newname = formatsCB->currentText();
-       if (newname == qt_(currentFormat().prettyname()))
+       if (newname == toqstr(translateIfPossible(currentFormat().prettyname())))
                return;
 
-       currentFormat().setPrettyname(fromqstr(newname));
+       currentFormat().setPrettyname(qstring_to_ucs4(newname));
        formatsChanged();
        updateView();
        changed();
@@ -2251,7 +2253,7 @@ Format & PrefFileformats::currentFormat()
 
 void PrefFileformats::on_formatNewPB_clicked()
 {
-       form_->formats().add("", "", "", "", "", "", "", Format::none);
+       form_->formats().add("", "", docstring(), "", "", "", "", Format::none);
        updateView();
        formatsCB->setCurrentIndex(0);
        formatsCB->setFocus(Qt::OtherFocusReason);
index a04c0c9a460bcf5b0c2125e19ffec736cfcf3180..13ba03d729764d4e21a17d2c8274782acab24718 100644 (file)
@@ -73,42 +73,20 @@ void GuiSendTo::updateContents()
 {
        all_formats_ = buffer().params().exportableFormats(false);
        sort(all_formats_.begin(), all_formats_.end(), Format::formatSorter);
-       
        // Save the current selection if any
-       Format const * current_format = 0;
+       Format const * current_format = nullptr;
        int const line = formatLW->currentRow();
-       if (line >= 0 && line <= formatLW->count()
+       if (line >= 0 && static_cast<unsigned int>(line) < all_formats_.size()
            && formatLW->selectedItems().size() > 0)
                current_format = all_formats_[line];
-
-       // Check whether the current contents of the browser will be
-       // changed by loading the contents of formats
-       vector<string> keys;
-       keys.resize(all_formats_.size());
-
-       vector<string>::iterator result = keys.begin();
-       vector<Format const *>::const_iterator it  = all_formats_.begin();
-       vector<Format const *>::const_iterator end = all_formats_.end();
-
-       int current_line = -1;
-       for (int ln = 0; it != end; ++it, ++result, ++ln) {
-               *result = (*it)->prettyname();
-               if (current_format 
-                   && (*it)->prettyname() == current_format->prettyname())
-                       current_line = ln;
-       }
-
-       // Reload the browser
+       // Reset the list widget
        formatLW->clear();
-
-       for (vector<string>::const_iterator it = keys.begin();
-            it != keys.end(); ++it) {
-               formatLW->addItem(qt_(*it));
+       for (Format const * f : all_formats_) {
+               formatLW->addItem(toqstr(translateIfPossible(f->prettyname())));
+               // Restore the selection
+               if (current_format && f->prettyname() == current_format->prettyname())
+                       formatLW->setCurrentRow(formatLW->count() - 1);
        }
-
-       // Restore the selection
-       if (current_line > -1)
-               formatLW->setCurrentItem(formatLW->item(current_line));
 }
 
 
index 39a72bb7bb6789a45158e5f7da950fb9750fd840..925dfeca336ae3a32c0d6f5b39223222260bd25a 100644 (file)
@@ -2583,32 +2583,25 @@ bool GuiView::exportBufferAs(Buffer & b, docstring const & iformat)
        QStringList types;
        QString const anyformat = qt_("Guess from extension (*.*)");
        types << anyformat;
-       Formats::const_iterator it = formats.begin();
+
        vector<Format const *> export_formats;
-       for (; it != formats.end(); ++it)
-               if (it->documentFormat())
-                       export_formats.push_back(&(*it));
-       sort(export_formats.begin(), export_formats.end(),
-            [](Format const *first, Format const *second) {
-                    QString name1 = qt_(first->prettyname());
-                    QString name2 = qt_(second->prettyname());
-                    return 0 < name2.localeAwareCompare(name1);
-            });
-       vector<Format const *>::const_iterator fit = export_formats.begin();
+       for (Format const & f : formats)
+               if (f.documentFormat())
+                       export_formats.push_back(&f);
+       sort(export_formats.begin(), export_formats.end(), Format::formatSorter);
        map<QString, string> fmap;
        QString filter;
        string ext;
-       for (; fit != export_formats.end(); ++fit) {
-               docstring const loc_prettyname =
-                       translateIfPossible(from_utf8((*fit)->prettyname()));
+       for (Format const * f : export_formats) {
+               docstring const loc_prettyname = translateIfPossible(f->prettyname());
                QString const loc_filter = toqstr(bformat(from_ascii("%1$s (*.%2$s)"),
                                                     loc_prettyname,
-                                                    from_ascii((*fit)->extension())));
+                                                    from_ascii(f->extension())));
                types << loc_filter;
-               fmap[loc_filter] = (*fit)->name();
-               if (from_ascii((*fit)->name()) == iformat) {
+               fmap[loc_filter] = f->name();
+               if (from_ascii(f->name()) == iformat) {
                        filter = loc_filter;
-                       ext = (*fit)->extension();
+                       ext = f->extension();
                }
        }
        string ofname = fname.onlyFileName();
index 4be43e259e640bde856ad32111de113cc46ec4bc..d347e1a173e94dba4251519bd1e87a4902011819 100644 (file)
@@ -340,7 +340,7 @@ void ViewSourceWidget::updateDefaultFormat()
                        continue;
                } 
 
-               QString const pretty = qt_(fmt->prettyname());
+               QString const pretty = toqstr(translateIfPossible(fmt->prettyname()));
                QString const qformat = toqstr(format);
                outputFormatCO->addItem(pretty, QVariant(qformat));
                if (qformat == view_format_)
index 8fd61592ad044d8a3a39de678d0388741b49fdd2..dce5c8cc0d283d42e8a05320e69376ff1b46aa22 100644 (file)
@@ -1072,14 +1072,12 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
        MenuItem item(MenuItem::Submenu, smenue);
        item.setSubmenu(MenuDefinition(smenue));
 
-       Formats::const_iterator fit = formats.begin();
-       Formats::const_iterator end = formats.end();
-       for (; fit != end ; ++fit) {
-               if ((*fit)->dummy())
+       for (Format const * f : formats) {
+               if (f->dummy())
                        continue;
 
-               docstring lab = from_utf8((*fit)->prettyname());
-               docstring const scut = from_utf8((*fit)->shortcut());
+               docstring lab = f->prettyname();
+               docstring const scut = from_utf8(f->shortcut());
                docstring const tmplab = lab;
 
                if (!scut.empty())
@@ -1096,7 +1094,7 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
                        break;
                case MenuItem::ViewFormats:
                case MenuItem::UpdateFormats:
-                       if ((*fit)->name() == buf->params().getDefaultOutputFormat()) {
+                       if (f->name() == buf->params().getDefaultOutputFormat()) {
                                docstring lbl = (kind == MenuItem::ViewFormats
                                        ? bformat(_("View [%1$s]|V"), label)
                                        : bformat(_("Update [%1$s]|U"), label));
@@ -1105,7 +1103,7 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
                        }
                // fall through
                case MenuItem::ExportFormats:
-                       if (!(*fit)->inExportMenu())
+                       if (!f->inExportMenu())
                                continue;
                        break;
                default:
@@ -1120,14 +1118,14 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
                        // note that at this point, we know that buf is not null
                        LATTEST(buf);
                        item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command,
-                               toqstr(label), FuncRequest(action, (*fit)->name())));
+                               toqstr(label), FuncRequest(action, f->name())));
                } else {
                        if (buf)
                                addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
-                                       FuncRequest(action, (*fit)->name())));
+                                       FuncRequest(action, f->name())));
                        else
                                add(MenuItem(MenuItem::Command, toqstr(label),
-                                       FuncRequest(action, (*fit)->name())));
+                                       FuncRequest(action, f->name())));
                }
        }
        if (view_update)
index 19b956090eb5d080ba7f7f0149ee98365e11d40b..a9f90d4a5a6ea77d79f031d423502c1219e0b28c 100644 (file)
@@ -194,19 +194,16 @@ ToolbarInfo & ToolbarInfo::read(Lexer & lex)
                case TO_VIEWFORMATS: {
                        vector<Format const *> formats = (code == TO_IMPORTFORMATS) ?
                                theConverters().importableFormats() :
-                               theConverters().exportableFormats(code != TO_EXPORTFORMATS);
+                               theConverters().exportableFormats(true);
                        sort(formats.begin(), formats.end());
-                       vector<Format const *>::const_iterator fit = formats.begin();
-                       vector<Format const *>::const_iterator end = formats.end();
-                       for (; fit != end ; ++fit) {
-                               if ((*fit)->dummy())
+                       for (Format const * f : formats) {
+                               if (f->dummy())
                                        continue;
                                if (code != TO_IMPORTFORMATS &&
-                                   !(*fit)->documentFormat())
+                                   !f->documentFormat())
                                        continue;
 
-                               docstring const prettyname =
-                                       from_utf8((*fit)->prettyname());
+                               docstring const prettyname = f->prettyname();
                                docstring tooltip;
                                FuncCode lfun = LFUN_NOACTION;
                                switch (code) {
@@ -227,7 +224,7 @@ ToolbarInfo & ToolbarInfo::read(Lexer & lex)
                                        tooltip = _("View %1$s");
                                        break;
                                }
-                               FuncRequest func(lfun, (*fit)->name(),
+                               FuncRequest func(lfun, f->name(),
                                                FuncRequest::TOOLBAR);
                                add(ToolbarItem(ToolbarItem::COMMAND, func,
                                                bformat(tooltip, prettyname)));