]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.C
This commit saves the need to check for lyx::use_gui in a number of places.
[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
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 using lyx::docstring;
45
46 using std::endl;
47
48
49 class FileDialog::Private {
50 public:
51         Button b1;
52         Button b2;
53 };
54
55
56 FileDialog::FileDialog(docstring 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(docstring const & path,
72                                           FileFilterList const & filters,
73                                           docstring const & suggested)
74 {
75         lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
76                            << "\", mask \"" << lyx::to_utf8(filters.as_string())
77                            << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
78         FileDialog::Result result;
79         result.first = FileDialog::Chosen;
80
81 #ifdef USE_NATIVE_FILEDIALOG
82         docstring const startsWith
83                 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
84         result.second = qstring_to_ucs4(QFileDialog::getSaveFileName(
85                 qApp->focusWidget(),
86                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
87 #else
88         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
89         dlg.setFileMode(QFileDialog::AnyFile);
90         dlg.setAcceptMode(QFileDialog::AcceptSave);
91
92         if (!suggested.empty())
93                 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);
100         dlg.hide();
101 #endif
102         return result;
103 }
104
105
106 FileDialog::Result const FileDialog::open(docstring const & path,
107                                           FileFilterList const & filters,
108                                           docstring const & suggested)
109 {
110         lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
111                            << "\", mask \"" << lyx::to_utf8(filters.as_string())
112                            << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
113         FileDialog::Result result;
114         result.first = FileDialog::Chosen;
115
116 #ifdef USE_NATIVE_FILEDIALOG
117         docstring const startsWith =
118                 lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
119         result.second = qstring_to_ucs4(QFileDialog::getOpenFileName(
120                 qApp->focusWidget(), 
121                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
122 #else
123         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
124
125         if (!suggested.empty())
126                 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);
133         dlg.hide();
134 #endif
135         return result;
136 }
137
138
139 FileDialog::Result const FileDialog::opendir(docstring const & path,
140                                             docstring const & suggested)
141 {
142         lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
143                            << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
144         FileDialog::Result result;
145         result.first = FileDialog::Chosen;
146
147 #ifdef USE_NATIVE_FILEDIALOG
148         docstring const startsWith
149                 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
150         result.second = qstring_to_ucs4(QFileDialog::getExistingDirectory(
151                 qApp->focusWidget(),
152                 toqstr(title_),toqstr(startsWith) ));
153 #else
154         FileFilterList const filter(_("Directories"));
155
156         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
157
158         dlg.setFileMode(QFileDialog::DirectoryOnly);
159
160         if (!suggested.empty())
161                 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);
168         dlg.hide();
169 #endif
170         return result;
171 }