Use GuiLyXFiles also for ui, kbd and bind files in prefs

This commit is contained in:
Juergen Spitzmueller 2019-03-22 14:11:46 +01:00
parent 9a2c5b515a
commit 58c4c11c4c
4 changed files with 126 additions and 63 deletions

View File

@ -239,6 +239,8 @@ QString const GuiLyXFiles::getSuffix()
{ {
if (type_ == "bind" || type_ == "ui") if (type_ == "bind" || type_ == "ui")
return toqstr(".") + type_; return toqstr(".") + type_;
else if (type_ == "kbd")
return ".kmap";
return ".lyx"; return ".lyx";
} }
@ -344,17 +346,43 @@ void GuiLyXFiles::setLanguage()
void GuiLyXFiles::on_browsePB_pressed() void GuiLyXFiles::on_browsePB_pressed()
{ {
bool const examples = (type_ == "examples"); QString path1 = toqstr(lyxrc.document_path);
FileDialog dlg(qt_("Select template file")); QString path2 = toqstr(lyxrc.example_path);
dlg.setButton1(qt_("D&ocuments"), toqstr(lyxrc.document_path)); QString title = qt_("Select example file");
if (examples) QString filter = qt_("LyX Documents (*.lyx)");
dlg.setButton2(qt_("&Examples"), toqstr(lyxrc.example_path)); QString b1 = qt_("D&ocuments");
else QString b2 = qt_("&Examples");
dlg.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path));
FileDialog::Result result = dlg.open(examples ? toqstr(lyxrc.example_path) if (type_ == "templates") {
: toqstr(lyxrc.template_path), path2 = toqstr(lyxrc.template_path);
QStringList(qt_("LyX Documents (*.lyx)"))); 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 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()) { if (result.first != FileDialog::Later && !result.second.isEmpty()) {
file_ = toqstr(FileName(fromqstr(result.second)).absFileName()); file_ = toqstr(FileName(fromqstr(result.second)).absFileName());
@ -513,6 +541,21 @@ bool GuiLyXFiles::initialiseParams(string const & type)
} }
void GuiLyXFiles::passParams(string const & data)
{
initialiseParams(data);
updateContents();
}
void GuiLyXFiles::selectItem(QString const item)
{
QList<QTreeWidgetItem *> twi = filesLW->findItems(item, Qt::MatchExactly|Qt::MatchRecursive);
if (!twi.isEmpty())
twi.first()->setSelected(true);
}
void GuiLyXFiles::paramsToDialog() void GuiLyXFiles::paramsToDialog()
{ {
if (type_ == "examples") if (type_ == "examples")
@ -537,7 +580,11 @@ void GuiLyXFiles::dispatchParams()
arg += fromqstr(file_); arg += fromqstr(file_);
FuncCode const lfun = getLfun(); FuncCode const lfun = getLfun();
dispatch(FuncRequest(lfun, arg)); if (lfun == LFUN_NOACTION)
// emit signal
fileSelected(file_);
else
dispatch(FuncRequest(lfun, arg));
} }

View File

@ -32,6 +32,12 @@ class GuiLyXFiles : public GuiDialog, public Ui::LyXFilesUi
public: public:
GuiLyXFiles(GuiView & lv); GuiLyXFiles(GuiView & lv);
/// A way to pass params to the dialog directly
void passParams(std::string const & data);
/// A way to pass params to the dialog directly
void selectItem(QString const item);
Q_SIGNALS:
void fileSelected(QString const file);
private Q_SLOTS: private Q_SLOTS:
void changed_adaptor(); void changed_adaptor();

View File

