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