2 * \file qt4/FileDialog.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
14 #include "frontends/FileDialog.h"
16 #include "FileDialog_private.h"
17 #include "qt_helpers.h"
22 #include "support/filefilterlist.h"
24 /** when this is defined, the code will use
25 * QFileDialog::getOpenFileName and friends to create filedialogs.
27 * - the dialog does not use the quick directory buttons (Button
29 * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
31 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
34 #define USE_NATIVE_FILEDIALOG 1
37 #ifdef USE_NATIVE_FILEDIALOG
38 #include <qapplication.h>
39 #include "support/filetools.h"
41 using lyx::support::makeAbsPath;
46 using support::FileFilterList;
50 class FileDialog::Private {
57 FileDialog::FileDialog(docstring const & t,
58 kb_action s, Button b1, Button b2)
59 : private_(new FileDialog::Private), title_(t), success_(s)
66 FileDialog::~FileDialog()
72 FileDialog::Result const FileDialog::save(docstring const & path,
73 FileFilterList const & filters,
74 docstring const & suggested)
76 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
77 << "\", mask \"" << lyx::to_utf8(filters.as_string())
78 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
79 FileDialog::Result result;
80 result.first = FileDialog::Chosen;
82 #ifdef USE_NATIVE_FILEDIALOG
83 docstring const startsWith
84 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
85 result.second = qstring_to_ucs4(QFileDialog::getSaveFileName(
87 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
89 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
90 dlg.setFileMode(QFileDialog::AnyFile);
91 dlg.setAcceptMode(QFileDialog::AcceptSave);
93 if (!suggested.empty())
94 dlg.selectFile(toqstr(suggested));
96 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
98 lyxerr[Debug::GUI] << "result " << res << endl;
99 if (res == QDialog::Accepted)
100 result.second = qstring_to_ucs4(dlg.selectedFiles()[0]);
107 FileDialog::Result const FileDialog::open(docstring const & path,
108 FileFilterList const & filters,
109 docstring const & suggested)
111 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
112 << "\", mask \"" << lyx::to_utf8(filters.as_string())
113 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
114 FileDialog::Result result;
115 result.first = FileDialog::Chosen;
117 #ifdef USE_NATIVE_FILEDIALOG
118 docstring const startsWith =
119 lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
120 result.second = qstring_to_ucs4(QFileDialog::getOpenFileName(
122 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
124 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
126 if (!suggested.empty())
127 dlg.selectFile(toqstr(suggested));
129 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
130 int res = dlg.exec();
131 lyxerr[Debug::GUI] << "result " << res << endl;
132 if (res == QDialog::Accepted)
133 result.second = qstring_to_ucs4(dlg.selectedFiles()[0]);
140 FileDialog::Result const FileDialog::opendir(docstring const & path,
141 docstring const & suggested)
143 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
144 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
145 FileDialog::Result result;
146 result.first = FileDialog::Chosen;
148 #ifdef USE_NATIVE_FILEDIALOG
149 docstring const startsWith
150 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
151 result.second = qstring_to_ucs4(QFileDialog::getExistingDirectory(
153 toqstr(title_),toqstr(startsWith) ));
155 FileFilterList const filter(_("Directories"));
157 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
159 dlg.setFileMode(QFileDialog::DirectoryOnly);
161 if (!suggested.empty())
162 dlg.selectFile(toqstr(suggested));
164 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
165 int res = dlg.exec();
166 lyxerr[Debug::GUI] << "result " << res << endl;
167 if (res == QDialog::Accepted)
168 result.second = qstring_to_ucs4(dlg.selectedFiles()[0]);