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