]> git.lyx.org Git - features.git/blob - src/frontends/qt2/FileDialog_private.C
Handle Qt-style file filters like
[features.git] / src / frontends / qt2 / FileDialog_private.C
1 /**
2  * \file FileDialog_private.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13
14 #include "FileDialog_private.h"
15 #include "qt_helpers.h"
16
17 #include "support/globbing.h"
18 #include "support/lstrings.h"
19
20 #include <qapplication.h>
21 #include <qtoolbutton.h>
22
23 using lyx::support::split;
24
25 using std::string;
26
27
28 namespace {
29
30 /// return the Qt form of the label
31 string const getLabel(string const & str) {
32         string label;
33         string sc(split(str, label, '|'));
34         if (sc.length() < 2)
35                 return label;
36         string::size_type pos = label.find(sc[1]);
37         if (pos == string::npos)
38                 return label;
39         label.insert(pos, 1, '&');
40         return label;
41 }
42
43 } // namespace anon
44
45
46 LyXFileDialog::LyXFileDialog(string const & p,
47                              lyx::support::FileFilterList const & filters,
48                              string const & t,
49                              FileDialog::Button const & b1,
50                              FileDialog::Button const & b2)
51         : QFileDialog(toqstr(p), toqstr(filters.str(true)),
52                       qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(), toqstr(t), true),
53           b1_(0), b2_(0)
54 {
55         setCaption(toqstr(t));
56
57         if (!b1.first.empty()) {
58                 b1_dir_ = b1.second;
59                 b1_ = new QToolButton(this);
60                 connect(b1_, SIGNAL(clicked()), this, SLOT(buttonClicked()));
61                 b1_->setText(toqstr(getLabel(b1.first)));
62                 addToolButton(b1_, true);
63         }
64
65         if (!b2.first.empty()) {
66                 b2_dir_ = b2.second;
67                 b2_ = new QToolButton(this);
68                 connect(b2_, SIGNAL(clicked()), this, SLOT(buttonClicked()));
69                 b2_->setText(toqstr(getLabel(b2.first)));
70                 addToolButton(b2_);
71         }
72 }
73
74
75 void LyXFileDialog::buttonClicked()
76 {
77         if (sender() == b1_)
78                 setDir(toqstr(b1_dir_));
79         else if (sender() == b2_)
80                 setDir(toqstr(b2_dir_));
81 }