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