]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LyXFileDialog.cpp
do what the FIXME suggested
[lyx.git] / src / frontends / qt4 / LyXFileDialog.cpp
1 /**
2  * \file LyXFileDialog.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "LyXFileDialog.h"
14
15 #include "qt_helpers.h"
16
17 #include "support/FileFilterList.h"
18 #include "support/lstrings.h"
19
20 #include <QApplication>
21 #include <QToolButton>
22 #include <QHBoxLayout>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 /// return the Qt form of the label
30 static docstring const getLabel(docstring const & ucs4str)
31 {
32         // FIXME UNICODE
33         string str = to_utf8(ucs4str);
34         string label;
35         string sc = split(str, label, '|');
36         if (sc.length() < 2)
37                 return from_utf8(label);
38         size_t pos = label.find(sc[1]);
39         if (pos != string::npos)
40                 label.insert(pos, 1, '&');
41         return from_utf8(label);
42 }
43
44
45 LyXFileDialog::LyXFileDialog(docstring const & t,
46                              docstring const & p,
47                              support::FileFilterList const & filters,
48                              FileDialog::Button const & b1,
49                              FileDialog::Button const & b2)
50                                  // FIXME replace that with guiApp->currentView()
51         : QFileDialog(qApp->focusWidget(),
52                       toqstr(t), toqstr(p), toqstr(filters.as_string()))
53 {
54         QString const path = toqstr(p);
55         QDir dir(path);
56         // FIXME: workaround for a bug in qt which makes LyX crash
57         // with hidden paths (bug 4513). Recheck with recent Qt versions.
58         if (path.contains("/."))
59                 dir.setFilter(QDir::Hidden);
60         setDirectory(dir);
61         setWindowTitle(toqstr(t));
62
63         QList<QHBoxLayout *> layout = findChildren<QHBoxLayout *>();
64
65         if (!b1.first.empty()) {
66                 b1_dir_ = b1.second;
67                 QToolButton * tb = new QToolButton(this);
68                 connect(tb, SIGNAL(clicked()), this, SLOT(button1Clicked()));
69                 tb->setText(toqstr(getLabel(b1.first)));
70                 layout.at(0)->addWidget(tb);
71         }
72
73         if (!b2.first.empty()) {
74                 b2_dir_ = b2.second;
75                 QToolButton * tb = new QToolButton(this);
76                 connect(tb, SIGNAL(clicked()), this, SLOT(button2Clicked()));
77                 tb->setText(toqstr(getLabel(b2.first)));
78                 layout.at(0)->addWidget(tb);
79         }
80 }
81
82
83 void LyXFileDialog::button1Clicked()
84 {
85         setDirectory(toqstr(b1_dir_));
86 }
87
88
89 void LyXFileDialog::button2Clicked()
90 {
91         setDirectory(toqstr(b2_dir_));
92 }
93
94 } // namespace lyx
95
96 #include "LyXFileDialog_moc.cpp"