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