2 * \file qt4/FileDialog.C
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 "FileDialog_private.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 \"" << lyx::to_utf8(path)
79 << "\", mask \"" << lyx::to_utf8(filters.as_string())
80 << "\", suggested \"" << lyx::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());
87 result.second = lyx::from_utf8(internal_path(fromqstr(
88 QFileDialog::getSaveFileName(qApp->focusWidget(),
89 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
91 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
92 dlg.setFileMode(QFileDialog::AnyFile);
93 dlg.setAcceptMode(QFileDialog::AcceptSave);
95 if (!suggested.empty())
96 dlg.selectFile(toqstr(suggested));
98 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
100 lyxerr[Debug::GUI] << "result " << res << endl;
101 if (res == QDialog::Accepted)
102 result.second = lyx::from_utf8(internal_path(
103 fromqstr(dlg.selectedFiles()[0])));
110 FileDialog::Result const FileDialog::open(docstring const & path,
111 FileFilterList const & filters,
112 docstring const & suggested)
114 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
115 << "\", mask \"" << lyx::to_utf8(filters.as_string())
116 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
117 FileDialog::Result result;
118 result.first = FileDialog::Chosen;
120 #ifdef USE_NATIVE_FILEDIALOG
121 docstring const startsWith = from_utf8(
122 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
123 result.second = lyx::from_utf8(internal_path(fromqstr(
124 QFileDialog::getOpenFileName(qApp->focusWidget(),
125 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
127 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
129 if (!suggested.empty())
130 dlg.selectFile(toqstr(suggested));
132 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
133 int res = dlg.exec();
134 lyxerr[Debug::GUI] << "result " << res << endl;
135 if (res == QDialog::Accepted)
136 result.second = lyx::from_utf8(internal_path(
137 fromqstr(dlg.selectedFiles()[0])));
144 FileDialog::Result const FileDialog::opendir(docstring const & path,
145 docstring const & suggested)
147 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
148 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
149 FileDialog::Result result;
150 result.first = FileDialog::Chosen;
152 #ifdef USE_NATIVE_FILEDIALOG
153 docstring const startsWith = from_utf8(
154 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
155 result.second = lyx::from_utf8(internal_path(fromqstr(
156 QFileDialog::getExistingDirectory(qApp->focusWidget(),
157 toqstr(title_),toqstr(startsWith)))));
159 FileFilterList const filter(_("Directories"));
161 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
163 dlg.setFileMode(QFileDialog::DirectoryOnly);
165 if (!suggested.empty())
166 dlg.selectFile(toqstr(suggested));
168 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
169 int res = dlg.exec();
170 lyxerr[Debug::GUI] << "result " << res << endl;
171 if (res == QDialog::Accepted)
172 result.second = lyx::from_utf8(internal_path(
173 fromqstr(dlg.selectedFiles()[0])));