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 \"" << to_utf8(path)
79 << "\", mask \"" << to_utf8(filters.as_string())
80 << "\", suggested \"" << 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 = 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);
94 dlg.setConfirmOverwrite(false);
96 if (!suggested.empty())
97 dlg.selectFile(toqstr(suggested));
99 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
100 int res = dlg.exec();
101 lyxerr[Debug::GUI] << "result " << res << endl;
102 if (res == QDialog::Accepted)
103 result.second = from_utf8(internal_path(
104 fromqstr(dlg.selectedFiles()[0])));
111 FileDialog::Result const FileDialog::open(docstring const & path,
112 FileFilterList const & filters,
113 docstring const & suggested)
115 lyxerr[Debug::GUI] << "Select with path \"" << to_utf8(path)
116 << "\", mask \"" << to_utf8(filters.as_string())
117 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
118 FileDialog::Result result;
119 result.first = FileDialog::Chosen;
121 #ifdef USE_NATIVE_FILEDIALOG
122 docstring const startsWith = from_utf8(
123 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
124 result.second = from_utf8(internal_path(fromqstr(
125 QFileDialog::getOpenFileName(qApp->focusWidget(),
126 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
128 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
130 if (!suggested.empty())
131 dlg.selectFile(toqstr(suggested));
133 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
134 int res = dlg.exec();
135 lyxerr[Debug::GUI] << "result " << res << endl;
136 if (res == QDialog::Accepted)
137 result.second = from_utf8(internal_path(
138 fromqstr(dlg.selectedFiles()[0])));
145 FileDialog::Result const FileDialog::opendir(docstring const & path,
146 docstring const & suggested)
148 lyxerr[Debug::GUI] << "Select with path \"" << to_utf8(path)
149 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
150 FileDialog::Result result;
151 result.first = FileDialog::Chosen;
153 #ifdef USE_NATIVE_FILEDIALOG
154 docstring const startsWith = from_utf8(
155 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
156 result.second = from_utf8(internal_path(fromqstr(
157 QFileDialog::getExistingDirectory(qApp->focusWidget(),
158 toqstr(title_),toqstr(startsWith)))));
160 FileFilterList const filter(_("Directories"));
162 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
164 dlg.setFileMode(QFileDialog::DirectoryOnly);
166 if (!suggested.empty())
167 dlg.selectFile(toqstr(suggested));
169 lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
170 int res = dlg.exec();
171 lyxerr[Debug::GUI] << "result " << res << endl;
172 if (res == QDialog::Accepted)
173 result.second = from_utf8(internal_path(
174 fromqstr(dlg.selectedFiles()[0])));