]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.C
Fix unreported bug related to 3246 by Richard Heck:
[lyx.git] / src / frontends / qt4 / FileDialog.C
1 /**
2  * \file qt4/FileDialog.C
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 "FileDialog_private.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  *
32  * Therefore there is a tradeoff in enabling or disabling this (JMarc)
33  */
34 #ifdef Q_WS_MACX
35 #define USE_NATIVE_FILEDIALOG 1
36 #endif
37
38 #ifdef USE_NATIVE_FILEDIALOG
39 #include <qapplication.h>
40 #include "support/filetools.h"
41
42 using lyx::support::makeAbsPath;
43 #endif
44
45 namespace lyx {
46
47 using support::FileFilterList;
48 using support::os::internal_path;
49 using std::endl;
50
51
52 class FileDialog::Private {
53 public:
54         Button b1;
55         Button b2;
56 };
57
58
59 FileDialog::FileDialog(docstring const & t,
60                        kb_action s, Button b1, Button b2)
61         : private_(new FileDialog::Private), title_(t), success_(s)
62 {
63         private_->b1 = b1;
64         private_->b2 = b2;
65 }
66
67
68 FileDialog::~FileDialog()
69 {
70         delete private_;
71 }
72
73
74 FileDialog::Result const FileDialog::save(docstring const & path,
75                                           FileFilterList const & filters,
76                                           docstring const & suggested)
77 {
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;
83
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()) ))));
90 #else
91         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
92         dlg.setFileMode(QFileDialog::AnyFile);
93         dlg.setAcceptMode(QFileDialog::AcceptSave);
94         dlg.setConfirmOverwrite(false);
95
96         if (!suggested.empty())
97                 dlg.selectFile(toqstr(suggested));
98
99         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
100         int res = dlg.exec();
101         lyxerr[Debug::GUI] << "result " << res << endl;
102         if (res == QDialog::Accepted)
103                 result.second = from_utf8(internal_path(
104                                         fromqstr(dlg.selectedFiles()[0])));
105         dlg.hide();
106 #endif
107         return result;
108 }
109
110
111 FileDialog::Result const FileDialog::open(docstring const & path,
112                                           FileFilterList const & filters,
113                                           docstring const & suggested)
114 {
115         lyxerr[Debug::GUI] << "Select with path \"" << to_utf8(path)
116                            << "\", mask \"" << to_utf8(filters.as_string())
117                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
118         FileDialog::Result result;
119         result.first = FileDialog::Chosen;
120
121 #ifdef USE_NATIVE_FILEDIALOG
122         docstring const startsWith = from_utf8(
123                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
124         result.second = from_utf8(internal_path(fromqstr(
125                 QFileDialog::getOpenFileName(qApp->focusWidget(), 
126                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
127 #else
128         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
129
130         if (!suggested.empty())
131                 dlg.selectFile(toqstr(suggested));
132
133         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
134         int res = dlg.exec();
135         lyxerr[Debug::GUI] << "result " << res << endl;
136         if (res == QDialog::Accepted)
137                 result.second = from_utf8(internal_path(
138                                         fromqstr(dlg.selectedFiles()[0])));
139         dlg.hide();
140 #endif
141         return result;
142 }
143
144
145 FileDialog::Result const FileDialog::opendir(docstring const & path,
146                                             docstring const & suggested)
147 {
148         lyxerr[Debug::GUI] << "Select with path \"" << to_utf8(path)
149                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
150         FileDialog::Result result;
151         result.first = FileDialog::Chosen;
152
153 #ifdef USE_NATIVE_FILEDIALOG
154         docstring const startsWith = from_utf8(
155                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
156         result.second = from_utf8(internal_path(fromqstr(
157                 QFileDialog::getExistingDirectory(qApp->focusWidget(),
158                 toqstr(title_),toqstr(startsWith)))));
159 #else
160         FileFilterList const filter(_("Directories"));
161
162         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
163
164         dlg.setFileMode(QFileDialog::DirectoryOnly);
165
166         if (!suggested.empty())
167                 dlg.selectFile(toqstr(suggested));
168
169         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
170         int res = dlg.exec();
171         lyxerr[Debug::GUI] << "result " << res << endl;
172         if (res == QDialog::Accepted)
173                 result.second = from_utf8(internal_path(
174                                         fromqstr(dlg.selectedFiles()[0])));
175         dlg.hide();
176 #endif
177         return result;
178 }
179
180
181 } // namespace lyx