]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiLyXFiles.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiLyXFiles.cpp
index 7a259f3a83b340553ad79da6a7ec27c065640d8e..98c43419fdc29c3e4cfcb5e2e4bbcb4aceb6d907 100644 (file)
@@ -38,6 +38,16 @@ using namespace lyx::support;
 namespace lyx {
 namespace frontend {
 
+namespace {
+
+QString const guiString(QString in)
+{
+       // recode specially encoded chars in file names (URL encoding and underbar)
+       return QString(QByteArray::fromPercentEncoding(in.toUtf8())).replace('_', ' ');
+}
+
+} // namespace anon
+
 
 QMap<QString, QString> GuiLyXFiles::getFiles()
 {
@@ -136,7 +146,6 @@ QMap<QString, QString> GuiLyXFiles::getFiles()
        // pre-fill the language combo (it will be updated once an item 
        // has been clicked)
        languageCO->clear();
-       languageCO->addItem(qt_("English"), toqstr("en"));
        QMap<QString, QString>::const_iterator i =available_languages_.constBegin();
        while (i != available_languages_.constEnd()) {
                languageCO->addItem(i.value(), i.key());
@@ -230,6 +239,8 @@ QString const GuiLyXFiles::getSuffix()
 {
        if (type_ == "bind" || type_ == "ui")
                return toqstr(".") + type_;
+       else if (type_ == "kbd")
+               return ".kmap";
        
        return ".lyx";
 }
@@ -306,6 +317,9 @@ void GuiLyXFiles::on_filesLW_itemClicked(QTreeWidgetItem * item, int)
 
 void GuiLyXFiles::setLanguage()
 {
+       // Enable language selection only if there is a selection.
+       languageCO->setEnabled(languageCO->count() > 1);
+       languageLA->setEnabled(languageCO->count() > 1);
        // first try last setting
        if (!savelang_.isEmpty()) {
                int index = languageCO->findData(savelang_);
@@ -332,17 +346,43 @@ void GuiLyXFiles::setLanguage()
 
 void GuiLyXFiles::on_browsePB_pressed()
 {
-       bool const examples = (type_ == "examples");
-       FileDialog dlg(qt_("Select template file"));
-       dlg.setButton1(qt_("D&ocuments"), toqstr(lyxrc.document_path));
-       if (examples)
-               dlg.setButton2(qt_("&Examples"), toqstr(lyxrc.example_path));
-       else
-               dlg.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path));
+       QString path1 = toqstr(lyxrc.document_path);
+       QString path2 = toqstr(lyxrc.example_path);
+       QString title = qt_("Select example file");
+       QString filter = qt_("LyX Documents (*.lyx)");
+       QString b1 = qt_("D&ocuments");
+       QString b2 = qt_("&Examples");
+
+       if (type_ == "templates") {
+               path2 = toqstr(lyxrc.template_path);
+               title = qt_("Select template file");
+               b1 = qt_("D&ocuments");
+               b2 = qt_("&Templates");
+       }
+       else if (type_ != "examples") {
+               path1 = toqstr(addName(package().user_support().absFileName(), fromqstr(type_)));
+               path2 = toqstr(addName(package().system_support().absFileName(), fromqstr(type_)));
+               b1 = qt_("&User files");
+               b2 = qt_("&System files");
+       }
+       if (type_ == "ui") {
+               title = qt_("Chose UI file");
+               filter = qt_("LyX UI Files (*.ui)");
+       }
+       if (type_ == "bind") {
+               title = qt_("Chose bind file");
+               filter = qt_("LyX Bind Files (*.bind)");
+       }
+       if (type_ == "kbd") {
+               title = qt_("Chose keyboard map");
+               filter = qt_("LyX Keymap Files (*.kmap)");
+       }
 
-       FileDialog::Result result = dlg.open(examples ? toqstr(lyxrc.example_path)
-                                                     : toqstr(lyxrc.template_path),
-                                QStringList(qt_("LyX Documents (*.lyx)")));
+       FileDialog dlg(title);
+       dlg.setButton1(b1, path1);
+       dlg.setButton2(b2, path2);
+
+       FileDialog::Result result = dlg.open(path2, QStringList(filter));
 
        if (result.first != FileDialog::Later && !result.second.isEmpty()) {
                file_ = toqstr(FileName(fromqstr(result.second)).absFileName());
@@ -375,10 +415,9 @@ void GuiLyXFiles::updateContents()
                        catsave = cat;
                        cat = catsave.left(catsave.indexOf('/'));
                        subcat = toqstr(translateIfPossible(
-                                       qstring_to_ucs4(catsave.mid(
-                                               catsave.indexOf('/') + 1).replace('_', ' '))));
+                                       qstring_to_ucs4(guiString(catsave.mid(catsave.indexOf('/') + 1)))));
                }
-               cat =  toqstr(translateIfPossible(qstring_to_ucs4(cat.replace('_', ' '))));
+               cat =  toqstr(translateIfPossible(qstring_to_ucs4(guiString(cat))));
                QTreeWidgetItem * catItem = new QTreeWidgetItem();
                if (!cats.contains(cat)) {
                        catItem->setText(0, cat);
@@ -391,8 +430,11 @@ void GuiLyXFiles::updateContents()
                QTreeWidgetItem * item = new QTreeWidgetItem();
                QString const filename = info.fileName();
                QString guiname = filename.left(filename.lastIndexOf(getSuffix())).replace('_', ' ');
-               if (translateName())
-                       guiname = toqstr(translateIfPossible(qstring_to_ucs4(guiname)));
+               // Special case: defaults.lyx
+               if (type_ == "templates" && guiname == "defaults")
+                       guiname = qt_("Default Template");
+               else if (translateName())
+                       guiname = toqstr(translateIfPossible(qstring_to_ucs4(guiString(guiname))));
                QIcon file_icon = (realpath.startsWith(toqstr(package().user_support().absFileName()))) ?
                                user_icon : system_icon;
                item->setIcon(0, file_icon);
@@ -502,6 +544,26 @@ bool GuiLyXFiles::initialiseParams(string const & type)
 }
 
 
+void GuiLyXFiles::passParams(string const & data)
+{
+       initialiseParams(data);
+       updateContents();
+}
+
+
+void GuiLyXFiles::selectItem(QString const item)
+{
+       /* Using an intermediary variable flags is needed up to at least
+        * Qt 5.5 because of a subtle namespace issue. See:
+        *   https://stackoverflow.com/questions/10755058/qflags-enum-type-conversion-fails-all-of-a-sudden
+        * for details.*/
+       Qt::MatchFlags const flags(Qt::MatchExactly|Qt::MatchRecursive);
+       QList<QTreeWidgetItem *> twi = filesLW->findItems(item, flags);
+       if (!twi.isEmpty())
+               twi.first()->setSelected(true);
+}
+
+
 void GuiLyXFiles::paramsToDialog()
 {
        if (type_ == "examples")
@@ -526,7 +588,11 @@ void GuiLyXFiles::dispatchParams()
        arg += fromqstr(file_);
        FuncCode const lfun = getLfun();
 
-       dispatch(FuncRequest(lfun, arg));
+       if (lfun == LFUN_NOACTION)
+               // emit signal
+               fileSelected(file_);
+       else
+               dispatch(FuncRequest(lfun, arg));
 }