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 QMap<QString, QString> localizedTemplates;
206 for (; i1 != i2; ++i1)
207 localizedTemplates.insert(qt_(i1->second.guiName), toqstr(i1->second.lyxName));
208 // Sort alphabetically by (localized) GUI name
209 QStringList keys = localizedTemplates.keys();
210 qSort(keys.begin(), keys.end(), SortLocaleAware);
211 for (QString & key : keys) {
212 QString const value = localizedTemplates[key];
213 externalCO->addItem(key, value);
216 // Fill the origins combo
217 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
218 originCO->addItem(qt_(origin_gui_strs[i]));
221 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
223 // remove all units from bb that depend on font or other dimensions
224 // we cannot use these, since we need to compare against absolute
225 // values from the image file.
226 xlUnitCO->noPercents();
227 xlUnitCO->removeFontDependent();
228 xrUnitCO->noPercents();
229 xrUnitCO->removeFontDependent();
230 ytUnitCO->noPercents();
231 ytUnitCO->removeFontDependent();
232 ybUnitCO->noPercents();
233 ybUnitCO->removeFontDependent();
237 bool GuiExternal::activateAspectratio() const
242 QString const wstr = widthED->text();
246 double val = wstr.trimmed().toDouble(&wIsDbl);
247 if (wIsDbl && float_equal(val, 0.0, 0.05))
250 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
253 QString const hstr = heightED->text();
257 val = hstr.trimmed().toDouble(&hIsDbl);
258 if (hIsDbl && float_equal(val, 0.0, 0.05))
260 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
267 bool GuiExternal::usingScale() const
269 return (widthUnitCO->itemData(
270 widthUnitCO->currentIndex()).toString() == "scale");
274 void GuiExternal::bbChanged()
281 void GuiExternal::browseClicked()
283 QString const template_name =
284 externalCO->itemData(externalCO->currentIndex()).toString();
285 QString const str = browse(fileED->text(), template_name);
286 if (!str.isEmpty()) {
287 fileED->setText(str);
293 void GuiExternal::change_adaptor()
299 void GuiExternal::extraChanged(const QString & text)
301 extra_[extraFormatCO->currentText()] = text;
306 void GuiExternal::formatChanged(const QString & format)
308 extraED->setText(extra_[format]);
312 void GuiExternal::getbbClicked()
319 string const filename = fromqstr(fileED->text());
320 if (filename.empty())
323 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
325 // try to get it from the file, if possible
326 string bb = graphics::readBB_from_PSFile(abs_file);
328 // we don't, so ask the Graphics Cache if it has loaded the file
332 graphics::Cache & gc = graphics::Cache::get();
333 if (gc.inCache(abs_file)) {
334 graphics::Image const * image = gc.item(abs_file)->image();
337 width = image->width();
338 height = image->height();
341 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
344 doubleToWidget(xlED, token(bb, ' ', 0));
345 doubleToWidget(ybED, token(bb, ' ', 1));
346 doubleToWidget(xrED, token(bb, ' ', 2));
347 doubleToWidget(ytED, token(bb, ' ', 3));
348 // the values from the file always have the bigpoint-unit bp
349 xlUnitCO->setCurrentIndex(0);
350 ybUnitCO->setCurrentIndex(0);
351 xrUnitCO->setCurrentIndex(0);
352 ytUnitCO->setCurrentIndex(0);
358 void GuiExternal::sizeChanged()
360 aspectratioCB->setEnabled(activateAspectratio());
365 void GuiExternal::templateChanged()
372 void GuiExternal::widthUnitChanged()
375 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
377 widthED->setValidator(unsignedLengthValidator(widthED));
379 heightED->setEnabled(!usingScale());
380 heightUnitCO->setEnabled(!usingScale());
385 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
386 external::RotationData const & data)
388 originCO.setCurrentIndex(int(data.origin()));
389 doubleToWidget(&angleED, data.angle);
393 static void getRotation(external::RotationData & data,
394 QLineEdit const & angleED, QComboBox const & originCO)
396 typedef external::RotationData::OriginType OriginType;
398 data.origin(static_cast<OriginType>(originCO.currentIndex()));
399 data.angle = widgetToDoubleStr(&angleED);
403 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
404 QLineEdit & heightED, LengthCombo & heightUnitCO,
405 QCheckBox & aspectratioCB,
406 external::ResizeData const & data)
408 bool using_scale = data.usingScale();
409 string scale = data.scale;
410 if (data.no_resize()) {
411 // Everything is zero, so default to this!
417 doubleToWidget(&widthED, scale);
418 widthUnitCO.setCurrentItem("scale");
420 lengthToWidgets(&widthED, &widthUnitCO,
421 data.width.asString(), Length::defaultUnit());
423 string const h = data.height.zero() ? string() : data.height.asString();
424 Length::UNIT const default_unit = data.width.zero() ?
425 Length::defaultUnit() : data.width.unit();
426 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
428 heightED.setEnabled(!using_scale);
429 heightUnitCO.setEnabled(!using_scale);
431 aspectratioCB.setChecked(data.keepAspectRatio);
433 bool const disable_aspectRatio = using_scale ||
434 data.width.zero() || data.height.zero();
435 aspectratioCB.setEnabled(!disable_aspectRatio);
439 static void getSize(external::ResizeData & data,
440 QLineEdit const & widthED, QComboBox const & widthUnitCO,
441 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
442 QCheckBox const & aspectratioCB, bool const scaling)
445 // scaling instead of a width
446 data.scale = widgetToDoubleStr(&widthED);
447 data.width = Length();
449 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
450 data.scale = string();
452 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
453 data.keepAspectRatio = aspectratioCB.isChecked();
457 void setCrop(QCheckBox & clipCB,
458 QLineEdit & xlED, QLineEdit & ybED,
459 QLineEdit & xrED, QLineEdit & ytED,
460 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
461 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
462 external::ClipData const & data)
464 clipCB.setChecked(data.clip);
465 Length::UNIT const default_unit = data.bbox.xl.zero() ?
466 Length::defaultUnit() : data.bbox.xl.unit();
467 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
468 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
469 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
470 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
474 static void getCrop(external::ClipData & data,
475 QCheckBox const & clipCB,
476 QLineEdit const & xlED, QLineEdit const & ybED,
477 QLineEdit const & xrED, QLineEdit const & ytED,
478 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
479 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
482 data.clip = clipCB.isChecked();
487 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
488 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
489 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
490 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
494 void GuiExternal::updateContents()
497 params_.filename.outputFileName(fromqstr(bufferFilePath()));
498 fileED->setText(toqstr(name));
500 externalCO->setCurrentIndex(
501 externalCO->findData(toqstr(params_.templatename())));
504 draftCB->setChecked(params_.draft);
506 displayGB->setChecked(params_.display);
507 displayscaleED->setText(QString::number(params_.lyxscale));
508 bool scaled = params_.display && !isBufferReadonly() &&
509 (params_.preview_mode != PREVIEW_INSTANT);
510 displayscaleED->setEnabled(scaled);
511 scaleLA->setEnabled(scaled);
512 displayGB->setEnabled(lyxrc.display_graphics);
515 setRotation(*angleED, *originCO, params_.rotationdata);
517 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
518 *aspectratioCB, params_.resizedata);
520 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
521 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
522 bbChanged_ = !params_.clipdata.bbox.empty();
528 void GuiExternal::updateTemplate()
530 external::TemplateManager const & etm =
531 external::TemplateManager::get();
532 external::Template const * const templ = etm.getTemplateByName(
533 fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
534 externalTB->setPlainText(toqstr(translateIfPossible(
535 templ ? templ->helpText : docstring())));
539 // Ascertain which (if any) transformations the template supports
540 // and disable tabs and Group Boxes hosting unsupported transforms.
541 typedef vector<external::TransformID> TransformIDs;
542 TransformIDs const transformIds = templ->transformIds;
543 TransformIDs::const_iterator tr_begin = transformIds.begin();
544 TransformIDs::const_iterator const tr_end = transformIds.end();
546 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
547 rotationGB->setEnabled(rotate);
549 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
550 scaleGB->setEnabled(resize);
552 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
553 cropGB->setEnabled(clip);
555 sizetab->setEnabled(rotate || resize || clip);
556 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
558 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
559 optionsGB->setEnabled(found);
561 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
562 !isBufferReadonly() && (templ->preview_mode != PREVIEW_INSTANT);
563 displayscaleED->setEnabled(scaled);
564 scaleLA->setEnabled(scaled);
569 // Ascertain whether the template has any formats supporting
570 // the 'Extra' option
573 extraFormatCO->clear();
575 external::Template::Formats::const_iterator it = templ->formats.begin();
576 external::Template::Formats::const_iterator end = templ->formats.end();
577 for (; it != end; ++it) {
578 if (it->second.option_transformers.find(external::Extra) ==
579 it->second.option_transformers.end())
581 string const format = it->first;
582 string const opt = params_.extradata.get(format);
583 extraFormatCO->addItem(toqstr(format));
584 extra_[toqstr(format)] = toqstr(opt);
587 bool const enabled = extraFormatCO->count() > 0;
589 optionsGB->setEnabled(enabled);
590 extraED->setEnabled(enabled && !isBufferReadonly());
591 extraFormatCO->setEnabled(enabled);
594 extraFormatCO->setCurrentIndex(0);
595 extraED->setText(extra_[extraFormatCO->currentText()]);
600 void GuiExternal::applyView()
602 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
603 params_.settemplate(fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
605 params_.draft = draftCB->isChecked();
606 params_.lyxscale = displayscaleED->text().toInt();
607 params_.display = displayGB->isChecked();
609 if (rotationGB->isEnabled())
610 getRotation(params_.rotationdata, *angleED, *originCO);
612 if (scaleGB->isEnabled())
613 getSize(params_.resizedata, *widthED, *widthUnitCO,
614 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
616 if (cropGB->isEnabled())
617 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
618 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
620 if (optionsGB->isEnabled()) {
621 MapType::const_iterator it = extra_.begin();
622 MapType::const_iterator const end = extra_.end();
623 for (; it != end; ++it)
624 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
629 bool GuiExternal::initialiseParams(string const & data)
631 InsetExternal::string2params(data, buffer(), params_);
636 void GuiExternal::clearParams()
638 params_ = InsetExternalParams();
642 void GuiExternal::dispatchParams()
644 string const lfun = InsetExternal::params2string(params_, buffer());
645 dispatch(FuncRequest(getLfun(), lfun));
649 static QStringList templateFilters(QString const & template_name)
651 /// Determine the template file extension
652 external::TemplateManager const & etm =
653 external::TemplateManager::get();
654 external::Template const * const et_ptr =
655 etm.getTemplateByName(fromqstr(template_name));
658 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
659 filter += to_utf8(_(et_ptr->guiName));
661 filter += et_ptr->fileRegExp;
664 return fileFilters(toqstr(filter));
668 QString GuiExternal::browse(QString const & input,
669 QString const & template_name) const
671 QString const title = qt_("Select external file");
672 QString const bufpath = bufferFilePath();
673 QStringList const filter = templateFilters(template_name);
675 QString const label1 = qt_("Documents|#o#O");
676 QString const dir1 = toqstr(lyxrc.document_path);
678 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
682 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
685 } // namespace frontend
688 #include "moc_GuiExternal.cpp"