]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiExternal.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiExternal.cpp
1 /**
2  * \file GuiExternal.cpp
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  * \author Asger Alstrup
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiExternal.h"
16
17 #include "FuncRequest.h"
18 #include "support/gettext.h"
19 #include "Length.h"
20 #include "LyXRC.h"
21
22 #include "insets/ExternalSupport.h"
23 #include "insets/ExternalTemplate.h"
24 #include "insets/InsetExternal.h"
25
26 #include "graphics/GraphicsCache.h"
27 #include "graphics/GraphicsCacheItem.h"
28 #include "graphics/GraphicsImage.h"
29
30 #include "support/convert.h"
31 #include "support/filetools.h"
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
34 #include "support/os.h"
35
36 #include "LengthCombo.h"
37 #include "qt_helpers.h"
38 #include "Validator.h"
39
40 #include <QCheckBox>
41 #include <QGroupBox>
42 #include <QLineEdit>
43 #include <QPushButton>
44 #include <QTabWidget>
45 #include <QTextBrowser>
46
47 using namespace std;
48 using namespace lyx::support;
49
50 namespace lyx {
51 namespace frontend {
52
53 using namespace external;
54
55 namespace {
56
57 RotationDataType origins[] = {
58         RotationData::DEFAULT,
59         RotationData::TOPLEFT,
60         RotationData::BOTTOMLEFT,
61         RotationData::BASELINELEFT,
62         RotationData::CENTER,
63         RotationData::TOPCENTER,
64         RotationData::BOTTOMCENTER,
65         RotationData::BASELINECENTER,
66         RotationData::TOPRIGHT,
67         RotationData::BOTTOMRIGHT,
68         RotationData::BASELINERIGHT
69 };
70
71
72 // These are the strings, corresponding to the above, that the GUI should
73 // use. Note that they can/should be translated.
74 char const * const origin_gui_strs[] = {
75         N_("Default"),
76         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
77         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
78         N_("Top right"), N_("Bottom right"), N_("Baseline right")
79 };
80
81 external::Template getTemplate(int i)
82 {
83         if (external::TemplateManager::get().getTemplates().empty())
84                 return Template();
85         external::TemplateManager::Templates::const_iterator i1
86                 = external::TemplateManager::get().getTemplates().begin();
87         advance(i1, i);
88         return i1->second;
89 }
90
91 } // namespace anon
92
93
94 GuiExternal::GuiExternal(GuiView & lv)
95         : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
96 {
97         setupUi(this);
98
99         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
100         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
101         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
102
103         /*
104         connect(displayGB, SIGNAL(toggled(bool)),
105                 displayscaleED, SLOT(setEnabled(bool)));
106                 */
107         connect(originCO, SIGNAL(activated(int)),
108                 this, SLOT(change_adaptor()));
109         connect(aspectratioCB, SIGNAL(stateChanged(int)),
110                 this, SLOT(change_adaptor()));
111         connect(browsePB, SIGNAL(clicked()),
112                 this, SLOT(browseClicked()));
113         connect(externalCO, SIGNAL(activated(QString)),
114                 this, SLOT(templateChanged()));
115         connect(extraED, SIGNAL(textChanged(QString)),
116                 this, SLOT(extraChanged(QString)));
117         connect(extraFormatCO, SIGNAL(activated(QString)),
118                 this, SLOT(formatChanged(QString)));
119         connect(widthUnitCO, SIGNAL(activated(int)),
120                 this, SLOT(widthUnitChanged()));
121         connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
122                 this, SLOT(change_adaptor()));
123         connect(displayGB, SIGNAL(toggled(bool)),
124                 this, SLOT(change_adaptor()));
125         connect(displayscaleED, SIGNAL(textChanged(QString)),
126                 this, SLOT(change_adaptor()));
127         connect(angleED, SIGNAL(textChanged(QString)),
128                 this, SLOT(change_adaptor()));
129         connect(widthED, SIGNAL(textChanged(QString)),
130                 this, SLOT(sizeChanged()));
131         connect(heightED, SIGNAL(textChanged(QString)),
132                 this, SLOT(sizeChanged()));
133         connect(fileED, SIGNAL(textChanged(QString)),
134                 this, SLOT(change_adaptor()));
135         connect(clipCB, SIGNAL(stateChanged(int)),
136                 this, SLOT(change_adaptor()));
137         connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
138         connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
139         connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
140         connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141         connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142         connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
143
144         QIntValidator * validator = new QIntValidator(displayscaleED);
145         validator->setBottom(1);
146         displayscaleED->setValidator(validator);
147
148         angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
149
150         xlED->setValidator(new QIntValidator(xlED));
151         ybED->setValidator(new QIntValidator(ybED));
152         xrED->setValidator(new QIntValidator(xrED));
153         ytED->setValidator(new QIntValidator(ytED));
154
155         widthED->setValidator(unsignedLengthValidator(widthED));
156         heightED->setValidator(unsignedLengthValidator(heightED));
157
158         setFocusProxy(fileED);
159
160         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
161
162         bc().setOK(okPB);
163         bc().setApply(applyPB);
164         bc().setCancel(closePB);
165
166         bc().addReadOnly(fileED);
167         bc().addReadOnly(browsePB);
168         bc().addReadOnly(externalCO);
169         bc().addReadOnly(draftCB);
170         bc().addReadOnly(displayscaleED);
171         bc().addReadOnly(displayGB);
172         bc().addReadOnly(angleED);
173         bc().addReadOnly(originCO);
174         bc().addReadOnly(heightUnitCO);
175         bc().addReadOnly(heightED);
176         bc().addReadOnly(aspectratioCB);
177         bc().addReadOnly(widthUnitCO);
178         bc().addReadOnly(widthED);
179         bc().addReadOnly(clipCB);
180         bc().addReadOnly(getbbPB);
181         bc().addReadOnly(ytED);
182         bc().addReadOnly(xlED);
183         bc().addReadOnly(xrED);
184         bc().addReadOnly(ybED);
185         bc().addReadOnly(extraFormatCO);
186         bc().addReadOnly(extraED);
187
188         bc().addCheckedLineEdit(angleED, angleLA);
189         bc().addCheckedLineEdit(displayscaleED, scaleLA);
190         bc().addCheckedLineEdit(heightED, heightLA);
191         bc().addCheckedLineEdit(widthED, widthLA);
192         bc().addCheckedLineEdit(xlED, lbLA);
193         bc().addCheckedLineEdit(ybED, lbLA);
194         bc().addCheckedLineEdit(xrED, rtLA);
195         bc().addCheckedLineEdit(ytED, rtLA);
196         bc().addCheckedLineEdit(fileED, fileLA);
197
198         external::TemplateManager::Templates::const_iterator i1, i2;
199         i1 = external::TemplateManager::get().getTemplates().begin();
200         i2 = external::TemplateManager::get().getTemplates().end();
201         for (; i1 != i2; ++i1)
202                 externalCO->addItem(qt_(i1->second.lyxName));
203
204         // Fill the origins combo
205         for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
206                 originCO->addItem(qt_(origin_gui_strs[i]));
207
208         // add scale item
209         widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
210 }
211
212
213 bool GuiExternal::activateAspectratio() const
214 {
215         if (usingScale())
216                 return false;
217
218         QString const wstr = widthED->text();
219         if (wstr.isEmpty())
220                 return false;
221         bool wIsDbl;
222         double val = wstr.trimmed().toDouble(&wIsDbl);
223         if (wIsDbl && float_equal(val, 0.0, 0.05))
224                 return false;
225         Length l;
226         if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
227                 return false;
228
229         QString const hstr = heightED->text();
230         if (hstr.isEmpty())
231                 return false;
232         bool hIsDbl;
233         val = hstr.trimmed().toDouble(&hIsDbl);
234         if (hIsDbl && float_equal(val, 0.0, 0.05))
235                 return false;
236         if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
237                 return false;
238
239         return true;
240 }
241
242
243 bool GuiExternal::usingScale() const
244 {
245         return (widthUnitCO->itemData(
246                 widthUnitCO->currentIndex()).toString() == "scale");
247 }
248
249
250 void GuiExternal::bbChanged()
251 {
252         bbChanged_ = true;
253         changed();
254 }
255
256
257 void GuiExternal::browseClicked()
258 {
259         int const choice =  externalCO->currentIndex();
260         QString const template_name = toqstr(getTemplate(choice).lyxName);
261         QString const str = browse(fileED->text(), template_name);
262         if (!str.isEmpty()) {
263                 fileED->setText(str);
264                 changed();
265         }
266 }
267
268
269 void GuiExternal::change_adaptor()
270 {
271         changed();
272 }
273
274
275 void GuiExternal::extraChanged(const QString & text)
276 {
277         extra_[extraFormatCO->currentText()] = text;
278         changed();
279 }
280
281
282 void GuiExternal::formatChanged(const QString & format)
283 {
284         extraED->setText(extra_[format]);
285 }
286
287
288 void GuiExternal::getbbClicked()
289 {
290         xlED->setText("0");
291         ybED->setText("0");
292         xrED->setText("0");
293         ytED->setText("0");
294
295         string const filename = fromqstr(fileED->text());
296         if (filename.empty())
297                 return;
298
299         FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilepath())));
300
301         // try to get it from the file, if possible
302         string bb = readBB_from_PSFile(abs_file);
303         if (bb.empty()) {
304                 // we don't, so ask the Graphics Cache if it has loaded the file
305                 int width = 0;
306                 int height = 0;
307
308                 graphics::Cache & gc = graphics::Cache::get();
309                 if (gc.inCache(abs_file)) {
310                         graphics::Image const * image = gc.item(abs_file)->image();
311
312                         if (image) {
313                                 width  = image->width();
314                                 height = image->height();
315                         }
316                 }
317                 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
318         }
319
320         xlED->setText(toqstr(token(bb, ' ', 0)));
321         ybED->setText(toqstr(token(bb, ' ', 1)));
322         xrED->setText(toqstr(token(bb, ' ', 2)));
323         ytED->setText(toqstr(token(bb, ' ', 3)));
324
325         bbChanged_ = false;
326 }
327
328
329 void GuiExternal::sizeChanged()
330 {
331         aspectratioCB->setEnabled(activateAspectratio());
332         changed();
333 }
334
335
336 void GuiExternal::templateChanged()
337 {
338         updateTemplate();
339         changed();
340 }
341
342
343 void GuiExternal::widthUnitChanged()
344 {
345         if (usingScale())
346                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
347         else
348                 widthED->setValidator(unsignedLengthValidator(widthED));
349
350         heightED->setEnabled(!usingScale());
351         heightUnitCO->setEnabled(!usingScale());
352         changed();
353 }
354
355
356 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
357         external::RotationData const & data)
358 {
359         originCO.setCurrentIndex(int(data.origin()));
360         doubleToWidget(&angleED, data.angle);
361 }
362
363
364 static void getRotation(external::RotationData & data,
365         QLineEdit const & angleED, QComboBox const & originCO)
366 {
367         typedef external::RotationData::OriginType OriginType;
368
369         data.origin(static_cast<OriginType>(originCO.currentIndex()));
370         data.angle = widgetToDoubleStr(&angleED);
371 }
372
373
374 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
375         QLineEdit & heightED, LengthCombo & heightUnitCO,
376         QCheckBox & aspectratioCB,
377         external::ResizeData const & data)
378 {
379         bool using_scale = data.usingScale();
380         string scale = data.scale;
381         if (data.no_resize()) {
382                 // Everything is zero, so default to this!
383                 using_scale = true;
384                 scale = "100";
385         }
386
387         if (using_scale) {
388                 doubleToWidget(&widthED, scale);
389                 widthUnitCO.setCurrentItem("scale");
390         } else
391                 lengthToWidgets(&widthED, &widthUnitCO,
392                                 data.width.asString(), Length::defaultUnit());
393
394         string const h = data.height.zero() ? string() : data.height.asString();
395         Length::UNIT const default_unit = data.width.zero() ?
396                 Length::defaultUnit() : data.width.unit();
397         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
398
399         heightED.setEnabled(!using_scale);
400         heightUnitCO.setEnabled(!using_scale);
401
402         aspectratioCB.setChecked(data.keepAspectRatio);
403
404         bool const disable_aspectRatio = using_scale ||
405                 data.width.zero() || data.height.zero();
406         aspectratioCB.setEnabled(!disable_aspectRatio);
407 }
408
409
410 static void getSize(external::ResizeData & data,
411         QLineEdit const & widthED, QComboBox const & widthUnitCO,
412         QLineEdit const & heightED, LengthCombo const & heightUnitCO,
413         QCheckBox const & aspectratioCB, bool const scaling)
414 {
415         if (scaling) {
416                 // scaling instead of a width
417                 data.scale = widgetToDoubleStr(&widthED);
418                 data.width = Length();
419         } else {
420                 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
421                 data.scale = string();
422         }
423         data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
424         data.keepAspectRatio = aspectratioCB.isChecked();
425 }
426
427
428 void setCrop(QCheckBox & clipCB,
429         QLineEdit & xlED, QLineEdit & ybED,
430         QLineEdit & xrED, QLineEdit & ytED,
431         external::ClipData const & data)
432 {
433         clipCB.setChecked(data.clip);
434         graphics::BoundingBox const & bbox = data.bbox;
435         xlED.setText(QString::number(bbox.xl));
436         ybED.setText(QString::number(bbox.yb));
437         xrED.setText(QString::number(bbox.xr));
438         ytED.setText(QString::number(bbox.yt));
439 }
440
441
442 static void getCrop(external::ClipData & data,
443         QCheckBox const & clipCB,
444         QLineEdit const & xlED, QLineEdit const & ybED,
445         QLineEdit const & xrED, QLineEdit const & ytED,
446         bool bb_changed)
447 {
448         data.clip = clipCB.isChecked();
449
450         if (!bb_changed)
451                 return;
452
453         data.bbox.xl = xlED.text().toInt();
454         data.bbox.yb = ybED.text().toInt();
455         data.bbox.xr = xrED.text().toInt();
456         data.bbox.yt = ytED.text().toInt();
457 }
458
459
460 void GuiExternal::updateContents()
461 {
462         string const name =
463                 params_.filename.outputFilename(fromqstr(bufferFilepath()));
464         fileED->setText(toqstr(name));
465
466         int index = 0;
467         external::TemplateManager::Templates::const_iterator i1, i2;
468         i1 = external::TemplateManager::get().getTemplates().begin();
469         i2 = external::TemplateManager::get().getTemplates().end();
470         for (int i = 0; i1 != i2; ++i1, ++i) {
471                 if (i1->second.lyxName == params_.templatename()) {
472                         index = i;
473                         break;
474                 }
475         }
476
477         externalCO->setCurrentIndex(index);
478         updateTemplate();
479
480         draftCB->setChecked(params_.draft);
481
482         displayGB->setChecked(params_.display);
483         displayscaleED->setText(QString::number(params_.lyxscale));
484         bool scaled = params_.display && !isBufferReadonly() &&
485                         (params_.preview_mode != PREVIEW_INSTANT);
486         displayscaleED->setEnabled(scaled);
487         scaleLA->setEnabled(scaled);
488         displayGB->setEnabled(lyxrc.display_graphics);
489
490
491         setRotation(*angleED, *originCO, params_.rotationdata);
492
493         setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
494                 *aspectratioCB, params_.resizedata);
495
496         setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
497         bbChanged_ = !params_.clipdata.bbox.empty();
498
499         isValid();
500 }
501
502
503 void GuiExternal::updateTemplate()
504 {
505         external::Template templ = getTemplate(externalCO->currentIndex());
506         externalTB->setPlainText(qt_(templ.helpText));
507
508         // Ascertain which (if any) transformations the template supports
509         // and disable tabs and Group Boxes hosting unsupported transforms.
510         typedef vector<external::TransformID> TransformIDs;
511         TransformIDs const transformIds = templ.transformIds;
512         TransformIDs::const_iterator tr_begin = transformIds.begin();
513         TransformIDs::const_iterator const tr_end = transformIds.end();
514
515         bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
516         rotationGB->setEnabled(found);
517
518         found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
519         scaleGB->setEnabled(found);
520
521         found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
522         cropGB->setEnabled(found);
523
524         tab->setTabEnabled(tab->indexOf(sizetab),
525                 rotationGB->isEnabled()
526                 || scaleGB->isEnabled()
527                 || cropGB->isEnabled());
528
529         found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
530         optionsGB->setEnabled(found);
531
532         bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
533                         !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
534         displayscaleED->setEnabled(scaled);
535         scaleLA->setEnabled(scaled);
536
537         if (!found)
538                 return;
539
540         // Ascertain whether the template has any formats supporting
541         // the 'Extra' option
542         extra_.clear();
543         extraED->clear();
544         extraFormatCO->clear();
545
546         external::Template::Formats::const_iterator it  = templ.formats.begin();
547         external::Template::Formats::const_iterator end = templ.formats.end();
548         for (; it != end; ++it) {
549                 if (it->second.option_transformers.find(external::Extra) ==
550                     it->second.option_transformers.end())
551                         continue;
552                 string const format = it->first;
553                 string const opt = params_.extradata.get(format);
554                 extraFormatCO->addItem(toqstr(format));
555                 extra_[toqstr(format)] = toqstr(opt);
556         }
557
558         bool const enabled = extraFormatCO->count()  > 0;
559
560         optionsGB->setEnabled(enabled);
561         extraED->setEnabled(enabled && !isBufferReadonly());
562         extraFormatCO->setEnabled(enabled);
563
564         if (enabled) {
565                 extraFormatCO->setCurrentIndex(0);
566                 extraED->setText(extra_[extraFormatCO->currentText()]);
567         }
568 }
569
570
571 void GuiExternal::applyView()
572 {
573         params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
574         params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
575
576         params_.draft = draftCB->isChecked();
577         params_.lyxscale = displayscaleED->text().toInt();
578         params_.display = displayGB->isChecked();
579
580         if (rotationGB->isEnabled())
581                 getRotation(params_.rotationdata, *angleED, *originCO);
582
583         if (scaleGB->isEnabled())
584                 getSize(params_.resizedata, *widthED, *widthUnitCO,
585                         *heightED, *heightUnitCO, *aspectratioCB, usingScale());
586
587         if (cropGB->isEnabled())
588                 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
589                         *xrED, *ytED, bbChanged_);
590
591         if (optionsGB->isEnabled()) {
592                 MapType::const_iterator it = extra_.begin();
593                 MapType::const_iterator const end = extra_.end();
594                 for (; it != end; ++it)
595                         params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
596         }
597 }
598
599
600 bool GuiExternal::initialiseParams(string const & data)
601 {
602         InsetExternal::string2params(data, buffer(), params_);
603         return true;
604 }
605
606
607 void GuiExternal::clearParams()
608 {
609         params_ = InsetExternalParams();
610 }
611
612
613 void GuiExternal::dispatchParams()
614 {
615         string const lfun = InsetExternal::params2string(params_, buffer());
616         dispatch(FuncRequest(getLfun(), lfun));
617 }
618
619
620 static QStringList templateFilters(QString const & template_name)
621 {
622         /// Determine the template file extension
623         external::TemplateManager const & etm =
624                 external::TemplateManager::get();
625         external::Template const * const et_ptr =
626                 etm.getTemplateByName(fromqstr(template_name));
627
628         return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
629 }
630
631
632 QString GuiExternal::browse(QString const & input,
633                                      QString const & template_name) const
634 {
635         QString const title = qt_("Select external file");
636         QString const bufpath = bufferFilepath();
637         QStringList const filter = templateFilters(template_name);
638
639         QString const label1 = qt_("Documents|#o#O");
640         QString const dir1 = toqstr(lyxrc.document_path);
641
642         return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
643 }
644
645
646 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
647
648
649 } // namespace frontend
650 } // namespace lyx
651
652 #include "moc_GuiExternal.cpp"