From b52cb87e9a86c43869f119bae5482c6e27f674fb Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Tue, 19 Mar 2019 17:55:19 +0100 Subject: [PATCH] GuiLyXFiles: add language selector. --- src/frontends/qt4/GuiLyXFiles.cpp | 103 +++++++++++++++++------------ src/frontends/qt4/GuiLyXFiles.h | 3 + src/frontends/qt4/ui/LyXFilesUi.ui | 24 ++++++- 3 files changed, 86 insertions(+), 44 deletions(-) diff --git a/src/frontends/qt4/GuiLyXFiles.cpp b/src/frontends/qt4/GuiLyXFiles.cpp index 6d380bc708..c5d5c050f0 100644 --- a/src/frontends/qt4/GuiLyXFiles.cpp +++ b/src/frontends/qt4/GuiLyXFiles.cpp @@ -51,53 +51,25 @@ void GuiLyXFiles::getFiles(QMap & in, QString const type) // We also consider i18n subdirectories and prefer them. QStringList dirs; QStringList relpaths; - QStringList langcodes; // The three locations to look at. string const user = addPath(package().user_support().absFileName(), fromqstr(type)); string const build = addPath(package().build_support().absFileName(), fromqstr(type)); string const system = addPath(package().system_support().absFileName(), fromqstr(type)); - // If the LANGUAGE variable is set, use it as a fallback for searching for files. - string lang = getGuiMessages().language(); - string const language = getEnv("LANGUAGE"); - if (!language.empty()) - lang += ":" + language; - - // Get all supported languages (by code) in order to exclude those - // dirs later. - QAbstractItemModel * language_model = guiApp->languageModel(); - for (int i = 0; i != language_model->rowCount(); ++i) { - QModelIndex index = language_model->index(i, 0); - Language const * lang = - languages.getLanguage(fromqstr(index.data(Qt::UserRole).toString())); - if (!lang) - continue; - string const code = lang->code(); - langcodes << toqstr(code); - // Also store code without country code - string const shortcode = token(code, '_', 0); - if (shortcode != code) - langcodes << toqstr(shortcode); - } - - for (auto const & l : getVectorFromString(lang, ":")) { - FileName tmp; - // First try with the full name - // `en' files are not in a subdirectory - if (l == "en") - break; - else { - dirs << toqstr(addPath(user, l)); - dirs << toqstr(addPath(build, l)); - dirs << toqstr(addPath(system, l)); - } + // First, query the current language subdir (except for English) + QString const lang = languageCO->currentData().toString(); + if (!lang.startsWith("en")) { + // First try with the full code + dirs << toqstr(addPath(user, fromqstr(lang))); + dirs << toqstr(addPath(build, fromqstr(lang))); + dirs << toqstr(addPath(system, fromqstr(lang))); // Then the name without country code - string const shortl = token(l, '_', 0); - if (shortl != l) { - dirs << toqstr(addPath(user, shortl)); - dirs << toqstr(addPath(build, shortl)); - dirs << toqstr(addPath(system, shortl)); + QString const shortl = lang.left(lang.indexOf('_')); + if (shortl != lang) { + dirs << toqstr(addPath(user, fromqstr(shortl))); + dirs << toqstr(addPath(build, fromqstr(shortl))); + dirs << toqstr(addPath(system, fromqstr(shortl))); } } @@ -124,8 +96,8 @@ void GuiLyXFiles::getFiles(QMap & in, QString const type) int sc = relpath.indexOf('/', s + 1); QString const subcat = (sc == -1) ? QString() : relpath.mid(s + 1, sc - s - 1); - if (langcodes.contains(cat) - && !langcodes.contains(dir.right(dir.lastIndexOf('/')))) + if (langcodes_.contains(cat) + && !langcodes_.contains(dir.right(dir.lastIndexOf('/')))) // Skip i18n dir continue; if (!subcat.isEmpty()) @@ -145,6 +117,47 @@ GuiLyXFiles::GuiLyXFiles(GuiView & lv) { setupUi(this); + // Get all supported languages (by code) in order to exclude those + // dirs later. + QAbstractItemModel * language_model = guiApp->languageModel(); + language_model->sort(0); + for (int i = 0; i != language_model->rowCount(); ++i) { + QModelIndex index = language_model->index(i, 0); + Language const * lang = + languages.getLanguage(fromqstr(index.data(Qt::UserRole).toString())); + if (!lang) + continue; + string const code = lang->code(); + languageCO->addItem(qt_(lang->display()), toqstr(code)); + langcodes_ << toqstr(code); + // Also store code without country code + string const shortcode = token(code, '_', 0); + if (shortcode != code) + langcodes_ << toqstr(shortcode); + } + // Preset to GUI language + string lang = getGuiMessages().language(); + string const language = getEnv("LANGUAGE"); + if (!language.empty()) + lang += ":" + language; + + for (auto const & l : getVectorFromString(lang, ":")) { + // First try with the full name + // `en' files are not in a subdirectory + int i = languageCO->findData(toqstr(l)); + if (i != -1) { + languageCO->setCurrentIndex(i); + break; + } + // Then the name without country code + string const shortl = token(l, '_', 0); + i = languageCO->findData(toqstr(l)); + if (i != -1) { + languageCO->setCurrentIndex(i); + break; + } + } + // The filter bar filter_ = new FancyLineEdit(this); filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png")); @@ -214,6 +227,12 @@ void GuiLyXFiles::on_fileTypeCO_activated(int) } +void GuiLyXFiles::on_languageCO_activated(int) +{ + updateContents(); +} + + void GuiLyXFiles::on_filesLW_itemDoubleClicked(QTreeWidgetItem *, int) { applyView(); diff --git a/src/frontends/qt4/GuiLyXFiles.h b/src/frontends/qt4/GuiLyXFiles.h index 45d611598e..e0e4219733 100644 --- a/src/frontends/qt4/GuiLyXFiles.h +++ b/src/frontends/qt4/GuiLyXFiles.h @@ -34,6 +34,7 @@ public: private Q_SLOTS: void changed_adaptor(); void on_fileTypeCO_activated(int); + void on_languageCO_activated(int); void on_filesLW_itemDoubleClicked(QTreeWidgetItem *, int); void on_browsePB_pressed(); void slotButtonBox(QAbstractButton *); @@ -74,6 +75,8 @@ private: QString type_; /// QString file_; + /// + QStringList langcodes_; }; } // namespace frontend diff --git a/src/frontends/qt4/ui/LyXFilesUi.ui b/src/frontends/qt4/ui/LyXFilesUi.ui index 5cae863a5e..41dea0eb1f 100644 --- a/src/frontends/qt4/ui/LyXFilesUi.ui +++ b/src/frontends/qt4/ui/LyXFilesUi.ui @@ -29,7 +29,7 @@ - + @@ -40,6 +40,26 @@ + + + Use this language if avaliable (if not, English is used as fallback) + + + &Language: + + + languageCO + + + + + + + Use this language if avaliable (if not, English is used as fallback) + + + + &Filter: @@ -49,7 +69,7 @@ - + -- 2.39.2