]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QExternalDialog.C
Handle Qt-style file filters like
[features.git] / src / frontends / qt2 / QExternalDialog.C
1 /**
2  * \file QExternalDialog.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 Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "insets/ExternalTemplate.h"
15
16 #include "controllers/ButtonController.h"
17 #include "controllers/ControlExternal.h"
18
19 #include "support/lstrings.h"
20 #include "support/lyxlib.h"
21
22 #include "QExternalDialog.h"
23
24 #include "lengthcombo.h"
25 #include "lengthvalidator.h"
26 #include "qt_helpers.h"
27 #include "QExternal.h"
28
29 #include <qcheckbox.h>
30 #include <qcombobox.h>
31 #include <qpushbutton.h>
32 #include <qfiledialog.h>
33 #include <qtextview.h>
34 #include <qlineedit.h>
35 #include <qvalidator.h>
36
37 using lyx::support::float_equal;
38 using lyx::support::isStrDbl;
39 using lyx::support::strToDbl;
40 using std::string;
41
42
43 namespace {
44
45 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
46 {
47         LengthValidator * v = new LengthValidator(ed);
48         v->setBottom(LyXLength());
49         return v;
50 }
51
52 } // namespace anon
53
54
55 QExternalDialog::QExternalDialog(QExternal * form)
56         : QExternalDialogBase(0, 0, false, 0),
57           form_(form)
58 {
59         connect(okPB, SIGNAL(clicked()),
60                 form, SLOT(slotOK()));
61         connect(applyPB, SIGNAL(clicked()),
62                 form, SLOT(slotApply()));
63         connect(closePB, SIGNAL(clicked()),
64                 form, SLOT(slotClose()));
65
66         QIntValidator * validator = new QIntValidator(displayscaleED);
67         validator->setBottom(1);
68         displayscaleED->setValidator(validator);
69
70         angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
71
72         xlED->setValidator(new QIntValidator(xlED));
73         ybED->setValidator(new QIntValidator(ybED));
74         xrED->setValidator(new QIntValidator(xrED));
75         ytED->setValidator(new QIntValidator(ytED));
76
77         // The width is initially set to 'scale' and so should accept
78         // a pure number only.
79         widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
80         heightED->setValidator(unsignedLengthValidator(heightED));
81 }
82
83
84 void QExternalDialog::show()
85 {
86         QExternalDialogBase::show();
87         fileED->setFocus();
88 }
89
90
91
92 bool QExternalDialog::activateAspectratio() const
93 {
94         if (widthUnitCO->currentItem() == 0)
95                 return false;
96
97         string const wstr = fromqstr(widthED->text());
98         if (wstr.empty())
99                 return false;
100         bool const wIsDbl = isStrDbl(wstr);
101         if (wIsDbl && float_equal(strToDbl(wstr), 0.0, 0.05))
102                 return false;
103         LyXLength l;
104         if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
105                 return false;
106
107         string const hstr = fromqstr(heightED->text());
108         if (hstr.empty())
109                 return false;
110         bool const hIsDbl = isStrDbl(hstr);
111         if (hIsDbl && float_equal(strToDbl(hstr), 0.0, 0.05))
112                 return false;
113         if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
114                 return false;
115
116         return true;
117 }
118
119
120 void QExternalDialog::bbChanged()
121 {
122         form_->controller().bbChanged(true);
123         form_->changed();
124 }
125
126
127 void QExternalDialog::browseClicked()
128 {
129         int const choice =  externalCO->currentItem();
130         string const template_name =
131                 form_->controller().getTemplate(choice).lyxName;
132         string const str =
133                 form_->controller().browse(fromqstr(fileED->text()),
134                                            template_name);
135         fileED->setText(toqstr(str));
136         form_->changed();
137 }
138
139
140 void QExternalDialog::change_adaptor()
141 {
142         form_->changed();
143 }
144
145
146 void QExternalDialog::closeEvent(QCloseEvent * e)
147 {
148         form_->slotWMHide();
149         e->accept();
150 }
151
152
153 void QExternalDialog::editClicked()
154 {
155         form_->controller().editExternal();
156 }
157
158
159
160 void QExternalDialog::extraChanged(const QString& text)
161 {
162         std::string const format = fromqstr(extraFormatCO->currentText());
163         form_->extra_[format] = text;
164         form_->changed();
165 }
166
167
168 void QExternalDialog::formatChanged(const QString& format)
169 {
170         extraED->setText(form_->extra_[fromqstr(format)]);
171 }
172
173
174 void QExternalDialog::getbbClicked()
175 {
176         form_->getBB();
177 }
178
179
180 void QExternalDialog::sizeChanged()
181 {
182         aspectratioCB->setEnabled(activateAspectratio());
183         form_->changed();
184 }
185
186
187 void QExternalDialog::templateChanged()
188 {
189         form_->updateTemplate();
190         form_->changed();
191 }
192
193
194 void QExternalDialog::widthUnitChanged()
195 {
196         bool useHeight = (widthUnitCO->currentItem() > 0);
197
198         if (useHeight)
199                 widthED->setValidator(unsignedLengthValidator(widthED));
200         else
201                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
202
203         heightED->setEnabled(useHeight);
204         heightUnitCO->setEnabled(useHeight);
205         form_->changed();
206 }