]> git.lyx.org Git - features.git/blobdiff - src/frontends/qt4/GuiLyXFiles.cpp
GuiLyXFiles: add language selector.
[features.git] / src / frontends / qt4 / GuiLyXFiles.cpp
index 6d380bc708f8a563ede0ede36f94a320813b9ef8..c5d5c050f00683f6a05faac77c7a001640aadfa4 100644 (file)
@@ -51,53 +51,25 @@ void GuiLyXFiles::getFiles(QMap<QString, QString> & 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<QString, QString> & 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();