]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiExternal.cpp
Give us some useful tooltips for modules.
[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 "Buffer.h"
18 #include "FuncRequest.h"
19 #include "support/gettext.h"
20 #include "Length.h"
21 #include "LyXRC.h"
22
23 #include "insets/ExternalSupport.h"
24 #include "insets/ExternalTemplate.h"
25 #include "insets/InsetExternal.h"
26
27 #include "graphics/GraphicsCache.h"
28 #include "graphics/GraphicsCacheItem.h"
29 #include "graphics/GraphicsImage.h"
30
31 #include "frontends/alert.h"
32
33 #include "support/convert.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/FileFilterList.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
38 #include "support/lyxlib.h"
39 #include "support/os.h"
40
41 #include "LengthCombo.h"
42 #include "qt_helpers.h"
43 #include "Validator.h"
44
45 #include <QCheckBox>
46 #include <QLineEdit>
47 #include <QPushButton>
48 #include <QTabWidget>
49 #include <QTextBrowser>
50
51 using namespace std;
52 using namespace lyx::support;
53
54 namespace lyx {
55 namespace frontend {
56
57 using namespace external;
58
59 namespace {
60
61 RotationDataType origins_array[] = {
62         RotationData::DEFAULT,
63         RotationData::TOPLEFT,
64         RotationData::BOTTOMLEFT,
65         RotationData::BASELINELEFT,
66         RotationData::CENTER,
67         RotationData::TOPCENTER,
68         RotationData::BOTTOMCENTER,
69         RotationData::BASELINECENTER,
70         RotationData::TOPRIGHT,
71         RotationData::BOTTOMRIGHT,
72         RotationData::BASELINERIGHT
73 };
74
75
76 size_type const origins_array_size =
77 sizeof(origins_array) / sizeof(origins_array[0]);
78
79 vector<external::RotationDataType> const
80 all_origins(origins_array, origins_array + origins_array_size);
81
82 // These are the strings, corresponding to the above, that the GUI should
83 // use. Note that they can/should be translated.
84 char const * const origin_gui_strs[] = {
85         N_("Default"),
86         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
87         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
88         N_("Top right"), N_("Bottom right"), N_("Baseline right")
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         connect(displayCB, SIGNAL(toggled(bool)),
104                 showCO, SLOT(setEnabled(bool)));
105         connect(displayCB, SIGNAL(toggled(bool)),
106                 displayscaleED, SLOT(setEnabled(bool)));
107         connect(showCO, SIGNAL(activated(QString)),
108                 this, SLOT(change_adaptor()));
109         connect(originCO, SIGNAL(activated(int)),
110                 this, SLOT(change_adaptor()));
111         connect(aspectratioCB, SIGNAL(stateChanged(int)),
112                 this, SLOT(change_adaptor()));
113         connect(browsePB, SIGNAL(clicked()),
114                 this, SLOT(browseClicked()));
115         connect(embedCB, SIGNAL(toggled(bool)),
116                 this, SLOT(change_adaptor()));
117         connect(editPB, SIGNAL(clicked()),
118                 this, SLOT(editClicked()));
119         connect(externalCO, SIGNAL(activated(QString)),
120                 this, SLOT(templateChanged()));
121         connect(extraED, SIGNAL(textChanged(QString)),
122                 this, SLOT(extraChanged(QString)));
123         connect(extraFormatCO, SIGNAL(activated(QString)),
124                 this, SLOT(formatChanged(QString)));
125         connect(widthUnitCO, SIGNAL(activated(int)),
126                 this, SLOT(widthUnitChanged()));
127         connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
128                 this, SLOT(change_adaptor()));
129         connect(displayCB, SIGNAL(stateChanged(int)),
130                 this, SLOT(change_adaptor()));
131         connect(displayscaleED, SIGNAL(textChanged(QString)),
132                 this, SLOT(change_adaptor()));
133         connect(angleED, SIGNAL(textChanged(QString)),
134                 this, SLOT(change_adaptor()));
135         connect(widthED, SIGNAL(textChanged(QString)),
136                 this, SLOT(sizeChanged()));
137         connect(heightED, SIGNAL(textChanged(QString)),
138                 this, SLOT(sizeChanged()));
139         connect(fileED, SIGNAL(textChanged(QString)),
140                 this, SLOT(change_adaptor()));
141         connect(clipCB, SIGNAL(stateChanged(int)),
142                 this, SLOT(change_adaptor()));
143         connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
144         connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
145         connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
146         connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
147         connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
148         connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
149
150         QIntValidator * validator = new QIntValidator(displayscaleED);
151         validator->setBottom(1);
152         displayscaleED->setValidator(validator);
153
154         angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
155
156         xlED->setValidator(new QIntValidator(xlED));
157         ybED->setValidator(new QIntValidator(ybED));
158         xrED->setValidator(new QIntValidator(xrED));
159         ytED->setValidator(new QIntValidator(ytED));
160
161         widthED->setValidator(unsignedLengthValidator(widthED));
162         heightED->setValidator(unsignedLengthValidator(heightED));
163
164         setFocusProxy(fileED);
165
166         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
167
168         bc().setOK(okPB);
169         bc().setApply(applyPB);
170         bc().setCancel(closePB);
171
172         bc().addReadOnly(fileED);
173         bc().addReadOnly(browsePB);
174         bc().addReadOnly(editPB);
175         bc().addReadOnly(externalCO);
176         bc().addReadOnly(draftCB);
177         bc().addReadOnly(displayscaleED);
178         bc().addReadOnly(showCO);
179         bc().addReadOnly(displayCB);
180         bc().addReadOnly(angleED);
181         bc().addReadOnly(originCO);
182         bc().addReadOnly(heightUnitCO);
183         bc().addReadOnly(heightED);
184         bc().addReadOnly(aspectratioCB);
185         bc().addReadOnly(widthUnitCO);
186         bc().addReadOnly(widthED);
187         bc().addReadOnly(clipCB);
188         bc().addReadOnly(getbbPB);
189         bc().addReadOnly(ytED);
190         bc().addReadOnly(xlED);
191         bc().addReadOnly(xrED);
192         bc().addReadOnly(ybED);
193         bc().addReadOnly(extraFormatCO);
194         bc().addReadOnly(extraED);
195
196         bc().addCheckedLineEdit(angleED, angleLA);
197         bc().addCheckedLineEdit(displayscaleED, scaleLA);
198         bc().addCheckedLineEdit(heightED, heightLA);
199         bc().addCheckedLineEdit(widthED, widthLA);
200         bc().addCheckedLineEdit(xlED, lbLA);
201         bc().addCheckedLineEdit(ybED, lbLA);
202         bc().addCheckedLineEdit(xrED, rtLA);
203         bc().addCheckedLineEdit(ytED, rtLA);
204         bc().addCheckedLineEdit(fileED, fileLA);
205
206         vector<string> templates = getTemplates();
207
208         for (vector<string>::const_iterator cit = templates.begin();
209                 cit != templates.end(); ++cit) {
210                 externalCO->addItem(qt_(*cit));
211         }
212
213         // Fill the origins combo
214         for (size_t i = 0; i != all_origins.size(); ++i)
215                 originCO->addItem(qt_(origin_gui_strs[i]));
216
217         // Fill the width combo
218         widthUnitCO->addItem(qt_("Scale%"));
219         for (int i = 0; i < num_units; i++)
220                 widthUnitCO->addItem(qt_(unit_name_gui[i]));
221 }
222
223
224 bool GuiExternal::activateAspectratio() const
225 {
226         if (widthUnitCO->currentIndex() == 0)
227                 return false;
228
229         string const wstr = fromqstr(widthED->text());
230         if (wstr.empty())
231                 return false;
232         bool const wIsDbl = isStrDbl(wstr);
233         if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
234                 return false;
235         Length l;
236         if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
237                 return false;
238
239         string const hstr = fromqstr(heightED->text());
240         if (hstr.empty())
241                 return false;
242         bool const hIsDbl = isStrDbl(hstr);
243         if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
244                 return false;
245         if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
246                 return false;
247
248         return true;
249 }
250
251
252 void GuiExternal::bbChanged()
253 {
254         bbChanged_ = true;
255         changed();
256 }
257
258
259 void GuiExternal::browseClicked()
260 {
261         int const choice =  externalCO->currentIndex();
262         QString const template_name = toqstr(getTemplate(choice).lyxName);
263         QString const str = browse(fileED->text(), template_name);
264         if (!str.isEmpty()) {
265                 fileED->setText(str);
266                 changed();
267         }
268 }
269
270
271 void GuiExternal::change_adaptor()
272 {
273         changed();
274 }
275
276
277 void GuiExternal::editClicked()
278 {
279         editExternal();
280 }
281
282
283
284 void GuiExternal::extraChanged(const QString& text)
285 {
286         string const format = fromqstr(extraFormatCO->currentText());
287         extra_[format] = text;
288         changed();
289 }
290
291
292 void GuiExternal::formatChanged(const QString& format)
293 {
294         extraED->setText(extra_[fromqstr(format)]);
295 }
296
297
298 void GuiExternal::getbbClicked()
299 {
300         getBB();
301 }
302
303
304 void GuiExternal::sizeChanged()
305 {
306         aspectratioCB->setEnabled(activateAspectratio());
307         changed();
308 }
309
310
311 void GuiExternal::templateChanged()
312 {
313         updateTemplate();
314         changed();
315 }
316
317
318 void GuiExternal::widthUnitChanged()
319 {
320         bool useHeight = (widthUnitCO->currentIndex() > 0);
321
322         if (useHeight)
323                 widthED->setValidator(unsignedLengthValidator(widthED));
324         else
325                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
326
327         heightED->setEnabled(useHeight);
328         heightUnitCO->setEnabled(useHeight);
329         changed();
330 }
331
332
333 static Length::UNIT defaultUnit()
334 {
335         Length::UNIT default_unit = Length::CM;
336         switch (lyxrc.default_papersize) {
337         case PAPER_USLETTER:
338         case PAPER_USLEGAL:
339         case PAPER_USEXECUTIVE:
340                 default_unit = Length::IN;
341                 break;
342         default:
343                 break;
344         }
345         return default_unit;
346 }
347
348
349 static void setDisplay(
350         QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
351         external::DisplayType display, unsigned int scale, bool read_only)
352 {
353         int item = 0;
354         switch (display) {
355         case external::DefaultDisplay:
356                 item = 0;
357                 break;
358         case external::MonochromeDisplay:
359                 item = 1;
360                 break;
361         case external::GrayscaleDisplay:
362                 item = 2;
363                 break;
364         case external::ColorDisplay:
365                 item = 3;
366                 break;
367         case external::PreviewDisplay:
368                 item = 4;
369                 break;
370         case external::NoDisplay:
371                 item = 0;
372                 break;
373         }
374
375         showCO.setCurrentIndex(item);
376         bool const no_display = display == external::NoDisplay;
377         showCO.setEnabled(!no_display && !read_only);
378         displayCB.setChecked(!no_display);
379         scaleED.setEnabled(!no_display && !read_only);
380         scaleED.setText(QString::number(scale));
381 }
382
383
384 static void getDisplay(external::DisplayType & display,
385                 unsigned int & scale,
386                 QCheckBox const & displayCB,
387                 QComboBox const & showCO,
388                 QLineEdit const & scaleED)
389 {
390         switch (showCO.currentIndex()) {
391         case 0:
392                 display = external::DefaultDisplay;
393                 break;
394         case 1:
395                 display = external::MonochromeDisplay;
396                 break;
397         case 2:
398                 display = external::GrayscaleDisplay;
399                 break;
400         case 3:
401                 display = external::ColorDisplay;
402                 break;
403         case 4:
404                 display = external::PreviewDisplay;
405                 break;
406         }
407
408         if (!displayCB.isChecked())
409                 display = external::NoDisplay;
410
411         scale = scaleED.text().toInt();
412 }
413
414
415 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
416         external::RotationData const & data)
417 {
418         originCO.setCurrentIndex(int(data.origin()));
419         angleED.setText(toqstr(data.angle));
420 }
421
422
423 static void getRotation(external::RotationData & data,
424         QLineEdit const & angleED, QComboBox const & originCO)
425 {
426         typedef external::RotationData::OriginType OriginType;
427
428         data.origin(static_cast<OriginType>(originCO.currentIndex()));
429         data.angle = fromqstr(angleED.text());
430 }
431
432
433 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
434         QLineEdit & heightED, LengthCombo & heightUnitCO,
435         QCheckBox & aspectratioCB,
436         external::ResizeData const & data)
437 {
438         bool using_scale = data.usingScale();
439         string scale = data.scale;
440         if (data.no_resize()) {
441                 // Everything is zero, so default to this!
442                 using_scale = true;
443                 scale = "100";
444         }
445
446         if (using_scale) {
447                 widthED.setText(toqstr(scale));
448                 widthUnitCO.setCurrentIndex(0);
449         } else {
450                 widthED.setText(QString::number(data.width.value()));
451                 // Because 'Scale' is position 0...
452                 // Note also that width cannot be zero here, so
453                 // we don't need to worry about the default unit.
454                 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
455         }
456
457         string const h = data.height.zero() ? string() : data.height.asString();
458         Length::UNIT default_unit = data.width.zero() ?
459                 defaultUnit() : data.width.unit();
460         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
461
462         heightED.setEnabled(!using_scale);
463         heightUnitCO.setEnabled(!using_scale);
464
465         aspectratioCB.setChecked(data.keepAspectRatio);
466
467         bool const disable_aspectRatio = using_scale ||
468                 data.width.zero() || data.height.zero();
469         aspectratioCB.setEnabled(!disable_aspectRatio);
470 }
471
472
473 static void getSize(external::ResizeData & data,
474         QLineEdit const & widthED, QComboBox const & widthUnitCO,
475         QLineEdit const & heightED, LengthCombo const & heightUnitCO,
476         QCheckBox const & aspectratioCB)
477 {
478         string const width = fromqstr(widthED.text());
479
480         if (widthUnitCO.currentIndex() > 0) {
481                 // Subtract one, because scale is 0.
482                 int const unit = widthUnitCO.currentIndex() - 1;
483
484                 Length w;
485                 if (isValidLength(width, &w))
486                         data.width = w;
487                 else if (isStrDbl(width))
488                         data.width = Length(convert<double>(width),
489                                            static_cast<Length::UNIT>(unit));
490                 else
491                         data.width = Length();
492
493                 data.scale = string();
494
495         } else {
496                 // scaling instead of a width
497                 data.scale = width;
498                 data.width = Length();
499         }
500
501         data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
502
503         data.keepAspectRatio = aspectratioCB.isChecked();
504 }
505
506
507 void setCrop(QCheckBox & clipCB,
508         QLineEdit & xlED, QLineEdit & ybED,
509         QLineEdit & xrED, QLineEdit & ytED,
510         external::ClipData const & data)
511 {
512         clipCB.setChecked(data.clip);
513         graphics::BoundingBox const & bbox = data.bbox;
514         xlED.setText(QString::number(bbox.xl));
515         ybED.setText(QString::number(bbox.yb));
516         xrED.setText(QString::number(bbox.xr));
517         ytED.setText(QString::number(bbox.yt));
518 }
519
520
521 static void getCrop(external::ClipData & data,
522         QCheckBox const & clipCB,
523         QLineEdit const & xlED, QLineEdit const & ybED,
524         QLineEdit const & xrED, QLineEdit const & ytED,
525         bool bb_changed)
526 {
527         data.clip = clipCB.isChecked();
528
529         if (!bb_changed)
530                 return;
531
532         data.bbox.xl = xlED.text().toInt();
533         data.bbox.yb = ybED.text().toInt();
534         data.bbox.xr = xrED.text().toInt();
535         data.bbox.yt = ytED.text().toInt();
536 }
537
538
539 static void getExtra(external::ExtraData & data,
540               GuiExternal::MapType const & extra)
541 {
542         typedef GuiExternal::MapType MapType;
543         MapType::const_iterator it = extra.begin();
544         MapType::const_iterator const end = extra.end();
545         for (; it != end; ++it)
546                 data.set(it->first, trim(fromqstr(it->second)));
547 }
548
549
550 void GuiExternal::updateContents()
551 {
552         tab->setCurrentIndex(0);
553
554         string const name =
555                 params_.filename.outputFilename(fromqstr(bufferFilepath()));
556         fileED->setText(toqstr(name));
557         embedCB->setCheckState(params_.filename.embedded() ? Qt::Checked : Qt::Unchecked);
558
559         externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
560         updateTemplate();
561
562         draftCB->setChecked(params_.draft);
563
564         setDisplay(*displayCB, *showCO, *displayscaleED,
565                    params_.display, params_.lyxscale, isBufferReadonly());
566
567         setRotation(*angleED, *originCO, params_.rotationdata);
568
569         setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
570                 *aspectratioCB, params_.resizedata);
571
572         setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
573         bbChanged_ = !params_.clipdata.bbox.empty();
574
575         isValid();
576 }
577
578
579 void GuiExternal::updateTemplate()
580 {
581         external::Template templ = getTemplate(externalCO->currentIndex());
582         externalTB->setPlainText(qt_(templ.helpText));
583
584         // Ascertain which (if any) transformations the template supports
585         // and disable tabs hosting unsupported transforms.
586         typedef vector<external::TransformID> TransformIDs;
587         TransformIDs const transformIds = templ.transformIds;
588         TransformIDs::const_iterator tr_begin = transformIds.begin();
589         TransformIDs::const_iterator const tr_end = transformIds.end();
590
591         bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
592         tab->setTabEnabled(tab->indexOf(rotatetab), found);
593         found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
594         tab->setTabEnabled(tab->indexOf(scaletab), found);
595
596         found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
597         tab->setTabEnabled(tab->indexOf(croptab), found);
598
599         found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
600         tab->setTabEnabled(tab->indexOf(optionstab), found);
601
602         if (!found)
603                 return;
604
605         // Ascertain whether the template has any formats supporting
606         // the 'Extra' option
607         extra_.clear();
608         extraED->clear();
609         extraFormatCO->clear();
610
611         external::Template::Formats::const_iterator it  = templ.formats.begin();
612         external::Template::Formats::const_iterator end = templ.formats.end();
613         for (; it != end; ++it) {
614                 if (it->second.option_transformers.find(external::Extra) ==
615                     it->second.option_transformers.end())
616                         continue;
617                 string const format = it->first;
618                 string const opt = params_.extradata.get(format);
619                 extraFormatCO->addItem(toqstr(format));
620                 extra_[format] = toqstr(opt);
621         }
622
623         bool const enabled = extraFormatCO->count()  > 0;
624
625         tab->setTabEnabled(
626                 tab->indexOf(optionstab), enabled);
627         extraED->setEnabled(enabled && !isBufferReadonly());
628         extraFormatCO->setEnabled(enabled);
629
630         if (enabled) {
631                 extraFormatCO->setCurrentIndex(0);
632                 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
633         }
634 }
635
636
637 void GuiExternal::applyView()
638 {
639         params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
640
641         try {
642                 Buffer & buf = buffer();
643                 EmbeddedFile file(fromqstr(fileED->text()), buf.filePath());
644                 file.setEmbed(embedCB->checkState() == Qt::Checked);
645                 // move file around if needed, an exception may be raised.
646                 file.enable(buf.embedded(), buf, true);
647                 // if things are OK..., embed params_.filename
648                 params_.filename.setEmbed(file.embedded());
649         } catch (ExceptionMessage const & message) {
650                 Alert::error(message.title_, message.details_);
651                 // failed to embed
652                 params_.filename.setEmbed(false);
653         }
654         
655         params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
656
657         params_.draft = draftCB->isChecked();
658
659         getDisplay(params_.display, params_.lyxscale,
660                    *displayCB, *showCO,
661                    *displayscaleED);
662
663         if (tab->isTabEnabled(tab->indexOf(rotatetab)))
664                 getRotation(params_.rotationdata, *angleED, *originCO);
665
666         if (tab->isTabEnabled(tab->indexOf(scaletab)))
667                 getSize(params_.resizedata, *widthED, *widthUnitCO,
668                         *heightED, *heightUnitCO, *aspectratioCB);
669
670         if (tab->isTabEnabled(tab->indexOf(croptab)))
671                 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
672                         *xrED, *ytED, bbChanged_);
673
674         if (tab->isTabEnabled(tab->indexOf(optionstab)))
675                 getExtra(params_.extradata, extra_);
676 }
677
678
679 void GuiExternal::getBB()
680 {
681         xlED->setText("0");
682         ybED->setText("0");
683         xrED->setText("0");
684         ytED->setText("0");
685
686         string const filename = fromqstr(fileED->text());
687         if (filename.empty())
688                 return;
689
690         string const bb = readBB(filename);
691         if (bb.empty())
692                 return;
693
694         xlED->setText(toqstr(token(bb, ' ', 0)));
695         ybED->setText(toqstr(token(bb, ' ', 1)));
696         xrED->setText(toqstr(token(bb, ' ', 2)));
697         ytED->setText(toqstr(token(bb, ' ', 3)));
698
699         bbChanged_ = false;
700 }
701
702
703 bool GuiExternal::initialiseParams(string const & data)
704 {
705         InsetExternal::string2params(data, buffer(), params_);
706         return true;
707 }
708
709
710 void GuiExternal::clearParams()
711 {
712         params_ = InsetExternalParams();
713 }
714
715
716 void GuiExternal::dispatchParams()
717 {
718         string const lfun = InsetExternal::params2string(params_, buffer());
719         dispatch(FuncRequest(getLfun(), lfun));
720 }
721
722
723 void GuiExternal::editExternal()
724 {
725         applyView();
726         string const lfun = InsetExternal::params2string(params_, buffer());
727         dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
728 }
729
730
731 vector<string> const GuiExternal::getTemplates() const
732 {
733         vector<string> result;
734
735         external::TemplateManager::Templates::const_iterator i1, i2;
736         i1 = external::TemplateManager::get().getTemplates().begin();
737         i2 = external::TemplateManager::get().getTemplates().end();
738
739         for (; i1 != i2; ++i1) {
740                 result.push_back(i1->second.lyxName);
741         }
742         return result;
743 }
744
745
746 int GuiExternal::getTemplateNumber(string const & name) const
747 {
748         external::TemplateManager::Templates::const_iterator i1, i2;
749         i1 = external::TemplateManager::get().getTemplates().begin();
750         i2 = external::TemplateManager::get().getTemplates().end();
751         for (int i = 0; i1 != i2; ++i1, ++i) {
752                 if (i1->second.lyxName == name)
753                         return i;
754         }
755
756         // we can get here if a LyX document has a template not installed
757         // on this machine.
758         return -1;
759 }
760
761
762 external::Template GuiExternal::getTemplate(int i) const
763 {
764         external::TemplateManager::Templates::const_iterator i1
765                 = external::TemplateManager::get().getTemplates().begin();
766
767         advance(i1, i);
768
769         return i1->second;
770 }
771
772
773 string const
774 GuiExternal::getTemplateFilters(string const & template_name) const
775 {
776         /// Determine the template file extension
777         external::TemplateManager const & etm =
778                 external::TemplateManager::get();
779         external::Template const * const et_ptr =
780                 etm.getTemplateByName(template_name);
781
782         if (et_ptr)
783                 return et_ptr->fileRegExp;
784
785         return string();
786 }
787
788
789 QString GuiExternal::browse(QString const & input,
790                                      QString const & template_name) const
791 {
792         QString const title = qt_("Select external file");
793         QString const bufpath = bufferFilepath();
794         FileFilterList const filter =
795                 FileFilterList(from_utf8(getTemplateFilters(fromqstr(template_name))));
796
797         QString const label1 = qt_("Documents|#o#O");
798         QString const dir1 = toqstr(lyxrc.document_path);
799
800         return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
801 }
802
803
804 string const GuiExternal::readBB(string const & file)
805 {
806         FileName const abs_file(makeAbsPath(file, fromqstr(bufferFilepath())));
807
808         // try to get it from the file, if possible. Zipped files are
809         // unzipped in the readBB_from_PSFile-Function
810         string const bb = readBB_from_PSFile(abs_file);
811         if (!bb.empty())
812                 return bb;
813
814         // we don't, so ask the Graphics Cache if it has loaded the file
815         int width = 0;
816         int height = 0;
817
818         graphics::Cache & gc = graphics::Cache::get();
819         if (gc.inCache(abs_file)) {
820                 graphics::Image const * image = gc.item(abs_file)->image();
821
822                 if (image) {
823                         width  = image->width();
824                         height = image->height();
825                 }
826         }
827
828         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
829 }
830
831
832 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
833
834
835 } // namespace frontend
836 } // namespace lyx
837
838 #include "GuiExternal_moc.cpp"