]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.cpp
do what the FIXME suggested
[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 "FileDialog.h"
15
16 #include "LyXFileDialog.h"
17 #include "qt_helpers.h"
18
19 #include "support/debug.h"
20 #include "support/FileFilterList.h"
21 #include "support/FileName.h"
22 #include "support/gettext.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
52
53 class FileDialog::Private {
54 public:
55         Button b1;
56         Button b2;
57 };
58
59
60 FileDialog::FileDialog(docstring const & t, kb_action s)
61         : private_(new FileDialog::Private), title_(t), success_(s)
62 {}
63
64
65 FileDialog::~FileDialog()
66 {
67         delete private_;
68 }
69
70
71 void FileDialog::setButton1(docstring const & label, docstring const & dir)
72 {
73         private_->b1.first = label;
74         private_->b1.second = dir;
75 }
76
77
78 void FileDialog::setButton2(docstring const & label, docstring const & dir)
79 {
80         private_->b2.first = label;
81         private_->b2.second = dir;
82 }
83
84
85 FileDialog::Result const FileDialog::save(docstring const & path,
86                                           FileFilterList const & filters,
87                                           docstring const & suggested)
88 {
89         LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
90                            << "\", mask \"" << to_utf8(filters.as_string())
91                            << "\", suggested \"" << to_utf8(suggested) << '"');
92         FileDialog::Result result;
93         result.first = FileDialog::Chosen;
94
95 #ifdef USE_NATIVE_FILEDIALOG
96         docstring const startsWith = from_utf8(
97                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
98         QString const name = 
99                 QFileDialog::getSaveFileName(qApp->focusWidget(),
100                                              toqstr(title_), 
101                                              toqstr(startsWith), 
102                                              toqstr(filters.as_string()),
103                                              0, 
104                                              QFileDialog::DontConfirmOverwrite);
105         result.second = from_utf8(internal_path(fromqstr(name)));
106 #else
107         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
108 #if QT_VERSION != 0x040203
109         dlg.setFileMode(QFileDialog::AnyFile);
110 #endif
111         dlg.setAcceptMode(QFileDialog::AcceptSave);
112         dlg.setConfirmOverwrite(false);
113
114         if (!suggested.empty())
115                 dlg.selectFile(toqstr(suggested));
116
117         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
118         int res = dlg.exec();
119         LYXERR(Debug::GUI, "result " << res);
120         if (res == QDialog::Accepted)
121                 result.second = from_utf8(internal_path(
122                                         fromqstr(dlg.selectedFiles()[0])));
123         dlg.hide();
124 #endif
125         return result;
126 }
127
128
129 FileDialog::Result const FileDialog::open(docstring const & path,
130                                           FileFilterList const & filters,
131                                           docstring const & suggested)
132 {
133         LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
134                            << "\", mask \"" << to_utf8(filters.as_string())
135                            << "\", suggested \"" << to_utf8(suggested) << '"');
136         FileDialog::Result result;
137         result.first = FileDialog::Chosen;
138
139 #ifdef USE_NATIVE_FILEDIALOG
140         docstring const startsWith = from_utf8(
141                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
142         result.second = from_utf8(internal_path(fromqstr(
143                 QFileDialog::getOpenFileName(qApp->focusWidget(),
144                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
145 #else
146         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
147
148         if (!suggested.empty())
149                 dlg.selectFile(toqstr(suggested));
150
151         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
152         int res = dlg.exec();
153         LYXERR(Debug::GUI, "result " << res);
154         if (res == QDialog::Accepted)
155                 result.second = from_utf8(internal_path(
156                                         fromqstr(dlg.selectedFiles()[0])));
157         dlg.hide();
158 #endif
159         return result;
160 }
161
162
163 FileDialog::Result const FileDialog::opendir(docstring const & path,
164                                             docstring const & suggested)
165 {
166         LYXERR(Debug::GUI, "Select with path \"" << to_utf8(path)
167                            << "\", suggested \"" << to_utf8(suggested) << '"');
168         FileDialog::Result result;
169         result.first = FileDialog::Chosen;
170
171 #ifdef USE_NATIVE_FILEDIALOG
172         docstring const startsWith = from_utf8(
173                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
174         result.second = from_utf8(internal_path(fromqstr(
175                 QFileDialog::getExistingDirectory(qApp->focusWidget(),
176                 toqstr(title_),toqstr(startsWith)))));
177 #else
178         FileFilterList const filter(_("Directories"));
179
180         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
181
182         dlg.setFileMode(QFileDialog::DirectoryOnly);
183
184         if (!suggested.empty())
185                 dlg.selectFile(toqstr(suggested));
186
187         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
188         int res = dlg.exec();
189         LYXERR(Debug::GUI, "result " << res);
190         if (res == QDialog::Accepted)
191                 result.second = from_utf8(internal_path(
192                                         fromqstr(dlg.selectedFiles()[0])));
193         dlg.hide();
194 #endif
195         return result;
196 }
197
198
199 } // namespace lyx