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 "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(QString const & t, kb_action s)
61 : private_(new FileDialog::Private), title_(t), success_(s)
65 FileDialog::~FileDialog()
71 void FileDialog::setButton1(QString const & label, QString const & dir)
73 private_->b1.first = label;
74 private_->b1.second = dir;
78 void FileDialog::setButton2(QString const & label, QString const & dir)
80 private_->b2.first = label;
81 private_->b2.second = dir;
85 FileDialog::Result FileDialog::save(QString const & path,
86 FileFilterList const & filters,
87 QString const & suggested)
89 LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
90 << "\", mask \"" << to_utf8(filters.as_string())
91 << "\", suggested \"" << fromqstr(suggested) << '"');
92 FileDialog::Result result;
93 result.first = FileDialog::Chosen;
95 #ifdef USE_NATIVE_FILEDIALOG
96 QString const startsWith = toqstr(
97 makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename());
99 QFileDialog::getSaveFileName(qApp->focusWidget(),
102 toqstr(filters.as_string()),
104 QFileDialog::DontConfirmOverwrite);
105 result.second = toqstr(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.isEmpty())
115 dlg.selectFile(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 = internalPath(dlg.selectedFiles()[0]);
128 FileDialog::Result FileDialog::open(QString const & path,
129 FileFilterList const & filters,
130 QString const & suggested)
132 LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
133 << "\", mask \"" << filters.as_string()
134 << "\", suggested \"" << fromqstr(suggested) << '"');
135 FileDialog::Result result;
136 result.first = FileDialog::Chosen;
138 #ifdef USE_NATIVE_FILEDIALOG
139 QString const startsWith = toqstr(
140 makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename());
141 result.second = internalPath(
142 QFileDialog::getOpenFileName(qApp->focusWidget(),
143 title_, startsWith, toqstr(filters.as_string()) ));
145 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
147 if (!suggested.isEmpty())
148 dlg.selectFile(suggested);
150 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
151 int res = dlg.exec();
152 LYXERR(Debug::GUI, "result " << res);
153 if (res == QDialog::Accepted)
154 result.second = internalPath(dlg.selectedFiles()[0]);
161 FileDialog::Result FileDialog::opendir(QString const & path,
162 QString const & suggested)
164 LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
165 << "\", suggested \"" << fromqstr(suggested) << '"');
166 FileDialog::Result result;
167 result.first = FileDialog::Chosen;
169 #ifdef USE_NATIVE_FILEDIALOG
170 QString const startsWith = toqstr(
171 makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename());
172 result.second = toqstr(internal_path(fromqstr(
173 QFileDialog::getExistingDirectory(qApp->focusWidget(),
174 title_, startsWith))));
176 FileFilterList const filter(_("Directories"));
178 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
180 dlg.setFileMode(QFileDialog::DirectoryOnly);
182 if (!suggested.isEmpty())
183 dlg.selectFile(suggested);
185 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
186 int res = dlg.exec();
187 LYXERR(Debug::GUI, "result " << res);
188 if (res == QDialog::Accepted)
189 result.second = internalPath(dlg.selectedFiles()[0]);