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(docstring 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(docstring const & path,
72 FileFilterList const & filters,
73 docstring const & suggested)
75 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
76 << "\", mask \"" << lyx::to_utf8(filters.as_string())
77 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
78 FileDialog::Result result;
79 result.first = FileDialog::Chosen;
81 #ifdef USE_NATIVE_FILEDIALOG
82 docstring const startsWith
83 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
84 result.second = qstring_to_ucs4(QFileDialog::getSaveFileName(
86 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
88 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
89 dlg.setFileMode(QFileDialog::AnyFile);
90 dlg.setAcceptMode(QFileDialog::AcceptSave);
92 if (!suggested.empty())
93 dlg.selectFile(toqstr(suggested));
95 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
97 lyxerr[Debug::GUI] << "result " << res << endl;
98 if (res == QDialog::Accepted)
99 result.second = qstring_to_ucs4(dlg.selectedFiles()[0]);
106 FileDialog::Result const FileDialog::open(docstring const & path,
107 FileFilterList const & filters,
108 docstring const & suggested)
110 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
111 << "\", mask \"" << lyx::to_utf8(filters.as_string())
112 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
113 FileDialog::Result result;
114 result.first = FileDialog::Chosen;
116 #ifdef USE_NATIVE_FILEDIALOG
117 docstring const startsWith =
118 lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
119 result.second = qstring_to_ucs4(QFileDialog::getOpenFileName(
121 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
123 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
125 if (!suggested.empty())
126 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);
139 FileDialog::Result const FileDialog::opendir(docstring const & path,
140 docstring const & suggested)
142 lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
143 << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
144 FileDialog::Result result;
145 result.first = FileDialog::Chosen;
147 #ifdef USE_NATIVE_FILEDIALOG
148 docstring const startsWith
149 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
150 result.second = qstring_to_ucs4(QFileDialog::getExistingDirectory(
152 toqstr(title_),toqstr(startsWith) ));
154 FileFilterList const filter(_("Directories"));
156 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
158 dlg.setFileMode(QFileDialog::DirectoryOnly);
160 if (!suggested.empty())
161 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);