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(buttonBox, SIGNAL(clicked(QAbstractButton *)),
92 this, SLOT(slotButtonBox(QAbstractButton *)));
95 connect(displayGB, SIGNAL(toggled(bool)),
96 displayscaleED, SLOT(setEnabled(bool)));
98 connect(originCO, SIGNAL(activated(int)),
99 this, SLOT(change_adaptor()));
100 connect(aspectratioCB, SIGNAL(stateChanged(int)),
101 this, SLOT(change_adaptor()));
102 connect(browsePB, SIGNAL(clicked()),
103 this, SLOT(browseClicked()));
104 connect(externalCO, SIGNAL(activated(QString)),
105 this, SLOT(templateChanged()));
106 connect(extraED, SIGNAL(textChanged(QString)),
107 this, SLOT(extraChanged(QString)));
108 connect(extraFormatCO, SIGNAL(activated(QString)),
109 this, SLOT(formatChanged(QString)));
110 connect(widthUnitCO, SIGNAL(activated(int)),
111 this, SLOT(widthUnitChanged()));
112 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
113 this, SLOT(change_adaptor()));
114 connect(displayGB, SIGNAL(toggled(bool)),
115 this, SLOT(change_adaptor()));
116 connect(displayscaleED, SIGNAL(textChanged(QString)),
117 this, SLOT(change_adaptor()));
118 connect(angleED, SIGNAL(textChanged(QString)),
119 this, SLOT(change_adaptor()));
120 connect(widthED, SIGNAL(textChanged(QString)),
121 this, SLOT(sizeChanged()));
122 connect(heightED, SIGNAL(textChanged(QString)),
123 this, SLOT(sizeChanged()));
124 connect(fileED, SIGNAL(textChanged(QString)),
125 this, SLOT(change_adaptor()));
126 connect(clipCB, SIGNAL(stateChanged(int)),
127 this, SLOT(change_adaptor()));
128 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
129 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
130 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
131 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
132 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
133 connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
134 this, SLOT(bbChanged()));
135 connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
136 this, SLOT(bbChanged()));
137 connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138 this, SLOT(bbChanged()));
139 connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
140 this, SLOT(bbChanged()));
141 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
143 QIntValidator * validator = new QIntValidator(displayscaleED);
144 validator->setBottom(1);
145 displayscaleED->setValidator(validator);
147 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
149 xlED->setValidator(unsignedLengthValidator(xlED));
150 ybED->setValidator(unsignedLengthValidator(ybED));
151 xrED->setValidator(unsignedLengthValidator(xrED));
152 ytED->setValidator(unsignedLengthValidator(ytED));
154 widthED->setValidator(unsignedLengthValidator(widthED));
155 heightED->setValidator(unsignedLengthValidator(heightED));
157 setFocusProxy(fileED);
159 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
161 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
162 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
163 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
165 bc().addReadOnly(fileED);
166 bc().addReadOnly(browsePB);
167 bc().addReadOnly(externalCO);
168 bc().addReadOnly(draftCB);
169 bc().addReadOnly(displayscaleED);
170 bc().addReadOnly(displayGB);
171 bc().addReadOnly(angleED);
172 bc().addReadOnly(originCO);
173 bc().addReadOnly(heightUnitCO);
174 bc().addReadOnly(heightED);
175 bc().addReadOnly(aspectratioCB);
176 bc().addReadOnly(widthUnitCO);
177 bc().addReadOnly(widthED);
178 bc().addReadOnly(clipCB);
179 bc().addReadOnly(getbbPB);
180 bc().addReadOnly(ytED);
181 bc().addReadOnly(xlED);
182 bc().addReadOnly(xrED);
183 bc().addReadOnly(ybED);
184 bc().addReadOnly(ytUnitCO);
185 bc().addReadOnly(xlUnitCO);
186 bc().addReadOnly(xrUnitCO);
187 bc().addReadOnly(ybUnitCO);
188 bc().addReadOnly(extraFormatCO);
189 bc().addReadOnly(extraED);
191 bc().addCheckedLineEdit(angleED, angleLA);
192 bc().addCheckedLineEdit(displayscaleED, scaleLA);
193 bc().addCheckedLineEdit(heightED, heightLA);
194 bc().addCheckedLineEdit(widthED, widthLA);
195 bc().addCheckedLineEdit(xlED, lbLA);
196 bc().addCheckedLineEdit(ybED, lbLA);
197 bc().addCheckedLineEdit(xrED, rtLA);
198 bc().addCheckedLineEdit(ytED, rtLA);
199 bc().addCheckedLineEdit(fileED, fileLA);
201 external::TemplateManager::Templates::const_iterator i1, i2;
202 i1 = external::TemplateManager::get().getTemplates().begin();
203 i2 = external::TemplateManager::get().getTemplates().end();
204 QMap<QString, QString> localizedTemplates;
205 for (; i1 != i2; ++i1)
206 localizedTemplates.insert(qt_(i1->second.guiName), toqstr(i1->second.lyxName));
207 // Sort alphabetically by (localized) GUI name
208 QStringList keys = localizedTemplates.keys();
209 sort(keys.begin(), keys.end(), SortLocaleAware);
210 for (QString & key : keys) {
211 QString const value = localizedTemplates[key];
212 externalCO->addItem(key, value);
215 // Fill the origins combo
216 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
217 originCO->addItem(qt_(origin_gui_strs[i]));
220 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
222 // remove all units from bb that depend on font or other dimensions
223 // we cannot use these, since we need to compare against absolute
224 // values from the image file.
225 xlUnitCO->noPercents();
226 xlUnitCO->removeFontDependent();
227 xrUnitCO->noPercents();
228 xrUnitCO->removeFontDependent();
229 ytUnitCO->noPercents();
230 ytUnitCO->removeFontDependent();
231 ybUnitCO->noPercents();
232 ybUnitCO->removeFontDependent();
236 bool GuiExternal::activateAspectratio() const
241 QString const wstr = widthED->text();
245 double val = wstr.trimmed().toDouble(&wIsDbl);
246 if (wIsDbl && float_equal(val, 0.0, 0.05))
249 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
252 QString const hstr = heightED->text();
256 val = hstr.trimmed().toDouble(&hIsDbl);
257 if (hIsDbl && float_equal(val, 0.0, 0.05))
259 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
266 bool GuiExternal::usingScale() const
268 return (widthUnitCO->itemData(
269 widthUnitCO->currentIndex()).toString() == "scale");
273 void GuiExternal::bbChanged()
280 void GuiExternal::browseClicked()
282 QString const template_name =
283 externalCO->itemData(externalCO->currentIndex()).toString();
284 QString const str = browse(fileED->text(), template_name);
285 if (!str.isEmpty()) {
286 fileED->setText(str);
292 void GuiExternal::change_adaptor()
298 void GuiExternal::extraChanged(const QString & text)
300 extra_[extraFormatCO->currentText()] = text;
305 void GuiExternal::formatChanged(const QString & format)
307 extraED->setText(extra_[format]);
311 void GuiExternal::getbbClicked()
318 string const filename = fromqstr(fileED->text());
319 if (filename.empty())
322 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
324 // try to get it from the file, if possible
325 string bb = graphics::readBB_from_PSFile(abs_file);
327 // we don't, so ask the Graphics Cache if it has loaded the file
331 graphics::Cache & gc = graphics::Cache::get();
332 if (gc.inCache(abs_file)) {
333 graphics::Image const * image = gc.item(abs_file)->image();
336 width = image->width();
337 height = image->height();
340 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
343 doubleToWidget(xlED, token(bb, ' ', 0));
344 doubleToWidget(ybED, token(bb, ' ', 1));
345 doubleToWidget(xrED, token(bb, ' ', 2));
346 doubleToWidget(ytED, token(bb, ' ', 3));
347 // the values from the file always have the bigpoint-unit bp
348 xlUnitCO->setCurrentIndex(0);
349 ybUnitCO->setCurrentIndex(0);
350 xrUnitCO->setCurrentIndex(0);
351 ytUnitCO->setCurrentIndex(0);
357 void GuiExternal::sizeChanged()
359 aspectratioCB->setEnabled(activateAspectratio());
364 void GuiExternal::templateChanged()
371 void GuiExternal::widthUnitChanged()
374 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
376 widthED->setValidator(unsignedLengthValidator(widthED));
378 heightED->setEnabled(!usingScale());
379 heightUnitCO->setEnabled(!usingScale());
384 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
385 external::RotationData const & data)
387 originCO.setCurrentIndex(int(data.origin()));
388 doubleToWidget(&angleED, data.angle);
392 static void getRotation(external::RotationData & data,
393 QLineEdit const & angleED, QComboBox const & originCO)
395 typedef external::RotationData::OriginType OriginType;
397 data.origin(static_cast<OriginType>(originCO.currentIndex()));
398 data.angle = widgetToDoubleStr(&angleED);
402 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
403 QLineEdit & heightED, LengthCombo & heightUnitCO,
404 QCheckBox & aspectratioCB,
405 external::ResizeData const & data)
407 bool using_scale = data.usingScale();
408 string scale = data.scale;
409 if (data.no_resize()) {
410 // Everything is zero, so default to this!
416 doubleToWidget(&widthED, scale);
417 widthUnitCO.setCurrentItem("scale");
419 lengthToWidgets(&widthED, &widthUnitCO,
420 data.width.asString(), Length::defaultUnit());
422 string const h = data.height.zero() ? string() : data.height.asString();
423 Length::UNIT const default_unit = data.width.zero() ?
424 Length::defaultUnit() : data.width.unit();
425 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
427 heightED.setEnabled(!using_scale);
428 heightUnitCO.setEnabled(!using_scale);
430 aspectratioCB.setChecked(data.keepAspectRatio);
432 bool const disable_aspectRatio = using_scale ||
433 data.width.zero() || data.height.zero();
434 aspectratioCB.setEnabled(!disable_aspectRatio);
438 static void getSize(external::ResizeData & data,
439 QLineEdit const & widthED, QComboBox const & widthUnitCO,
440 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
441 QCheckBox const & aspectratioCB, bool const scaling)
444 // scaling instead of a width
445 data.scale = widgetToDoubleStr(&widthED);
446 data.width = Length();
448 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
449 data.scale = string();
451 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
452 data.keepAspectRatio = aspectratioCB.isChecked();
456 void setCrop(QCheckBox & clipCB,
457 QLineEdit & xlED, QLineEdit & ybED,
458 QLineEdit & xrED, QLineEdit & ytED,
459 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
460 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
461 external::ClipData const & data)
463 clipCB.setChecked(data.clip);
464 Length::UNIT const default_unit = data.bbox.xl.zero() ?
465 Length::defaultUnit() : data.bbox.xl.unit();
466 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
467 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
468 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
469 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
473 static void getCrop(external::ClipData & data,
474 QCheckBox const & clipCB,
475 QLineEdit const & xlED, QLineEdit const & ybED,
476 QLineEdit const & xrED, QLineEdit const & ytED,
477 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
478 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
481 data.clip = clipCB.isChecked();
486 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
487 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
488 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
489 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
493 void GuiExternal::updateContents()
495 if (params_.filename.empty())
496 tab->setCurrentIndex(0);
499 params_.filename.outputFileName(fromqstr(bufferFilePath()));
500 fileED->setText(toqstr(name));
502 externalCO->setCurrentIndex(
503 externalCO->findData(toqstr(params_.templatename())));
506 draftCB->setChecked(params_.draft);
508 displayGB->setChecked(params_.display);
509 displayscaleED->setText(QString::number(params_.lyxscale));
510 bool scaled = params_.display && !isBufferReadonly() &&
511 (params_.preview_mode != PREVIEW_INSTANT);
512 displayscaleED->setEnabled(scaled);
513 scaleLA->setEnabled(scaled);
514 displayGB->setEnabled(lyxrc.display_graphics);
517 setRotation(*angleED, *originCO, params_.rotationdata);
519 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
520 *aspectratioCB, params_.resizedata);
522 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
523 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
524 bbChanged_ = !params_.clipdata.bbox.empty();
530 void GuiExternal::updateTemplate()
532 external::TemplateManager const & etm =
533 external::TemplateManager::get();
534 external::Template const * const templ = etm.getTemplateByName(
535 fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
536 externalTB->setPlainText(toqstr(translateIfPossible(
537 templ ? templ->helpText : docstring())));
541 // Ascertain which (if any) transformations the template supports
542 // and disable tabs and Group Boxes hosting unsupported transforms.
543 typedef vector<external::TransformID> TransformIDs;
544 TransformIDs const transformIds = templ->transformIds;
545 TransformIDs::const_iterator tr_begin = transformIds.begin();
546 TransformIDs::const_iterator const tr_end = transformIds.end();
548 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
549 rotationGB->setEnabled(rotate);
551 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
552 scaleGB->setEnabled(resize);
554 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
555 cropGB->setEnabled(clip);
557 sizetab->setEnabled(rotate || resize || clip);
558 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
560 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
561 optionsGB->setEnabled(found);
563 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
564 !isBufferReadonly() && (templ->preview_mode != PREVIEW_INSTANT);
565 displayscaleED->setEnabled(scaled);
566 scaleLA->setEnabled(scaled);
571 // Ascertain whether the template has any formats supporting
572 // the 'Extra' option
575 extraFormatCO->clear();
577 external::Template::Formats::const_iterator it = templ->formats.begin();
578 external::Template::Formats::const_iterator end = templ->formats.end();
579 for (; it != end; ++it) {
580 if (it->second.option_transformers.find(external::Extra) ==
581 it->second.option_transformers.end())
583 string const format = it->first;
584 string const opt = params_.extradata.get(format);
585 extraFormatCO->addItem(toqstr(format));
586 extra_[toqstr(format)] = toqstr(opt);
589 bool const enabled = extraFormatCO->count() > 0;
591 optionsGB->setEnabled(enabled);
592 extraED->setEnabled(enabled && !isBufferReadonly());
593 extraFormatCO->setEnabled(enabled);
596 extraFormatCO->setCurrentIndex(0);
597 extraED->setText(extra_[extraFormatCO->currentText()]);
602 void GuiExternal::applyView()
604 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
605 params_.settemplate(fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
607 params_.draft = draftCB->isChecked();
608 params_.lyxscale = displayscaleED->text().toInt();
609 params_.display = displayGB->isChecked();
611 if (rotationGB->isEnabled())
612 getRotation(params_.rotationdata, *angleED, *originCO);
614 if (scaleGB->isEnabled())
615 getSize(params_.resizedata, *widthED, *widthUnitCO,
616 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
618 if (cropGB->isEnabled())
619 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
620 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
622 if (optionsGB->isEnabled()) {
623 MapType::const_iterator it = extra_.begin();
624 MapType::const_iterator const end = extra_.end();
625 for (; it != end; ++it)
626 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
631 bool GuiExternal::initialiseParams(string const & sdata)
633 InsetExternal::string2params(sdata, buffer(), params_);
638 void GuiExternal::clearParams()
640 params_ = InsetExternalParams();
644 void GuiExternal::dispatchParams()
646 string const lfun = InsetExternal::params2string(params_, buffer());
647 dispatch(FuncRequest(getLfun(), lfun));
651 static QStringList templateFilters(QString const & template_name)
653 /// Determine the template file extension
654 external::TemplateManager const & etm =
655 external::TemplateManager::get();
656 external::Template const * const et_ptr =
657 etm.getTemplateByName(fromqstr(template_name));
660 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
661 filter += to_utf8(_(et_ptr->guiName));
663 filter += et_ptr->fileRegExp;
666 return fileFilters(toqstr(filter));
670 QString GuiExternal::browse(QString const & input,
671 QString const & template_name) const
673 QString const title = qt_("Select external file");
674 QString const bufpath = bufferFilePath();
675 QStringList const filter = templateFilters(template_name);
677 QString const label1 = qt_("D&ocuments");
678 QString const dir1 = toqstr(lyxrc.document_path);
680 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
684 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
687 } // namespace frontend
690 #include "moc_GuiExternal.cpp"