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 \"" << fromqstr(path)
84 << "\", mask \"" << fromqstr(filters.join(";;"))
85 << "\", suggested \"" << fromqstr(suggested) << '"');
86 FileDialog::Result result;
87 result.first = FileDialog::Chosen;
89 #ifdef USE_NATIVE_FILEDIALOG
90 QString const startsWith = makeAbsPath(suggested, path);
92 QFileDialog::getSaveFileName(qApp->focusWidget(),
93 title_, startsWith, filters, 0, QFileDialog::DontConfirmOverwrite);
94 result.second = toqstr(internal_path(fromqstr(name)));
96 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
97 #if QT_VERSION != 0x040203
98 dlg.setFileMode(QFileDialog::AnyFile);
100 dlg.setAcceptMode(QFileDialog::AcceptSave);
101 dlg.setConfirmOverwrite(false);
103 if (!suggested.isEmpty())
104 dlg.selectFile(suggested);
106 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
107 int res = dlg.exec();
108 LYXERR(Debug::GUI, "result " << res);
109 if (res == QDialog::Accepted)
110 result.second = internalPath(dlg.selectedFiles()[0]);
117 FileDialog::Result FileDialog::open(QString const & path,
118 QStringList const & filters, QString const & suggested)
120 LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
121 << "\", mask \"" << fromqstr(filters.join(";;"))
122 << "\", suggested \"" << fromqstr(suggested) << '"');
123 FileDialog::Result result;
124 result.first = FileDialog::Chosen;
126 #ifdef USE_NATIVE_FILEDIALOG
127 QString const startsWith = makeAbsPath(suggested, path);
128 result.second = internalPath(
129 QFileDialog::getOpenFileName(qApp->focusWidget(),
130 title_, startsWith, filters));
132 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
134 if (!suggested.isEmpty())
135 dlg.selectFile(suggested);
137 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
138 int res = dlg.exec();
139 LYXERR(Debug::GUI, "result " << res);
140 if (res == QDialog::Accepted)
141 result.second = internalPath(dlg.selectedFiles()[0]);
148 FileDialog::Result FileDialog::opendir(QString const & path,
149 QString const & suggested)
151 LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
152 << "\", suggested \"" << fromqstr(suggested) << '"');
153 FileDialog::Result result;
154 result.first = FileDialog::Chosen;
156 #ifdef USE_NATIVE_FILEDIALOG
157 QString const startsWith = makeAbsPath(suggested, path);
158 result.second = toqstr(internal_path(fromqstr(
159 QFileDialog::getExistingDirectory(qApp->focusWidget(),
160 title_, startsWith))));
162 LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")),
163 private_->b1, private_->b2);
165 dlg.setFileMode(QFileDialog::DirectoryOnly);
167 if (!suggested.isEmpty())
168 dlg.selectFile(suggested);
170 LYXERR(Debug::GUI, "Synchronous FileDialog: ");
171 int res = dlg.exec();
172 LYXERR(Debug::GUI, "result " << res);
173 if (res == QDialog::Accepted)
174 result.second = internalPath(dlg.selectedFiles()[0]);