From aff97fe9b5f09de63ef1c4f46141fe3a84d37a4d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Sun, 20 Apr 2008 19:56:01 +0000 Subject: [PATCH] move the filefilter stuff closer to the only place where it is used git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24395 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/FileDialog.cpp | 39 ++--- src/frontends/qt4/FileDialog.h | 9 +- src/frontends/qt4/GuiBibtex.cpp | 5 +- src/frontends/qt4/GuiClipboard.cpp | 9 +- src/frontends/qt4/GuiDocument.cpp | 3 +- src/frontends/qt4/GuiExternal.cpp | 11 +- src/frontends/qt4/GuiGraphics.cpp | 3 +- src/frontends/qt4/GuiInclude.cpp | 9 +- src/frontends/qt4/GuiPrefs.cpp | 34 ++-- src/frontends/qt4/GuiPrefs.h | 3 - src/frontends/qt4/GuiPrint.cpp | 3 +- src/frontends/qt4/GuiView.cpp | 21 +-- src/frontends/qt4/LyXFileDialog.cpp | 6 +- src/frontends/qt4/LyXFileDialog.h | 4 +- src/frontends/qt4/qt_helpers.cpp | 230 ++++++++++++++++++++++++++++ src/frontends/qt4/qt_helpers.h | 4 +- src/frontends/qt4/qt_i18n.h | 30 ++++ src/support/FileFilterList.cpp | 186 ---------------------- src/support/FileFilterList.h | 78 ---------- src/support/Makefile.am | 2 - 20 files changed, 319 insertions(+), 370 deletions(-) create mode 100644 src/frontends/qt4/qt_i18n.h delete mode 100644 src/support/FileFilterList.cpp delete mode 100644 src/support/FileFilterList.h diff --git a/src/frontends/qt4/FileDialog.cpp b/src/frontends/qt4/FileDialog.cpp index c6e8b85c8d..a3af75db1a 100644 --- a/src/frontends/qt4/FileDialog.cpp +++ b/src/frontends/qt4/FileDialog.cpp @@ -17,7 +17,6 @@ #include "qt_helpers.h" #include "support/debug.h" -#include "support/FileFilterList.h" #include "support/FileName.h" #include "support/gettext.h" #include "support/os.h" @@ -39,14 +38,10 @@ #ifdef USE_NATIVE_FILEDIALOG #include -#include "support/filetools.h" - -using lyx::support::makeAbsPath; #endif namespace lyx { -using support::FileFilterList; using support::os::internal_path; @@ -83,25 +78,19 @@ void FileDialog::setButton2(QString const & label, QString const & dir) FileDialog::Result FileDialog::save(QString const & path, - FileFilterList const & filters, - QString const & suggested) + QStringList const & filters, QString const & suggested) { LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path) - << "\", mask \"" << to_utf8(filters.as_string()) + << "\", mask \"" << fromqstr(filters.join(";;")) << "\", suggested \"" << fromqstr(suggested) << '"'); FileDialog::Result result; result.first = FileDialog::Chosen; #ifdef USE_NATIVE_FILEDIALOG - QString const startsWith = toqstr( - makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename()); + QString const startsWith = makeAbsPath(suggested, path); QString const name = QFileDialog::getSaveFileName(qApp->focusWidget(), - title_, - startsWith, - toqstr(filters.as_string()), - 0, - QFileDialog::DontConfirmOverwrite); + title_, startsWith, filters, 0, QFileDialog::DontConfirmOverwrite); result.second = toqstr(internal_path(fromqstr(name))); #else LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2); @@ -126,21 +115,19 @@ FileDialog::Result FileDialog::save(QString const & path, FileDialog::Result FileDialog::open(QString const & path, - FileFilterList const & filters, - QString const & suggested) + QStringList const & filters, QString const & suggested) { LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path) - << "\", mask \"" << filters.as_string() + << "\", mask \"" << fromqstr(filters.join(";;")) << "\", suggested \"" << fromqstr(suggested) << '"'); FileDialog::Result result; result.first = FileDialog::Chosen; #ifdef USE_NATIVE_FILEDIALOG - QString const startsWith = toqstr( - makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename()); + QString const startsWith = makeAbsPath(suggested, path); result.second = internalPath( QFileDialog::getOpenFileName(qApp->focusWidget(), - title_, startsWith, toqstr(filters.as_string()) )); + title_, startsWith, filters)); #else LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2); @@ -159,7 +146,7 @@ FileDialog::Result FileDialog::open(QString const & path, FileDialog::Result FileDialog::opendir(QString const & path, - QString const & suggested) + QString const & suggested) { LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path) << "\", suggested \"" << fromqstr(suggested) << '"'); @@ -167,15 +154,13 @@ FileDialog::Result FileDialog::opendir(QString const & path, result.first = FileDialog::Chosen; #ifdef USE_NATIVE_FILEDIALOG - QString const startsWith = toqstr( - makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename()); + QString const startsWith = makeAbsPath(suggested, path); result.second = toqstr(internal_path(fromqstr( QFileDialog::getExistingDirectory(qApp->focusWidget(), title_, startsWith)))); #else - FileFilterList const filter(_("Directories")); - - LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2); + LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")), + private_->b1, private_->b2); dlg.setFileMode(QFileDialog::DirectoryOnly); diff --git a/src/frontends/qt4/FileDialog.h b/src/frontends/qt4/FileDialog.h index 9dbd54e578..f5da6eb73e 100644 --- a/src/frontends/qt4/FileDialog.h +++ b/src/frontends/qt4/FileDialog.h @@ -21,9 +21,6 @@ namespace lyx { -namespace support { class FileFilterList; } - - /** * \class FileDialog * \brief GUI-I definition of file dialog interface @@ -62,8 +59,7 @@ public: void setButton2(QString const & label, QString const & dir); /// Choose a file for opening, starting in directory \c path. - Result open(QString const & path, - support::FileFilterList const & filters, + Result open(QString const & path, QStringList const & filters, QString const & suggested = QString()); /// Choose a directory, starting in directory \c path. @@ -71,8 +67,7 @@ public: QString const & suggested = QString()); /// Choose a file for saving, starting in directory \c path. - Result save(QString const & path, - support::FileFilterList const & filters, + Result save(QString const & path, QStringList const & filters, QString const & suggested = QString()); private: diff --git a/src/frontends/qt4/GuiBibtex.cpp b/src/frontends/qt4/GuiBibtex.cpp index a1c97cd6ed..28286367b4 100644 --- a/src/frontends/qt4/GuiBibtex.cpp +++ b/src/frontends/qt4/GuiBibtex.cpp @@ -32,7 +32,6 @@ #include "support/debug.h" #include "support/ExceptionMessage.h" -#include "support/FileFilterList.h" #include "support/FileName.h" #include "support/filetools.h" // changeExtension #include "support/gettext.h" @@ -424,7 +423,7 @@ QString GuiBibtex::browseBib(QString const & in_name) const { QString const label1 = qt_("Documents|#o#O"); QString const dir1 = toqstr(lyxrc.document_path); - FileFilterList const filter(_("BibTeX Databases (*.bib)")); + QStringList const filter(qt_("BibTeX Databases (*.bib)")); return browseRelFile(in_name, bufferFilepath(), qt_("Select a BibTeX database to add"), filter, false, label1, dir1); } @@ -434,7 +433,7 @@ QString GuiBibtex::browseBst(QString const & in_name) const { QString const label1 = qt_("Documents|#o#O"); QString const dir1 = toqstr(lyxrc.document_path); - FileFilterList const filter(_("BibTeX Styles (*.bst)")); + QStringList const filter(qt_("BibTeX Styles (*.bst)")); return browseRelFile(in_name, bufferFilepath(), qt_("Select a BibTeX style"), filter, false, label1, dir1); } diff --git a/src/frontends/qt4/GuiClipboard.cpp b/src/frontends/qt4/GuiClipboard.cpp index ad032181e2..3c7798870e 100644 --- a/src/frontends/qt4/GuiClipboard.cpp +++ b/src/frontends/qt4/GuiClipboard.cpp @@ -25,7 +25,6 @@ #include "support/convert.h" #include "support/debug.h" #include "support/filetools.h" -#include "support/FileFilterList.h" #include "support/gettext.h" #include "support/lstrings.h" #ifdef Q_WS_MACX @@ -347,16 +346,16 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur, while (true) { // create file type filter, putting the prefered on to the front - docstring filterSpec; + QStringList filter; for (size_t i = 0; i != types.size(); ++i) { docstring s = bformat(_("%1$s Files"), typeNames[types[i]]) + " (*." + from_ascii(extensions[types[i]]) + ")"; if (types[i] == type) - filterSpec = s + filterSpec; + filter.prepend(toqstr(s)); else - filterSpec += ";;" + s; + filter.append(toqstr(s)); } - FileFilterList const filter(filterSpec); + filter = fileFilters(filter.join(";;")); // show save dialog for the graphic FileDialog dlg(qt_("Choose a filename to save the pasted graphic as")); diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index bc4864fce3..4eedfbabef 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -43,7 +43,6 @@ #include "support/debug.h" #include "support/FileName.h" -#include "support/FileFilterList.h" #include "support/filetools.h" #include "support/lstrings.h" @@ -1223,7 +1222,7 @@ void GuiDocument::browseLayout() { QString const label1 = qt_("Layouts|#o#O"); QString const dir1 = toqstr(lyxrc.document_path); - FileFilterList const filter(_("LyX Layout (*.layout)")); + QStringList const filter(qt_("LyX Layout (*.layout)")); QString file = browseRelFile(QString(), bufferFilepath(), qt_("Local layout file"), filter, false, label1, dir1); diff --git a/src/frontends/qt4/GuiExternal.cpp b/src/frontends/qt4/GuiExternal.cpp index 2fe1ce848a..6a77567b2b 100644 --- a/src/frontends/qt4/GuiExternal.cpp +++ b/src/frontends/qt4/GuiExternal.cpp @@ -28,7 +28,6 @@ #include "graphics/GraphicsImage.h" #include "support/convert.h" -#include "support/FileFilterList.h" #include "support/filetools.h" #include "support/lstrings.h" #include "support/lyxlib.h" @@ -707,14 +706,15 @@ void GuiExternal::editExternal() } -static string templateFilters(string const & template_name) +static QStringList templateFilters(QString const & template_name) { /// Determine the template file extension external::TemplateManager const & etm = external::TemplateManager::get(); external::Template const * const et_ptr = - etm.getTemplateByName(template_name); - return et_ptr ? et_ptr->fileRegExp : string(); + etm.getTemplateByName(fromqstr(template_name)); + + return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString()); } @@ -723,8 +723,7 @@ QString GuiExternal::browse(QString const & input, { QString const title = qt_("Select external file"); QString const bufpath = bufferFilepath(); - FileFilterList const filter = - FileFilterList(from_utf8(templateFilters(fromqstr(template_name)))); + QStringList const filter = templateFilters(template_name); QString const label1 = qt_("Documents|#o#O"); QString const dir1 = toqstr(lyxrc.document_path); diff --git a/src/frontends/qt4/GuiGraphics.cpp b/src/frontends/qt4/GuiGraphics.cpp index aaa8bc5c79..1eff038b7d 100644 --- a/src/frontends/qt4/GuiGraphics.cpp +++ b/src/frontends/qt4/GuiGraphics.cpp @@ -32,7 +32,6 @@ #include "support/convert.h" #include "support/debug.h" -#include "support/FileFilterList.h" #include "support/filetools.h" #include "support/gettext.h" #include "support/lstrings.h" @@ -751,7 +750,7 @@ QString GuiGraphics::browse(QString const & in_name) const clipdir = addName(package().system_support().absFilename(), "clipart"); return browseRelFile(in_name, bufferFilepath(), - title, FileFilterList(), false, + title, fileFilters(QString()), false, qt_("Clipart|#C#c"), toqstr(clipdir), qt_("Documents|#o#O"), toqstr(lyxrc.document_path)); } diff --git a/src/frontends/qt4/GuiInclude.cpp b/src/frontends/qt4/GuiInclude.cpp index 7caf50fb18..5633059109 100644 --- a/src/frontends/qt4/GuiInclude.cpp +++ b/src/frontends/qt4/GuiInclude.cpp @@ -17,15 +17,14 @@ #include "Buffer.h" #include "Format.h" #include "FuncRequest.h" -#include "support/gettext.h" #include "LyXRC.h" #include "qt_helpers.h" #include "LyXRC.h" -#include "support/os.h" +#include "support/gettext.h" #include "support/lstrings.h" -#include "support/FileFilterList.h" +#include "support/os.h" #include "support/FileName.h" #include "support/filetools.h" @@ -309,11 +308,11 @@ QString GuiInclude::browse(QString const & in_name, Type in_type) const QString const title = qt_("Select document to include"); // input TeX, verbatim, or LyX file ? - FileFilterList filters; + QStringList filters; switch (in_type) { case INCLUDE: case INPUT: - filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)")); + filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)")); break; case VERBATIM: case LISTINGS: diff --git a/src/frontends/qt4/GuiPrefs.cpp b/src/frontends/qt4/GuiPrefs.cpp index 1b781fb45a..3e0a3db042 100644 --- a/src/frontends/qt4/GuiPrefs.cpp +++ b/src/frontends/qt4/GuiPrefs.cpp @@ -32,7 +32,6 @@ #include "Session.h" #include "support/debug.h" -#include "support/FileFilterList.h" #include "support/FileName.h" #include "support/filetools.h" #include "support/foreach.h" @@ -86,7 +85,7 @@ namespace frontend { */ QString browseFile(QString const & filename, QString const & title, - support::FileFilterList const & filters, + QStringList const & filters, bool save = false, QString const & label1 = QString(), QString const & dir1 = QString(), @@ -121,7 +120,7 @@ QString browseLibFile(QString const & dir, QString const & name, QString const & ext, QString const & title, - support::FileFilterList const & filters) + QStringList const & filters) { // FIXME UNICODE QString const label1 = qt_("System files|#S#s"); @@ -182,7 +181,7 @@ QString browseDir(QString const & pathname, } // namespace frontend QString browseRelFile(QString const & filename, QString const & refpath, - QString const & title, FileFilterList const & filters, bool save, + QString const & title, QStringList const & filters, bool save, QString const & label1, QString const & dir1, QString const & label2, QString const & dir2) { @@ -988,7 +987,7 @@ void PrefPaths::update(LyXRC const & rc) void PrefPaths::select_exampledir() { - QString file = form_->browsedir(internalPath(exampleDirED->text()), + QString file = browseDir(internalPath(exampleDirED->text()), qt_("Select directory for example files")); if (!file.isEmpty()) exampleDirED->setText(file); @@ -997,7 +996,7 @@ void PrefPaths::select_exampledir() void PrefPaths::select_templatedir() { - QString file = form_->browsedir(internalPath(templateDirED->text()), + QString file = browseDir(internalPath(templateDirED->text()), qt_("Select a document templates directory")); if (!file.isEmpty()) templateDirED->setText(file); @@ -1006,7 +1005,7 @@ void PrefPaths::select_templatedir() void PrefPaths::select_tempdir() { - QString file = form_->browsedir(internalPath(tempDirED->text()), + QString file = browseDir(internalPath(tempDirED->text()), qt_("Select a temporary directory")); if (!file.isEmpty()) tempDirED->setText(file); @@ -1015,7 +1014,7 @@ void PrefPaths::select_tempdir() void PrefPaths::select_backupdir() { - QString file = form_->browsedir(internalPath(backupDirED->text()), + QString file = browseDir(internalPath(backupDirED->text()), qt_("Select a backups directory")); if (!file.isEmpty()) backupDirED->setText(file); @@ -1024,7 +1023,7 @@ void PrefPaths::select_backupdir() void PrefPaths::select_workingdir() { - QString file = form_->browsedir(internalPath(workingDirED->text()), + QString file = browseDir(internalPath(workingDirED->text()), qt_("Select a document directory")); if (!file.isEmpty()) workingDirED->setText(file); @@ -2549,42 +2548,35 @@ void GuiPreferences::updateScreenFonts() QString GuiPreferences::browsebind(QString const & file) const { return browseLibFile("bind", file, "bind", qt_("Choose bind file"), - FileFilterList(_("LyX bind files (*.bind)"))); + QStringList(qt_("LyX bind files (*.bind)"))); } QString GuiPreferences::browseUI(QString const & file) const { return browseLibFile("ui", file, "ui", qt_("Choose UI file"), - FileFilterList(_("LyX UI files (*.ui)"))); + QStringList(qt_("LyX UI files (*.ui)"))); } QString GuiPreferences::browsekbmap(QString const & file) const { return browseLibFile("kbd", file, "kmap", qt_("Choose keyboard map"), - FileFilterList(_("LyX keyboard maps (*.kmap)"))); + QStringList(qt_("LyX keyboard maps (*.kmap)"))); } QString GuiPreferences::browsedict(QString const & file) const { return browseFile(file, qt_("Choose personal dictionary"), - FileFilterList(lyxrc.use_spell_lib ? _("*.pws") : _("*.ispell"))); + QStringList(lyxrc.use_spell_lib ? qt_("*.pws") : qt_("*.ispell"))); } QString GuiPreferences::browse(QString const & file, QString const & title) const { - return browseFile(file, title, FileFilterList(), true); -} - - -QString GuiPreferences::browsedir(QString const & path, - QString const & title) const -{ - return browseDir(path, title); + return browseFile(file, title, QStringList(), true); } diff --git a/src/frontends/qt4/GuiPrefs.h b/src/frontends/qt4/GuiPrefs.h index afca6d4b42..36a2b2e15e 100644 --- a/src/frontends/qt4/GuiPrefs.h +++ b/src/frontends/qt4/GuiPrefs.h @@ -490,9 +490,6 @@ public: /// general browse QString browse(QString const & file, QString const & title) const; - /// browse directory - QString browsedir(QString const & path, QString const & title) const; - /// set a color void setColor(ColorCode col, std::string const & hex); diff --git a/src/frontends/qt4/GuiPrint.cpp b/src/frontends/qt4/GuiPrint.cpp index 20003e48ac..af74910ff1 100644 --- a/src/frontends/qt4/GuiPrint.cpp +++ b/src/frontends/qt4/GuiPrint.cpp @@ -23,7 +23,6 @@ #include "LyXRC.h" #include "support/convert.h" -#include "support/FileFilterList.h" #include "support/filetools.h" #include "support/gettext.h" #include "support/os.h" @@ -87,7 +86,7 @@ void GuiPrint::browseClicked() { QString file = browseRelFile(QString(), bufferFilepath(), qt_("Print to file"), - FileFilterList(_("PostScript files (*.ps)")), true); + QStringList(qt_("PostScript files (*.ps)")), true); if (!file.isEmpty()) { fileED->setText(file); changed(); diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index ffb301e488..bf3a906a82 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -55,7 +55,6 @@ #include "support/assert.h" #include "support/debug.h" -#include "support/FileFilterList.h" #include "support/FileName.h" #include "support/filetools.h" #include "support/gettext.h" @@ -1117,9 +1116,8 @@ static FileName selectTemplateFile() dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path)); dlg.setButton1(qt_("Templates|#T#t"), toqstr(lyxrc.template_path)); - FileDialog::Result result = - dlg.open(toqstr(lyxrc.template_path), - FileFilterList(_("LyX Documents (*.lyx)"))); + FileDialog::Result result = dlg.open(toqstr(lyxrc.template_path), + QStringList(qt_("LyX Documents (*.lyx)"))); if (result.first == FileDialog::Later) return FileName(); @@ -1178,7 +1176,7 @@ void GuiView::openDocument(string const & fname) toqstr(addPath(package().system_support().absFilename(), "examples"))); FileDialog::Result result = - dlg.open(toqstr(initpath), FileFilterList(_("LyX Documents (*.lyx)"))); + dlg.open(toqstr(initpath), QStringList(qt_("LyX Documents (*.lyx)"))); if (result.first == FileDialog::Later) return; @@ -1317,7 +1315,7 @@ void GuiView::importDocument(string const & argument) filter += ')'; FileDialog::Result result = - dlg.open(toqstr(initpath), FileFilterList(filter)); + dlg.open(toqstr(initpath), fileFilters(toqstr(filter))); if (result.first == FileDialog::Later) return; @@ -1430,9 +1428,8 @@ void GuiView::insertLyXFile(docstring const & fname) toqstr(addPath(package().system_support().absFilename(), "examples"))); - FileDialog::Result result = - dlg.open(toqstr(initpath), - FileFilterList(_("LyX Documents (*.lyx)"))); + FileDialog::Result result = dlg.open(toqstr(initpath), + QStringList(qt_("LyX Documents (*.lyx)"))); if (result.first == FileDialog::Later) return; @@ -1470,7 +1467,7 @@ void GuiView::insertPlaintextFile(docstring const & fname, LFUN_FILE_INSERT_PLAINTEXT_PARA : LFUN_FILE_INSERT_PLAINTEXT)); FileDialog::Result result = dlg.open(toqstr(bv->buffer().filePath()), - FileFilterList()); + QStringList()); if (result.first == FileDialog::Later) return; @@ -1511,11 +1508,9 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname) if (!isLyXFilename(fname.absFilename())) fname.changeExtension(".lyx"); - FileFilterList const filter(_("LyX Documents (*.lyx)")); - FileDialog::Result result = dlg.save(toqstr(fname.onlyPath().absFilename()), - filter, + QStringList(qt_("LyX Documents (*.lyx)")), toqstr(fname.onlyFileName())); if (result.first == FileDialog::Later) diff --git a/src/frontends/qt4/LyXFileDialog.cpp b/src/frontends/qt4/LyXFileDialog.cpp index e0f94e7386..0df10992e2 100644 --- a/src/frontends/qt4/LyXFileDialog.cpp +++ b/src/frontends/qt4/LyXFileDialog.cpp @@ -14,7 +14,6 @@ #include "qt_helpers.h" -#include "support/FileFilterList.h" #include "support/lstrings.h" #include @@ -44,12 +43,13 @@ static QString getLabel(QString const & qstr) LyXFileDialog::LyXFileDialog(QString const & title, QString const & path, - support::FileFilterList const & filters, + QStringList const & filters, FileDialog::Button const & b1, FileDialog::Button const & b2) // FIXME replace that with guiApp->currentView() - : QFileDialog(qApp->focusWidget(), title, path, toqstr(filters.as_string())) + : QFileDialog(qApp->focusWidget(), title, path) { + setFilters(filters); QDir dir(path); // FIXME: workaround for a bug in qt which makes LyX crash // with hidden paths (bug 4513). Recheck with recent Qt versions. diff --git a/src/frontends/qt4/LyXFileDialog.h b/src/frontends/qt4/LyXFileDialog.h index 2419b2cad2..60d7fd2d6d 100644 --- a/src/frontends/qt4/LyXFileDialog.h +++ b/src/frontends/qt4/LyXFileDialog.h @@ -20,8 +20,6 @@ class QToolButton; namespace lyx { -namespace support { class FileFilterList; } - class LyXFileDialog : public QFileDialog { Q_OBJECT @@ -29,7 +27,7 @@ class LyXFileDialog : public QFileDialog public: LyXFileDialog(QString const & title, QString const & path, - support::FileFilterList const & filters, + QStringList const & filters, FileDialog::Button const & b1, FileDialog::Button const & b2); diff --git a/src/frontends/qt4/qt_helpers.cpp b/src/frontends/qt4/qt_helpers.cpp index 5cb11cbea5..89ce0d5717 100644 --- a/src/frontends/qt4/qt_helpers.cpp +++ b/src/frontends/qt4/qt_helpers.cpp @@ -43,6 +43,12 @@ #include #include +// for FileFilter. +// FIXME: Remove +#include +#include + + using namespace std; using namespace lyx::support; @@ -303,4 +309,228 @@ QString getExtension(QString const & name) return toqstr(support::getExtension(fromqstr(name))); } + +/** Convert relative path into absolute path based on a basepath. + If relpath is absolute, just use that. + If basepath doesn't exist use CWD. + */ +QString makeAbsPath(QString const & relpath, QString const & base) +{ + return toqstr(support::makeAbsPath(fromqstr(relpath), + fromqstr(base)).absFilename()); +} + + +///////////////////////////////////////////////////////////////////////// +// +// FileFilterList +// +///////////////////////////////////////////////////////////////////////// + +/** Given a string such as + * " ... *.{abc,def} ", + * convert the csh-style brace expresions: + * " ... *.abc *.def ". + * Requires no system support, so should work equally on Unix, Mac, Win32. + */ +static string const convert_brace_glob(string const & glob) +{ + // Matches " *.{abc,def,ghi}", storing "*." as group 1 and + // "abc,def,ghi" as group 2. + static boost::regex const glob_re(" *([^ {]*)\\{([^ }]+)\\}"); + // Matches "abc" and "abc,", storing "abc" as group 1. + static boost::regex const block_re("([^,}]+),?"); + + string pattern; + + string::const_iterator it = glob.begin(); + string::const_iterator const end = glob.end(); + while (true) { + boost::match_results what; + if (!boost::regex_search(it, end, what, glob_re)) { + // Ensure that no information is lost. + pattern += string(it, end); + break; + } + + // Everything from the start of the input to + // the start of the match. + pattern += string(what[-1].first, what[-1].second); + + // Given " *.{abc,def}", head == "*." and tail == "abc,def". + string const head = string(what[1].first, what[1].second); + string const tail = string(what[2].first, what[2].second); + + // Split the ','-separated chunks of tail so that + // $head{$chunk1,$chunk2} becomes "$head$chunk1 $head$chunk2". + string const fmt = " " + head + "$1"; + pattern += boost::regex_merge(tail, block_re, fmt); + + // Increment the iterator to the end of the match. + it += distance(it, what[0].second); + } + + return pattern; +} + + +struct Filter +{ + /* \param description text describing the filters. + * \param one or more wildcard patterns, separated by + * whitespace. + */ + Filter(docstring const & description, std::string const & globs); + + docstring const & description() const { return desc_; } + + QString toString() const; + + docstring desc_; + std::vector globs_; +}; + + +Filter::Filter(docstring const & description, string const & globs) + : desc_(description) +{ + typedef boost::tokenizer > Tokenizer; + boost::char_separator const separator(" "); + + // Given " ... *.{abc,def} ", expand to + // " ... *.abc *.def " + string const expanded_globs = convert_brace_glob(globs); + + // Split into individual globs. + vector matches; + Tokenizer const tokens(expanded_globs, separator); + globs_ = vector(tokens.begin(), tokens.end()); +} + + +QString Filter::toString() const +{ + QString s; + + bool const has_description = desc_.empty(); + + if (has_description) { + s += toqstr(desc_); + s += " ("; + } + + for (size_t i = 0; i != globs_.size(); ++i) { + if (i > 0) + s += ' '; + s += toqstr(globs_[i]); + } + + if (has_description) + s += ')'; + return s; +} + + +/** \c FileFilterList parses a Qt-style list of available file filters + * to generate the corresponding vector. + * For example "TeX documents (*.tex);;LyX Documents (*.lyx)" + * will be parsed to fill a vector of size 2, whilst "*.{p[bgp]m} *.pdf" + * will result in a vector of size 1 in which the description field is empty. + */ +struct FileFilterList +{ + // FIXME UNICODE: globs_ should be unicode... + /** \param qt_style_filter a list of available file filters. + * Eg. "TeX documents (*.tex);;LyX Documents (*.lyx)". + * The "All files (*)" filter is always added to the list. + */ + explicit FileFilterList(docstring const & qt_style_filter = + docstring()); + + typedef std::vector::size_type size_type; + + bool empty() const { return filters_.empty(); } + size_type size() const { return filters_.size(); } + Filter & operator[](size_type i) { return filters_[i]; } + Filter const & operator[](size_type i) const { return filters_[i]; } + + void parse_filter(std::string const & filter); + std::vector filters_; +}; + + +FileFilterList::FileFilterList(docstring const & qt_style_filter) +{ + // FIXME UNICODE + string const filter = to_utf8(qt_style_filter) + + (qt_style_filter.empty() ? string() : ";;") + + to_utf8(_("All Files ")) +#if defined(_WIN32) + + ("(*.*)"); +#else + + ("(*)"); +#endif + + // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)" + // into individual filters. + static boost::regex const separator_re(";;"); + + string::const_iterator it = filter.begin(); + string::const_iterator const end = filter.end(); + while (true) { + boost::match_results what; + + if (!boost::regex_search(it, end, what, separator_re)) { + parse_filter(string(it, end)); + break; + } + + // Everything from the start of the input to + // the start of the match. + parse_filter(string(what[-1].first, what[-1].second)); + + // Increment the iterator to the end of the match. + it += distance(it, what[0].second); + } +} + + +void FileFilterList::parse_filter(string const & filter) +{ + // Matches "TeX documents (*.tex)", + // storing "TeX documents " as group 1 and "*.tex" as group 2. + static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$"); + + boost::match_results what; + if (!boost::regex_search(filter, what, filter_re)) { + // Just a glob, no description. + filters_.push_back(Filter(docstring(), trim(filter))); + } else { + // FIXME UNICODE + docstring const desc = from_utf8(string(what[1].first, what[1].second)); + string const globs = string(what[2].first, what[2].second); + filters_.push_back(Filter(trim(desc), trim(globs))); + } +} + + +/** \returns the equivalent of the string passed in + * although any brace expressions are expanded. + * (E.g. "*.{png,jpg}" -> "*.png *.jpg") + */ +QStringList fileFilters(QString const & desc) +{ + // we have: "*.{gif,png,jpg,bmp,pbm,ppm,tga,tif,xpm,xbm}" + // but need: "*.cpp;*.cc;*.C;*.cxx;*.c++" + FileFilterList filters(qstring_to_ucs4(desc)); + LYXERR0("DESC: " << fromqstr(desc)); + QStringList list; + for (size_t i = 0; i != filters.filters_.size(); ++i) { + QString f = filters.filters_[i].toString(); + LYXERR0("FILTER: " << fromqstr(f)); + list.append(f); + } + return list; +} + } // namespace lyx diff --git a/src/frontends/qt4/qt_helpers.h b/src/frontends/qt4/qt_helpers.h index a088c00efe..6e5a2c6cb8 100644 --- a/src/frontends/qt4/qt_helpers.h +++ b/src/frontends/qt4/qt_helpers.h @@ -29,7 +29,6 @@ class LengthCombo; namespace lyx { -namespace support { class FileFilterList; } namespace support { class FileName; } namespace frontend { @@ -83,7 +82,7 @@ support::FileName libFileSearch(QString const & dir, QString const & name, QString browseRelFile(QString const & filename, QString const & refpath, QString const & title, - support::FileFilterList const & filters, + QStringList const & filters, bool save = false, QString const & label1 = QString(), QString const & dir1 = QString(), @@ -105,6 +104,7 @@ QStringList texFileList(QString const & filename); QString internalPath(QString const &); QString onlyFilename(QString const & str); QString onlyPath(QString const & str); +QStringList fileFilters(QString const & description); QString changeExtension(QString const & oldname, QString const & extension); diff --git a/src/frontends/qt4/qt_i18n.h b/src/frontends/qt4/qt_i18n.h new file mode 100644 index 0000000000..29b966e99e --- /dev/null +++ b/src/frontends/qt4/qt_i18n.h @@ -0,0 +1,30 @@ +// -*- C++ -*- +/** + * \file qt_i18n.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Dekel Tsur + * \author Richard Heck + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef QTI18N_H +#define QTI18N_H + +class QString; + +namespace lyx { + +/** +* qt_ - i18nize string and convert to QString + * + * Use this in qt4/ instead of _() + */ +QString const qt_(char const * str, const char * comment = 0); + + +} // namespace lyx + +#endif // QTI18_H diff --git a/src/support/FileFilterList.cpp b/src/support/FileFilterList.cpp deleted file mode 100644 index c35eadfe1c..0000000000 --- a/src/support/FileFilterList.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/** - * \file FileFilterList.cpp - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "support/FileFilterList.h" - -#include "support/lstrings.h" -#include "support/gettext.h" - -#include -#include - -#include - -using namespace std; - -namespace { - -/** Given a string such as - * " ... *.{abc,def} ", - * convert the csh-style brace expresions: - * " ... *.abc *.def ". - * Requires no system support, so should work equally on Unix, Mac, Win32. - */ -string const convert_brace_glob(string const & glob) -{ - // Matches " *.{abc,def,ghi}", storing "*." as group 1 and - // "abc,def,ghi" as group 2. - static boost::regex const glob_re(" *([^ {]*)\\{([^ }]+)\\}"); - // Matches "abc" and "abc,", storing "abc" as group 1. - static boost::regex const block_re("([^,}]+),?"); - - string pattern; - - string::const_iterator it = glob.begin(); - string::const_iterator const end = glob.end(); - while (true) { - boost::match_results what; - if (!boost::regex_search(it, end, what, glob_re)) { - // Ensure that no information is lost. - pattern += string(it, end); - break; - } - - // Everything from the start of the input to - // the start of the match. - pattern += string(what[-1].first, what[-1].second); - - // Given " *.{abc,def}", head == "*." and tail == "abc,def". - string const head = string(what[1].first, what[1].second); - string const tail = string(what[2].first, what[2].second); - - // Split the ','-separated chunks of tail so that - // $head{$chunk1,$chunk2} becomes "$head$chunk1 $head$chunk2". - string const fmt = " " + head + "$1"; - pattern += boost::regex_merge(tail, block_re, fmt); - - // Increment the iterator to the end of the match. - it += distance(it, what[0].second); - } - - return pattern; -} - -} // namespace anon - - -namespace lyx { -namespace support { - -FileFilterList::Filter::Filter(docstring const & description, - string const & globs) - : desc_(description) -{ - typedef boost::tokenizer > Tokenizer; - boost::char_separator const separator(" "); - - // Given " ... *.{abc,def} ", expand to - // " ... *.abc *.def " - string const expanded_globs = convert_brace_glob(globs); - - // Split into individual globs. - vector matches; - Tokenizer const tokens(expanded_globs, separator); - globs_ = vector(tokens.begin(), tokens.end()); -} - - -FileFilterList::FileFilterList(docstring const & qt_style_filter) -{ - // FIXME UNICODE - string const filter = to_utf8(qt_style_filter) - + (qt_style_filter.empty() ? string() : ";;") - + to_utf8(_("All Files ")) -#if defined(_WIN32) - + ("(*.*)"); -#else - + ("(*)"); -#endif - - // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)" - // into individual filters. - static boost::regex const separator_re(";;"); - - string::const_iterator it = filter.begin(); - string::const_iterator const end = filter.end(); - while (true) { - boost::match_results what; - - if (!boost::regex_search(it, end, what, separator_re)) { - parse_filter(string(it, end)); - break; - } - - // Everything from the start of the input to - // the start of the match. - parse_filter(string(what[-1].first, what[-1].second)); - - // Increment the iterator to the end of the match. - it += distance(it, what[0].second); - } -} - - -void FileFilterList::parse_filter(string const & filter) -{ - // Matches "TeX documents (*.tex)", - // storing "TeX documents " as group 1 and "*.tex" as group 2. - static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$"); - - boost::match_results what; - if (!boost::regex_search(filter, what, filter_re)) { - // Just a glob, no description. - filters_.push_back(Filter(docstring(), trim(filter))); - } else { - // FIXME UNICODE - docstring const desc = from_utf8(string(what[1].first, what[1].second)); - string const globs = string(what[2].first, what[2].second); - filters_.push_back(Filter(trim(desc), trim(globs))); - } -} - - -docstring const FileFilterList::as_string() const -{ - // FIXME UNICODE - ostringstream ss; - - vector::const_iterator fit = filters_.begin(); - vector::const_iterator const fend = filters_.end(); - for (; fit != fend; ++fit) { - Filter::glob_iterator const gbegin = fit->begin(); - Filter::glob_iterator const gend = fit->end(); - if (gbegin == gend) - continue; - - if (ss.tellp() > 0) - ss << ";;"; - - bool const has_description = !fit->description().empty(); - if (has_description) - ss << to_utf8(fit->description()) << " ("; - - for (Filter::glob_iterator git = gbegin; git != gend; ++git) { - if (git != gbegin) - ss << ' '; - ss << *git; - } - - if (has_description) - ss << ')'; - } - - return from_utf8(ss.str()); -} - -} // namespace support -} // namespace lyx diff --git a/src/support/FileFilterList.h b/src/support/FileFilterList.h deleted file mode 100644 index 449763026e..0000000000 --- a/src/support/FileFilterList.h +++ /dev/null @@ -1,78 +0,0 @@ -// -*- C++ -*- -/** - * \file FileFilterList.h - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - */ - -#ifndef FILE_FILTER_LIST_H -#define FILE_FILTER_LIST_H - -#include "support/docstring.h" - -#include -#include - -namespace lyx { -namespace support { - -/** \c FileFilterList parses a Qt-style list of available file filters - * to generate the corresponding vector. - * For example "TeX documents (*.tex);;LyX Documents (*.lyx)" - * will be parsed to fill a vector of size 2, whilst "*.{p[bgp]m} *.pdf" - * will result in a vector of size 1 in which the description field is empty. - */ -class FileFilterList { -public: - // FIXME UNICODE: globs_ should be unicode... - class Filter { - docstring desc_; - std::vector globs_; - public: - /* \param description text describing the filters. - * \param one or more wildcard patterns, separated by - * whitespace. - */ - Filter(docstring const & description, - std::string const & globs); - - docstring const & description() const { return desc_; } - - typedef std::vector::const_iterator glob_iterator; - glob_iterator begin() const { return globs_.begin(); } - glob_iterator end() const { return globs_.end(); } - }; - - /** \param qt_style_filter a list of available file filters. - * Eg. "TeX documents (*.tex);;LyX Documents (*.lyx)". - * The "All files (*)" filter is always added to the list. - */ - explicit FileFilterList(docstring const & qt_style_filter = - docstring()); - - typedef std::vector::size_type size_type; - - bool empty() const { return filters_.empty(); } - size_type size() const { return filters_.size(); } - Filter & operator[](size_type i) { return filters_[i]; } - Filter const & operator[](size_type i) const { return filters_[i]; } - - /** \returns the equivalent of the string passed to the c-tor - * although any brace expressions are expanded. - * (E.g. "*.{png,jpg}" -> "*.png *.jpg") - */ - docstring const as_string() const; - -private: - void parse_filter(std::string const & filter); - std::vector filters_; -}; - -} // namespace support -} // namespace lyx - -#endif // NOT FILE_FILTER_LIST_H diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 86c5f4d7f2..0408a08a6c 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -55,8 +55,6 @@ liblyxsupport_la_SOURCES = \ environment.h \ environment.cpp \ ExceptionMessage.h \ - FileFilterList.cpp \ - FileFilterList.h \ FileName.cpp \ FileName.h \ FileNameList.h \ -- 2.39.2