]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QExternal.C
9c2a3b1aaa465e43326b449824469705b0220c72
[lyx.git] / src / frontends / qt2 / QExternal.C
1 /**
2  * \file QExternal.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 "lengthcommon.h"
17 #include "lyxrc.h"
18
19 #include "controllers/ControlExternal.h"
20
21 #include "insets/ExternalTemplate.h"
22 #include "insets/insetexternal.h"
23
24 #include "support/lstrings.h"
25 #include "support/convert.h"
26
27 #include "QExternal.h"
28 #include "QExternalDialog.h"
29 #include "Qt2BC.h"
30
31 #include "checkedwidgets.h"
32 #include "lengthcombo.h"
33 #include "qt_helpers.h"
34
35 #include <qlineedit.h>
36 #include <qpushbutton.h>
37 #include <qcheckbox.h>
38 #include <qcombobox.h>
39 #include <qtabwidget.h>
40 #include <qtextview.h>
41
42 namespace external = lyx::external;
43
44 using lyx::support::isStrDbl;
45 using lyx::support::strToDbl;
46 using lyx::support::strToInt;
47 using lyx::support::token;
48 using lyx::support::trim;
49
50 using std::string;
51 using std::vector;
52 using std::find;
53
54 namespace lyx {
55 namespace frontend {
56
57 namespace {
58
59 LyXLength::UNIT defaultUnit()
60 {
61         LyXLength::UNIT default_unit = LyXLength::CM;
62         switch (lyxrc.default_papersize) {
63         case PAPER_USLETTER:
64         case PAPER_LEGALPAPER:
65         case PAPER_EXECUTIVEPAPER:
66                 default_unit = LyXLength::IN;
67                 break;
68         default:
69                 break;
70         }
71         return default_unit;
72 }
73
74
75 void setDisplay(QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
76                 external::DisplayType display, unsigned int scale,
77                 bool read_only)
78 {
79         int item = 0;
80         switch (display) {
81         case external::DefaultDisplay:
82                 item = 0;
83                 break;
84         case external::MonochromeDisplay:
85                 item = 1;
86                 break;
87         case external::GrayscaleDisplay:
88                 item = 2;
89                 break;
90         case external::ColorDisplay:
91                 item = 3;
92                 break;
93         case external::PreviewDisplay:
94                 item = 4;
95                 break;
96         case external::NoDisplay:
97                 item = 0;
98                 break;
99         }
100
101         showCO.setCurrentItem(item);
102         bool const no_display = display == external::NoDisplay;
103         showCO.setEnabled(!no_display && !read_only);
104         displayCB.setChecked(!no_display);
105         scaleED.setEnabled(!no_display && !read_only);
106         scaleED.setText(toqstr(convert<string>(scale)));
107 }
108
109
110 void getDisplay(external::DisplayType & display,
111                 unsigned int & scale,
112                 QCheckBox const & displayCB,
113                 QComboBox const & showCO,
114                 QLineEdit const & scaleED)
115 {
116         switch (showCO.currentItem()) {
117         case 0:
118                 display = external::DefaultDisplay;
119                 break;
120         case 1:
121                 display = external::MonochromeDisplay;
122                 break;
123         case 2:
124                 display = external::GrayscaleDisplay;
125                 break;
126         case 3:
127                 display = external::ColorDisplay;
128                 break;
129         case 4:
130                 display = external::PreviewDisplay;
131                 break;
132         }
133
134         if (!displayCB.isChecked())
135                 display = external::NoDisplay;
136
137         scale = strToInt(fromqstr(scaleED.text()));
138 }
139
140
141 void setRotation(QLineEdit & angleED, QComboBox & originCO,
142                  external::RotationData const & data)
143 {
144         originCO.setCurrentItem(int(data.origin()));
145         angleED.setText(toqstr(data.angle));
146 }
147
148
149 void getRotation(external::RotationData & data,
150                  QLineEdit const & angleED, QComboBox const & originCO)
151 {
152         typedef external::RotationData::OriginType OriginType;
153
154         data.origin(static_cast<OriginType>(originCO.currentItem()));
155         data.angle = fromqstr(angleED.text());
156 }
157
158
159 void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
160              QLineEdit & heightED, LengthCombo & heightUnitCO,
161              QCheckBox & aspectratioCB,
162              external::ResizeData const & data)
163 {
164         bool using_scale = data.usingScale();
165         std::string scale = data.scale;
166         if (data.no_resize()) {
167                 // Everything is zero, so default to this!
168                 using_scale = true;
169                 scale = "100";
170         }
171
172         if (using_scale) {
173                 widthED.setText(toqstr(scale));
174                 widthUnitCO.setCurrentItem(0);
175         } else {
176                 widthED.setText(toqstr(convert<string>(data.width.value())));
177                 // Because 'Scale' is position 0...
178                 // Note also that width cannot be zero here, so
179                 // we don't need to worry about the default unit.
180                 widthUnitCO.setCurrentItem(data.width.unit() + 1);
181         }
182
183         string const h = data.height.zero() ? string() : data.height.asString();
184         LyXLength::UNIT default_unit = data.width.zero() ?
185                 defaultUnit() : data.width.unit();
186         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
187
188         heightED.setEnabled(!using_scale);
189         heightUnitCO.setEnabled(!using_scale);
190
191         aspectratioCB.setChecked(data.keepAspectRatio);
192
193         bool const disable_aspectRatio = using_scale ||
194                 data.width.zero() || data.height.zero();
195         aspectratioCB.setEnabled(!disable_aspectRatio);
196 }
197
198
199 void getSize(external::ResizeData & data,
200              QLineEdit const & widthED, QComboBox const & widthUnitCO,
201              QLineEdit const & heightED, LengthCombo const & heightUnitCO,
202              QCheckBox const & aspectratioCB)
203 {
204         string const width = fromqstr(widthED.text());
205
206         if (widthUnitCO.currentItem() > 0) {
207                 // Subtract one, because scale is 0.
208                 int const unit = widthUnitCO.currentItem() - 1;
209
210                 LyXLength w;
211                 if (isValidLength(width, &w))
212                         data.width = w;
213                 else if (isStrDbl(width))
214                         data.width = LyXLength(strToDbl(width),
215                                            static_cast<LyXLength::UNIT>(unit));
216                 else
217                         data.width = LyXLength();
218
219                 data.scale = string();
220
221         } else {
222                 // scaling instead of a width
223                 data.scale = width;
224                 data.width = LyXLength();
225         }
226
227         data.height = LyXLength(widgetsToLength(&heightED, &heightUnitCO));
228
229         data.keepAspectRatio = aspectratioCB.isChecked();
230 }
231
232
233 void setCrop(QCheckBox & clipCB,
234              QLineEdit & xlED, QLineEdit & ybED,
235              QLineEdit & xrED, QLineEdit & ytED,
236              external::ClipData const & data)
237 {
238         clipCB.setChecked(data.clip);
239         graphics::BoundingBox const & bbox = data.bbox;
240         xlED.setText(toqstr(convert<string>(bbox.xl)));
241         ybED.setText(toqstr(convert<string>(bbox.yb)));
242         xrED.setText(toqstr(convert<string>(bbox.xr)));
243         ytED.setText(toqstr(convert<string>(bbox.yt)));
244 }
245
246
247 void getCrop(external::ClipData & data,
248              QCheckBox const & clipCB,
249              QLineEdit const & xlED, QLineEdit const & ybED,
250              QLineEdit const & xrED, QLineEdit const & ytED,
251              bool bb_changed)
252 {
253         data.clip = clipCB.isChecked();
254
255         if (!bb_changed)
256                 return;
257
258         data.bbox.xl = strToInt(fromqstr(xlED.text()));
259         data.bbox.yb = strToInt(fromqstr(ybED.text()));
260         data.bbox.xr = strToInt(fromqstr(xrED.text()));
261         data.bbox.yt = strToInt(fromqstr(ytED.text()));
262 }
263
264
265 void getExtra(external::ExtraData & data,
266               QExternal::MapType const & extra)
267 {
268         typedef QExternal::MapType MapType;
269         MapType::const_iterator it  = extra.begin();
270         MapType::const_iterator const end = extra.end();
271         for (; it != end; ++it)
272                 data.set(it->first, trim(fromqstr(it->second)));
273 }
274
275 } // namespace anon
276
277
278 typedef QController<ControlExternal, QView<QExternalDialog> > base_class;
279
280 QExternal::QExternal(Dialog & parent)
281         : base_class(parent, _("LyX: External Material"))
282 {}
283
284
285 void QExternal::build_dialog()
286 {
287         dialog_.reset(new QExternalDialog(this));
288
289         bcview().setOK(dialog_->okPB);
290         bcview().setApply(dialog_->applyPB);
291         bcview().setCancel(dialog_->closePB);
292
293         bcview().addReadOnly(dialog_->fileED);
294         bcview().addReadOnly(dialog_->browsePB);
295         bcview().addReadOnly(dialog_->editPB);
296         bcview().addReadOnly(dialog_->externalCO);
297         bcview().addReadOnly(dialog_->draftCB);
298         bcview().addReadOnly(dialog_->displayscaleED);
299         bcview().addReadOnly(dialog_->showCO);
300         bcview().addReadOnly(dialog_->displayCB);
301         bcview().addReadOnly(dialog_->angleED);
302         bcview().addReadOnly(dialog_->originCO);
303         bcview().addReadOnly(dialog_->heightUnitCO);
304         bcview().addReadOnly(dialog_->heightED);
305         bcview().addReadOnly(dialog_->aspectratioCB);
306         bcview().addReadOnly(dialog_->widthUnitCO);
307         bcview().addReadOnly(dialog_->widthED);
308         bcview().addReadOnly(dialog_->clipCB);
309         bcview().addReadOnly(dialog_->getbbPB);
310         bcview().addReadOnly(dialog_->ytED);
311         bcview().addReadOnly(dialog_->xlED);
312         bcview().addReadOnly(dialog_->xrED);
313         bcview().addReadOnly(dialog_->ybED);
314         bcview().addReadOnly(dialog_->extraFormatCO);
315         bcview().addReadOnly(dialog_->extraED);
316
317         addCheckedLineEdit(bcview(), dialog_->angleED, dialog_->angleLA);
318         addCheckedLineEdit(bcview(), dialog_->displayscaleED, dialog_->scaleLA);
319         addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
320         addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
321         addCheckedLineEdit(bcview(), dialog_->xlED, dialog_->lbLA);
322         addCheckedLineEdit(bcview(), dialog_->ybED, dialog_->lbLA);
323         addCheckedLineEdit(bcview(), dialog_->xrED, dialog_->rtLA);
324         addCheckedLineEdit(bcview(), dialog_->ytED, dialog_->rtLA);
325
326         std::vector<string> templates(controller().getTemplates());
327
328         for (std::vector<string>::const_iterator cit = templates.begin();
329                 cit != templates.end(); ++cit) {
330                 dialog_->externalCO->insertItem(toqstr(*cit), -1);
331         }
332
333         // Fill the origins combo
334         typedef vector<external::RotationDataType> Origins;
335         Origins const & all_origins = external::all_origins();
336         for (Origins::size_type i = 0; i != all_origins.size(); ++i)
337                 dialog_->originCO->insertItem(toqstr(external::origin_gui_str(i)));
338
339         // Fill the width combo
340         dialog_->widthUnitCO->insertItem(qt_("Scale%"));
341         for (int i = 0; i < num_units; i++)
342                 dialog_->widthUnitCO->insertItem(unit_name_gui[i], -1);
343 }
344
345
346 void QExternal::update_contents()
347 {
348         dialog_->tab->setCurrentPage(0);
349         InsetExternalParams const & params = controller().params();
350
351         string const name =
352                 params.filename.outputFilename(kernel().bufferFilepath());
353         dialog_->fileED->setText(toqstr(name));
354
355         dialog_->externalCO->setCurrentItem(
356                 controller().getTemplateNumber(params.templatename()));
357         updateTemplate();
358
359         dialog_->draftCB->setChecked(params.draft);
360
361         setDisplay(*dialog_->displayCB, *dialog_->showCO,
362                    *dialog_->displayscaleED,
363                    params.display, params.lyxscale, readOnly());
364
365         setRotation(*dialog_->angleED, *dialog_->originCO, params.rotationdata);
366
367         setSize(*dialog_->widthED, *dialog_->widthUnitCO,
368                 *dialog_->heightED, *dialog_->heightUnitCO,
369                 *dialog_->aspectratioCB,
370                 params.resizedata);
371
372         setCrop(*dialog_->clipCB,
373                 *dialog_->xlED, *dialog_->ybED,
374                 *dialog_->xrED, *dialog_->ytED,
375                 params.clipdata);
376         controller().bbChanged(!params.clipdata.bbox.empty());
377
378         isValid();
379 }
380
381
382 void QExternal::updateTemplate()
383 {
384         external::Template templ =
385                 controller().getTemplate(dialog_->externalCO->currentItem());
386         dialog_->externalTV->setText(toqstr(templ.helpText));
387
388         // Ascertain which (if any) transformations the template supports
389         // and disable tabs hosting unsupported transforms.
390         typedef vector<external::TransformID> TransformIDs;
391         TransformIDs const transformIds = templ.transformIds;
392         TransformIDs::const_iterator tr_begin = transformIds.begin();
393         TransformIDs::const_iterator const tr_end = transformIds.end();
394
395         bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
396         dialog_->tab->setTabEnabled(dialog_->rotatetab, found);
397
398         found = find(tr_begin, tr_end, external::Resize) != tr_end;
399         dialog_->tab->setTabEnabled(dialog_->scaletab, found);
400
401         found = find(tr_begin, tr_end, external::Clip) != tr_end;
402         dialog_->tab->setTabEnabled(dialog_->croptab, found);
403
404         found = find(tr_begin, tr_end, external::Extra) != tr_end;
405         dialog_->tab->setTabEnabled(dialog_->optionstab, found);
406         if (!found)
407                 return;
408
409         // Ascertain whether the template has any formats supporting
410         // the 'Extra' option
411         QLineEdit * const extraED = dialog_->extraED;
412         QComboBox * const extraCB = dialog_->extraFormatCO;
413
414         extra_.clear();
415         extraED->clear();
416         extraCB->clear();
417
418         external::Template::Formats::const_iterator it  = templ.formats.begin();
419         external::Template::Formats::const_iterator end = templ.formats.end();
420         for (; it != end; ++it) {
421                 if (it->second.option_transformers.find(external::Extra) ==
422                     it->second.option_transformers.end())
423                         continue;
424                 string const format = it->first;
425                 string const opt = controller().params().extradata.get(format);
426                 extraCB->insertItem(toqstr(format));
427                 extra_[format] = toqstr(opt);
428         }
429
430         bool const enabled = extraCB->count()  > 0;
431
432         dialog_->tab->setTabEnabled(dialog_->optionstab, enabled);
433         extraED->setEnabled(enabled && !kernel().isBufferReadonly());
434         extraCB->setEnabled(enabled);
435
436         if (enabled) {
437                 extraCB->setCurrentItem(0);
438                 extraED->setText(extra_[fromqstr(extraCB->currentText())]);
439         }
440 }
441
442
443 void QExternal::apply()
444 {
445         InsetExternalParams params = controller().params();
446
447         params.filename.set(fromqstr(dialog_->fileED->text()),
448                             kernel().bufferFilepath());
449
450         params.settemplate(controller().getTemplate(
451                                    dialog_->externalCO->currentItem()).lyxName);
452
453         params.draft = dialog_->draftCB->isChecked();
454
455         getDisplay(params.display, params.lyxscale,
456                    *dialog_->displayCB, *dialog_->showCO,
457                    *dialog_->displayscaleED);
458
459         if (dialog_->tab->isTabEnabled(dialog_->rotatetab))
460                 getRotation(params.rotationdata,
461                             *dialog_->angleED, *dialog_->originCO);
462
463         if (dialog_->tab->isTabEnabled(dialog_->scaletab))
464                 getSize(params.resizedata,
465                         *dialog_->widthED, *dialog_->widthUnitCO,
466                         *dialog_->heightED, *dialog_->heightUnitCO,
467                         *dialog_->aspectratioCB);
468
469         if (dialog_->tab->isTabEnabled(dialog_->croptab))
470                 getCrop(params.clipdata,
471                         *dialog_->clipCB,
472                         *dialog_->xlED, *dialog_->ybED,
473                         *dialog_->xrED, *dialog_->ytED,
474                         controller().bbChanged());
475
476         if (dialog_->tab->isTabEnabled(dialog_->optionstab))
477                 getExtra(params.extradata, extra_);
478
479         controller().setParams(params);
480 }
481
482
483 void QExternal::getBB()
484 {
485         dialog_->xlED->setText("0");
486         dialog_->ybED->setText("0");
487         dialog_->xrED->setText("0");
488         dialog_->ytED->setText("0");
489
490         string const filename = fromqstr(dialog_->fileED->text());
491         if (filename.empty())
492                 return;
493
494         string const bb = controller().readBB(filename);
495         if (bb.empty())
496                 return;
497
498         dialog_->xlED->setText(toqstr(token(bb, ' ', 0)));
499         dialog_->ybED->setText(toqstr(token(bb, ' ', 1)));
500         dialog_->xrED->setText(toqstr(token(bb, ' ', 2)));
501         dialog_->ytED->setText(toqstr(token(bb, ' ', 3)));
502
503         controller().bbChanged(false);
504 }
505
506 } // namespace frontend
507 } // namespace lyx