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