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"
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(QFileDialog::getSaveFileName(
84 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
85 title_.c_str(), toqstr(startsWith), toqstr(filters.as_string()) ));
87 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
88 dlg.setFileMode(QFileDialog::AnyFile);
89 dlg.setAcceptMode(QFileDialog::AcceptSave);
91 if (!suggested.empty())
92 dlg.selectFile(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(QFileDialog::getOpenFileName(
118 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
119 title_.c_str(), toqstr(startsWith), toqstr(filters.as_string()) ));
121 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
123 if (!suggested.empty())
124 dlg.selectFile(toqstr(suggested));
126 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
127 int res = dlg.exec();
128 lyxerr[Debug::GUI] << "result " << res << endl;
129 if (res == QDialog::Accepted)
130 result.second = fromqstr(dlg.selectedFile());
137 FileDialog::Result const FileDialog::opendir(string const & path,
138 string const & suggested)
140 lyxerr[Debug::GUI] << "Select with path \"" << path
141 << "\", suggested \"" << suggested << '"' << endl;
142 FileDialog::Result result;
143 result.first = FileDialog::Chosen;
145 #ifdef USE_NATIVE_FILEDIALOG
146 string const startsWith = makeAbsPath(suggested, path);
147 result.second = fromqstr(QFileDialog::getExistingDirectory(
148 qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
149 title_.c_str(),toqstr(startsWith) ));
151 FileFilterList const filter(_("Directories"));
153 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
155 dlg.setFileMode(QFileDialog::DirectoryOnly);
157 if (!suggested.empty())
158 dlg.selectFile(toqstr(suggested));
160 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
161 int res = dlg.exec();
162 lyxerr[Debug::GUI] << "result " << res << endl;
163 if (res == QDialog::Accepted)
164 result.second = fromqstr(dlg.selectedFile());