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