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