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 "frontends/FileDialog.h"
16 #include "LyXFileDialog.h"
17 #include "qt_helpers.h"
22 #include "support/FileFilterList.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.
32 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
35 #define USE_NATIVE_FILEDIALOG 1
38 #ifdef USE_NATIVE_FILEDIALOG
39 #include <qapplication.h>
40 #include "support/filetools.h"
42 using lyx::support::makeAbsPath;
47 using support::FileFilterList;
48 using support::os::internal_path;
52 class FileDialog::Private {
59 FileDialog::FileDialog(docstring const & t,
60 kb_action s, Button b1, Button b2)
61 : private_(new FileDialog::Private), title_(t), success_(s)
68 FileDialog::~FileDialog()
74 FileDialog::Result const FileDialog::save(docstring const & path,
75 FileFilterList const & filters,
76 docstring const & suggested)
78 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
79 << "\", mask \"" << to_utf8(filters.as_string())
80 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
81 FileDialog::Result result;
82 result.first = FileDialog::Chosen;
84 #ifdef USE_NATIVE_FILEDIALOG
85 docstring const startsWith = from_utf8(
86 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
88 QFileDialog::getSaveFileName(qApp->focusWidget(),
91 toqstr(filters.as_string()),
93 QFileDialog::DontConfirmOverwrite);
94 result.second = from_utf8(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.empty())
104 dlg.selectFile(toqstr(suggested));
106 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
107 int res = dlg.exec();
108 LYXERR(Debug::GUI) << "result " << res << endl;
109 if (res == QDialog::Accepted)
110 result.second = from_utf8(internal_path(
111 fromqstr(dlg.selectedFiles()[0])));
118 FileDialog::Result const FileDialog::open(docstring const & path,
119 FileFilterList const & filters,
120 docstring const & suggested)
122 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
123 << "\", mask \"" << to_utf8(filters.as_string())
124 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
125 FileDialog::Result result;
126 result.first = FileDialog::Chosen;
128 #ifdef USE_NATIVE_FILEDIALOG
129 docstring const startsWith = from_utf8(
130 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
131 result.second = from_utf8(internal_path(fromqstr(
132 QFileDialog::getOpenFileName(qApp->focusWidget(),
133 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
135 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
137 if (!suggested.empty())
138 dlg.selectFile(toqstr(suggested));
140 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
141 int res = dlg.exec();
142 LYXERR(Debug::GUI) << "result " << res << endl;
143 if (res == QDialog::Accepted)
144 result.second = from_utf8(internal_path(
145 fromqstr(dlg.selectedFiles()[0])));
152 FileDialog::Result const FileDialog::opendir(docstring const & path,
153 docstring const & suggested)
155 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
156 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
157 FileDialog::Result result;
158 result.first = FileDialog::Chosen;
160 #ifdef USE_NATIVE_FILEDIALOG
161 docstring const startsWith = from_utf8(
162 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
163 result.second = from_utf8(internal_path(fromqstr(
164 QFileDialog::getExistingDirectory(qApp->focusWidget(),
165 toqstr(title_),toqstr(startsWith)))));
167 FileFilterList const filter(_("Directories"));
169 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
171 dlg.setFileMode(QFileDialog::DirectoryOnly);
173 if (!suggested.empty())
174 dlg.selectFile(toqstr(suggested));
176 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
177 int res = dlg.exec();
178 LYXERR(Debug::GUI) << "result " << res << endl;
179 if (res == QDialog::Accepted)
180 result.second = from_utf8(internal_path(
181 fromqstr(dlg.selectedFiles()[0])));