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