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)
{
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);
}

View 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);
///

View 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()) << "\" \"";

View 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);
}

View 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);

View 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_line > -1)
formatLW->setCurrentItem(formatLW->item(current_line));
if (current_format && f->prettyname() == current_format->prettyname())
formatLW->setCurrentRow(formatLW->count() - 1);
}
}

View 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();

View 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_)

View 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)

View 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)));