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