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 class FileDialog::Private {
56 FileDialog::FileDialog(string const & t,
57 kb_action s, Button b1, Button b2)
58 : private_(new FileDialog::Private), title_(t), success_(s)
65 FileDialog::~FileDialog()
71 FileDialog::Result const FileDialog::save(string const & path,
72 FileFilterList const & filters,
73 string const & suggested)
75 lyxerr[Debug::GUI] << "Select with path \"" << path
76 << "\", mask \"" << filters.as_string()
77 << "\", suggested \"" << suggested << '"' << endl;
78 FileDialog::Result result;
79 result.first = FileDialog::Chosen;
81 #ifdef USE_NATIVE_FILEDIALOG
82 string const startsWith = makeAbsPath(suggested, path);
83 result.second = fromqstr(
84 Q3FileDialog::getSaveFileName(toqstr(startsWith),
85 toqstr(filters.as_string()),
86 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
89 LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
90 dlg.setMode(Q3FileDialog::AnyFile);
92 if (!suggested.empty())
93 dlg.setSelection(toqstr(suggested));
95 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
97 lyxerr[Debug::GUI] << "result " << res << endl;
98 if (res == QDialog::Accepted)
99 result.second = fromqstr(dlg.selectedFile());
106 FileDialog::Result const FileDialog::open(string const & path,
107 FileFilterList const & filters,
108 string const & suggested)
110 lyxerr[Debug::GUI] << "Select with path \"" << path
111 << "\", mask \"" << filters.as_string()
112 << "\", suggested \"" << suggested << '"' << endl;
113 FileDialog::Result result;
114 result.first = FileDialog::Chosen;
116 #ifdef USE_NATIVE_FILEDIALOG
117 string const startsWith = makeAbsPath(suggested, path);
118 result.second = fromqstr(
119 Q3FileDialog::getOpenFileName(toqstr(startsWith),
120 toqstr(filters.as_string()),
121 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
124 LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
126 if (!suggested.empty())
127 dlg.setSelection(toqstr(suggested));
129 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
130 int res = dlg.exec();
131 lyxerr[Debug::GUI] << "result " << res << endl;
132 if (res == QDialog::Accepted)
133 result.second = fromqstr(dlg.selectedFile());
140 FileDialog::Result const FileDialog::opendir(string const & path,
141 string const & suggested)
143 lyxerr[Debug::GUI] << "Select with path \"" << path
144 << "\", suggested \"" << suggested << '"' << endl;
145 FileDialog::Result result;
146 result.first = FileDialog::Chosen;
148 #ifdef USE_NATIVE_FILEDIALOG
149 string const startsWith = makeAbsPath(suggested, path);
150 result.second = fromqstr(
151 Q3FileDialog::getExistingDirectory(toqstr(startsWith),
152 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
155 FileFilterList const filter(_("Directories"));
157 LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2);
159 dlg.setMode(Q3FileDialog::DirectoryOnly);
161 if (!suggested.empty())
162 dlg.setSelection(toqstr(suggested));
164 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
165 int res = dlg.exec();
166 lyxerr[Debug::GUI] << "result " << res << endl;
167 if (res == QDialog::Accepted)
168 result.second = fromqstr(dlg.selectedFile());