]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.cpp
da79152377fdb13bdd9c52c2694f6c9880c72dcb
[lyx.git] / src / frontends / qt4 / FileDialog.cpp
1 /**
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.
5  *
6  * \author John Levon
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "frontends/FileDialog.h"
15
16 #include "LyXFileDialog.h"
17 #include "qt_helpers.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "support/FileFilterList.h"
23 #include "support/os.h"
24
25 /** when this is defined, the code will use
26  * QFileDialog::getOpenFileName and friends to create filedialogs.
27  * Effects:
28  * - the dialog does not use the quick directory buttons (Button
29  *   parameters);
30  * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
31  * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
32  *   file dialog (http://bugzilla.lyx.org/show_bug.cgi?id=3907).
33  *
34  * Therefore there is a tradeoff in enabling or disabling this (JMarc)
35  */
36 #if defined(Q_WS_MACX) || (defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN))
37 #define USE_NATIVE_FILEDIALOG 1
38 #endif
39
40 #ifdef USE_NATIVE_FILEDIALOG
41 #include <QApplication>
42 #include "support/filetools.h"
43
44 using lyx::support::makeAbsPath;
45 #endif
46
47 namespace lyx {
48
49 using support::FileFilterList;
50 using support::os::internal_path;
51 using std::endl;
52
53
54 class FileDialog::Private {
55 public:
56         Button b1;
57         Button b2;
58 };
59
60
61 FileDialog::FileDialog(docstring const & t,
62                        kb_action s, Button b1, Button b2)
63         : private_(new FileDialog::Private), title_(t), success_(s)
64 {
65         private_->b1 = b1;
66         private_->b2 = b2;
67 }
68
69
70 FileDialog::~FileDialog()
71 {
72         delete private_;
73 }
74
75
76 FileDialog::Result const FileDialog::save(docstring const & path,
77                                           FileFilterList const & filters,
78                                           docstring const & suggested)
79 {
80         LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
81                            << "\", mask \"" << to_utf8(filters.as_string())
82                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
83         FileDialog::Result result;
84         result.first = FileDialog::Chosen;
85
86 #ifdef USE_NATIVE_FILEDIALOG
87         docstring const startsWith = from_utf8(
88                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
89         QString const name = 
90                 QFileDialog::getSaveFileName(qApp->focusWidget(),
91                                              toqstr(title_), 
92                                              toqstr(startsWith), 
93                                              toqstr(filters.as_string()),
94                                              0, 
95                                              QFileDialog::DontConfirmOverwrite);
96         result.second = from_utf8(internal_path(fromqstr(name)));
97 #else
98         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
99 #if QT_VERSION != 0x040203
100         dlg.setFileMode(QFileDialog::AnyFile);
101 #endif
102         dlg.setAcceptMode(QFileDialog::AcceptSave);
103         dlg.setConfirmOverwrite(false);
104
105         if (!suggested.empty())
106                 dlg.selectFile(toqstr(suggested));
107
108         LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
109         int res = dlg.exec();
110         LYXERR(Debug::GUI) << "result " << res << endl;
111         if (res == QDialog::Accepted)
112                 result.second = from_utf8(internal_path(
113                                         fromqstr(dlg.selectedFiles()[0])));
114         dlg.hide();
115 #endif
116         return result;
117 }
118
119
120 FileDialog::Result const FileDialog::open(docstring const & path,
121                                           FileFilterList const & filters,
122                                           docstring const & suggested)
123 {
124         LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
125                            << "\", mask \"" << to_utf8(filters.as_string())
126                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
127         FileDialog::Result result;
128         result.first = FileDialog::Chosen;
129
130 #ifdef USE_NATIVE_FILEDIALOG
131         docstring const startsWith = from_utf8(
132                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
133         result.second = from_utf8(internal_path(fromqstr(
134                 QFileDialog::getOpenFileName(qApp->focusWidget(),
135                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
136 #else
137         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
138
139         if (!suggested.empty())
140                 dlg.selectFile(toqstr(suggested));
141
142         LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
143         int res = dlg.exec();
144         LYXERR(Debug::GUI) << "result " << res << endl;
145         if (res == QDialog::Accepted)
146                 result.second = from_utf8(internal_path(
147                                         fromqstr(dlg.selectedFiles()[0])));
148         dlg.hide();
149 #endif
150         return result;
151 }
152
153
154 FileDialog::Result const FileDialog::opendir(docstring const & path,
155                                             docstring const & suggested)
156 {
157         LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
158                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
159         FileDialog::Result result;
160         result.first = FileDialog::Chosen;
161
162 #ifdef USE_NATIVE_FILEDIALOG
163         docstring const startsWith = from_utf8(
164                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
165         result.second = from_utf8(internal_path(fromqstr(
166                 QFileDialog::getExistingDirectory(qApp->focusWidget(),
167                 toqstr(title_),toqstr(startsWith)))));
168 #else
169         FileFilterList const filter(_("Directories"));
170
171         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
172
173         dlg.setFileMode(QFileDialog::DirectoryOnly);
174
175         if (!suggested.empty())
176                 dlg.selectFile(toqstr(suggested));
177
178         LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
179         int res = dlg.exec();
180         LYXERR(Debug::GUI) << "result " << res << endl;
181         if (res == QDialog::Accepted)
182                 result.second = from_utf8(internal_path(
183                                         fromqstr(dlg.selectedFiles()[0])));
184         dlg.hide();
185 #endif
186         return result;
187 }
188
189
190 } // namespace lyx