2 * \file qt4/FileDialog.cpp
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 "LyXFileDialog.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 #if QT_VERSION != 0x040203
93 dlg.setFileMode(QFileDialog::AnyFile);
95 dlg.setAcceptMode(QFileDialog::AcceptSave);
96 dlg.setConfirmOverwrite(false);
98 if (!suggested.empty())
99 dlg.selectFile(toqstr(suggested));
101 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
102 int res = dlg.exec();
103 LYXERR(Debug::GUI) << "result " << res << endl;
104 if (res == QDialog::Accepted)
105 result.second = from_utf8(internal_path(
106 fromqstr(dlg.selectedFiles()[0])));
113 FileDialog::Result const FileDialog::open(docstring const & path,
114 FileFilterList const & filters,
115 docstring const & suggested)
117 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
118 << "\", mask \"" << to_utf8(filters.as_string())
119 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
120 FileDialog::Result result;
121 result.first = FileDialog::Chosen;
123 #ifdef USE_NATIVE_FILEDIALOG
124 docstring const startsWith = from_utf8(
125 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
126 result.second = from_utf8(internal_path(fromqstr(
127 QFileDialog::getOpenFileName(qApp->focusWidget(),
128 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
130 LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
132 if (!suggested.empty())
133 dlg.selectFile(toqstr(suggested));
135 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
136 int res = dlg.exec();
137 LYXERR(Debug::GUI) << "result " << res << endl;
138 if (res == QDialog::Accepted)
139 result.second = from_utf8(internal_path(
140 fromqstr(dlg.selectedFiles()[0])));
147 FileDialog::Result const FileDialog::opendir(docstring const & path,
148 docstring const & suggested)
150 LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
151 << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
152 FileDialog::Result result;
153 result.first = FileDialog::Chosen;
155 #ifdef USE_NATIVE_FILEDIALOG
156 docstring const startsWith = from_utf8(
157 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
158 result.second = from_utf8(internal_path(fromqstr(
159 QFileDialog::getExistingDirectory(qApp->focusWidget(),
160 toqstr(title_),toqstr(startsWith)))));
162 FileFilterList const filter(_("Directories"));
164 LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
166 dlg.setFileMode(QFileDialog::DirectoryOnly);
168 if (!suggested.empty())
169 dlg.selectFile(toqstr(suggested));
171 LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
172 int res = dlg.exec();
173 LYXERR(Debug::GUI) << "result " << res << endl;
174 if (res == QDialog::Accepted)
175 result.second = from_utf8(internal_path(
176 fromqstr(dlg.selectedFiles()[0])));