]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.cpp
On Mac, moving down a paragraph should place the cursor at the end of the current...
[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 "FileDialog.h"
15
16 #include "LyXFileDialog.h"
17 #include "qt_helpers.h"
18
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/gettext.h"
23 #include "support/os.h"
24
25 #include <string>
26
27 /** when this is defined, the code will use
28  * QFileDialog::getOpenFileName and friends to create filedialogs.
29  * Effects:
30  * - the dialog does not use the quick directory buttons (Button
31  *   parameters);
32  * - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
33  * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
34  *   file dialog (http://www.lyx.org/trac/ticket/3907).
35  *
36  * Therefore there is a tradeoff in enabling or disabling this (JMarc)
37  */
38 #if defined(Q_WS_MACX) || (defined(Q_WS_WIN) && !defined(Q_OS_CYGWIN))
39 #define USE_NATIVE_FILEDIALOG 1
40 #endif
41
42 #ifdef USE_NATIVE_FILEDIALOG
43 #include <QApplication>
44 #endif
45
46 namespace lyx {
47
48 using namespace support;
49
50
51 class FileDialog::Private {
52 public:
53         Button b1;
54         Button b2;
55 };
56
57
58 FileDialog::FileDialog(QString const & t, FuncCode s)
59         : private_(new FileDialog::Private), title_(t), success_(s)
60 {}
61
62
63 FileDialog::~FileDialog()
64 {
65         delete private_;
66 }
67
68
69 void FileDialog::setButton1(QString const & label, QString const & dir)
70 {
71         private_->b1.first = label;
72         private_->b1.second = dir;
73 }
74
75
76 void FileDialog::setButton2(QString const & label, QString const & dir)
77 {
78         private_->b2.first = label;
79         private_->b2.second = dir;
80 }
81
82
83 FileDialog::Result FileDialog::save(QString const & path,
84         QStringList const & filters, QString const & suggested,
85         QString * selectedFilter)
86 {
87         LYXERR(Debug::GUI, "Select with path \"" << path
88                            << "\", mask \"" << filters.join(";;")
89                            << "\", suggested \"" << suggested << '"');
90
91         FileDialog::Result result;
92         result.first = FileDialog::Chosen;
93
94 #ifdef USE_NATIVE_FILEDIALOG
95         QString const startsWith = makeAbsPath(suggested, path);
96         QString const name = 
97                 QFileDialog::getSaveFileName(qApp->focusWidget(),
98                 title_, startsWith, filters.join(";;"),
99                 selectedFilter, QFileDialog::DontConfirmOverwrite);
100         if (name.isNull())
101                 result.first = FileDialog::Later;
102         else
103                 result.second = toqstr(os::internal_path(fromqstr(name)));
104 #else
105         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
106         dlg.setFileMode(QFileDialog::AnyFile);
107         dlg.setAcceptMode(QFileDialog::AcceptSave);
108         dlg.setConfirmOverwrite(false);
109
110         if (!suggested.isEmpty())
111                 dlg.selectFile(suggested);
112
113         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
114         int res = dlg.exec();
115         LYXERR(Debug::GUI, "result " << res);
116         if (res == QDialog::Accepted)
117                 result.second = internalPath(dlg.selectedFiles()[0]);
118         else
119                 result.first = FileDialog::Later;
120         if (selectedFilter != 0)
121                 *selectedFilter = dlg.selectedNameFilter();
122         dlg.hide();
123 #endif
124         return result;
125 }
126
127
128 FileDialog::Result FileDialog::save(QString const & path,
129         QStringList const & filters, QString const & suggested)
130 {
131         return save(path, filters, suggested, 0);
132 }
133
134
135 FileDialog::Result FileDialog::open(QString const & path,
136         QStringList const & filters, QString const & suggested)
137 {
138         LYXERR(Debug::GUI, "Select with path \"" << path
139                            << "\", mask \"" << filters.join(";;")
140                            << "\", suggested \"" << suggested << '"');
141         FileDialog::Result result;
142         result.first = FileDialog::Chosen;
143
144 #ifdef USE_NATIVE_FILEDIALOG
145         QString const startsWith = makeAbsPath(suggested, path);
146         QString const file = QFileDialog::getOpenFileName(qApp->focusWidget(),
147                 title_, startsWith, filters.join(";;"));
148         if (file.isNull())
149                 result.first = FileDialog::Later;
150         else
151                 result.second = internalPath(file);
152 #else
153         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
154
155         if (!suggested.isEmpty())
156                 dlg.selectFile(suggested);
157
158         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
159         int res = dlg.exec();
160         LYXERR(Debug::GUI, "result " << res);
161         if (res == QDialog::Accepted)
162                 result.second = internalPath(dlg.selectedFiles()[0]);
163         else
164                 result.first = FileDialog::Later;
165         dlg.hide();
166 #endif
167         return result;
168 }
169
170
171 FileDialog::Result FileDialog::opendir(QString const & path,
172         QString const & suggested)
173 {
174         LYXERR(Debug::GUI, "Select with path \"" << path
175                            << "\", suggested \"" << suggested << '"');
176         FileDialog::Result result;
177         result.first = FileDialog::Chosen;
178
179 #ifdef USE_NATIVE_FILEDIALOG
180         QString const startsWith = toqstr(makeAbsPath(fromqstr(suggested),
181                 fromqstr(path)).absFileName());
182         QString const dir = QFileDialog::getExistingDirectory(qApp->focusWidget(),
183                 title_, startsWith);
184         if (dir.isNull())
185                 result.first = FileDialog::Later;
186         else
187                 result.second = toqstr(os::internal_path(fromqstr(dir)));
188 #else
189         LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")),
190                 private_->b1, private_->b2);
191
192         dlg.setFileMode(QFileDialog::DirectoryOnly);
193
194         if (!suggested.isEmpty())
195                 dlg.selectFile(suggested);
196
197         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
198         int res = dlg.exec();
199         LYXERR(Debug::GUI, "result " << res);
200         if (res == QDialog::Accepted)
201                 result.second = internalPath(dlg.selectedFiles()[0]);
202         else
203                 result.first = FileDialog::Later;
204         dlg.hide();
205 #endif
206         return result;
207 }
208
209
210 } // namespace lyx