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"
17 #include "FuncRequest.h"
18 #include "support/gettext.h"
22 #include "insets/ExternalSupport.h"
23 #include "insets/ExternalTemplate.h"
24 #include "insets/InsetExternal.h"
26 #include "graphics/epstools.h"
27 #include "graphics/GraphicsCache.h"
28 #include "graphics/GraphicsCacheItem.h"
29 #include "graphics/GraphicsImage.h"
31 #include "support/convert.h"
32 #include "support/filetools.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")
82 external::Template getTemplate(int i)
84 if (external::TemplateManager::get().getTemplates().empty())
86 external::TemplateManager::Templates::const_iterator i1
87 = external::TemplateManager::get().getTemplates().begin();
95 GuiExternal::GuiExternal(GuiView & lv)
96 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
100 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
101 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
102 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
105 connect(displayGB, SIGNAL(toggled(bool)),
106 displayscaleED, SLOT(setEnabled(bool)));
108 connect(originCO, SIGNAL(activated(int)),
109 this, SLOT(change_adaptor()));
110 connect(aspectratioCB, SIGNAL(stateChanged(int)),
111 this, SLOT(change_adaptor()));
112 connect(browsePB, SIGNAL(clicked()),
113 this, SLOT(browseClicked()));
114 connect(externalCO, SIGNAL(activated(QString)),
115 this, SLOT(templateChanged()));
116 connect(extraED, SIGNAL(textChanged(QString)),
117 this, SLOT(extraChanged(QString)));
118 connect(extraFormatCO, SIGNAL(activated(QString)),
119 this, SLOT(formatChanged(QString)));
120 connect(widthUnitCO, SIGNAL(activated(int)),
121 this, SLOT(widthUnitChanged()));
122 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
123 this, SLOT(change_adaptor()));
124 connect(displayGB, SIGNAL(toggled(bool)),
125 this, SLOT(change_adaptor()));
126 connect(displayscaleED, SIGNAL(textChanged(QString)),
127 this, SLOT(change_adaptor()));
128 connect(angleED, SIGNAL(textChanged(QString)),
129 this, SLOT(change_adaptor()));
130 connect(widthED, SIGNAL(textChanged(QString)),
131 this, SLOT(sizeChanged()));
132 connect(heightED, SIGNAL(textChanged(QString)),
133 this, SLOT(sizeChanged()));
134 connect(fileED, SIGNAL(textChanged(QString)),
135 this, SLOT(change_adaptor()));
136 connect(clipCB, SIGNAL(stateChanged(int)),
137 this, SLOT(change_adaptor()));
138 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
139 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
140 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
144 this, SLOT(bbChanged()));
145 connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
146 this, SLOT(bbChanged()));
147 connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
148 this, SLOT(bbChanged()));
149 connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
150 this, SLOT(bbChanged()));
151 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
153 QIntValidator * validator = new QIntValidator(displayscaleED);
154 validator->setBottom(1);
155 displayscaleED->setValidator(validator);
157 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
159 xlED->setValidator(unsignedLengthValidator(xlED));
160 ybED->setValidator(unsignedLengthValidator(ybED));
161 xrED->setValidator(unsignedLengthValidator(xrED));
162 ytED->setValidator(unsignedLengthValidator(ytED));
164 widthED->setValidator(unsignedLengthValidator(widthED));
165 heightED->setValidator(unsignedLengthValidator(heightED));
167 setFocusProxy(fileED);
169 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
172 bc().setApply(applyPB);
173 bc().setCancel(closePB);
175 bc().addReadOnly(fileED);
176 bc().addReadOnly(browsePB);
177 bc().addReadOnly(externalCO);
178 bc().addReadOnly(draftCB);
179 bc().addReadOnly(displayscaleED);
180 bc().addReadOnly(displayGB);
181 bc().addReadOnly(angleED);
182 bc().addReadOnly(originCO);
183 bc().addReadOnly(heightUnitCO);
184 bc().addReadOnly(heightED);
185 bc().addReadOnly(aspectratioCB);
186 bc().addReadOnly(widthUnitCO);
187 bc().addReadOnly(widthED);
188 bc().addReadOnly(clipCB);
189 bc().addReadOnly(getbbPB);
190 bc().addReadOnly(ytED);
191 bc().addReadOnly(xlED);
192 bc().addReadOnly(xrED);
193 bc().addReadOnly(ybED);
194 bc().addReadOnly(ytUnitCO);
195 bc().addReadOnly(xlUnitCO);
196 bc().addReadOnly(xrUnitCO);
197 bc().addReadOnly(ybUnitCO);
198 bc().addReadOnly(extraFormatCO);
199 bc().addReadOnly(extraED);
201 bc().addCheckedLineEdit(angleED, angleLA);
202 bc().addCheckedLineEdit(displayscaleED, scaleLA);
203 bc().addCheckedLineEdit(heightED, heightLA);
204 bc().addCheckedLineEdit(widthED, widthLA);
205 bc().addCheckedLineEdit(xlED, lbLA);
206 bc().addCheckedLineEdit(ybED, lbLA);
207 bc().addCheckedLineEdit(xrED, rtLA);
208 bc().addCheckedLineEdit(ytED, rtLA);
209 bc().addCheckedLineEdit(fileED, fileLA);
211 external::TemplateManager::Templates::const_iterator i1, i2;
212 i1 = external::TemplateManager::get().getTemplates().begin();
213 i2 = external::TemplateManager::get().getTemplates().end();
214 for (; i1 != i2; ++i1)
215 externalCO->addItem(qt_(i1->second.guiName));
217 // Fill the origins combo
218 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
219 originCO->addItem(qt_(origin_gui_strs[i]));
222 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
224 // remove all units from bb that depend on font or other dimensions
225 // we cannot use these, since we need to compare against absolute
226 // values from the image file.
227 xlUnitCO->noPercents();
228 xlUnitCO->removeFontDependent();
229 xrUnitCO->noPercents();
230 xrUnitCO->removeFontDependent();
231 ytUnitCO->noPercents();
232 ytUnitCO->removeFontDependent();
233 ybUnitCO->noPercents();
234 ybUnitCO->removeFontDependent();
238 bool GuiExternal::activateAspectratio() const
243 QString const wstr = widthED->text();
247 double val = wstr.trimmed().toDouble(&wIsDbl);
248 if (wIsDbl && float_equal(val, 0.0, 0.05))
251 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
254 QString const hstr = heightED->text();
258 val = hstr.trimmed().toDouble(&hIsDbl);
259 if (hIsDbl && float_equal(val, 0.0, 0.05))
261 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
268 bool GuiExternal::usingScale() const
270 return (widthUnitCO->itemData(
271 widthUnitCO->currentIndex()).toString() == "scale");
275 void GuiExternal::bbChanged()
282 void GuiExternal::browseClicked()
284 int const choice = externalCO->currentIndex();
285 QString const template_name = toqstr(getTemplate(choice).lyxName);
286 QString const str = browse(fileED->text(), template_name);
287 if (!str.isEmpty()) {
288 fileED->setText(str);
294 void GuiExternal::change_adaptor()
300 void GuiExternal::extraChanged(const QString & text)
302 extra_[extraFormatCO->currentText()] = text;
307 void GuiExternal::formatChanged(const QString & format)
309 extraED->setText(extra_[format]);
313 void GuiExternal::getbbClicked()
320 string const filename = fromqstr(fileED->text());
321 if (filename.empty())
324 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
326 // try to get it from the file, if possible
327 string bb = graphics::readBB_from_PSFile(abs_file);
329 // we don't, so ask the Graphics Cache if it has loaded the file
333 graphics::Cache & gc = graphics::Cache::get();
334 if (gc.inCache(abs_file)) {
335 graphics::Image const * image = gc.item(abs_file)->image();
338 width = image->width();
339 height = image->height();
342 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
345 doubleToWidget(xlED, token(bb, ' ', 0));
346 doubleToWidget(ybED, token(bb, ' ', 1));
347 doubleToWidget(xrED, token(bb, ' ', 2));
348 doubleToWidget(ytED, token(bb, ' ', 3));
349 // the values from the file always have the bigpoint-unit bp
350 xlUnitCO->setCurrentIndex(0);
351 ybUnitCO->setCurrentIndex(0);
352 xrUnitCO->setCurrentIndex(0);
353 ytUnitCO->setCurrentIndex(0);
359 void GuiExternal::sizeChanged()
361 aspectratioCB->setEnabled(activateAspectratio());
366 void GuiExternal::templateChanged()
373 void GuiExternal::widthUnitChanged()
376 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
378 widthED->setValidator(unsignedLengthValidator(widthED));
380 heightED->setEnabled(!usingScale());
381 heightUnitCO->setEnabled(!usingScale());
386 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
387 external::RotationData const & data)
389 originCO.setCurrentIndex(int(data.origin()));
390 doubleToWidget(&angleED, data.angle);
394 static void getRotation(external::RotationData & data,
395 QLineEdit const & angleED, QComboBox const & originCO)
397 typedef external::RotationData::OriginType OriginType;
399 data.origin(static_cast<OriginType>(originCO.currentIndex()));
400 data.angle = widgetToDoubleStr(&angleED);
404 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
405 QLineEdit & heightED, LengthCombo & heightUnitCO,
406 QCheckBox & aspectratioCB,
407 external::ResizeData const & data)
409 bool using_scale = data.usingScale();
410 string scale = data.scale;
411 if (data.no_resize()) {
412 // Everything is zero, so default to this!
418 doubleToWidget(&widthED, scale);
419 widthUnitCO.setCurrentItem("scale");
421 lengthToWidgets(&widthED, &widthUnitCO,
422 data.width.asString(), Length::defaultUnit());
424 string const h = data.height.zero() ? string() : data.height.asString();
425 Length::UNIT const default_unit = data.width.zero() ?
426 Length::defaultUnit() : data.width.unit();
427 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
429 heightED.setEnabled(!using_scale);
430 heightUnitCO.setEnabled(!using_scale);
432 aspectratioCB.setChecked(data.keepAspectRatio);
434 bool const disable_aspectRatio = using_scale ||
435 data.width.zero() || data.height.zero();
436 aspectratioCB.setEnabled(!disable_aspectRatio);
440 static void getSize(external::ResizeData & data,
441 QLineEdit const & widthED, QComboBox const & widthUnitCO,
442 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
443 QCheckBox const & aspectratioCB, bool const scaling)
446 // scaling instead of a width
447 data.scale = widgetToDoubleStr(&widthED);
448 data.width = Length();
450 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
451 data.scale = string();
453 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
454 data.keepAspectRatio = aspectratioCB.isChecked();
458 void setCrop(QCheckBox & clipCB,
459 QLineEdit & xlED, QLineEdit & ybED,
460 QLineEdit & xrED, QLineEdit & ytED,
461 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
462 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
463 external::ClipData const & data)
465 clipCB.setChecked(data.clip);
466 Length::UNIT const default_unit = data.bbox.xl.zero() ?
467 Length::defaultUnit() : data.bbox.xl.unit();
468 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
469 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
470 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
471 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
475 static void getCrop(external::ClipData & data,
476 QCheckBox const & clipCB,
477 QLineEdit const & xlED, QLineEdit const & ybED,
478 QLineEdit const & xrED, QLineEdit const & ytED,
479 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
480 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
483 data.clip = clipCB.isChecked();
488 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
489 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
490 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
491 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
495 void GuiExternal::updateContents()
498 params_.filename.outputFileName(fromqstr(bufferFilePath()));
499 fileED->setText(toqstr(name));
502 external::TemplateManager::Templates::const_iterator i1, i2;
503 i1 = external::TemplateManager::get().getTemplates().begin();
504 i2 = external::TemplateManager::get().getTemplates().end();
505 for (int i = 0; i1 != i2; ++i1, ++i) {
506 if (i1->second.lyxName == params_.templatename()) {
512 externalCO->setCurrentIndex(index);
515 draftCB->setChecked(params_.draft);
517 displayGB->setChecked(params_.display);
518 displayscaleED->setText(QString::number(params_.lyxscale));
519 bool scaled = params_.display && !isBufferReadonly() &&
520 (params_.preview_mode != PREVIEW_INSTANT);
521 displayscaleED->setEnabled(scaled);
522 scaleLA->setEnabled(scaled);
523 displayGB->setEnabled(lyxrc.display_graphics);
526 setRotation(*angleED, *originCO, params_.rotationdata);
528 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
529 *aspectratioCB, params_.resizedata);
531 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
532 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
533 bbChanged_ = !params_.clipdata.bbox.empty();
539 void GuiExternal::updateTemplate()
541 external::Template templ = getTemplate(externalCO->currentIndex());
542 externalTB->setPlainText(qt_(templ.helpText));
544 // Ascertain which (if any) transformations the template supports
545 // and disable tabs and Group Boxes hosting unsupported transforms.
546 typedef vector<external::TransformID> TransformIDs;
547 TransformIDs const transformIds = templ.transformIds;
548 TransformIDs::const_iterator tr_begin = transformIds.begin();
549 TransformIDs::const_iterator const tr_end = transformIds.end();
551 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
552 rotationGB->setEnabled(rotate);
554 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
555 scaleGB->setEnabled(resize);
557 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
558 cropGB->setEnabled(clip);
560 sizetab->setEnabled(rotate || resize || clip);
561 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
563 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
564 optionsGB->setEnabled(found);
566 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
567 !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
568 displayscaleED->setEnabled(scaled);
569 scaleLA->setEnabled(scaled);
574 // Ascertain whether the template has any formats supporting
575 // the 'Extra' option
578 extraFormatCO->clear();
580 external::Template::Formats::const_iterator it = templ.formats.begin();
581 external::Template::Formats::const_iterator end = templ.formats.end();
582 for (; it != end; ++it) {
583 if (it->second.option_transformers.find(external::Extra) ==
584 it->second.option_transformers.end())
586 string const format = it->first;
587 string const opt = params_.extradata.get(format);
588 extraFormatCO->addItem(toqstr(format));
589 extra_[toqstr(format)] = toqstr(opt);
592 bool const enabled = extraFormatCO->count() > 0;
594 optionsGB->setEnabled(enabled);
595 extraED->setEnabled(enabled && !isBufferReadonly());
596 extraFormatCO->setEnabled(enabled);
599 extraFormatCO->setCurrentIndex(0);
600 extraED->setText(extra_[extraFormatCO->currentText()]);
605 void GuiExternal::applyView()
607 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
608 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
610 params_.draft = draftCB->isChecked();
611 params_.lyxscale = displayscaleED->text().toInt();
612 params_.display = displayGB->isChecked();
614 if (rotationGB->isEnabled())
615 getRotation(params_.rotationdata, *angleED, *originCO);
617 if (scaleGB->isEnabled())
618 getSize(params_.resizedata, *widthED, *widthUnitCO,
619 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
621 if (cropGB->isEnabled())
622 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
623 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
625 if (optionsGB->isEnabled()) {
626 MapType::const_iterator it = extra_.begin();
627 MapType::const_iterator const end = extra_.end();
628 for (; it != end; ++it)
629 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
634 bool GuiExternal::initialiseParams(string const & data)
636 InsetExternal::string2params(data, buffer(), params_);
641 void GuiExternal::clearParams()
643 params_ = InsetExternalParams();
647 void GuiExternal::dispatchParams()
649 string const lfun = InsetExternal::params2string(params_, buffer());
650 dispatch(FuncRequest(getLfun(), lfun));
654 static QStringList templateFilters(QString const & template_name)
656 /// Determine the template file extension
657 external::TemplateManager const & etm =
658 external::TemplateManager::get();
659 external::Template const * const et_ptr =
660 etm.getTemplateByName(fromqstr(template_name));
663 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
664 filter += et_ptr->guiName;
666 filter += et_ptr->fileRegExp;
669 return fileFilters(toqstr(filter));
673 QString GuiExternal::browse(QString const & input,
674 QString const & template_name) const
676 QString const title = qt_("Select external file");
677 QString const bufpath = bufferFilePath();
678 QStringList const filter = templateFilters(template_name);
680 QString const label1 = qt_("Documents|#o#O");
681 QString const dir1 = toqstr(lyxrc.document_path);
683 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
687 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
690 } // namespace frontend
693 #include "moc_GuiExternal.cpp"