2 * \file qt4/FileDialog.cpp
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 "LyXFileDialog.h"
17 #include "qt_helpers.h"
22 #include "support/FileFilterList.h"
23 #include "support/os.h"
25 /** when this is defined, the code will use
26 * QFileDialog::getOpenFileName and friends to create filedialogs.
28 * - the dialog does not use the quick directory buttons (Button
30 * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
31 * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
32 * file dialog (http://bugzilla.lyx.org/show_bug.cgi?id=3907).
34 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
36 #if defined(Q_WS_MACX) || (defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN))
37 #define USE_NATIVE_FILEDIALOG 1
40 #ifdef USE_NATIVE_FILEDIALOG
41 #include <QApplication>
42 #include "support/filetools.h"
44 using lyx::support::makeAbsPath;
49 using support::FileFilterList;
50 using support::os::internal_path;
54 class FileDialog::Private {
61 FileDialog::FileDialog(docstring const & t,
62 kb_action s, Button b1, Button b2)
63 : private_(new FileDialog::Private), title_(t), success_(s)
70 FileDialog::~FileDialog()
76 FileDialog::Result const FileDialog::save(docstring const & path,
77 FileFilterList const & filters,
78 docstring const & suggested)
80 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
81 << "\", mask \"" << to_utf8(filters.as_string())
82 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
83 FileDialog::Result result;
84 result.first = FileDialog::Chosen;
86 #ifdef USE_NATIVE_FILEDIALOG
87 docstring const startsWith = from_utf8(
88 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
90 QFileDialog::getSaveFileName(qApp->focusWidget(),
93 toqstr(filters.as_string()),
95 QFileDialog::DontConfirmOverwrite);
96 result.second = from_utf8(internal_path(fromqstr(name)));
98 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
99 #if QT_VERSION != 0x040203
100 dlg.setFileMode(QFileDialog::AnyFile);
102 dlg.setAcceptMode(QFileDialog::AcceptSave);
103 dlg.setConfirmOverwrite(false);
105 if (!suggested.empty())
106 dlg.selectFile(toqstr(suggested));
108 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
109 int res = dlg.exec();
110 LYXERR(Debug::GUI) << "result " << res << endl;
111 if (res == QDialog::Accepted)
112 result.second = from_utf8(internal_path(
113 fromqstr(dlg.selectedFiles()[0])));
120 FileDialog::Result const FileDialog::open(docstring const & path,
121 FileFilterList const & filters,
122 docstring const & suggested)
124 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
125 << "\", mask \"" << to_utf8(filters.as_string())
126 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
127 FileDialog::Result result;
128 result.first = FileDialog::Chosen;
130 #ifdef USE_NATIVE_FILEDIALOG
131 docstring const startsWith = from_utf8(
132 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
133 result.second = from_utf8(internal_path(fromqstr(
134 QFileDialog::getOpenFileName(qApp->focusWidget(),
135 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
137 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
139 if (!suggested.empty())
140 dlg.selectFile(toqstr(suggested));
142 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
143 int res = dlg.exec();
144 LYXERR(Debug::GUI) << "result " << res << endl;
145 if (res == QDialog::Accepted)
146 result.second = from_utf8(internal_path(
147 fromqstr(dlg.selectedFiles()[0])));
154 FileDialog::Result const FileDialog::opendir(docstring const & path,
155 docstring const & suggested)
157 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
158 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
159 FileDialog::Result result;
160 result.first = FileDialog::Chosen;
162 #ifdef USE_NATIVE_FILEDIALOG
163 docstring const startsWith = from_utf8(
164 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
165 result.second = from_utf8(internal_path(fromqstr(
166 QFileDialog::getExistingDirectory(qApp->focusWidget(),
167 toqstr(title_),toqstr(startsWith)))));
169 FileFilterList const filter(_("Directories"));
171 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
173 dlg.setFileMode(QFileDialog::DirectoryOnly);
175 if (!suggested.empty())
176 dlg.selectFile(toqstr(suggested));
178 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
179 int res = dlg.exec();
180 LYXERR(Debug::GUI) << "result " << res << endl;
181 if (res == QDialog::Accepted)
182 result.second = from_utf8(internal_path(
183 fromqstr(dlg.selectedFiles()[0])));