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