]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FileDialog.C
enable Font cache only for MacOSX and inline width() for other platform.
[lyx.git] / src / frontends / qt4 / FileDialog.C
1 /**
2  * \file qt4/FileDialog.C
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 "frontends/FileDialog.h"
15
16 #include "FileDialog_private.h"
17 #include "qt_helpers.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "support/filefilterlist.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  *
31  * Therefore there is a tradeoff in enabling or disabling this (JMarc)
32  */
33 #ifdef Q_WS_MACX
34 #define USE_NATIVE_FILEDIALOG 1
35 #endif
36
37 #ifdef USE_NATIVE_FILEDIALOG
38 #include <qapplication.h>
39 #include "support/filetools.h"
40 using lyx::support::makeAbsPath;
41 #endif
42
43 using lyx::support::FileFilterList;
44
45 using std::endl;
46 using std::string;
47
48
49 class FileDialog::Private {
50 public:
51         Button b1;
52         Button b2;
53 };
54
55
56 FileDialog::FileDialog(string const & t,
57                        kb_action s, Button b1, Button b2)
58         : private_(new FileDialog::Private), title_(t), success_(s)
59 {
60         private_->b1 = b1;
61         private_->b2 = b2;
62 }
63
64
65 FileDialog::~FileDialog()
66 {
67         delete private_;
68 }
69
70
71 FileDialog::Result const FileDialog::save(string const & path,
72                                           FileFilterList const & filters,
73                                           string const & suggested)
74 {
75         lyxerr[Debug::GUI] << "Select with path \"" << path
76                            << "\", mask \"" << filters.as_string()
77                            << "\", suggested \"" << suggested << '"' << endl;
78         FileDialog::Result result;
79         result.first = FileDialog::Chosen;
80
81 #ifdef USE_NATIVE_FILEDIALOG
82         string const startsWith = makeAbsPath(suggested, path);
83         result.second = fromqstr(QFileDialog::getSaveFileName(
84                 qApp->focusWidget(),
85                 title_.c_str(), toqstr(startsWith), toqstr(filters.as_string()) ));
86 #else
87         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
88         dlg.setFileMode(QFileDialog::AnyFile);
89         dlg.setAcceptMode(QFileDialog::AcceptSave);
90
91         if (!suggested.empty())
92                 dlg.selectFile(toqstr(suggested));
93
94         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
95         int res = dlg.exec();
96         lyxerr[Debug::GUI] << "result " << res << endl;
97         if (res == QDialog::Accepted)
98                 result.second = fromqstr(dlg.selectedFiles()[0]);
99         dlg.hide();
100 #endif
101         return result;
102 }
103
104
105 FileDialog::Result const FileDialog::open(string const & path,
106                                           FileFilterList const & filters,
107                                           string const & suggested)
108 {
109         lyxerr[Debug::GUI] << "Select with path \"" << path
110                            << "\", mask \"" << filters.as_string()
111                            << "\", suggested \"" << suggested << '"' << endl;
112         FileDialog::Result result;
113         result.first = FileDialog::Chosen;
114
115 #ifdef USE_NATIVE_FILEDIALOG
116         string const startsWith = makeAbsPath(suggested, path);
117         result.second = fromqstr(QFileDialog::getOpenFileName(
118                 qApp->focusWidget(), 
119                 title_.c_str(), toqstr(startsWith), toqstr(filters.as_string()) ));
120 #else
121         LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
122
123         if (!suggested.empty())
124                 dlg.selectFile(toqstr(suggested));
125
126         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
127         int res = dlg.exec();
128         lyxerr[Debug::GUI] << "result " << res << endl;
129         if (res == QDialog::Accepted)
130                 result.second = fromqstr(dlg.selectedFiles()[0]);
131         dlg.hide();
132 #endif
133         return result;
134 }
135
136
137 FileDialog::Result const FileDialog::opendir(string const & path,
138                                             string const & suggested)
139 {
140         lyxerr[Debug::GUI] << "Select with path \"" << path
141                            << "\", suggested \"" << suggested << '"' << endl;
142         FileDialog::Result result;
143         result.first = FileDialog::Chosen;
144
145 #ifdef USE_NATIVE_FILEDIALOG
146         string const startsWith = makeAbsPath(suggested, path);
147         result.second = fromqstr(QFileDialog::getExistingDirectory(
148                 qApp->focusWidget(),
149                 title_.c_str(),toqstr(startsWith) ));
150 #else
151         FileFilterList const filter(lyx::to_utf8(_("Directories")));
152
153         LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
154
155         dlg.setFileMode(QFileDialog::DirectoryOnly);
156
157         if (!suggested.empty())
158                 dlg.selectFile(toqstr(suggested));
159
160         lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
161         int res = dlg.exec();
162         lyxerr[Debug::GUI] << "result " << res << endl;
163         if (res == QDialog::Accepted)
164                 result.second = fromqstr(dlg.selectedFiles()[0]);
165         dlg.hide();
166 #endif
167         return result;
168 }