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"
19 #include "support/debug.h"
20 #include "support/FileFilterList.h"
21 #include "support/FileName.h"
22 #include "support/gettext.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;
53 class FileDialog::Private {
60 FileDialog::FileDialog(docstring const & t, kb_action s)
61 : private_(new FileDialog::Private), title_(t), success_(s)
65 FileDialog::~FileDialog()
71 void FileDialog::setButton1(docstring const & label, docstring const & dir)
73 private_->b1.first = label;
74 private_->b1.second = dir;
78 void FileDialog::setButton2(docstring const & label, docstring const & dir)
80 private_->b2.first = label;
81 private_->b2.second = dir;
85 FileDialog::Result const FileDialog::save(docstring const & path,
86 FileFilterList const & filters,
87 docstring const & suggested)
89 LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
90 << "\", mask \"" << to_utf8(filters.as_string())
91 << "\", suggested \"" << to_utf8(suggested) << '"');
92 FileDialog::Result result;
93 result.first = FileDialog::Chosen;
95 #ifdef USE_NATIVE_FILEDIALOG
96 docstring const startsWith = from_utf8(
97 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
99 QFileDialog::getSaveFileName(qApp->focusWidget(),
102 toqstr(filters.as_string()),
104 QFileDialog::DontConfirmOverwrite);
105 result.second = from_utf8(internal_path(fromqstr(name)));
107 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
108 #if QT_VERSION != 0x040203
109 dlg.setFileMode(QFileDialog::AnyFile);
111 dlg.setAcceptMode(QFileDialog::AcceptSave);
112 dlg.setConfirmOverwrite(false);
114 if (!suggested.empty())
115 dlg.selectFile(toqstr(suggested));
117 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
118 int res = dlg.exec();
119 LYXERR(Debug::GUI, "result " << res);
120 if (res == QDialog::Accepted)
121 result.second = from_utf8(internal_path(
122 fromqstr(dlg.selectedFiles()[0])));
129 FileDialog::Result const FileDialog::open(docstring const & path,
130 FileFilterList const & filters,
131 docstring const & suggested)
133 LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
134 << "\", mask \"" << to_utf8(filters.as_string())
135 << "\", suggested \"" << to_utf8(suggested) << '"');
136 FileDialog::Result result;
137 result.first = FileDialog::Chosen;
139 #ifdef USE_NATIVE_FILEDIALOG
140 docstring const startsWith = from_utf8(
141 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
142 result.second = from_utf8(internal_path(fromqstr(
143 QFileDialog::getOpenFileName(qApp->focusWidget(),
144 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
146 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
148 if (!suggested.empty())
149 dlg.selectFile(toqstr(suggested));
151 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
152 int res = dlg.exec();
153 LYXERR(Debug::GUI, "result " << res);
154 if (res == QDialog::Accepted)
155 result.second = from_utf8(internal_path(
156 fromqstr(dlg.selectedFiles()[0])));
163 FileDialog::Result const FileDialog::opendir(docstring const & path,
164 docstring const & suggested)
166 LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
167 << "\", suggested \"" << to_utf8(suggested) << '"');
168 FileDialog::Result result;
169 result.first = FileDialog::Chosen;
171 #ifdef USE_NATIVE_FILEDIALOG
172 docstring const startsWith = from_utf8(
173 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
174 result.second = from_utf8(internal_path(fromqstr(
175 QFileDialog::getExistingDirectory(qApp->focusWidget(),
176 toqstr(title_),toqstr(startsWith)))));
178 FileFilterList const filter(_("Directories"));
180 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
182 dlg.setFileMode(QFileDialog::DirectoryOnly);
184 if (!suggested.empty())
185 dlg.selectFile(toqstr(suggested));
187 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
188 int res = dlg.exec();
189 LYXERR(Debug::GUI, "result " << res);
190 if (res == QDialog::Accepted)
191 result.second = from_utf8(internal_path(
192 fromqstr(dlg.selectedFiles()[0])));