]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FileDialog.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / qt2 / 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 struct FileDialog::Private {
50         Button b1;
51         Button b2;
52 };
53
54
55 FileDialog::FileDialog(string const & t,
56                        kb_action s, Button b1, Button b2)
57         : private_(new FileDialog::Private), title_(t), success_(s)
58 {
59         private_->b1 = b1;
60         private_->b2 = b2;
61 }
62
63
64 FileDialog::~FileDialog()
65 {
66         delete private_;
67 }
68
69
70 FileDialog::Result const FileDialog::save(string const & path,
71                                           FileFilterList const & filters,
72                                           string const & suggested)
73 {
74         lyxerr[Debug::GUI] << "Select with path \"" << path
75                            << "\", mask \"" << filters.as_string()
76                            << "\", suggested \"" << suggested << '"' << endl;
77         FileDialog::Result result;
78         result.first = FileDialog::Chosen;
79
80 #ifdef USE_NATIVE_FILEDIALOG
81         string const startsWith = MakeAbsPath(suggested, path);
82         result.second = fromqstr(
83                 QFileDialog::getSaveFileName(toqstr(startsWith),
84                                              toqstr(filters.as_string()),
85                                              qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
86                                              title_.c_str()));
87 #else
88         LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
89         dlg.setMode(QFileDialog::AnyFile);
90
91         if (!suggested.empty())
92                 dlg.setSelection(toqstr(suggested));
93
94         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
95         int res = dlg.exec();
96         lyxerr[Debug::GUI] << "result " << res << endl;
97         if (res == QDialog::Accepted)
98                 result.second = fromqstr(dlg.selectedFile());
99         dlg.hide();
100 #endif
101         return result;
102 }
103
104
105 FileDialog::Result const FileDialog::open(string const & path,
106                                           FileFilterList const & filters,
107                                           string const & suggested)
108 {
109         lyxerr[Debug::GUI] << "Select with path \"" << path
110                            << "\", mask \"" << filters.as_string()
111                            << "\", suggested \"" << suggested << '"' << endl;
112         FileDialog::Result result;
113         result.first = FileDialog::Chosen;
114
115 #ifdef USE_NATIVE_FILEDIALOG
116         string const startsWith = MakeAbsPath(suggested, path);
117         result.second = fromqstr(
118                 QFileDialog::getOpenFileName(toqstr(startsWith),
119                                              toqstr(filters.as_string()),
120                                              qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
121                                              title_.c_str()));
122 #else
123         LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
124
125         if (!suggested.empty())
126                 dlg.setSelection(toqstr(suggested));
127
128         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
129         int res = dlg.exec();
130         lyxerr[Debug::GUI] << "result " << res << endl;
131         if (res == QDialog::Accepted)
132                 result.second = fromqstr(dlg.selectedFile());
133         dlg.hide();
134 #endif
135         return result;
136 }
137
138
139 FileDialog::Result const FileDialog::opendir(string const & path,
140                                             string const & suggested)
141 {
142         lyxerr[Debug::GUI] << "Select with path \"" << path
143                            << "\", suggested \"" << suggested << '"' << endl;
144         FileDialog::Result result;
145         result.first = FileDialog::Chosen;
146
147 #ifdef USE_NATIVE_FILEDIALOG
148         string const startsWith = MakeAbsPath(suggested, path);
149         result.second = fromqstr(
150                 QFileDialog::getExistingDirectory(toqstr(startsWith),
151                                                   qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
152                                                   title_.c_str()));
153 #else
154         FileFilterList const filter(_("Directories"));
155
156         LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2);
157
158         dlg.setMode(QFileDialog::DirectoryOnly);
159
160         if (!suggested.empty())
161                 dlg.setSelection(toqstr(suggested));
162
163         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
164         int res = dlg.exec();
165         lyxerr[Debug::GUI] << "result " << res << endl;
166         if (res == QDialog::Accepted)
167                 result.second = fromqstr(dlg.selectedFile());
168         dlg.hide();
169 #endif
170         return result;
171 }