2 * \file qt2/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"
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.
31 * Therefore there is a tradeoff in enabling or disabling this (JMarc)
34 #define USE_NATIVE_FILEDIALOG 1
37 #ifdef USE_NATIVE_FILEDIALOG
38 #include <qapplication.h>
39 #include "support/filetools.h"
40 using lyx::support::MakeAbsPath;
43 using lyx::support::FileFilterList;
49 struct FileDialog::Private {
55 FileDialog::FileDialog(string const & t,
56 kb_action s, Button b1, Button b2)
57 : private_(new FileDialog::Private), title_(t), success_(s)
64 FileDialog::~FileDialog()
70 FileDialog::Result const FileDialog::save(string const & path,
71 FileFilterList const & filters,
72 string const & suggested)
74 lyxerr[Debug::GUI] << "Select with path \"" << path
75 << "\", mask \"" << filters.as_string()
76 << "\", suggested \"" << suggested << '"' << endl;
77 FileDialog::Result result;
78 result.first = FileDialog::Chosen;
80 #ifdef USE_NATIVE_FILEDIALOG
81 string const startsWith = MakeAbsPath(suggested, path);
82 result.second = fromqstr(
83 QFileDialog::getSaveFileName(toqstr(startsWith),
84 toqstr(filters.as_string()),
85 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
88 LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
89 dlg.setMode(QFileDialog::AnyFile);
91 if (!suggested.empty())
92 dlg.setSelection(toqstr(suggested));
94 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
96 lyxerr[Debug::GUI] << "result " << res << endl;
97 if (res == QDialog::Accepted)
98 result.second = fromqstr(dlg.selectedFile());
105 FileDialog::Result const FileDialog::open(string const & path,
106 FileFilterList const & filters,
107 string const & suggested)
109 lyxerr[Debug::GUI] << "Select with path \"" << path
110 << "\", mask \"" << filters.as_string()
111 << "\", suggested \"" << suggested << '"' << endl;
112 FileDialog::Result result;
113 result.first = FileDialog::Chosen;
115 #ifdef USE_NATIVE_FILEDIALOG
116 string const startsWith = MakeAbsPath(suggested, path);
117 result.second = fromqstr(
118 QFileDialog::getOpenFileName(toqstr(startsWith),
119 toqstr(filters.as_string()),
120 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
123 LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
125 if (!suggested.empty())
126 dlg.setSelection(toqstr(suggested));
128 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
129 int res = dlg.exec();
130 lyxerr[Debug::GUI] << "result " << res << endl;
131 if (res == QDialog::Accepted)
132 result.second = fromqstr(dlg.selectedFile());
139 FileDialog::Result const FileDialog::opendir(string const & path,
140 string const & suggested)
142 lyxerr[Debug::GUI] << "Select with path \"" << path
143 << "\", suggested \"" << suggested << '"' << endl;
144 FileDialog::Result result;
145 result.first = FileDialog::Chosen;
147 #ifdef USE_NATIVE_FILEDIALOG
148 string const startsWith = MakeAbsPath(suggested, path);
149 result.second = fromqstr(
150 QFileDialog::getExistingDirectory(toqstr(startsWith),
151 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
154 FileFilterList const filter(_("Directories"));
156 LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2);
158 dlg.setMode(QFileDialog::DirectoryOnly);
160 if (!suggested.empty())
161 dlg.setSelection(toqstr(suggested));
163 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
164 int res = dlg.exec();
165 lyxerr[Debug::GUI] << "result " << res << endl;
166 if (res == QDialog::Accepted)
167 result.second = fromqstr(dlg.selectedFile());