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/gettext.h"
22 #include "support/os.h"
24 /** when this is defined, the code will use
25 * QFileDialog::getOpenFileName and friends to create filedialogs.
27 * - the dialog does not use the quick directory buttons (Button
29 * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
30 * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
31 * file dialog (http://bugzilla.lyx.org/show_bug.cgi?id=3907).
33 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
35 #if defined(Q_WS_MACX) || (defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN))
36 #define USE_NATIVE_FILEDIALOG 1
39 #ifdef USE_NATIVE_FILEDIALOG
40 #include <QApplication>
45 using support::os::internal_path;
48 class FileDialog::Private {
55 FileDialog::FileDialog(QString const & t, FuncCode s)
56 : private_(new FileDialog::Private), title_(t), success_(s)
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)
83 LYXERR(Debug::GUI, "Select with path \"" << path
84 << "\", mask \"" << filters.join(";;")
85 << "\", suggested \"" << suggested << '"');
87 FileDialog::Result result;
88 result.first = FileDialog::Chosen;
90 #ifdef USE_NATIVE_FILEDIALOG
91 QString const startsWith = makeAbsPath(suggested, path);
93 QFileDialog::getSaveFileName(qApp->focusWidget(),
94 title_, startsWith, filters, 0, QFileDialog::DontConfirmOverwrite);
95 result.second = toqstr(internal_path(fromqstr(name)));
97 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
98 #if QT_VERSION != 0x040203
99 dlg.setFileMode(QFileDialog::AnyFile);
101 dlg.setAcceptMode(QFileDialog::AcceptSave);
102 dlg.setConfirmOverwrite(false);
104 if (!suggested.isEmpty())
105 dlg.selectFile(suggested);
107 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
108 int res = dlg.exec();
109 LYXERR(Debug::GUI, "result " << res);
110 if (res == QDialog::Accepted)
111 result.second = internalPath(dlg.selectedFiles()[0]);
118 FileDialog::Result FileDialog::open(QString const & path,
119 QStringList const & filters, QString const & suggested)
121 LYXERR(Debug::GUI, "Select with path \"" << path
122 << "\", mask \"" << filters.join(";;")
123 << "\", suggested \"" << suggested << '"');
124 FileDialog::Result result;
125 result.first = FileDialog::Chosen;
127 #ifdef USE_NATIVE_FILEDIALOG
128 QString const startsWith = makeAbsPath(suggested, path);
129 result.second = internalPath(
130 QFileDialog::getOpenFileName(qApp->focusWidget(),
131 title_, startsWith, filters));
133 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
135 if (!suggested.isEmpty())
136 dlg.selectFile(suggested);
138 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
139 int res = dlg.exec();
140 LYXERR(Debug::GUI, "result " << res);
141 if (res == QDialog::Accepted)
142 result.second = internalPath(dlg.selectedFiles()[0]);
149 FileDialog::Result FileDialog::opendir(QString const & path,
150 QString const & suggested)
152 LYXERR(Debug::GUI, "Select with path \"" << path
153 << "\", suggested \"" << suggested << '"');
154 FileDialog::Result result;
155 result.first = FileDialog::Chosen;
157 #ifdef USE_NATIVE_FILEDIALOG
158 QString const startsWith = makeAbsPath(suggested, path);
159 result.second = toqstr(internal_path(fromqstr(
160 QFileDialog::getExistingDirectory(qApp->focusWidget(),
161 title_, startsWith))));
163 LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")),
164 private_->b1, private_->b2);
166 dlg.setFileMode(QFileDialog::DirectoryOnly);
168 if (!suggested.isEmpty())
169 dlg.selectFile(suggested);
171 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
172 int res = dlg.exec();
173 LYXERR(Debug::GUI, "result " << res);
174 if (res == QDialog::Accepted)
175 result.second = internalPath(dlg.selectedFiles()[0]);