2 * \file GuiExternal.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
8 * \author Asger Alstrup
10 * Full author contact details are available in file CREDITS.
15 #include "GuiExternal.h"
18 #include "FuncRequest.h"
19 #include "support/gettext.h"
23 #include "insets/ExternalSupport.h"
24 #include "insets/ExternalTemplate.h"
25 #include "insets/InsetExternal.h"
27 #include "graphics/epstools.h"
28 #include "graphics/GraphicsCache.h"
29 #include "graphics/GraphicsCacheItem.h"
30 #include "graphics/GraphicsImage.h"
32 #include "support/convert.h"
33 #include "support/filetools.h"
34 #include "support/lstrings.h"
35 #include "support/lyxlib.h"
36 #include "support/os.h"
38 #include "LengthCombo.h"
39 #include "qt_helpers.h"
40 #include "Validator.h"
45 #include <QPushButton>
47 #include <QTextBrowser>
50 using namespace lyx::support;
55 using namespace external;
59 RotationDataType origins[] = {
60 RotationData::DEFAULT,
61 RotationData::TOPLEFT,
62 RotationData::BOTTOMLEFT,
63 RotationData::BASELINELEFT,
65 RotationData::TOPCENTER,
66 RotationData::BOTTOMCENTER,
67 RotationData::BASELINECENTER,
68 RotationData::TOPRIGHT,
69 RotationData::BOTTOMRIGHT,
70 RotationData::BASELINERIGHT
74 // These are the strings, corresponding to the above, that the GUI should
75 // use. Note that they can/should be translated.
76 char const * const origin_gui_strs[] = {
78 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
79 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
80 N_("Top right"), N_("Bottom right"), N_("Baseline right")
86 GuiExternal::GuiExternal(GuiView & lv)
87 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
91 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
92 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
93 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
96 connect(displayGB, SIGNAL(toggled(bool)),
97 displayscaleED, SLOT(setEnabled(bool)));
99 connect(originCO, SIGNAL(activated(int)),
100 this, SLOT(change_adaptor()));
101 connect(aspectratioCB, SIGNAL(stateChanged(int)),
102 this, SLOT(change_adaptor()));
103 connect(browsePB, SIGNAL(clicked()),
104 this, SLOT(browseClicked()));
105 connect(externalCO, SIGNAL(activated(QString)),
106 this, SLOT(templateChanged()));
107 connect(extraED, SIGNAL(textChanged(QString)),
108 this, SLOT(extraChanged(QString)));
109 connect(extraFormatCO, SIGNAL(activated(QString)),
110 this, SLOT(formatChanged(QString)));
111 connect(widthUnitCO, SIGNAL(activated(int)),
112 this, SLOT(widthUnitChanged()));
113 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
114 this, SLOT(change_adaptor()));
115 connect(displayGB, SIGNAL(toggled(bool)),
116 this, SLOT(change_adaptor()));
117 connect(displayscaleED, SIGNAL(textChanged(QString)),
118 this, SLOT(change_adaptor()));
119 connect(angleED, SIGNAL(textChanged(QString)),
120 this, SLOT(change_adaptor()));
121 connect(widthED, SIGNAL(textChanged(QString)),
122 this, SLOT(sizeChanged()));
123 connect(heightED, SIGNAL(textChanged(QString)),
124 this, SLOT(sizeChanged()));
125 connect(fileED, SIGNAL(textChanged(QString)),
126 this, SLOT(change_adaptor()));
127 connect(clipCB, SIGNAL(stateChanged(int)),
128 this, SLOT(change_adaptor()));
129 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
130 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
131 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
132 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
133 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
134 connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
135 this, SLOT(bbChanged()));
136 connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
137 this, SLOT(bbChanged()));
138 connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
139 this, SLOT(bbChanged()));
140 connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
141 this, SLOT(bbChanged()));
142 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
144 QIntValidator * validator = new QIntValidator(displayscaleED);
145 validator->setBottom(1);
146 displayscaleED->setValidator(validator);
148 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
150 xlED->setValidator(unsignedLengthValidator(xlED));
151 ybED->setValidator(unsignedLengthValidator(ybED));
152 xrED->setValidator(unsignedLengthValidator(xrED));
153 ytED->setValidator(unsignedLengthValidator(ytED));
155 widthED->setValidator(unsignedLengthValidator(widthED));
156 heightED->setValidator(unsignedLengthValidator(heightED));
158 setFocusProxy(fileED);
160 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
163 bc().setApply(applyPB);
164 bc().setCancel(closePB);
166 bc().addReadOnly(fileED);
167 bc().addReadOnly(browsePB);
168 bc().addReadOnly(externalCO);
169 bc().addReadOnly(draftCB);
170 bc().addReadOnly(displayscaleED);
171 bc().addReadOnly(displayGB);
172 bc().addReadOnly(angleED);
173 bc().addReadOnly(originCO);
174 bc().addReadOnly(heightUnitCO);
175 bc().addReadOnly(heightED);
176 bc().addReadOnly(aspectratioCB);
177 bc().addReadOnly(widthUnitCO);
178 bc().addReadOnly(widthED);
179 bc().addReadOnly(clipCB);
180 bc().addReadOnly(getbbPB);
181 bc().addReadOnly(ytED);
182 bc().addReadOnly(xlED);
183 bc().addReadOnly(xrED);
184 bc().addReadOnly(ybED);
185 bc().addReadOnly(ytUnitCO);
186 bc().addReadOnly(xlUnitCO);
187 bc().addReadOnly(xrUnitCO);
188 bc().addReadOnly(ybUnitCO);
189 bc().addReadOnly(extraFormatCO);
190 bc().addReadOnly(extraED);
192 bc().addCheckedLineEdit(angleED, angleLA);
193 bc().addCheckedLineEdit(displayscaleED, scaleLA);
194 bc().addCheckedLineEdit(heightED, heightLA);
195 bc().addCheckedLineEdit(widthED, widthLA);
196 bc().addCheckedLineEdit(xlED, lbLA);
197 bc().addCheckedLineEdit(ybED, lbLA);
198 bc().addCheckedLineEdit(xrED, rtLA);
199 bc().addCheckedLineEdit(ytED, rtLA);
200 bc().addCheckedLineEdit(fileED, fileLA);
202 external::TemplateManager::Templates::const_iterator i1, i2;
203 i1 = external::TemplateManager::get().getTemplates().begin();
204 i2 = external::TemplateManager::get().getTemplates().end();
205 for (; i1 != i2; ++i1)
206 externalCO->addItem(qt_(i1->second.guiName), toqstr(i1->second.lyxName));
207 // Sort alphabetically by(localized) GUI name
208 externalCO->model()->sort(0);
210 // Fill the origins combo
211 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
212 originCO->addItem(qt_(origin_gui_strs[i]));
215 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
217 // remove all units from bb that depend on font or other dimensions
218 // we cannot use these, since we need to compare against absolute
219 // values from the image file.
220 xlUnitCO->noPercents();
221 xlUnitCO->removeFontDependent();
222 xrUnitCO->noPercents();
223 xrUnitCO->removeFontDependent();
224 ytUnitCO->noPercents();
225 ytUnitCO->removeFontDependent();
226 ybUnitCO->noPercents();
227 ybUnitCO->removeFontDependent();
231 bool GuiExternal::activateAspectratio() const
236 QString const wstr = widthED->text();
240 double val = wstr.trimmed().toDouble(&wIsDbl);
241 if (wIsDbl && float_equal(val, 0.0, 0.05))
244 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
247 QString const hstr = heightED->text();
251 val = hstr.trimmed().toDouble(&hIsDbl);
252 if (hIsDbl && float_equal(val, 0.0, 0.05))
254 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
261 bool GuiExternal::usingScale() const
263 return (widthUnitCO->itemData(
264 widthUnitCO->currentIndex()).toString() == "scale");
268 void GuiExternal::bbChanged()
275 void GuiExternal::browseClicked()
277 QString const template_name =
278 externalCO->itemData(externalCO->currentIndex()).toString();
279 QString const str = browse(fileED->text(), template_name);
280 if (!str.isEmpty()) {
281 fileED->setText(str);
287 void GuiExternal::change_adaptor()
293 void GuiExternal::extraChanged(const QString & text)
295 extra_[extraFormatCO->currentText()] = text;
300 void GuiExternal::formatChanged(const QString & format)
302 extraED->setText(extra_[format]);
306 void GuiExternal::getbbClicked()
313 string const filename = fromqstr(fileED->text());
314 if (filename.empty())
317 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
319 // try to get it from the file, if possible
320 string bb = graphics::readBB_from_PSFile(abs_file);
322 // we don't, so ask the Graphics Cache if it has loaded the file
326 graphics::Cache & gc = graphics::Cache::get();
327 if (gc.inCache(abs_file)) {
328 graphics::Image const * image = gc.item(abs_file)->image();
331 width = image->width();
332 height = image->height();
335 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
338 doubleToWidget(xlED, token(bb, ' ', 0));
339 doubleToWidget(ybED, token(bb, ' ', 1));
340 doubleToWidget(xrED, token(bb, ' ', 2));
341 doubleToWidget(ytED, token(bb, ' ', 3));
342 // the values from the file always have the bigpoint-unit bp
343 xlUnitCO->setCurrentIndex(0);
344 ybUnitCO->setCurrentIndex(0);
345 xrUnitCO->setCurrentIndex(0);
346 ytUnitCO->setCurrentIndex(0);
352 void GuiExternal::sizeChanged()
354 aspectratioCB->setEnabled(activateAspectratio());
359 void GuiExternal::templateChanged()
366 void GuiExternal::widthUnitChanged()
369 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
371 widthED->setValidator(unsignedLengthValidator(widthED));
373 heightED->setEnabled(!usingScale());
374 heightUnitCO->setEnabled(!usingScale());
379 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
380 external::RotationData const & data)
382 originCO.setCurrentIndex(int(data.origin()));
383 doubleToWidget(&angleED, data.angle);
387 static void getRotation(external::RotationData & data,
388 QLineEdit const & angleED, QComboBox const & originCO)
390 typedef external::RotationData::OriginType OriginType;
392 data.origin(static_cast<OriginType>(originCO.currentIndex()));
393 data.angle = widgetToDoubleStr(&angleED);
397 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
398 QLineEdit & heightED, LengthCombo & heightUnitCO,
399 QCheckBox & aspectratioCB,
400 external::ResizeData const & data)
402 bool using_scale = data.usingScale();
403 string scale = data.scale;
404 if (data.no_resize()) {
405 // Everything is zero, so default to this!
411 doubleToWidget(&widthED, scale);
412 widthUnitCO.setCurrentItem("scale");
414 lengthToWidgets(&widthED, &widthUnitCO,
415 data.width.asString(), Length::defaultUnit());
417 string const h = data.height.zero() ? string() : data.height.asString();
418 Length::UNIT const default_unit = data.width.zero() ?
419 Length::defaultUnit() : data.width.unit();
420 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
422 heightED.setEnabled(!using_scale);
423 heightUnitCO.setEnabled(!using_scale);
425 aspectratioCB.setChecked(data.keepAspectRatio);
427 bool const disable_aspectRatio = using_scale ||
428 data.width.zero() || data.height.zero();
429 aspectratioCB.setEnabled(!disable_aspectRatio);
433 static void getSize(external::ResizeData & data,
434 QLineEdit const & widthED, QComboBox const & widthUnitCO,
435 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
436 QCheckBox const & aspectratioCB, bool const scaling)
439 // scaling instead of a width
440 data.scale = widgetToDoubleStr(&widthED);
441 data.width = Length();
443 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
444 data.scale = string();
446 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
447 data.keepAspectRatio = aspectratioCB.isChecked();
451 void setCrop(QCheckBox & clipCB,
452 QLineEdit & xlED, QLineEdit & ybED,
453 QLineEdit & xrED, QLineEdit & ytED,
454 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
455 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
456 external::ClipData const & data)
458 clipCB.setChecked(data.clip);
459 Length::UNIT const default_unit = data.bbox.xl.zero() ?
460 Length::defaultUnit() : data.bbox.xl.unit();
461 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
462 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
463 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
464 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
468 static void getCrop(external::ClipData & data,
469 QCheckBox const & clipCB,
470 QLineEdit const & xlED, QLineEdit const & ybED,
471 QLineEdit const & xrED, QLineEdit const & ytED,
472 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
473 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
476 data.clip = clipCB.isChecked();
481 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
482 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
483 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
484 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
488 void GuiExternal::updateContents()
491 params_.filename.outputFileName(fromqstr(bufferFilePath()));
492 fileED->setText(toqstr(name));
494 externalCO->setCurrentIndex(
495 externalCO->findData(toqstr(params_.templatename())));
498 draftCB->setChecked(params_.draft);
500 displayGB->setChecked(params_.display);
501 displayscaleED->setText(QString::number(params_.lyxscale));
502 bool scaled = params_.display && !isBufferReadonly() &&
503 (params_.preview_mode != PREVIEW_INSTANT);
504 displayscaleED->setEnabled(scaled);
505 scaleLA->setEnabled(scaled);
506 displayGB->setEnabled(lyxrc.display_graphics);
509 setRotation(*angleED, *originCO, params_.rotationdata);
511 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
512 *aspectratioCB, params_.resizedata);
514 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
515 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
516 bbChanged_ = !params_.clipdata.bbox.empty();
522 void GuiExternal::updateTemplate()
524 external::TemplateManager const & etm =
525 external::TemplateManager::get();
526 external::Template const * const templ = etm.getTemplateByName(
527 fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
528 externalTB->setPlainText(toqstr(translateIfPossible(templ->helpText)));
530 // Ascertain which (if any) transformations the template supports
531 // and disable tabs and Group Boxes hosting unsupported transforms.
532 typedef vector<external::TransformID> TransformIDs;
533 TransformIDs const transformIds = templ->transformIds;
534 TransformIDs::const_iterator tr_begin = transformIds.begin();
535 TransformIDs::const_iterator const tr_end = transformIds.end();
537 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
538 rotationGB->setEnabled(rotate);
540 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
541 scaleGB->setEnabled(resize);
543 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
544 cropGB->setEnabled(clip);
546 sizetab->setEnabled(rotate || resize || clip);
547 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
549 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
550 optionsGB->setEnabled(found);
552 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
553 !isBufferReadonly() && (templ->preview_mode != PREVIEW_INSTANT);
554 displayscaleED->setEnabled(scaled);
555 scaleLA->setEnabled(scaled);
560 // Ascertain whether the template has any formats supporting
561 // the 'Extra' option
564 extraFormatCO->clear();
566 external::Template::Formats::const_iterator it = templ->formats.begin();
567 external::Template::Formats::const_iterator end = templ->formats.end();
568 for (; it != end; ++it) {
569 if (it->second.option_transformers.find(external::Extra) ==
570 it->second.option_transformers.end())
572 string const format = it->first;
573 string const opt = params_.extradata.get(format);
574 extraFormatCO->addItem(toqstr(format));
575 extra_[toqstr(format)] = toqstr(opt);
578 bool const enabled = extraFormatCO->count() > 0;
580 optionsGB->setEnabled(enabled);
581 extraED->setEnabled(enabled && !isBufferReadonly());
582 extraFormatCO->setEnabled(enabled);
585 extraFormatCO->setCurrentIndex(0);
586 extraED->setText(extra_[extraFormatCO->currentText()]);
591 void GuiExternal::applyView()
593 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
594 params_.settemplate(fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
596 params_.draft = draftCB->isChecked();
597 params_.lyxscale = displayscaleED->text().toInt();
598 params_.display = displayGB->isChecked();
600 if (rotationGB->isEnabled())
601 getRotation(params_.rotationdata, *angleED, *originCO);
603 if (scaleGB->isEnabled())
604 getSize(params_.resizedata, *widthED, *widthUnitCO,
605 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
607 if (cropGB->isEnabled())
608 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
609 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
611 if (optionsGB->isEnabled()) {
612 MapType::const_iterator it = extra_.begin();
613 MapType::const_iterator const end = extra_.end();
614 for (; it != end; ++it)
615 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
620 bool GuiExternal::initialiseParams(string const & data)
622 InsetExternal::string2params(data, buffer(), params_);
627 void GuiExternal::clearParams()
629 params_ = InsetExternalParams();
633 void GuiExternal::dispatchParams()
635 string const lfun = InsetExternal::params2string(params_, buffer());
636 dispatch(FuncRequest(getLfun(), lfun));
640 static QStringList templateFilters(QString const & template_name)
642 /// Determine the template file extension
643 external::TemplateManager const & etm =
644 external::TemplateManager::get();
645 external::Template const * const et_ptr =
646 etm.getTemplateByName(fromqstr(template_name));
649 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
650 filter += to_utf8(_(et_ptr->guiName));
652 filter += et_ptr->fileRegExp;
655 return fileFilters(toqstr(filter));
659 QString GuiExternal::browse(QString const & input,
660 QString const & template_name) const
662 QString const title = qt_("Select external file");
663 QString const bufpath = bufferFilePath();
664 QStringList const filter = templateFilters(template_name);
666 QString const label1 = qt_("Documents|#o#O");
667 QString const dir1 = toqstr(lyxrc.document_path);
669 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
673 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
676 } // namespace frontend
679 #include "moc_GuiExternal.cpp"