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(int)),
104 this, SLOT(templateChanged()));
105 connect(extraED, SIGNAL(textChanged(QString)),
106 this, SLOT(extraChanged(QString)));
107 connect(extraFormatCO, SIGNAL(activated(int)),
108 this, SLOT(formatChanged(int)));
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 // Add validated widgets to those that will be
191 // visually marked if invalid
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 // We also mark the tabs the widgets are in
203 int const tabindex = tab->indexOf(sizetab);
204 bc().addCheckedLineEdit(angleED, tab, tabindex);
205 bc().addCheckedLineEdit(heightED, tab, tabindex);
206 bc().addCheckedLineEdit(widthED, tab, tabindex);
207 bc().addCheckedLineEdit(xlED, tab, tabindex);
208 bc().addCheckedLineEdit(ybED, tab, tabindex);
209 bc().addCheckedLineEdit(xrED, tab, tabindex);
210 bc().addCheckedLineEdit(ytED, tab, tabindex);
211 bc().addCheckedLineEdit(displayscaleED, tab, tab->indexOf(lyxviewtab));
212 bc().addCheckedLineEdit(fileED, tab, tab->indexOf(filetab));
214 external::TemplateManager::Templates::const_iterator i1, i2;
215 i1 = external::TemplateManager::get().getTemplates().begin();
216 i2 = external::TemplateManager::get().getTemplates().end();
217 QMap<QString, QString> localizedTemplates;
218 for (; i1 != i2; ++i1)
219 localizedTemplates.insert(qt_(i1->second.guiName), toqstr(i1->second.lyxName));
220 // Sort alphabetically by (localized) GUI name
221 QStringList keys = localizedTemplates.keys();
222 sort(keys.begin(), keys.end(), SortLocaleAware);
223 for (QString & key : keys) {
224 QString const value = localizedTemplates[key];
225 externalCO->addItem(key, value);
228 // Fill the origins combo
229 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
230 originCO->addItem(qt_(origin_gui_strs[i]));
233 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
235 // remove all units from bb that depend on font or other dimensions
236 // we cannot use these, since we need to compare against absolute
237 // values from the image file.
238 xlUnitCO->noPercents();
239 xlUnitCO->removeFontDependent();
240 xrUnitCO->noPercents();
241 xrUnitCO->removeFontDependent();
242 ytUnitCO->noPercents();
243 ytUnitCO->removeFontDependent();
244 ybUnitCO->noPercents();
245 ybUnitCO->removeFontDependent();
249 bool GuiExternal::activateAspectratio() const
254 QString const wstr = widthED->text();
258 double val = wstr.trimmed().toDouble(&wIsDbl);
259 if (wIsDbl && float_equal(val, 0.0, 0.05))
262 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
265 QString const hstr = heightED->text();
269 val = hstr.trimmed().toDouble(&hIsDbl);
270 if (hIsDbl && float_equal(val, 0.0, 0.05))
272 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
279 bool GuiExternal::usingScale() const
281 return (widthUnitCO->itemData(
282 widthUnitCO->currentIndex()).toString() == "scale");
286 void GuiExternal::bbChanged()
293 void GuiExternal::browseClicked()
295 QString const template_name =
296 externalCO->itemData(externalCO->currentIndex()).toString();
297 QString const str = browse(fileED->text(), template_name);
298 if (!str.isEmpty()) {
299 fileED->setText(str);
305 void GuiExternal::change_adaptor()
311 void GuiExternal::extraChanged(const QString & text)
313 extra_[extraFormatCO->currentText()] = text;
318 void GuiExternal::formatChanged(int const i)
320 extraED->setText(extra_[extraFormatCO->itemText(i)]);
324 void GuiExternal::getbbClicked()
331 string const filename = fromqstr(fileED->text());
332 if (filename.empty())
335 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
337 // try to get it from the file, if possible
338 string bb = graphics::readBB_from_PSFile(abs_file);
340 // we don't, so ask the Graphics Cache if it has loaded the file
344 graphics::Cache & gc = graphics::Cache::get();
345 if (gc.inCache(abs_file)) {
346 graphics::Image const * image = gc.item(abs_file)->image();
349 width = image->width();
350 height = image->height();
353 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
356 doubleToWidget(xlED, token(bb, ' ', 0));
357 doubleToWidget(ybED, token(bb, ' ', 1));
358 doubleToWidget(xrED, token(bb, ' ', 2));
359 doubleToWidget(ytED, token(bb, ' ', 3));
360 // the values from the file always have the bigpoint-unit bp
361 xlUnitCO->setCurrentIndex(0);
362 ybUnitCO->setCurrentIndex(0);
363 xrUnitCO->setCurrentIndex(0);
364 ytUnitCO->setCurrentIndex(0);
370 void GuiExternal::sizeChanged()
372 aspectratioCB->setEnabled(activateAspectratio());
377 void GuiExternal::templateChanged()
384 void GuiExternal::widthUnitChanged()
387 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
389 widthED->setValidator(unsignedLengthValidator(widthED));
391 heightED->setEnabled(!usingScale());
392 heightUnitCO->setEnabled(!usingScale());
397 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
398 external::RotationData const & data)
400 originCO.setCurrentIndex(int(data.origin()));
401 doubleToWidget(&angleED, data.angle);
405 static void getRotation(external::RotationData & data,
406 QLineEdit const & angleED, QComboBox const & originCO)
408 typedef external::RotationData::OriginType OriginType;
410 data.origin(static_cast<OriginType>(originCO.currentIndex()));
411 data.angle = widgetToDoubleStr(&angleED);
415 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
416 QLineEdit & heightED, LengthCombo & heightUnitCO,
417 QCheckBox & aspectratioCB,
418 external::ResizeData const & data)
420 bool using_scale = data.usingScale();
421 string scale = data.scale;
422 if (data.no_resize()) {
423 // Everything is zero, so default to this!
429 doubleToWidget(&widthED, scale);
430 widthUnitCO.setCurrentItem("scale");
432 lengthToWidgets(&widthED, &widthUnitCO,
433 data.width.asString(), Length::defaultUnit());
435 string const h = data.height.zero() ? string() : data.height.asString();
436 Length::UNIT const default_unit = data.width.zero() ?
437 Length::defaultUnit() : data.width.unit();
438 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
440 heightED.setEnabled(!using_scale);
441 heightUnitCO.setEnabled(!using_scale);
443 aspectratioCB.setChecked(data.keepAspectRatio);
445 bool const disable_aspectRatio = using_scale ||
446 data.width.zero() || data.height.zero();
447 aspectratioCB.setEnabled(!disable_aspectRatio);
451 static void getSize(external::ResizeData & data,
452 QLineEdit const & widthED, QComboBox const & widthUnitCO,
453 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
454 QCheckBox const & aspectratioCB, bool const scaling)
457 // scaling instead of a width
458 data.scale = widgetToDoubleStr(&widthED);
459 data.width = Length();
461 data.width = widgetsToLength(&widthED, &widthUnitCO);
462 data.scale = string();
464 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
465 data.keepAspectRatio = aspectratioCB.isChecked();
469 void setCrop(QCheckBox & clipCB,
470 QLineEdit & xlED, QLineEdit & ybED,
471 QLineEdit & xrED, QLineEdit & ytED,
472 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
473 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
474 external::ClipData const & data)
476 clipCB.setChecked(data.clip);
477 Length::UNIT const default_unit = data.bbox.xl.zero() ?
478 Length::defaultUnit() : data.bbox.xl.unit();
479 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
480 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
481 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
482 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
486 static void getCrop(external::ClipData & data,
487 QCheckBox const & clipCB,
488 QLineEdit const & xlED, QLineEdit const & ybED,
489 QLineEdit const & xrED, QLineEdit const & ytED,
490 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
491 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
494 data.clip = clipCB.isChecked();
499 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
500 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
501 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
502 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
506 void GuiExternal::updateContents()
508 if (params_.filename.empty())
509 tab->setCurrentIndex(0);
512 params_.filename.outputFileName(fromqstr(bufferFilePath()));
513 fileED->setText(toqstr(name));
515 externalCO->setCurrentIndex(
516 externalCO->findData(toqstr(params_.templatename())));
519 draftCB->setChecked(params_.draft);
521 displayGB->setChecked(params_.display);
522 displayscaleED->setText(QString::number(params_.lyxscale));
523 bool scaled = params_.display && !isBufferReadonly() &&
524 (params_.preview_mode != PREVIEW_INSTANT);
525 displayscaleED->setEnabled(scaled);
526 scaleLA->setEnabled(scaled);
527 displayGB->setEnabled(lyxrc.display_graphics);
530 setRotation(*angleED, *originCO, params_.rotationdata);
532 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
533 *aspectratioCB, params_.resizedata);
535 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
536 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
537 bbChanged_ = !params_.clipdata.bbox.empty();
543 void GuiExternal::updateTemplate()
545 external::TemplateManager const & etm =
546 external::TemplateManager::get();
547 external::Template const * const templ = etm.getTemplateByName(
548 fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
549 externalTB->setPlainText(toqstr(translateIfPossible(
550 templ ? templ->helpText : docstring())));
554 // Ascertain which (if any) transformations the template supports
555 // and disable tabs and Group Boxes hosting unsupported transforms.
556 typedef vector<external::TransformID> TransformIDs;
557 TransformIDs const transformIds = templ->transformIds;
558 TransformIDs::const_iterator tr_begin = transformIds.begin();
559 TransformIDs::const_iterator const tr_end = transformIds.end();
561 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
562 rotationGB->setEnabled(rotate);
564 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
565 scaleGB->setEnabled(resize);
567 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
568 cropGB->setEnabled(clip);
570 sizetab->setEnabled(rotate || resize || clip);
571 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
573 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
574 optionsGB->setEnabled(found);
576 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
577 !isBufferReadonly() && (templ->preview_mode != PREVIEW_INSTANT);
578 displayscaleED->setEnabled(scaled);
579 scaleLA->setEnabled(scaled);
584 // Ascertain whether the template has any formats supporting
585 // the 'Extra' option
588 extraFormatCO->clear();
590 external::Template::Formats::const_iterator it = templ->formats.begin();
591 external::Template::Formats::const_iterator end = templ->formats.end();
592 for (; it != end; ++it) {
593 if (it->second.option_transformers.find(external::Extra) ==
594 it->second.option_transformers.end())
596 string const format = it->first;
597 string const opt = params_.extradata.get(format);
598 extraFormatCO->addItem(toqstr(format));
599 extra_[toqstr(format)] = toqstr(opt);
602 bool const enabled = extraFormatCO->count() > 0;
604 optionsGB->setEnabled(enabled);
605 extraED->setEnabled(enabled && !isBufferReadonly());
606 extraFormatCO->setEnabled(enabled);
609 extraFormatCO->setCurrentIndex(0);
610 extraED->setText(extra_[extraFormatCO->currentText()]);
615 void GuiExternal::applyView()
617 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
618 params_.settemplate(fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
620 params_.draft = draftCB->isChecked();
621 params_.lyxscale = displayscaleED->text().toInt();
622 params_.display = displayGB->isChecked();
624 if (rotationGB->isEnabled())
625 getRotation(params_.rotationdata, *angleED, *originCO);
627 if (scaleGB->isEnabled())
628 getSize(params_.resizedata, *widthED, *widthUnitCO,
629 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
631 if (cropGB->isEnabled())
632 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
633 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
635 if (optionsGB->isEnabled()) {
636 MapType::const_iterator it = extra_.begin();
637 MapType::const_iterator const end = extra_.end();
638 for (; it != end; ++it)
639 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
644 bool GuiExternal::initialiseParams(string const & sdata)
646 InsetExternal::string2params(sdata, buffer(), params_);
651 void GuiExternal::clearParams()
653 params_ = InsetExternalParams();
657 void GuiExternal::dispatchParams()
659 string const lfun = InsetExternal::params2string(params_, buffer());
660 dispatch(FuncRequest(getLfun(), lfun));
665 static QStringList templateFilters(QString const & template_name)
667 /// Determine the template file extension
668 external::TemplateManager const & etm =
669 external::TemplateManager::get();
670 external::Template const * const et_ptr =
671 etm.getTemplateByName(fromqstr(template_name));
674 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
675 filter += to_utf8(_(et_ptr->guiName));
677 filter += et_ptr->fileRegExp;
680 return fileFilters(toqstr(filter));
684 QString GuiExternal::browse(QString const & input,
685 QString const & template_name)
687 QString const title = qt_("Select external file");
688 QString const bufpath = bufferFilePath();
689 QStringList const filter = templateFilters(template_name);
691 QString const label1 = qt_("D&ocuments");
692 QString const dir1 = toqstr(lyxrc.document_path);
694 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
698 } // namespace frontend
701 #include "moc_GuiExternal.cpp"