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"
22 #include "insets/ExternalSupport.h"
23 #include "insets/ExternalTemplate.h"
25 #include "graphics/epstools.h"
26 #include "graphics/GraphicsCache.h"
27 #include "graphics/GraphicsCacheItem.h"
28 #include "graphics/GraphicsImage.h"
30 #include "support/convert.h"
31 #include "support/filetools.h"
32 #include "support/Length.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
37 #include "LengthCombo.h"
38 #include "qt_helpers.h"
39 #include "Validator.h"
44 #include <QPushButton>
46 #include <QTextBrowser>
49 using namespace lyx::support;
54 using namespace external;
58 RotationDataType origins[] = {
59 RotationData::DEFAULT,
60 RotationData::TOPLEFT,
61 RotationData::BOTTOMLEFT,
62 RotationData::BASELINELEFT,
64 RotationData::TOPCENTER,
65 RotationData::BOTTOMCENTER,
66 RotationData::BASELINECENTER,
67 RotationData::TOPRIGHT,
68 RotationData::BOTTOMRIGHT,
69 RotationData::BASELINERIGHT
73 // These are the strings, corresponding to the above, that the GUI should
74 // use. Note that they can/should be translated.
75 char const * const origin_gui_strs[] = {
77 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
78 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
79 N_("Top right"), N_("Bottom right"), N_("Baseline right")
85 GuiExternal::GuiExternal(GuiView & lv)
86 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
90 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
91 this, SLOT(slotButtonBox(QAbstractButton *)));
94 connect(displayGB, SIGNAL(toggled(bool)),
95 displayscaleED, SLOT(setEnabled(bool)));
97 connect(originCO, SIGNAL(activated(int)),
98 this, SLOT(change_adaptor()));
99 connect(aspectratioCB, SIGNAL(stateChanged(int)),
100 this, SLOT(change_adaptor()));
101 connect(browsePB, SIGNAL(clicked()),
102 this, SLOT(browseClicked()));
103 connect(externalCO, SIGNAL(activated(QString)),
104 this, SLOT(templateChanged()));
105 connect(extraED, SIGNAL(textChanged(QString)),
106 this, SLOT(extraChanged(QString)));
107 connect(extraFormatCO, SIGNAL(activated(QString)),
108 this, SLOT(formatChanged(QString)));
109 connect(widthUnitCO, SIGNAL(activated(int)),
110 this, SLOT(widthUnitChanged()));
111 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
112 this, SLOT(change_adaptor()));
113 connect(displayGB, SIGNAL(toggled(bool)),
114 this, SLOT(change_adaptor()));
115 connect(displayscaleED, SIGNAL(textChanged(QString)),
116 this, SLOT(change_adaptor()));
117 connect(angleED, SIGNAL(textChanged(QString)),
118 this, SLOT(change_adaptor()));
119 connect(widthED, SIGNAL(textChanged(QString)),
120 this, SLOT(sizeChanged()));
121 connect(heightED, SIGNAL(textChanged(QString)),
122 this, SLOT(sizeChanged()));
123 connect(fileED, SIGNAL(textChanged(QString)),
124 this, SLOT(change_adaptor()));
125 connect(clipCB, SIGNAL(stateChanged(int)),
126 this, SLOT(change_adaptor()));
127 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
128 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
129 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
130 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
131 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
132 connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
133 this, SLOT(bbChanged()));
134 connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
135 this, SLOT(bbChanged()));
136 connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
137 this, SLOT(bbChanged()));
138 connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
139 this, SLOT(bbChanged()));
140 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
142 QIntValidator * validator = new QIntValidator(displayscaleED);
143 validator->setBottom(1);
144 displayscaleED->setValidator(validator);
146 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
148 xlED->setValidator(unsignedLengthValidator(xlED));
149 ybED->setValidator(unsignedLengthValidator(ybED));
150 xrED->setValidator(unsignedLengthValidator(xrED));
151 ytED->setValidator(unsignedLengthValidator(ytED));
153 widthED->setValidator(unsignedLengthValidator(widthED));
154 heightED->setValidator(unsignedLengthValidator(heightED));
156 setFocusProxy(fileED);
158 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
160 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
161 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
162 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
164 bc().addReadOnly(fileED);
165 bc().addReadOnly(browsePB);
166 bc().addReadOnly(externalCO);
167 bc().addReadOnly(draftCB);
168 bc().addReadOnly(displayscaleED);
169 bc().addReadOnly(displayGB);
170 bc().addReadOnly(angleED);
171 bc().addReadOnly(originCO);
172 bc().addReadOnly(heightUnitCO);
173 bc().addReadOnly(heightED);
174 bc().addReadOnly(aspectratioCB);
175 bc().addReadOnly(widthUnitCO);
176 bc().addReadOnly(widthED);
177 bc().addReadOnly(clipCB);
178 bc().addReadOnly(getbbPB);
179 bc().addReadOnly(ytED);
180 bc().addReadOnly(xlED);
181 bc().addReadOnly(xrED);
182 bc().addReadOnly(ybED);
183 bc().addReadOnly(ytUnitCO);
184 bc().addReadOnly(xlUnitCO);
185 bc().addReadOnly(xrUnitCO);
186 bc().addReadOnly(ybUnitCO);
187 bc().addReadOnly(extraFormatCO);
188 bc().addReadOnly(extraED);
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);
200 external::TemplateManager::Templates::const_iterator i1, i2;
201 i1 = external::TemplateManager::get().getTemplates().begin();
202 i2 = external::TemplateManager::get().getTemplates().end();
203 QMap<QString, QString> localizedTemplates;
204 for (; i1 != i2; ++i1)
205 localizedTemplates.insert(qt_(i1->second.guiName), toqstr(i1->second.lyxName));
206 // Sort alphabetically by (localized) GUI name
207 QStringList keys = localizedTemplates.keys();
208 sort(keys.begin(), keys.end(), SortLocaleAware);
209 for (QString & key : keys) {
210 QString const value = localizedTemplates[key];
211 externalCO->addItem(key, value);
214 // Fill the origins combo
215 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
216 originCO->addItem(qt_(origin_gui_strs[i]));
219 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
221 // remove all units from bb that depend on font or other dimensions
222 // we cannot use these, since we need to compare against absolute
223 // values from the image file.
224 xlUnitCO->noPercents();
225 xlUnitCO->removeFontDependent();
226 xrUnitCO->noPercents();
227 xrUnitCO->removeFontDependent();
228 ytUnitCO->noPercents();
229 ytUnitCO->removeFontDependent();
230 ybUnitCO->noPercents();
231 ybUnitCO->removeFontDependent();
235 bool GuiExternal::activateAspectratio() const
240 QString const wstr = widthED->text();
244 double val = wstr.trimmed().toDouble(&wIsDbl);
245 if (wIsDbl && float_equal(val, 0.0, 0.05))
248 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
251 QString const hstr = heightED->text();
255 val = hstr.trimmed().toDouble(&hIsDbl);
256 if (hIsDbl && float_equal(val, 0.0, 0.05))
258 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
265 bool GuiExternal::usingScale() const
267 return (widthUnitCO->itemData(
268 widthUnitCO->currentIndex()).toString() == "scale");
272 void GuiExternal::bbChanged()
279 void GuiExternal::browseClicked()
281 QString const template_name =
282 externalCO->itemData(externalCO->currentIndex()).toString();
283 QString const str = browse(fileED->text(), template_name);
284 if (!str.isEmpty()) {
285 fileED->setText(str);
291 void GuiExternal::change_adaptor()
297 void GuiExternal::extraChanged(const QString & text)
299 extra_[extraFormatCO->currentText()] = text;
304 void GuiExternal::formatChanged(const QString & format)
306 extraED->setText(extra_[format]);
310 void GuiExternal::getbbClicked()
317 string const filename = fromqstr(fileED->text());
318 if (filename.empty())
321 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
323 // try to get it from the file, if possible
324 string bb = graphics::readBB_from_PSFile(abs_file);
326 // we don't, so ask the Graphics Cache if it has loaded the file
330 graphics::Cache & gc = graphics::Cache::get();
331 if (gc.inCache(abs_file)) {
332 graphics::Image const * image = gc.item(abs_file)->image();
335 width = image->width();
336 height = image->height();
339 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
342 doubleToWidget(xlED, token(bb, ' ', 0));
343 doubleToWidget(ybED, token(bb, ' ', 1));
344 doubleToWidget(xrED, token(bb, ' ', 2));
345 doubleToWidget(ytED, token(bb, ' ', 3));
346 // the values from the file always have the bigpoint-unit bp
347 xlUnitCO->setCurrentIndex(0);
348 ybUnitCO->setCurrentIndex(0);
349 xrUnitCO->setCurrentIndex(0);
350 ytUnitCO->setCurrentIndex(0);
356 void GuiExternal::sizeChanged()
358 aspectratioCB->setEnabled(activateAspectratio());
363 void GuiExternal::templateChanged()
370 void GuiExternal::widthUnitChanged()
373 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
375 widthED->setValidator(unsignedLengthValidator(widthED));
377 heightED->setEnabled(!usingScale());
378 heightUnitCO->setEnabled(!usingScale());
383 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
384 external::RotationData const & data)
386 originCO.setCurrentIndex(int(data.origin()));
387 doubleToWidget(&angleED, data.angle);
391 static void getRotation(external::RotationData & data,
392 QLineEdit const & angleED, QComboBox const & originCO)
394 typedef external::RotationData::OriginType OriginType;
396 data.origin(static_cast<OriginType>(originCO.currentIndex()));
397 data.angle = widgetToDoubleStr(&angleED);
401 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
402 QLineEdit & heightED, LengthCombo & heightUnitCO,
403 QCheckBox & aspectratioCB,
404 external::ResizeData const & data)
406 bool using_scale = data.usingScale();
407 string scale = data.scale;
408 if (data.no_resize()) {
409 // Everything is zero, so default to this!
415 doubleToWidget(&widthED, scale);
416 widthUnitCO.setCurrentItem("scale");
418 lengthToWidgets(&widthED, &widthUnitCO,
419 data.width.asString(), Length::defaultUnit());
421 string const h = data.height.zero() ? string() : data.height.asString();
422 Length::UNIT const default_unit = data.width.zero() ?
423 Length::defaultUnit() : data.width.unit();
424 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
426 heightED.setEnabled(!using_scale);
427 heightUnitCO.setEnabled(!using_scale);
429 aspectratioCB.setChecked(data.keepAspectRatio);
431 bool const disable_aspectRatio = using_scale ||
432 data.width.zero() || data.height.zero();
433 aspectratioCB.setEnabled(!disable_aspectRatio);
437 static void getSize(external::ResizeData & data,
438 QLineEdit const & widthED, QComboBox const & widthUnitCO,
439 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
440 QCheckBox const & aspectratioCB, bool const scaling)
443 // scaling instead of a width
444 data.scale = widgetToDoubleStr(&widthED);
445 data.width = Length();
447 data.width = widgetsToLength(&widthED, &widthUnitCO);
448 data.scale = string();
450 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
451 data.keepAspectRatio = aspectratioCB.isChecked();
455 void setCrop(QCheckBox & clipCB,
456 QLineEdit & xlED, QLineEdit & ybED,
457 QLineEdit & xrED, QLineEdit & ytED,
458 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
459 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
460 external::ClipData const & data)
462 clipCB.setChecked(data.clip);
463 Length::UNIT const default_unit = data.bbox.xl.zero() ?
464 Length::defaultUnit() : data.bbox.xl.unit();
465 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
466 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
467 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
468 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
472 static void getCrop(external::ClipData & data,
473 QCheckBox const & clipCB,
474 QLineEdit const & xlED, QLineEdit const & ybED,
475 QLineEdit const & xrED, QLineEdit const & ytED,
476 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
477 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
480 data.clip = clipCB.isChecked();
485 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
486 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
487 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
488 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
492 void GuiExternal::updateContents()
494 if (params_.filename.empty())
495 tab->setCurrentIndex(0);
498 params_.filename.outputFileName(fromqstr(bufferFilePath()));
499 fileED->setText(toqstr(name));
501 externalCO->setCurrentIndex(
502 externalCO->findData(toqstr(params_.templatename())));
505 draftCB->setChecked(params_.draft);
507 displayGB->setChecked(params_.display);
508 displayscaleED->setText(QString::number(params_.lyxscale));
509 bool scaled = params_.display && !isBufferReadonly() &&
510 (params_.preview_mode != PREVIEW_INSTANT);
511 displayscaleED->setEnabled(scaled);
512 scaleLA->setEnabled(scaled);
513 displayGB->setEnabled(lyxrc.display_graphics);
516 setRotation(*angleED, *originCO, params_.rotationdata);
518 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
519 *aspectratioCB, params_.resizedata);
521 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
522 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
523 bbChanged_ = !params_.clipdata.bbox.empty();
529 void GuiExternal::updateTemplate()
531 external::TemplateManager const & etm =
532 external::TemplateManager::get();
533 external::Template const * const templ = etm.getTemplateByName(
534 fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
535 externalTB->setPlainText(toqstr(translateIfPossible(
536 templ ? templ->helpText : docstring())));
540 // Ascertain which (if any) transformations the template supports
541 // and disable tabs and Group Boxes hosting unsupported transforms.
542 typedef vector<external::TransformID> TransformIDs;
543 TransformIDs const transformIds = templ->transformIds;
544 TransformIDs::const_iterator tr_begin = transformIds.begin();
545 TransformIDs::const_iterator const tr_end = transformIds.end();
547 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
548 rotationGB->setEnabled(rotate);
550 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
551 scaleGB->setEnabled(resize);
553 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
554 cropGB->setEnabled(clip);
556 sizetab->setEnabled(rotate || resize || clip);
557 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
559 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
560 optionsGB->setEnabled(found);
562 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
563 !isBufferReadonly() && (templ->preview_mode != PREVIEW_INSTANT);
564 displayscaleED->setEnabled(scaled);
565 scaleLA->setEnabled(scaled);
570 // Ascertain whether the template has any formats supporting
571 // the 'Extra' option
574 extraFormatCO->clear();
576 external::Template::Formats::const_iterator it = templ->formats.begin();
577 external::Template::Formats::const_iterator end = templ->formats.end();
578 for (; it != end; ++it) {
579 if (it->second.option_transformers.find(external::Extra) ==
580 it->second.option_transformers.end())
582 string const format = it->first;
583 string const opt = params_.extradata.get(format);
584 extraFormatCO->addItem(toqstr(format));
585 extra_[toqstr(format)] = toqstr(opt);
588 bool const enabled = extraFormatCO->count() > 0;
590 optionsGB->setEnabled(enabled);
591 extraED->setEnabled(enabled && !isBufferReadonly());
592 extraFormatCO->setEnabled(enabled);
595 extraFormatCO->setCurrentIndex(0);
596 extraED->setText(extra_[extraFormatCO->currentText()]);
601 void GuiExternal::applyView()
603 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
604 params_.settemplate(fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
606 params_.draft = draftCB->isChecked();
607 params_.lyxscale = displayscaleED->text().toInt();
608 params_.display = displayGB->isChecked();
610 if (rotationGB->isEnabled())
611 getRotation(params_.rotationdata, *angleED, *originCO);
613 if (scaleGB->isEnabled())
614 getSize(params_.resizedata, *widthED, *widthUnitCO,
615 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
617 if (cropGB->isEnabled())
618 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
619 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
621 if (optionsGB->isEnabled()) {
622 MapType::const_iterator it = extra_.begin();
623 MapType::const_iterator const end = extra_.end();
624 for (; it != end; ++it)
625 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
630 bool GuiExternal::initialiseParams(string const & sdata)
632 InsetExternal::string2params(sdata, buffer(), params_);
637 void GuiExternal::clearParams()
639 params_ = InsetExternalParams();
643 void GuiExternal::dispatchParams()
645 string const lfun = InsetExternal::params2string(params_, buffer());
646 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 } // namespace frontend
687 #include "moc_GuiExternal.cpp"