]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiExternal.cpp
68bdda37ad76a44f8d2055fa71402e542a490bdb
[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> templates = getTemplates();
201
202         for (vector<string>::const_iterator cit = templates.begin();
203                 cit != templates.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
278 void GuiExternal::extraChanged(const QString& text)
279 {
280         string const format = fromqstr(extraFormatCO->currentText());
281         extra_[format] = text;
282         changed();
283 }
284
285
286 void GuiExternal::formatChanged(const QString& format)
287 {
288         extraED->setText(extra_[fromqstr(format)]);
289 }
290
291
292 void GuiExternal::getbbClicked()
293 {
294         getBB();
295 }
296
297
298 void GuiExternal::sizeChanged()
299 {
300         aspectratioCB->setEnabled(activateAspectratio());
301         changed();
302 }
303
304
305 void GuiExternal::templateChanged()
306 {
307         updateTemplate();
308         changed();
309 }
310
311
312 void GuiExternal::widthUnitChanged()
313 {
314         bool useHeight = (widthUnitCO->currentIndex() > 0);
315
316         if (useHeight)
317                 widthED->setValidator(unsignedLengthValidator(widthED));
318         else
319                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
320
321         heightED->setEnabled(useHeight);
322         heightUnitCO->setEnabled(useHeight);
323         changed();
324 }
325
326
327 static Length::UNIT defaultUnit()
328 {
329         Length::UNIT default_unit = Length::CM;
330         switch (lyxrc.default_papersize) {
331         case PAPER_USLETTER:
332         case PAPER_USLEGAL:
333         case PAPER_USEXECUTIVE:
334                 default_unit = Length::IN;
335                 break;
336         default:
337                 break;
338         }
339         return default_unit;
340 }
341
342
343 static void setDisplay(
344         QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
345         external::DisplayType display, unsigned int scale, bool read_only)
346 {
347         int item = 0;
348         switch (display) {
349         case external::DefaultDisplay:
350                 item = 0;
351                 break;
352         case external::MonochromeDisplay:
353                 item = 1;
354                 break;
355         case external::GrayscaleDisplay:
356                 item = 2;
357                 break;
358         case external::ColorDisplay:
359                 item = 3;
360                 break;
361         case external::PreviewDisplay:
362                 item = 4;
363                 break;
364         case external::NoDisplay:
365                 item = 0;
366                 break;
367         }
368
369         showCO.setCurrentIndex(item);
370         bool const no_display = display == external::NoDisplay;
371         showCO.setEnabled(!no_display && !read_only);
372         displayCB.setChecked(!no_display);
373         scaleED.setEnabled(!no_display && !read_only);
374         scaleED.setText(QString::number(scale));
375 }
376
377
378 static void getDisplay(external::DisplayType & display,
379                 unsigned int & scale,
380                 QCheckBox const & displayCB,
381                 QComboBox const & showCO,
382                 QLineEdit const & scaleED)
383 {
384         switch (showCO.currentIndex()) {
385         case 0:
386                 display = external::DefaultDisplay;
387                 break;
388         case 1:
389                 display = external::MonochromeDisplay;
390                 break;
391         case 2:
392                 display = external::GrayscaleDisplay;
393                 break;
394         case 3:
395                 display = external::ColorDisplay;
396                 break;
397         case 4:
398                 display = external::PreviewDisplay;
399                 break;
400         }
401
402         if (!displayCB.isChecked())
403                 display = external::NoDisplay;
404
405         scale = scaleED.text().toInt();
406 }
407
408
409 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
410         external::RotationData const & data)
411 {
412         originCO.setCurrentIndex(int(data.origin()));
413         angleED.setText(toqstr(data.angle));
414 }
415
416
417 static void getRotation(external::RotationData & data,
418         QLineEdit const & angleED, QComboBox const & originCO)
419 {
420         typedef external::RotationData::OriginType OriginType;
421
422         data.origin(static_cast<OriginType>(originCO.currentIndex()));
423         data.angle = fromqstr(angleED.text());
424 }
425
426
427 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
428         QLineEdit & heightED, LengthCombo & heightUnitCO,
429         QCheckBox & aspectratioCB,
430         external::ResizeData const & data)
431 {
432         bool using_scale = data.usingScale();
433         string scale = data.scale;
434         if (data.no_resize()) {
435                 // Everything is zero, so default to this!
436                 using_scale = true;
437                 scale = "100";
438         }
439
440         if (using_scale) {
441                 widthED.setText(toqstr(scale));
442                 widthUnitCO.setCurrentIndex(0);
443         } else {
444                 widthED.setText(QString::number(data.width.value()));
445                 // Because 'Scale' is position 0...
446                 // Note also that width cannot be zero here, so
447                 // we don't need to worry about the default unit.
448                 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
449         }
450
451         string const h = data.height.zero() ? string() : data.height.asString();
452         Length::UNIT default_unit = data.width.zero() ?
453                 defaultUnit() : data.width.unit();
454         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
455
456         heightED.setEnabled(!using_scale);
457         heightUnitCO.setEnabled(!using_scale);
458
459         aspectratioCB.setChecked(data.keepAspectRatio);
460
461         bool const disable_aspectRatio = using_scale ||
462                 data.width.zero() || data.height.zero();
463         aspectratioCB.setEnabled(!disable_aspectRatio);
464 }
465
466
467 static void getSize(external::ResizeData & data,
468         QLineEdit const & widthED, QComboBox const & widthUnitCO,
469         QLineEdit const & heightED, LengthCombo const & heightUnitCO,
470         QCheckBox const & aspectratioCB)
471 {
472         string const width = fromqstr(widthED.text());
473
474         if (widthUnitCO.currentIndex() > 0) {
475                 // Subtract one, because scale is 0.
476                 int const unit = widthUnitCO.currentIndex() - 1;
477
478                 Length w;
479                 if (isValidLength(width, &w))
480                         data.width = w;
481                 else if (isStrDbl(width))
482                         data.width = Length(convert<double>(width),
483                                            static_cast<Length::UNIT>(unit));
484                 else
485                         data.width = Length();
486
487                 data.scale = string();
488
489         } else {
490                 // scaling instead of a width
491                 data.scale = width;
492                 data.width = Length();
493         }
494
495         data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
496
497         data.keepAspectRatio = aspectratioCB.isChecked();
498 }
499
500
501 void setCrop(QCheckBox & clipCB,
502         QLineEdit & xlED, QLineEdit & ybED,
503         QLineEdit & xrED, QLineEdit & ytED,
504         external::ClipData const & data)
505 {
506         clipCB.setChecked(data.clip);
507         graphics::BoundingBox const & bbox = data.bbox;
508         xlED.setText(QString::number(bbox.xl));
509         ybED.setText(QString::number(bbox.yb));
510         xrED.setText(QString::number(bbox.xr));
511         ytED.setText(QString::number(bbox.yt));
512 }
513
514
515 static void getCrop(external::ClipData & data,
516         QCheckBox const & clipCB,
517         QLineEdit const & xlED, QLineEdit const & ybED,
518         QLineEdit const & xrED, QLineEdit const & ytED,
519         bool bb_changed)
520 {
521         data.clip = clipCB.isChecked();
522
523         if (!bb_changed)
524                 return;
525
526         data.bbox.xl = xlED.text().toInt();
527         data.bbox.yb = ybED.text().toInt();
528         data.bbox.xr = xrED.text().toInt();
529         data.bbox.yt = ytED.text().toInt();
530 }
531
532
533 static void getExtra(external::ExtraData & data,
534               GuiExternal::MapType const & extra)
535 {
536         typedef GuiExternal::MapType MapType;
537         MapType::const_iterator it = extra.begin();
538         MapType::const_iterator const end = extra.end();
539         for (; it != end; ++it)
540                 data.set(it->first, trim(fromqstr(it->second)));
541 }
542
543
544 void GuiExternal::updateContents()
545 {
546         tab->setCurrentIndex(0);
547
548         string const name =
549                 params_.filename.outputFilename(fromqstr(bufferFilepath()));
550         fileED->setText(toqstr(name));
551
552         externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
553         updateTemplate();
554
555         draftCB->setChecked(params_.draft);
556
557         setDisplay(*displayCB, *showCO, *displayscaleED,
558                    params_.display, params_.lyxscale, isBufferReadonly());
559
560         setRotation(*angleED, *originCO, params_.rotationdata);
561
562         setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
563                 *aspectratioCB, params_.resizedata);
564
565         setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
566         bbChanged_ = !params_.clipdata.bbox.empty();
567
568         isValid();
569 }
570
571
572 void GuiExternal::updateTemplate()
573 {
574         external::Template templ = getTemplate(externalCO->currentIndex());
575         externalTB->setPlainText(qt_(templ.helpText));
576
577         // Ascertain which (if any) transformations the template supports
578         // and disable tabs hosting unsupported transforms.
579         typedef vector<external::TransformID> TransformIDs;
580         TransformIDs const transformIds = templ.transformIds;
581         TransformIDs::const_iterator tr_begin = transformIds.begin();
582         TransformIDs::const_iterator const tr_end = transformIds.end();
583
584         bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
585         tab->setTabEnabled(tab->indexOf(rotatetab), found);
586         found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
587         tab->setTabEnabled(tab->indexOf(scaletab), found);
588
589         found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
590         tab->setTabEnabled(tab->indexOf(croptab), found);
591
592         found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
593         tab->setTabEnabled(tab->indexOf(optionstab), found);
594
595         if (!found)
596                 return;
597
598         // Ascertain whether the template has any formats supporting
599         // the 'Extra' option
600         extra_.clear();
601         extraED->clear();
602         extraFormatCO->clear();
603
604         external::Template::Formats::const_iterator it  = templ.formats.begin();
605         external::Template::Formats::const_iterator end = templ.formats.end();
606         for (; it != end; ++it) {
607                 if (it->second.option_transformers.find(external::Extra) ==
608                     it->second.option_transformers.end())
609                         continue;
610                 string const format = it->first;
611                 string const opt = params_.extradata.get(format);
612                 extraFormatCO->addItem(toqstr(format));
613                 extra_[format] = toqstr(opt);
614         }
615
616         bool const enabled = extraFormatCO->count()  > 0;
617
618         tab->setTabEnabled(
619                 tab->indexOf(optionstab), enabled);
620         extraED->setEnabled(enabled && !isBufferReadonly());
621         extraFormatCO->setEnabled(enabled);
622
623         if (enabled) {
624                 extraFormatCO->setCurrentIndex(0);
625                 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
626         }
627 }
628
629
630 void GuiExternal::applyView()
631 {
632         params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
633         params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
634
635         params_.draft = draftCB->isChecked();
636
637         getDisplay(params_.display, params_.lyxscale,
638                    *displayCB, *showCO,
639                    *displayscaleED);
640
641         if (tab->isTabEnabled(tab->indexOf(rotatetab)))
642                 getRotation(params_.rotationdata, *angleED, *originCO);
643
644         if (tab->isTabEnabled(tab->indexOf(scaletab)))
645                 getSize(params_.resizedata, *widthED, *widthUnitCO,
646                         *heightED, *heightUnitCO, *aspectratioCB);
647
648         if (tab->isTabEnabled(tab->indexOf(croptab)))
649                 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
650                         *xrED, *ytED, bbChanged_);
651
652         if (tab->isTabEnabled(tab->indexOf(optionstab)))
653                 getExtra(params_.extradata, extra_);
654 }
655
656
657 void GuiExternal::getBB()
658 {
659         xlED->setText("0");
660         ybED->setText("0");
661         xrED->setText("0");
662         ytED->setText("0");
663
664         string const filename = fromqstr(fileED->text());
665         if (filename.empty())
666                 return;
667
668         string const bb = readBB(filename);
669         if (bb.empty())
670                 return;
671
672         xlED->setText(toqstr(token(bb, ' ', 0)));
673         ybED->setText(toqstr(token(bb, ' ', 1)));
674         xrED->setText(toqstr(token(bb, ' ', 2)));
675         ytED->setText(toqstr(token(bb, ' ', 3)));
676
677         bbChanged_ = false;
678 }
679
680
681 bool GuiExternal::initialiseParams(string const & data)
682 {
683         InsetExternal::string2params(data, buffer(), params_);
684         return true;
685 }
686
687
688 void GuiExternal::clearParams()
689 {
690         params_ = InsetExternalParams();
691 }
692
693
694 void GuiExternal::dispatchParams()
695 {
696         string const lfun = InsetExternal::params2string(params_, buffer());
697         dispatch(FuncRequest(getLfun(), lfun));
698 }
699
700
701 void GuiExternal::editExternal()
702 {
703         applyView();
704         string const lfun = InsetExternal::params2string(params_, buffer());
705         dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
706 }
707
708
709 vector<string> const GuiExternal::getTemplates() const
710 {
711         vector<string> result;
712
713         external::TemplateManager::Templates::const_iterator i1, i2;
714         i1 = external::TemplateManager::get().getTemplates().begin();
715         i2 = external::TemplateManager::get().getTemplates().end();
716
717         for (; i1 != i2; ++i1) {
718                 result.push_back(i1->second.lyxName);
719         }
720         return result;
721 }
722
723
724 int GuiExternal::getTemplateNumber(string const & name) const
725 {
726         external::TemplateManager::Templates::const_iterator i1, i2;
727         i1 = external::TemplateManager::get().getTemplates().begin();
728         i2 = external::TemplateManager::get().getTemplates().end();
729         for (int i = 0; i1 != i2; ++i1, ++i) {
730                 if (i1->second.lyxName == name)
731                         return i;
732         }
733
734         // we can get here if a LyX document has a template not installed
735         // on this machine.
736         return -1;
737 }
738
739
740 external::Template GuiExternal::getTemplate(int i) const
741 {
742         external::TemplateManager::Templates::const_iterator i1
743                 = external::TemplateManager::get().getTemplates().begin();
744
745         advance(i1, i);
746
747         return i1->second;
748 }
749
750
751 string const
752 GuiExternal::getTemplateFilters(string const & template_name) const
753 {
754         /// Determine the template file extension
755         external::TemplateManager const & etm =
756                 external::TemplateManager::get();
757         external::Template const * const et_ptr =
758                 etm.getTemplateByName(template_name);
759
760         if (et_ptr)
761                 return et_ptr->fileRegExp;
762
763         return string();
764 }
765
766
767 QString GuiExternal::browse(QString const & input,
768                                      QString const & template_name) const
769 {
770         QString const title = qt_("Select external file");
771         QString const bufpath = bufferFilepath();
772         FileFilterList const filter =
773                 FileFilterList(from_utf8(getTemplateFilters(fromqstr(template_name))));
774
775         QString const label1 = qt_("Documents|#o#O");
776         QString const dir1 = toqstr(lyxrc.document_path);
777
778         return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
779 }
780
781
782 string const GuiExternal::readBB(string const & file)
783 {
784         FileName const abs_file(makeAbsPath(file, fromqstr(bufferFilepath())));
785
786         // try to get it from the file, if possible. Zipped files are
787         // unzipped in the readBB_from_PSFile-Function
788         string const bb = readBB_from_PSFile(abs_file);
789         if (!bb.empty())
790                 return bb;
791
792         // we don't, so ask the Graphics Cache if it has loaded the file
793         int width = 0;
794         int height = 0;
795
796         graphics::Cache & gc = graphics::Cache::get();
797         if (gc.inCache(abs_file)) {
798                 graphics::Image const * image = gc.item(abs_file)->image();
799
800                 if (image) {
801                         width  = image->width();
802                         height = image->height();
803                 }
804         }
805
806         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
807 }
808
809
810 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
811
812
813 } // namespace frontend
814 } // namespace lyx
815
816 #include "GuiExternal_moc.cpp"