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, kb_action s)
62 : private_(new FileDialog::Private), title_(t), success_(s)
66 FileDialog::~FileDialog()
72 void FileDialog::setButton1(docstring const & label, docstring const & dir)
74 private_->b1.first = label;
75 private_->b1.second = dir;
79 void FileDialog::setButton2(docstring const & label, docstring const & dir)
81 private_->b2.first = label;
82 private_->b2.second = dir;
86 FileDialog::Result const FileDialog::save(docstring const & path,
87 FileFilterList const & filters,
88 docstring const & suggested)
90 LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
91 << "\", mask \"" << to_utf8(filters.as_string())
92 << "\", suggested \"" << to_utf8(suggested) << '"');
93 FileDialog::Result result;
94 result.first = FileDialog::Chosen;
96 #ifdef USE_NATIVE_FILEDIALOG
97 docstring const startsWith = from_utf8(
98 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
100 QFileDialog::getSaveFileName(qApp->focusWidget(),
103 toqstr(filters.as_string()),
105 QFileDialog::DontConfirmOverwrite);
106 result.second = from_utf8(internal_path(fromqstr(name)));
108 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
109 #if QT_VERSION != 0x040203
110 dlg.setFileMode(QFileDialog::AnyFile);
112 dlg.setAcceptMode(QFileDialog::AcceptSave);
113 dlg.setConfirmOverwrite(false);
115 if (!suggested.empty())
116 dlg.selectFile(toqstr(suggested));
118 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
119 int res = dlg.exec();
120 LYXERR(Debug::GUI, "result " << res);
121 if (res == QDialog::Accepted)
122 result.second = from_utf8(internal_path(
123 fromqstr(dlg.selectedFiles()[0])));
130 FileDialog::Result const FileDialog::open(docstring const & path,
131 FileFilterList const & filters,
132 docstring const & suggested)
134 LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
135 << "\", mask \"" << to_utf8(filters.as_string())
136 << "\", suggested \"" << to_utf8(suggested) << '"');
137 FileDialog::Result result;
138 result.first = FileDialog::Chosen;
140 #ifdef USE_NATIVE_FILEDIALOG
141 docstring const startsWith = from_utf8(
142 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
143 result.second = from_utf8(internal_path(fromqstr(
144 QFileDialog::getOpenFileName(qApp->focusWidget(),
145 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
147 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
149 if (!suggested.empty())
150 dlg.selectFile(toqstr(suggested));
152 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
153 int res = dlg.exec();
154 LYXERR(Debug::GUI, "result " << res);
155 if (res == QDialog::Accepted)
156 result.second = from_utf8(internal_path(
157 fromqstr(dlg.selectedFiles()[0])));
164 FileDialog::Result const FileDialog::opendir(docstring const & path,
165 docstring const & suggested)
167 LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
168 << "\", suggested \"" << to_utf8(suggested) << '"');
169 FileDialog::Result result;
170 result.first = FileDialog::Chosen;
172 #ifdef USE_NATIVE_FILEDIALOG
173 docstring const startsWith = from_utf8(
174 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
175 result.second = from_utf8(internal_path(fromqstr(
176 QFileDialog::getExistingDirectory(qApp->focusWidget(),
177 toqstr(title_),toqstr(startsWith)))));
179 FileFilterList const filter(_("Directories"));
181 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
183 dlg.setFileMode(QFileDialog::DirectoryOnly);
185 if (!suggested.empty())
186 dlg.selectFile(toqstr(suggested));
188 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
189 int res = dlg.exec();
190 LYXERR(Debug::GUI, "result " << res);
191 if (res == QDialog::Accepted)
192 result.second = from_utf8(internal_path(
193 fromqstr(dlg.selectedFiles()[0])));