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/FileName.h"
21 #include "support/filetools.h"
22 #include "support/gettext.h"
23 #include "support/os.h"
27 /** when this is defined, the code will use
28 * QFileDialog::getOpenFileName and friends to create filedialogs.
30 * - the dialog does not use the quick directory buttons (Button
32 * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
33 * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
34 * file dialog (http://www.lyx.org/trac/ticket/3907).
36 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
38 #if defined(Q_WS_MACX) || (defined(Q_WS_WIN) && !defined(Q_OS_CYGWIN))
39 #define USE_NATIVE_FILEDIALOG 1
42 #ifdef USE_NATIVE_FILEDIALOG
43 #include <QApplication>
48 using namespace support;
51 class FileDialog::Private {
58 FileDialog::FileDialog(QString const & t, FuncCode s)
59 : private_(new FileDialog::Private), title_(t), success_(s)
63 FileDialog::~FileDialog()
69 void FileDialog::setButton1(QString const & label, QString const & dir)
71 private_->b1.first = label;
72 private_->b1.second = dir;
76 void FileDialog::setButton2(QString const & label, QString const & dir)
78 private_->b2.first = label;
79 private_->b2.second = dir;
83 FileDialog::Result FileDialog::save(QString const & path,
84 QStringList const & filters, QString const & suggested,
85 QString * selectedFilter)
87 LYXERR(Debug::GUI, "Select with path \"" << path
88 << "\", mask \"" << filters.join(";;")
89 << "\", suggested \"" << suggested << '"');
91 FileDialog::Result result;
92 result.first = FileDialog::Chosen;
94 #ifdef USE_NATIVE_FILEDIALOG
95 QString const startsWith = makeAbsPath(suggested, path);
97 QFileDialog::getSaveFileName(qApp->focusWidget(),
98 title_, startsWith, filters.join(";;"),
99 selectedFilter, QFileDialog::DontConfirmOverwrite);
101 result.first = FileDialog::Later;
103 result.second = toqstr(os::internal_path(fromqstr(name)));
105 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
106 #if QT_VERSION != 0x040203
107 dlg.setFileMode(QFileDialog::AnyFile);
109 dlg.setAcceptMode(QFileDialog::AcceptSave);
110 dlg.setConfirmOverwrite(false);
112 if (!suggested.isEmpty())
113 dlg.selectFile(suggested);
115 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
116 int res = dlg.exec();
117 LYXERR(Debug::GUI, "result " << res);
118 if (res == QDialog::Accepted)
119 result.second = internalPath(dlg.selectedFiles()[0]);
121 result.first = FileDialog::Later;
122 if (selectedFilter != 0)
123 *selectedFilter = dlg.selectedNameFilter();
130 FileDialog::Result FileDialog::save(QString const & path,
131 QStringList const & filters, QString const & suggested)
133 return save(path, filters, suggested, 0);
137 FileDialog::Result FileDialog::open(QString const & path,
138 QStringList const & filters, QString const & suggested)
140 LYXERR(Debug::GUI, "Select with path \"" << path
141 << "\", mask \"" << filters.join(";;")
142 << "\", suggested \"" << suggested << '"');
143 FileDialog::Result result;
144 result.first = FileDialog::Chosen;
146 #ifdef USE_NATIVE_FILEDIALOG
147 QString const startsWith = makeAbsPath(suggested, path);
148 QString const file = QFileDialog::getOpenFileName(qApp->focusWidget(),
149 title_, startsWith, filters.join(";;"));
151 result.first = FileDialog::Later;
153 result.second = internalPath(file);
155 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
157 if (!suggested.isEmpty())
158 dlg.selectFile(suggested);
160 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
161 int res = dlg.exec();
162 LYXERR(Debug::GUI, "result " << res);
163 if (res == QDialog::Accepted)
164 result.second = internalPath(dlg.selectedFiles()[0]);
166 result.first = FileDialog::Later;
173 FileDialog::Result FileDialog::opendir(QString const & path,
174 QString const & suggested)
176 LYXERR(Debug::GUI, "Select with path \"" << path
177 << "\", suggested \"" << suggested << '"');
178 FileDialog::Result result;
179 result.first = FileDialog::Chosen;
181 #ifdef USE_NATIVE_FILEDIALOG
182 QString const startsWith = toqstr(makeAbsPath(fromqstr(suggested),
183 fromqstr(path)).absFileName());
184 QString const dir = QFileDialog::getExistingDirectory(qApp->focusWidget(),
187 result.first = FileDialog::Later;
189 result.second = toqstr(os::internal_path(fromqstr(dir)));
191 LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")),
192 private_->b1, private_->b2);
194 dlg.setFileMode(QFileDialog::DirectoryOnly);
196 if (!suggested.isEmpty())
197 dlg.selectFile(suggested);
199 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
200 int res = dlg.exec();
201 LYXERR(Debug::GUI, "result " << res);
202 if (res == QDialog::Accepted)
203 result.second = internalPath(dlg.selectedFiles()[0]);
205 result.first = FileDialog::Later;