2 * \file qt/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"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25 #include "support/os.h"
29 #include <QApplication>
31 /** when LyXRC::use_native_filedialog is true, we use
32 * QFileDialog::getOpenFileName and friends to create filedialogs.
34 * - the dialog does not use the quick directory buttons (Button
36 * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
37 * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
38 * file dialog (http://www.lyx.org/trac/ticket/3907).
40 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
45 using namespace support;
48 class FileDialog::Private {
55 FileDialog::FileDialog(QString const & t)
56 : private_(new FileDialog::Private), title_(t)
60 FileDialog::~FileDialog()
66 void FileDialog::setButton1(QString const & label, QString const & dir)
68 private_->b1.first = label;
69 private_->b1.second = dir;
73 void FileDialog::setButton2(QString const & label, QString const & dir)
75 private_->b2.first = label;
76 private_->b2.second = dir;
80 FileDialog::Result FileDialog::save(QString const & path,
81 QStringList const & filters, QString const & suggested,
82 QString * selectedFilter)
84 LYXERR(Debug::GUI, "Select with path \"" << path
85 << "\", mask \"" << filters.join(";;")
86 << "\", suggested \"" << suggested << '"');
88 FileDialog::Result result;
89 result.first = FileDialog::Chosen;
91 if (lyxrc.use_native_filedialog) {
92 QString const startsWith = makeAbsPath(suggested, path);
94 QFileDialog::getSaveFileName(qApp->focusWidget(),
95 title_, startsWith, filters.join(";;"),
96 selectedFilter, QFileDialog::DontConfirmOverwrite);
98 result.first = FileDialog::Later;
100 result.second = toqstr(os::internal_path(fromqstr(name)));
102 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
103 dlg.setFileMode(QFileDialog::AnyFile);
104 dlg.setAcceptMode(QFileDialog::AcceptSave);
105 dlg.setOption(QFileDialog::DontConfirmOverwrite, true);
106 if (selectedFilter != 0 && !selectedFilter->isEmpty())
107 dlg.selectNameFilter(*selectedFilter);
109 if (!suggested.isEmpty())
110 dlg.selectFile(suggested);
112 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
113 int res = dlg.exec();
114 LYXERR(Debug::GUI, "result " << res);
115 if (res == QDialog::Accepted)
116 result.second = internalPath(dlg.selectedFiles()[0]);
118 result.first = FileDialog::Later;
119 if (selectedFilter != 0)
120 *selectedFilter = dlg.selectedNameFilter();
127 FileDialog::Result FileDialog::save(QString const & path,
128 QStringList const & filters, QString const & suggested)
130 return save(path, filters, suggested, 0);
134 FileDialog::Result FileDialog::open(QString const & path,
135 QStringList const & filters, QString const & suggested)
137 FileDialog::Result result;
138 FileDialog::Results results = openMulti(path, filters, suggested, false);
139 result.first = results.first;
140 result.second = results.second.at(0);
145 FileDialog::Results FileDialog::openMulti(QString const & path,
146 QStringList const & filters, QString const & suggested, bool multi)
148 LYXERR(Debug::GUI, "Select with path \"" << path
149 << "\", mask \"" << filters.join(";;")
150 << "\", suggested \"" << suggested << '"');
151 FileDialog::Results results;
152 results.first = FileDialog::Chosen;
154 if (lyxrc.use_native_filedialog) {
155 QString const startsWith = makeAbsPath(suggested, path);
158 files = QFileDialog::getOpenFileNames(qApp->focusWidget(),
159 title_, startsWith, filters.join(";;"));
161 files << QFileDialog::getOpenFileName(qApp->focusWidget(),
162 title_, startsWith, filters.join(";;"));
164 results.first = FileDialog::Later;
166 for (const auto& file : files)
167 results.second << internalPath(file);
170 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
172 if (!suggested.isEmpty())
173 dlg.selectFile(suggested);
175 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
176 int res = dlg.exec();
177 LYXERR(Debug::GUI, "result " << res);
178 if (res == QDialog::Accepted)
179 results.second << internalPath(dlg.selectedFiles()[0]);
181 results.first = FileDialog::Later;
188 FileDialog::Result FileDialog::opendir(QString const & path,
189 QString const & suggested)
191 LYXERR(Debug::GUI, "Select with path \"" << path
192 << "\", suggested \"" << suggested << '"');
193 FileDialog::Result result;
194 result.first = FileDialog::Chosen;
196 if (lyxrc.use_native_filedialog) {
197 QString const startsWith =
198 toqstr(makeAbsPath(fromqstr(suggested), fromqstr(path)).absFileName());
200 QFileDialog::getExistingDirectory(qApp->focusWidget(), title_, startsWith);
202 result.first = FileDialog::Later;
204 result.second = toqstr(os::internal_path(fromqstr(dir)));
206 LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")),
207 private_->b1, private_->b2);
209 dlg.setFileMode(QFileDialog::Directory);
210 dlg.setOption(QFileDialog::ShowDirsOnly, true);
212 if (!suggested.isEmpty())
213 dlg.selectFile(suggested);
215 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
216 int res = dlg.exec();
217 LYXERR(Debug::GUI, "result " << res);
218 if (res == QDialog::Accepted)
219 result.second = internalPath(dlg.selectedFiles()[0]);
221 result.first = FileDialog::Later;