]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.C
* Painter.h:
[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
41 using lyx::support::makeAbsPath;
42 #endif
43
44 namespace lyx {
45
46 using support::FileFilterList;
47 using std::endl;
48
49
50 class FileDialog::Private {
51 public:
52         Button b1;
53         Button b2;
54 };
55
56
57 FileDialog::FileDialog(docstring const & t,
58                        kb_action s, Button b1, Button b2)
59         : private_(new FileDialog::Private), title_(t), success_(s)
60 {
61         private_->b1 = b1;
62         private_->b2 = b2;
63 }
64
65
66 FileDialog::~FileDialog()
67 {
68         delete private_;
69 }
70
71
72 FileDialog::Result const FileDialog::save(docstring const & path,
73                                           FileFilterList const & filters,
74                                           docstring const & suggested)
75 {
76         lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
77                            << "\", mask \"" << lyx::to_utf8(filters.as_string())
78                            << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
79         FileDialog::Result result;
80         result.first = FileDialog::Chosen;
81
82 #ifdef USE_NATIVE_FILEDIALOG
83         docstring const startsWith
84                 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
85         result.second = qstring_to_ucs4(QFileDialog::getSaveFileName(
86                 qApp->focusWidget(),
87                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
88 #else
89         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
90         dlg.setFileMode(QFileDialog::AnyFile);
91         dlg.setAcceptMode(QFileDialog::AcceptSave);
92
93         if (!suggested.empty())
94                 dlg.selectFile(toqstr(suggested));
95
96         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
97         int res = dlg.exec();
98         lyxerr[Debug::GUI] << "result " << res << endl;
99         if (res == QDialog::Accepted)
100                 result.second = qstring_to_ucs4(dlg.selectedFiles()[0]);
101         dlg.hide();
102 #endif
103         return result;
104 }
105
106
107 FileDialog::Result const FileDialog::open(docstring const & path,
108                                           FileFilterList const & filters,
109                                           docstring const & suggested)
110 {
111         lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
112                            << "\", mask \"" << lyx::to_utf8(filters.as_string())
113                            << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
114         FileDialog::Result result;
115         result.first = FileDialog::Chosen;
116
117 #ifdef USE_NATIVE_FILEDIALOG
118         docstring const startsWith =
119                 lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
120         result.second = qstring_to_ucs4(QFileDialog::getOpenFileName(
121                 qApp->focusWidget(), 
122                 toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ));
123 #else
124         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
125
126         if (!suggested.empty())
127                 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);
134         dlg.hide();
135 #endif
136         return result;
137 }
138
139
140 FileDialog::Result const FileDialog::opendir(docstring const & path,
141                                             docstring const & suggested)
142 {
143         lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
144                            << "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
145         FileDialog::Result result;
146         result.first = FileDialog::Chosen;
147
148 #ifdef USE_NATIVE_FILEDIALOG
149         docstring const startsWith
150                 = lyx::from_utf8(makeAbsPath(lyx::to_utf8(suggested), lyx::to_utf8(path)));
151         result.second = qstring_to_ucs4(QFileDialog::getExistingDirectory(
152                 qApp->focusWidget(),
153                 toqstr(title_),toqstr(startsWith) ));
154 #else
155         FileFilterList const filter(_("Directories"));
156
157         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
158
159         dlg.setFileMode(QFileDialog::DirectoryOnly);
160
161         if (!suggested.empty())
162                 dlg.selectFile(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 = qstring_to_ucs4(dlg.selectedFiles()[0]);
169         dlg.hide();
170 #endif
171         return result;
172 }
173
174
175 } // namespace lyx