Fix Unicode use in Format's prettyname

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.
This commit is contained in:
Guillaume Munch 2016-08-28 01:27:37 +01:00
parent efbec20320
commit cb0a4c6639
10 changed files with 96 additions and 127 deletions

View File

@ -106,16 +106,18 @@ private:
bool Format::formatSorter(Format const * lhs, Format const * rhs) 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) 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 & s, string const & v, string const & ed,
string const & m, int flags) string const & m, int flags)
: name_(n), prettyname_(p), shortcut_(s), viewer_(v), : 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) void Formats::add(string const & name)
{ {
if (!getFormat(name)) if (!getFormat(name))
add(name, name, name, string(), string(), string(), add(name, name, from_utf8(name), string(), string(), string(),
string(), Format::document); string(), Format::document);
} }
void Formats::add(string const & name, string const & extensions, 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 & viewer, string const & editor,
string const & mime, int flags) string const & mime, int flags)
{ {
@ -803,7 +805,7 @@ docstring const Formats::prettyName(string const & name) const
{ {
Format const * format = getFormat(name); Format const * format = getFormat(name);
if (format) if (format)
return from_utf8(format->prettyname()); return format->prettyname();
else else
return from_utf8(name); return from_utf8(name);
} }

View File

@ -42,7 +42,7 @@ public:
zipped_native = 8 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 & s, std::string const & v, std::string const & ed,
std::string const & m, int); std::string const & m, int);
/// ///
@ -68,9 +68,9 @@ public:
/// ///
void setExtensions(std::string const & v); 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_; } std::string const shortcut() const { return shortcut_; }
/// ///
@ -106,7 +106,7 @@ private:
/// Filename extensions, the first one being the default /// Filename extensions, the first one being the default
std::vector<std::string> extension_list_; std::vector<std::string> extension_list_;
/// Name presented to the user. Needs to be unique. /// Name presented to the user. Needs to be unique.
trivstring prettyname_; trivdocstring prettyname_;
/// Keyboard shortcut for the View and Export menu. /// Keyboard shortcut for the View and Export menu.
trivstring shortcut_; trivstring shortcut_;
/*! /*!
@ -176,7 +176,7 @@ public:
void add(std::string const & name); void add(std::string const & name);
/// ///
void add(std::string const & name, std::string const & extensions, 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 & viewer, std::string const & editor,
std::string const & mime, int flags); std::string const & mime, int flags);
/// ///

View File

@ -987,11 +987,12 @@ LyXRC::ReturnValues LyXRC::read(Lexer & lexrc, bool check_format)
} }
case RC_FILEFORMAT: { case RC_FILEFORMAT: {
bool ok = true; bool ok = true;
string format, extensions, prettyname, shortcut; string format, extensions, shortcut;
docstring prettyname;
if (!(lexrc >> format >> extensions)) if (!(lexrc >> format >> extensions))
ok = false; ok = false;
if (ok && lexrc.next(true)) if (ok && lexrc.next(true))
prettyname = lexrc.getString(); prettyname = lexrc.getDocString();
else else
ok = false; ok = false;
if (ok) if (ok)
@ -2524,7 +2525,7 @@ void LyXRC::write(ostream & os, bool ignore_system_lyxrc, string const & name) c
format->mime() != cit->mime()) { format->mime() != cit->mime()) {
os << "\\format \"" << cit->name() << "\" \"" os << "\\format \"" << cit->name() << "\" \""
<< cit->extensions() << "\" \"" << cit->extensions() << "\" \""
<< cit->prettyname() << "\" \"" << to_utf8(cit->prettyname()) << "\" \""
<< cit->shortcut() << "\" \"" << cit->shortcut() << "\" \""
<< escapeCommand(cit->viewer()) << "\" \"" << escapeCommand(cit->viewer()) << "\" \""
<< escapeCommand(cit->editor()) << "\" \""; << escapeCommand(cit->editor()) << "\" \"";

View File

@ -2553,14 +2553,12 @@ void GuiDocument::updateDefaultFormat()
outputModule->defaultFormatCO->clear(); outputModule->defaultFormatCO->clear();
outputModule->defaultFormatCO->addItem(qt_("Default"), outputModule->defaultFormatCO->addItem(qt_("Default"),
QVariant(QString("default"))); QVariant(QString("default")));
typedef vector<Format const *> Formats; vector<Format const *> formats = param_copy.exportableFormats(true);
Formats formats = param_copy.exportableFormats(true);
sort(formats.begin(), formats.end(), Format::formatSorter); sort(formats.begin(), formats.end(), Format::formatSorter);
Formats::const_iterator cit = formats.begin(); for (Format const * f : formats)
Formats::const_iterator end = formats.end(); outputModule->defaultFormatCO->addItem
for (; cit != end; ++cit) (toqstr(translateIfPossible(f->prettyname())),
outputModule->defaultFormatCO->addItem(qt_((*cit)->prettyname()), QVariant(toqstr(f->name())));
QVariant(toqstr((*cit)->name())));
outputModule->defaultFormatCO->blockSignals(false); outputModule->defaultFormatCO->blockSignals(false);
} }

View File

@ -1636,20 +1636,22 @@ void PrefConverters::updateRC(LyXRC const & rc)
void PrefConverters::updateGui() void PrefConverters::updateGui()
{ {
QString const pattern("%1 -> %2");
form_->formats().sort(); form_->formats().sort();
form_->converters().update(form_->formats()); form_->converters().update(form_->formats());
// save current selection // save current selection
QString current = converterFromCO->currentText() QString current =
+ " -> " + converterToCO->currentText(); pattern
.arg(converterFromCO->currentText())
.arg(converterToCO->currentText());
converterFromCO->clear(); converterFromCO->clear();
converterToCO->clear(); converterToCO->clear();
Formats::const_iterator cit = form_->formats().begin(); for (Format const & f : form_->formats()) {
Formats::const_iterator end = form_->formats().end(); QString const name = toqstr(translateIfPossible(f.prettyname()));
for (; cit != end; ++cit) { converterFromCO->addItem(name);
converterFromCO->addItem(qt_(cit->prettyname())); converterToCO->addItem(name);
converterToCO->addItem(qt_(cit->prettyname()));
} }
// currentRowChanged(int) is also triggered when updating the listwidget // currentRowChanged(int) is also triggered when updating the listwidget
@ -1657,19 +1659,20 @@ void PrefConverters::updateGui()
convertersLW->blockSignals(true); convertersLW->blockSignals(true);
convertersLW->clear(); convertersLW->clear();
Converters::const_iterator ccit = form_->converters().begin(); for (Converter const & c : form_->converters()) {
Converters::const_iterator cend = form_->converters().end();
for (; ccit != cend; ++ccit) {
QString const name = QString const name =
qt_(ccit->From()->prettyname()) + " -> " + qt_(ccit->To()->prettyname()); pattern
int type = form_->converters().getNumber(ccit->From()->name(), ccit->To()->name()); .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); new QListWidgetItem(name, convertersLW, type);
} }
convertersLW->sortItems(Qt::AscendingOrder); convertersLW->sortItems(Qt::AscendingOrder);
convertersLW->blockSignals(false); convertersLW->blockSignals(false);
// restore selection // restore selection
if (!current.isEmpty()) { if (current != pattern.arg(QString()).arg(QString())) {
QList<QListWidgetItem *> const item = QList<QListWidgetItem *> const item =
convertersLW->findItems(current, Qt::MatchExactly); convertersLW->findItems(current, Qt::MatchExactly);
if (!item.isEmpty()) if (!item.isEmpty())
@ -1884,7 +1887,7 @@ public:
private: private:
QString toString(Format const & format) const QString toString(Format const & format) const
{ {
return qt_(format.prettyname()); return toqstr(translateIfPossible(format.prettyname()));
} }
}; };
@ -1931,13 +1934,13 @@ PrefFileformats::PrefFileformats(GuiPreferences * form)
namespace { namespace {
string const l10n_shortcut(string const & prettyname, string const & shortcut) string const l10n_shortcut(docstring const & prettyname, string const & shortcut)
{ {
if (shortcut.empty()) if (shortcut.empty())
return string(); return string();
string l10n_format = string l10n_format =
to_utf8(_(prettyname + '|' + shortcut)); to_utf8(_(to_utf8(prettyname) + '|' + shortcut));
return split(l10n_format, '|'); return split(l10n_format, '|');
} }
@ -1986,25 +1989,24 @@ void PrefFileformats::updateView()
defaultFormatCB->clear(); defaultFormatCB->clear();
defaultOTFFormatCB->clear(); defaultOTFFormatCB->clear();
form_->formats().sort(); form_->formats().sort();
Formats::const_iterator cit = form_->formats().begin(); for (Format const & f : formats) {
Formats::const_iterator end = form_->formats().end(); QString const prettyname = toqstr(translateIfPossible(f.prettyname()));
for (; cit != end; ++cit) { formatsCB->addItem(prettyname,
formatsCB->addItem(qt_(cit->prettyname()), QVariant(form_->formats().getNumber(f.name())));
QVariant(form_->formats().getNumber(cit->name()))); if (f.viewer().empty())
if (cit->viewer().empty())
continue; continue;
if (form_->converters().isReachable("xhtml", cit->name()) if (form_->converters().isReachable("xhtml", f.name())
|| form_->converters().isReachable("dviluatex", cit->name()) || form_->converters().isReachable("dviluatex", f.name())
|| form_->converters().isReachable("luatex", cit->name()) || form_->converters().isReachable("luatex", f.name())
|| form_->converters().isReachable("xetex", cit->name())) { || form_->converters().isReachable("xetex", f.name())) {
defaultFormatCB->addItem(qt_(cit->prettyname()), defaultFormatCB->addItem(prettyname,
QVariant(toqstr(cit->name()))); QVariant(toqstr(f.name())));
defaultOTFFormatCB->addItem(qt_(cit->prettyname()), defaultOTFFormatCB->addItem(prettyname,
QVariant(toqstr(cit->name()))); QVariant(toqstr(f.name())));
} else if (form_->converters().isReachable("latex", cit->name()) } else if (form_->converters().isReachable("latex", f.name())
|| form_->converters().isReachable("pdflatex", cit->name())) || form_->converters().isReachable("pdflatex", f.name()))
defaultFormatCB->addItem(qt_(cit->prettyname()), defaultFormatCB->addItem(prettyname,
QVariant(toqstr(cit->name()))); QVariant(toqstr(f.name())));
} }
// restore selections // restore selections
@ -2146,10 +2148,10 @@ void PrefFileformats::on_formatsCB_editTextChanged(const QString &)
void PrefFileformats::updatePrettyname() void PrefFileformats::updatePrettyname()
{ {
QString const newname = formatsCB->currentText(); QString const newname = formatsCB->currentText();
if (newname == qt_(currentFormat().prettyname())) if (newname == toqstr(translateIfPossible(currentFormat().prettyname())))
return; return;
currentFormat().setPrettyname(fromqstr(newname)); currentFormat().setPrettyname(qstring_to_ucs4(newname));
formatsChanged(); formatsChanged();
updateView(); updateView();
changed(); changed();
@ -2251,7 +2253,7 @@ Format & PrefFileformats::currentFormat()
void PrefFileformats::on_formatNewPB_clicked() void PrefFileformats::on_formatNewPB_clicked()
{ {
form_->formats().add("", "", "", "", "", "", "", Format::none); form_->formats().add("", "", docstring(), "", "", "", "", Format::none);
updateView(); updateView();
formatsCB->setCurrentIndex(0); formatsCB->setCurrentIndex(0);
formatsCB->setFocus(Qt::OtherFocusReason); formatsCB->setFocus(Qt::OtherFocusReason);

View File

@ -73,42 +73,20 @@ void GuiSendTo::updateContents()
{ {
all_formats_ = buffer().params().exportableFormats(false); all_formats_ = buffer().params().exportableFormats(false);
sort(all_formats_.begin(), all_formats_.end(), Format::formatSorter); sort(all_formats_.begin(), all_formats_.end(), Format::formatSorter);
// Save the current selection if any // Save the current selection if any
Format const * current_format = 0; Format const * current_format = nullptr;
int const line = formatLW->currentRow(); 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) && formatLW->selectedItems().size() > 0)
current_format = all_formats_[line]; current_format = all_formats_[line];
// Reset the list widget
// 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
formatLW->clear(); formatLW->clear();
for (Format const * f : all_formats_) {
for (vector<string>::const_iterator it = keys.begin(); formatLW->addItem(toqstr(translateIfPossible(f->prettyname())));
it != keys.end(); ++it) { // Restore the selection
formatLW->addItem(qt_(*it)); 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));
} }

View File

@ -2583,32 +2583,25 @@ bool GuiView::exportBufferAs(Buffer & b, docstring const & iformat)
QStringList types; QStringList types;
QString const anyformat = qt_("Guess from extension (*.*)"); QString const anyformat = qt_("Guess from extension (*.*)");
types << anyformat; types << anyformat;
Formats::const_iterator it = formats.begin();
vector<Format const *> export_formats; vector<Format const *> export_formats;
for (; it != formats.end(); ++it) for (Format const & f : formats)
if (it->documentFormat()) if (f.documentFormat())
export_formats.push_back(&(*it)); export_formats.push_back(&f);
sort(export_formats.begin(), export_formats.end(), sort(export_formats.begin(), export_formats.end(), Format::formatSorter);
[](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();
map<QString, string> fmap; map<QString, string> fmap;
QString filter; QString filter;
string ext; string ext;
for (; fit != export_formats.end(); ++fit) { for (Format const * f : export_formats) {
docstring const loc_prettyname = docstring const loc_prettyname = translateIfPossible(f->prettyname());
translateIfPossible(from_utf8((*fit)->prettyname()));
QString const loc_filter = toqstr(bformat(from_ascii("%1$s (*.%2$s)"), QString const loc_filter = toqstr(bformat(from_ascii("%1$s (*.%2$s)"),
loc_prettyname, loc_prettyname,
from_ascii((*fit)->extension()))); from_ascii(f->extension())));
types << loc_filter; types << loc_filter;
fmap[loc_filter] = (*fit)->name(); fmap[loc_filter] = f->name();
if (from_ascii((*fit)->name()) == iformat) { if (from_ascii(f->name()) == iformat) {
filter = loc_filter; filter = loc_filter;
ext = (*fit)->extension(); ext = f->extension();
} }
} }
string ofname = fname.onlyFileName(); string ofname = fname.onlyFileName();

View File

@ -340,7 +340,7 @@ void ViewSourceWidget::updateDefaultFormat()
continue; continue;
} }
QString const pretty = qt_(fmt->prettyname()); QString const pretty = toqstr(translateIfPossible(fmt->prettyname()));
QString const qformat = toqstr(format); QString const qformat = toqstr(format);
outputFormatCO->addItem(pretty, QVariant(qformat)); outputFormatCO->addItem(pretty, QVariant(qformat));
if (qformat == view_format_) if (qformat == view_format_)

View File

@ -1072,14 +1072,12 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
MenuItem item(MenuItem::Submenu, smenue); MenuItem item(MenuItem::Submenu, smenue);
item.setSubmenu(MenuDefinition(smenue)); item.setSubmenu(MenuDefinition(smenue));
Formats::const_iterator fit = formats.begin(); for (Format const * f : formats) {
Formats::const_iterator end = formats.end(); if (f->dummy())
for (; fit != end ; ++fit) {
if ((*fit)->dummy())
continue; continue;
docstring lab = from_utf8((*fit)->prettyname()); docstring lab = f->prettyname();
docstring const scut = from_utf8((*fit)->shortcut()); docstring const scut = from_utf8(f->shortcut());
docstring const tmplab = lab; docstring const tmplab = lab;
if (!scut.empty()) if (!scut.empty())
@ -1096,7 +1094,7 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
break; break;
case MenuItem::ViewFormats: case MenuItem::ViewFormats:
case MenuItem::UpdateFormats: case MenuItem::UpdateFormats:
if ((*fit)->name() == buf->params().getDefaultOutputFormat()) { if (f->name() == buf->params().getDefaultOutputFormat()) {
docstring lbl = (kind == MenuItem::ViewFormats docstring lbl = (kind == MenuItem::ViewFormats
? bformat(_("View [%1$s]|V"), label) ? bformat(_("View [%1$s]|V"), label)
: bformat(_("Update [%1$s]|U"), label)); : bformat(_("Update [%1$s]|U"), label));
@ -1105,7 +1103,7 @@ void MenuDefinition::expandFormats(MenuItem::Kind const kind, Buffer const * buf
} }
// fall through // fall through
case MenuItem::ExportFormats: case MenuItem::ExportFormats:
if (!(*fit)->inExportMenu()) if (!f->inExportMenu())
continue; continue;
break; break;
default: 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 // note that at this point, we know that buf is not null
LATTEST(buf); LATTEST(buf);
item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command, item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command,
toqstr(label), FuncRequest(action, (*fit)->name()))); toqstr(label), FuncRequest(action, f->name())));
} else { } else {
if (buf) if (buf)
addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label), addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
FuncRequest(action, (*fit)->name()))); FuncRequest(action, f->name())));
else else
add(MenuItem(MenuItem::Command, toqstr(label), add(MenuItem(MenuItem::Command, toqstr(label),
FuncRequest(action, (*fit)->name()))); FuncRequest(action, f->name())));
} }
} }
if (view_update) if (view_update)

View File

@ -194,19 +194,16 @@ ToolbarInfo & ToolbarInfo::read(Lexer & lex)
case TO_VIEWFORMATS: { case TO_VIEWFORMATS: {
vector<Format const *> formats = (code == TO_IMPORTFORMATS) ? vector<Format const *> formats = (code == TO_IMPORTFORMATS) ?
theConverters().importableFormats() : theConverters().importableFormats() :
theConverters().exportableFormats(code != TO_EXPORTFORMATS); theConverters().exportableFormats(true);
sort(formats.begin(), formats.end()); sort(formats.begin(), formats.end());
vector<Format const *>::const_iterator fit = formats.begin(); for (Format const * f : formats) {
vector<Format const *>::const_iterator end = formats.end(); if (f->dummy())
for (; fit != end ; ++fit) {
if ((*fit)->dummy())
continue; continue;
if (code != TO_IMPORTFORMATS && if (code != TO_IMPORTFORMATS &&
!(*fit)->documentFormat()) !f->documentFormat())
continue; continue;
docstring const prettyname = docstring const prettyname = f->prettyname();
from_utf8((*fit)->prettyname());
docstring tooltip; docstring tooltip;
FuncCode lfun = LFUN_NOACTION; FuncCode lfun = LFUN_NOACTION;
switch (code) { switch (code) {
@ -227,7 +224,7 @@ ToolbarInfo & ToolbarInfo::read(Lexer & lex)
tooltip = _("View %1$s"); tooltip = _("View %1$s");
break; break;
} }
FuncRequest func(lfun, (*fit)->name(), FuncRequest func(lfun, f->name(),
FuncRequest::TOOLBAR); FuncRequest::TOOLBAR);
add(ToolbarItem(ToolbarItem::COMMAND, func, add(ToolbarItem(ToolbarItem::COMMAND, func,
bformat(tooltip, prettyname))); bformat(tooltip, prettyname)));