]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.C
fda0481e5611a9e4f5b60622feeada8d2b550f4c
[lyx.git] / src / frontends / qt4 / FileDialog.C
1 /**
2  * \file qt2/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
24 /** when this is defined, the code will use
25  * QFileDialog::getOpenFileName and friends to create filedialogs.
26  * Effects:
27  * - the dialog does not use the quick directory buttons (Button
28  *   parameters);
29  * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
30  *
31  * Therefore there is a tradeoff in enabling or disabling this (JMarc)
32  */
33 #ifdef Q_WS_MACX
34 #define USE_NATIVE_FILEDIALOG 1
35 #endif
36
37 #ifdef USE_NATIVE_FILEDIALOG
38 #include <qapplication.h>
39 #include "support/filetools.h"
40 using lyx::support::makeAbsPath;
41 #endif
42
43 using lyx::support::FileFilterList;
44
45 using std::endl;
46 using std::string;
47
48
49 class FileDialog::Private {
50 public:
51         Button b1;
52         Button b2;
53 };
54
55
56 FileDialog::FileDialog(string const & t,
57                        kb_action s, Button b1, Button b2)
58         : private_(new FileDialog::Private), title_(t), success_(s)
59 {
60         private_->b1 = b1;
61         private_->b2 = b2;
62 }
63
64
65 FileDialog::~FileDialog()
66 {
67         delete private_;
68 }
69
70
71 FileDialog::Result const FileDialog::save(string const & path,
72                                           FileFilterList const & filters,
73                                           string const & suggested)
74 {
75         lyxerr[Debug::GUI] << "Select with path \"" << path
76                            << "\", mask \"" << filters.as_string()
77                            << "\", suggested \"" << suggested << '"' << endl;
78         FileDialog::Result result;
79         result.first = FileDialog::Chosen;
80
81 #ifdef USE_NATIVE_FILEDIALOG
82         string const startsWith = makeAbsPath(suggested, path);
83         result.second = fromqstr(
84                 Q3FileDialog::getSaveFileName(toqstr(startsWith),
85                                              toqstr(filters.as_string()),
86                                              qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
87                                              title_.c_str()));
88 #else
89         LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
90         dlg.setMode(Q3FileDialog::AnyFile);
91
92         if (!suggested.empty())
93                 dlg.setSelection(toqstr(suggested));
94
95         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
96         int res = dlg.exec();
97         lyxerr[Debug::GUI] << "result " << res << endl;
98         if (res == QDialog::Accepted)
99                 result.second = fromqstr(dlg.selectedFile());
100         dlg.hide();
101 #endif
102         return result;
103 }
104
105
106 FileDialog::Result const FileDialog::open(string const & path,
107                                           FileFilterList const & filters,
108                                           string const & suggested)
109 {
110         lyxerr[Debug::GUI] << "Select with path \"" << path
111                            << "\", mask \"" << filters.as_string()
112                            << "\", suggested \"" << suggested << '"' << endl;
113         FileDialog::Result result;
114         result.first = FileDialog::Chosen;
115
116 #ifdef USE_NATIVE_FILEDIALOG
117         string const startsWith = makeAbsPath(suggested, path);
118         result.second = fromqstr(
119                 Q3FileDialog::getOpenFileName(toqstr(startsWith),
120                                              toqstr(filters.as_string()),
121                                              qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
122                                              title_.c_str()));
123 #else
124         LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
125
126         if (!suggested.empty())
127                 dlg.setSelection(toqstr(suggested));
128
129         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
130         int res = dlg.exec();
131         lyxerr[Debug::GUI] << "result " << res << endl;
132         if (res == QDialog::Accepted)
133                 result.second = fromqstr(dlg.selectedFile());
134         dlg.hide();
135 #endif
136         return result;
137 }
138
139
140 FileDialog::Result const FileDialog::opendir(string const & path,
141                                             string const & suggested)
142 {
143         lyxerr[Debug::GUI] << "Select with path \"" << path
144                            << "\", suggested \"" << suggested << '"' << endl;
145         FileDialog::Result result;
146         result.first = FileDialog::Chosen;
147
148 #ifdef USE_NATIVE_FILEDIALOG
149         string const startsWith = makeAbsPath(suggested, path);
150         result.second = fromqstr(
151                 Q3FileDialog::getExistingDirectory(toqstr(startsWith),
152                                                   qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
153                                                   title_.c_str()));
154 #else
155         FileFilterList const filter(_("Directories"));
156
157         LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2);
158
159         dlg.setMode(Q3FileDialog::DirectoryOnly);
160
161         if (!suggested.empty())
162                 dlg.setSelection(toqstr(suggested));
163
164         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
165         int res = dlg.exec();
166         lyxerr[Debug::GUI] << "result " << res << endl;
167         if (res == QDialog::Accepted)
168                 result.second = fromqstr(dlg.selectedFile());
169         dlg.hide();
170 #endif
171         return result;
172 }