]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.cpp
move the filefilter stuff closer to the only place where it is used
[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/gettext.h"
22 #include "support/os.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  * - with Qt/Win and Qt <= 4.3.0, there was a number of bugs with our own
31  *   file dialog (http://bugzilla.lyx.org/show_bug.cgi?id=3907).
32  *
33  * Therefore there is a tradeoff in enabling or disabling this (JMarc)
34  */
35 #if defined(Q_WS_MACX) || (defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN))
36 #define USE_NATIVE_FILEDIALOG 1
37 #endif
38
39 #ifdef USE_NATIVE_FILEDIALOG
40 #include <QApplication>
41 #endif
42
43 namespace lyx {
44
45 using support::os::internal_path;
46
47
48 class FileDialog::Private {
49 public:
50         Button b1;
51         Button b2;
52 };
53
54
55 FileDialog::FileDialog(QString const & t, FuncCode s)
56         : private_(new FileDialog::Private), title_(t), success_(s)
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 {
83         LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
84                            << "\", mask \"" << fromqstr(filters.join(";;"))
85                            << "\", suggested \"" << fromqstr(suggested) << '"');
86         FileDialog::Result result;
87         result.first = FileDialog::Chosen;
88
89 #ifdef USE_NATIVE_FILEDIALOG
90         QString const startsWith = makeAbsPath(suggested, path);
91         QString const name = 
92                 QFileDialog::getSaveFileName(qApp->focusWidget(),
93              title_, startsWith, filters, 0, QFileDialog::DontConfirmOverwrite);
94         result.second = toqstr(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.isEmpty())
104                 dlg.selectFile(suggested);
105
106         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
107         int res = dlg.exec();
108         LYXERR(Debug::GUI, "result " << res);
109         if (res == QDialog::Accepted)
110                 result.second = internalPath(dlg.selectedFiles()[0]);
111         dlg.hide();
112 #endif
113         return result;
114 }
115
116
117 FileDialog::Result FileDialog::open(QString const & path,
118         QStringList const & filters, QString const & suggested)
119 {
120         LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
121                            << "\", mask \"" << fromqstr(filters.join(";;"))
122                            << "\", suggested \"" << fromqstr(suggested) << '"');
123         FileDialog::Result result;
124         result.first = FileDialog::Chosen;
125
126 #ifdef USE_NATIVE_FILEDIALOG
127         QString const startsWith = makeAbsPath(suggested, path);
128         result.second = internalPath(
129                 QFileDialog::getOpenFileName(qApp->focusWidget(),
130                 title_, startsWith, filters));
131 #else
132         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
133
134         if (!suggested.isEmpty())
135                 dlg.selectFile(suggested);
136
137         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
138         int res = dlg.exec();
139         LYXERR(Debug::GUI, "result " << res);
140         if (res == QDialog::Accepted)
141                 result.second = internalPath(dlg.selectedFiles()[0]);
142         dlg.hide();
143 #endif
144         return result;
145 }
146
147
148 FileDialog::Result FileDialog::opendir(QString const & path,
149         QString const & suggested)
150 {
151         LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path)
152                            << "\", suggested \"" << fromqstr(suggested) << '"');
153         FileDialog::Result result;
154         result.first = FileDialog::Chosen;
155
156 #ifdef USE_NATIVE_FILEDIALOG
157         QString const startsWith = makeAbsPath(suggested, path);
158         result.second = toqstr(internal_path(fromqstr(
159                 QFileDialog::getExistingDirectory(qApp->focusWidget(),
160                 title_, startsWith))));
161 #else
162         LyXFileDialog dlg(title_, path, QStringList(qt_("Directories")),
163                 private_->b1, private_->b2);
164
165         dlg.setFileMode(QFileDialog::DirectoryOnly);
166
167         if (!suggested.isEmpty())
168                 dlg.selectFile(suggested);
169
170         LYXERR(Debug::GUI, "Synchronous FileDialog: ");
171         int res = dlg.exec();
172         LYXERR(Debug::GUI, "result " << res);
173         if (res == QDialog::Accepted)
174                 result.second = internalPath(dlg.selectedFiles()[0]);
175         dlg.hide();
176 #endif
177         return result;
178 }
179
180
181 } // namespace lyx