]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.cpp
bee4e6ad1f73f5e76956453ea2bbd084bd32d011
[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  *
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 #if QT_VERSION != 0x040203
93         dlg.setFileMode(QFileDialog::AnyFile);
94 #endif
95         dlg.setAcceptMode(QFileDialog::AcceptSave);
96         dlg.setConfirmOverwrite(false);
97
98         if (!suggested.empty())
99                 dlg.selectFile(toqstr(suggested));
100
101         LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
102         int res = dlg.exec();
103         LYXERR(Debug::GUI) << "result " << res << endl;
104         if (res == QDialog::Accepted)
105                 result.second = from_utf8(internal_path(
106                                         fromqstr(dlg.selectedFiles()[0])));
107         dlg.hide();
108 #endif
109         return result;
110 }
111
112
113 FileDialog::Result const FileDialog::open(docstring const & path,
114                                           FileFilterList const & filters,
115                                           docstring const & suggested)
116 {
117         LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
118                            << "\", mask \"" << to_utf8(filters.as_string())
119                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
120         FileDialog::Result result;
121         result.first = FileDialog::Chosen;
122
123 #ifdef USE_NATIVE_FILEDIALOG
124         docstring const startsWith = from_utf8(
125                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
126         result.second = from_utf8(internal_path(fromqstr(
127                 QFileDialog::getOpenFileName(qApp->focusWidget(), 
128                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
129 #else
130         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
131
132         if (!suggested.empty())
133                 dlg.selectFile(toqstr(suggested));
134
135         LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
136         int res = dlg.exec();
137         LYXERR(Debug::GUI) << "result " << res << endl;
138         if (res == QDialog::Accepted)
139                 result.second = from_utf8(internal_path(
140                                         fromqstr(dlg.selectedFiles()[0])));
141         dlg.hide();
142 #endif
143         return result;
144 }
145
146
147 FileDialog::Result const FileDialog::opendir(docstring const & path,
148                                             docstring const & suggested)
149 {
150         LYXERR(Debug::GUI) << "Select with path \"" << to_utf8(path)
151                            << "\", suggested \"" << to_utf8(suggested) << '"' << endl;
152         FileDialog::Result result;
153         result.first = FileDialog::Chosen;
154
155 #ifdef USE_NATIVE_FILEDIALOG
156         docstring const startsWith = from_utf8(
157                 makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
158         result.second = from_utf8(internal_path(fromqstr(
159                 QFileDialog::getExistingDirectory(qApp->focusWidget(),
160                 toqstr(title_),toqstr(startsWith)))));
161 #else
162         FileFilterList const filter(_("Directories"));
163
164         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
165
166         dlg.setFileMode(QFileDialog::DirectoryOnly);
167
168         if (!suggested.empty())
169                 dlg.selectFile(toqstr(suggested));
170
171         LYXERR(Debug::GUI) << "Synchronous FileDialog: " << endl;
172         int res = dlg.exec();
173         LYXERR(Debug::GUI) << "result " << res << endl;
174         if (res == QDialog::Accepted)
175                 result.second = from_utf8(internal_path(
176                                         fromqstr(dlg.selectedFiles()[0])));
177         dlg.hide();
178 #endif
179         return result;
180 }
181
182
183 } // namespace lyx