@ -19,6 +19,8 @@
#include "GuiFontExample.h" #include "GuiFontExample.h"
#include "GuiFontLoader.h" #include "GuiFontLoader.h"
#include "GuiKeySymbol.h" #include "GuiKeySymbol.h"
#include "GuiLyXFiles.h"
#include "GuiView.h"
#include "qt_helpers.h" #include "qt_helpers.h"
#include "Validator.h" #include "Validator.h"
@ -125,46 +127,6 @@ QString browseFile(QString const & filename,
} }
/** Wrapper around browseFile which tries to provide a filename
* relative to the user or system directory. The dir, name and ext
* parameters have the same meaning as in the
* support::LibFileSearch function.
*/
QString browseLibFile(QString const & dir,
QString const & name,
QString const & ext,
QString const & title,
QStringList const & filters)
{
// FIXME UNICODE
QString const label1 = qt_("&System files");
QString const dir1 =
toqstr(addName(package().system_support().absFileName(), fromqstr(dir)));
QString const label2 = qt_("&User files");
QString const dir2 =
toqstr(addName(package().user_support().absFileName(), fromqstr(dir)));
QString const result = browseFile(toqstr(
libFileSearch(dir, name, ext).absFileName()),
title, filters, false, dir1, dir2, QString(), QString(), dir1);
// remove the extension if it is the default one
QString noextresult;
if (getExtension(result) == ext)
noextresult = removeExtension(result);
else
noextresult = result;
// remove the directory, if it is the default one
QString const file = onlyFileName(noextresult);
if (toqstr(libFileSearch(dir, file, ext).absFileName()) == result)
return file;
else
return noextresult;
}
/** Launch a file dialog and return the chosen directory. /** Launch a file dialog and return the chosen directory.
pathname: a suggested pathname. pathname: a suggested pathname.
title: the title of the dialog. title: the title of the dialog.
@ -3493,6 +3455,10 @@ GuiPreferences::GuiPreferences(GuiView & lv)
bc().setApply(buttonBox->button(QDialogButtonBox::Apply)); bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel)); bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
bc().setRestore(buttonBox->button(QDialogButtonBox::Reset)); bc().setRestore(buttonBox->button(QDialogButtonBox::Reset));
glf = new GuiLyXFiles(lv);
connect(glf, SIGNAL(fileSelected(QString)),
this, SLOT(slotFileSelected(QString)));
} }
@ -3593,24 +3559,59 @@ void GuiPreferences::setColor(ColorCode col, QString const & hex)
} }
QString GuiPreferences::browsebind(QString const & file) const void GuiPreferences::slotFileSelected(QString const file)
{ {
return browseLibFile("bind", file, "bind", qt_("Choose bind file"), uifile_ = file;
QStringList(qt_("LyX bind files (*.bind)")));
} }
QString GuiPreferences::browseUI(QString const & file) const /** Wrapper around browseFile which tries to provide a filename
* relative to the user or system directory. The dir, name and ext
* parameters have the same meaning as in the
* support::LibFileSearch function.
*/
QString GuiPreferences::browseLibFile(QString const & dir,
QString const & name, QString const & ext)
{ {
return browseLibFile("ui", file, "ui", qt_("Choose UI file"), uifile_.clear();
QStringList(qt_("LyX UI files (*.ui)")));
glf->passParams(fromqstr(dir));
glf->selectItem(name);
glf->exec();
QString const result = uifile_;
// remove the extension if it is the default one
QString noextresult;
if (getExtension(result) == ext)
noextresult = removeExtension(result);
else
noextresult = result;
// remove the directory, if it is the default one
QString const file = onlyFileName(noextresult);
if (toqstr(libFileSearch(dir, file, ext).absFileName()) == result)
return file;
else
return noextresult;
} }
QString GuiPreferences::browsekbmap(QString const & file) const QString GuiPreferences::browsebind(QString const & file)
{ {
return browseLibFile("kbd", file, "kmap", qt_("Choose keyboard map"), return browseLibFile("bind", file, "bind");
QStringList(qt_("LyX keyboard maps (*.kmap)"))); }
QString GuiPreferences::browseUI(QString const & file)
{
return browseLibFile("ui", file, "ui");
}
QString GuiPreferences::browsekbmap(QString const & file)
{
return browseLibFile("kbd", file, "kmap");
} }

View File

@ -15,6 +15,7 @@
#define GUIPREFS_H #define GUIPREFS_H
#include "GuiDialog.h" #include "GuiDialog.h"
#include "GuiLyXFiles.h"
#include "Converter.h" #include "Converter.h"
#include "Format.h" #include "Format.h"
@ -68,6 +69,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
void change_adaptor(); void change_adaptor();
void slotFileSelected(QString);
Q_SIGNALS: Q_SIGNALS:
void prefsApplied(LyXRC const & rc); void prefsApplied(LyXRC const & rc);
@ -88,9 +90,9 @@ public:
bool isBufferDependent() const { return false; } bool isBufferDependent() const { return false; }
/// various file pickers /// various file pickers
QString browsebind(QString const & file) const; QString browsebind(QString const & file);
QString browseUI(QString const & file) const; QString browseUI(QString const & file);
QString browsekbmap(QString const & file) const; QString browsekbmap(QString const & file);
/// general browse /// general browse
QString browse(QString const & file, QString const & title) const; QString browse(QString const & file, QString const & title) const;
@ -106,6 +108,9 @@ public:
private: private:
/// ///
void addModule(PrefModule * module); void addModule(PrefModule * module);
///
QString browseLibFile(QString const & dir,
QString const & name, QString const & ext);
/// temporary lyxrc /// temporary lyxrc
LyXRC rc_; LyXRC rc_;
@ -118,6 +123,10 @@ private:
/// A list of colors to be dispatched /// A list of colors to be dispatched
std::vector<std::string> colors_; std::vector<std::string> colors_;
/// UI file selector
GuiLyXFiles * glf;
/// Selected UI file
QString uifile_;
}; };