]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QExternalDialog.C
enable Font cache only for MacOSX and inline width() for other platform.
[lyx.git] / src / frontends / qt4 / 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 "validators.h"
29 #include "qt_helpers.h"
30 #include "QExternal.h"
31
32 #include <QCloseEvent>
33 #include <QCheckBox>
34 #include <QPushButton>
35 #include <QTextBrowser>
36 #include <QLineEdit>
37
38
39 using lyx::support::float_equal;
40 using lyx::support::isStrDbl;
41 using std::string;
42
43 namespace lyx {
44 namespace frontend {
45
46 QExternalDialog::QExternalDialog(QExternal * form)
47         : form_(form)
48 {
49         setupUi(this);
50         connect(okPB, SIGNAL(clicked()),
51                 form, SLOT(slotOK()));
52         connect(applyPB, SIGNAL(clicked()),
53                 form, SLOT(slotApply()));
54         connect(closePB, SIGNAL(clicked()),
55                 form, SLOT(slotClose()));
56
57         connect( displayCB, SIGNAL( toggled(bool) ), showCO, SLOT( setEnabled(bool) ) );
58         connect( displayCB, SIGNAL( toggled(bool) ), displayscaleED, SLOT( setEnabled(bool) ) );
59         connect( showCO, SIGNAL( activated(const QString&) ), this, SLOT( change_adaptor() ) );
60         connect( originCO, SIGNAL( activated(int) ), this, SLOT( change_adaptor() ) );
61         connect( aspectratioCB, SIGNAL( stateChanged(int) ), this, SLOT( change_adaptor() ) );
62         connect( browsePB, SIGNAL( clicked() ), this, SLOT( browseClicked() ) );
63         connect( editPB, SIGNAL( clicked() ), this, SLOT( editClicked() ) );
64         connect( externalCO, SIGNAL( activated(const QString&) ), this, SLOT( templateChanged() ) );
65         connect( extraED, SIGNAL( textChanged(const QString&) ), this, SLOT( extraChanged(const QString&) ) );
66         connect( extraFormatCO, SIGNAL( activated(const QString&) ), this, SLOT( formatChanged(const QString&) ) );
67         connect( widthUnitCO, SIGNAL( activated(int) ), this, SLOT( widthUnitChanged() ) );
68         connect( heightUnitCO, SIGNAL( selectionChanged(LyXLength::UNIT) ), this, SLOT( change_adaptor() ) );
69         connect( displayCB, SIGNAL( stateChanged(int) ), this, SLOT( change_adaptor() ) );
70         connect( displayscaleED, SIGNAL( textChanged(const QString&) ), this, SLOT( change_adaptor() ) );
71         connect( angleED, SIGNAL( textChanged(const QString&) ), this, SLOT( change_adaptor() ) );
72         connect( widthED, SIGNAL( textChanged(const QString&) ), this, SLOT( sizeChanged() ) );
73         connect( heightED, SIGNAL( textChanged(const QString&) ), this, SLOT( sizeChanged() ) );
74         connect( fileED, SIGNAL( textChanged(const QString&) ), this, SLOT( change_adaptor() ) );
75         connect( clipCB, SIGNAL( stateChanged(int) ), this, SLOT( change_adaptor() ) );
76         connect( getbbPB, SIGNAL( clicked() ), this, SLOT( getbbClicked() ) );
77         connect( xrED, SIGNAL( textChanged(const QString&) ), this, SLOT( bbChanged() ) );
78         connect( ytED, SIGNAL( textChanged(const QString&) ), this, SLOT( bbChanged() ) );
79         connect( xlED, SIGNAL( textChanged(const QString&) ), this, SLOT( bbChanged() ) );
80         connect( ybED, SIGNAL( textChanged(const QString&) ), this, SLOT( bbChanged() ) );
81         connect( draftCB, SIGNAL( clicked() ), this, SLOT( change_adaptor() ) );
82
83         QIntValidator * validator = new QIntValidator(displayscaleED);
84         validator->setBottom(1);
85         displayscaleED->setValidator(validator);
86
87         angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
88
89         xlED->setValidator(new QIntValidator(xlED));
90         ybED->setValidator(new QIntValidator(ybED));
91         xrED->setValidator(new QIntValidator(xrED));
92         ytED->setValidator(new QIntValidator(ytED));
93
94         widthED->setValidator(unsignedLengthValidator(widthED));
95         heightED->setValidator(unsignedLengthValidator(heightED));
96
97         fileED->setValidator(new PathValidator(true, fileED));
98 }
99
100
101 void QExternalDialog::show()
102 {
103         QDialog::show();
104         fileED->setFocus();
105 }
106
107
108
109 bool QExternalDialog::activateAspectratio() const
110 {
111         if (widthUnitCO->currentIndex() == 0)
112                 return false;
113
114         string const wstr = fromqstr(widthED->text());
115         if (wstr.empty())
116                 return false;
117         bool const wIsDbl = isStrDbl(wstr);
118         if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
119                 return false;
120         LyXLength l;
121         if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
122                 return false;
123
124         string const hstr = fromqstr(heightED->text());
125         if (hstr.empty())
126                 return false;
127         bool const hIsDbl = isStrDbl(hstr);
128         if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
129                 return false;
130         if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
131                 return false;
132
133         return true;
134 }
135
136
137 void QExternalDialog::bbChanged()
138 {
139         form_->controller().bbChanged(true);
140         form_->changed();
141 }
142
143
144 void QExternalDialog::browseClicked()
145 {
146         int const choice =  externalCO->currentIndex();
147         string const template_name =
148                 form_->controller().getTemplate(choice).lyxName;
149         string const str =
150                 form_->controller().browse(fromqstr(fileED->text()),
151                                            template_name);
152         fileED->setText(toqstr(str));
153         form_->changed();
154 }
155
156
157 void QExternalDialog::change_adaptor()
158 {
159         form_->changed();
160 }
161
162
163 void QExternalDialog::closeEvent(QCloseEvent * e)
164 {
165         form_->slotWMHide();
166         e->accept();
167 }
168
169
170 void QExternalDialog::editClicked()
171 {
172         form_->controller().editExternal();
173 }
174
175
176
177 void QExternalDialog::extraChanged(const QString& text)
178 {
179         std::string const format = fromqstr(extraFormatCO->currentText());
180         form_->extra_[format] = text;
181         form_->changed();
182 }
183
184
185 void QExternalDialog::formatChanged(const QString& format)
186 {
187         extraED->setText(form_->extra_[fromqstr(format)]);
188 }
189
190
191 void QExternalDialog::getbbClicked()
192 {
193         form_->getBB();
194 }
195
196
197 void QExternalDialog::sizeChanged()
198 {
199         aspectratioCB->setEnabled(activateAspectratio());
200         form_->changed();
201 }
202
203
204 void QExternalDialog::templateChanged()
205 {
206         form_->updateTemplate();
207         form_->changed();
208 }
209
210
211 void QExternalDialog::widthUnitChanged()
212 {
213         bool useHeight = (widthUnitCO->currentIndex() > 0);
214
215         if (useHeight)
216                 widthED->setValidator(unsignedLengthValidator(widthED));
217         else
218                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
219
220         heightED->setEnabled(useHeight);
221         heightUnitCO->setEnabled(useHeight);
222         form_->changed();
223 }
224
225 } // namespace frontend
226 } // namespace lyx
227
228 #include "QExternalDialog_moc.cpp"