]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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(QString const & t, FuncCode 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(QString const & label, QString const & dir)
72 {
73         private_->b1.first = label;
74         private_->b1.second = dir;
75 }
76
77
78 void FileDialog::setButton2(QString const & label, QString const & dir)
79 {
80         private_->b2.first = label;
81         private_->b2.second = dir;
82 }
83
84
85 FileDialog::Result FileDialog::save(QString const & path,
86                                           FileFilterList const & filters,
87                                           QString const & suggested)
88 {
89         LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
90                            << "\", mask \"" << to_utf8(filters.as_string())
91                            << "\", suggested \"" << fromqstr(suggested) << '"');
92         FileDialog::Result result;
93         result.first = FileDialog::Chosen;
94
95 #ifdef USE_NATIVE_FILEDIALOG
96         QString const startsWith = toqstr(
97                 makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename());
98         QString const name = 
99                 QFileDialog::getSaveFileName(qApp->focusWidget(),
100                                              title_, 
101                                              startsWith, 
102                                              toqstr(filters.as_string()),
103                                              0, 
104                                              QFileDialog::DontConfirmOverwrite);
105         result.second = toqstr(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.isEmpty())
115                 dlg.selectFile(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 = internalPath(dlg.selectedFiles()[0]);
122         dlg.hide();
123 #endif
124         return result;
125 }
126
127
128 FileDialog::Result FileDialog::open(QString const & path,
129                                           FileFilterList const & filters,
130                                           QString const & suggested)
131 {
132         LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
133                            << "\", mask \"" << filters.as_string()
134                            << "\", suggested \"" << fromqstr(suggested) << '"');
135         FileDialog::Result result;
136         result.first = FileDialog::Chosen;
137
138 #ifdef USE_NATIVE_FILEDIALOG
139         QString const startsWith = toqstr(
140                 makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename());
141         result.second = internalPath(
142                 QFileDialog::getOpenFileName(qApp->focusWidget(),
143                 title_, startsWith, toqstr(filters.as_string()) ));
144 #else
145         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
146
147         if (!suggested.isEmpty())
148                 dlg.selectFile(suggested);
149
150         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
151         int res = dlg.exec();
152         LYXERR(Debug::GUI, "result " << res);
153         if (res == QDialog::Accepted)
154                 result.second = internalPath(dlg.selectedFiles()[0]);
155         dlg.hide();
156 #endif
157         return result;
158 }
159
160
161 FileDialog::Result FileDialog::opendir(QString const & path,
162                                             QString const & suggested)
163 {
164         LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
165                            << "\", suggested \"" << fromqstr(suggested) << '"');
166         FileDialog::Result result;
167         result.first = FileDialog::Chosen;
168
169 #ifdef USE_NATIVE_FILEDIALOG
170         QString const startsWith = toqstr(
171                 makeAbsPath(fromqstr(suggested), fromqstr(path)).absFilename());
172         result.second = toqstr(internal_path(fromqstr(
173                 QFileDialog::getExistingDirectory(qApp->focusWidget(),
174                 title_, startsWith))));
175 #else
176         FileFilterList const filter(_("Directories"));
177
178         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
179
180         dlg.setFileMode(QFileDialog::DirectoryOnly);
181
182         if (!suggested.isEmpty())
183                 dlg.selectFile(suggested);
184
185         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
186         int res = dlg.exec();
187         LYXERR(Debug::GUI, "result " << res);
188         if (res == QDialog::Accepted)
189                 result.second = internalPath(dlg.selectedFiles()[0]);
190         dlg.hide();
191 #endif
192         return result;
193 }
194
195
196 } // namespace lyx