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(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
145 QIntValidator * validator = new QIntValidator(displayscaleED);
146 validator->setBottom(1);
147 displayscaleED->setValidator(validator);
149 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
151 xlED->setValidator(new QIntValidator(xlED));
152 ybED->setValidator(new QIntValidator(ybED));
153 xrED->setValidator(new QIntValidator(xrED));
154 ytED->setValidator(new QIntValidator(ytED));
156 widthED->setValidator(unsignedLengthValidator(widthED));
157 heightED->setValidator(unsignedLengthValidator(heightED));
159 setFocusProxy(fileED);
161 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
164 bc().setApply(applyPB);
165 bc().setCancel(closePB);
167 bc().addReadOnly(fileED);
168 bc().addReadOnly(browsePB);
169 bc().addReadOnly(externalCO);
170 bc().addReadOnly(draftCB);
171 bc().addReadOnly(displayscaleED);
172 bc().addReadOnly(displayGB);
173 bc().addReadOnly(angleED);
174 bc().addReadOnly(originCO);
175 bc().addReadOnly(heightUnitCO);
176 bc().addReadOnly(heightED);
177 bc().addReadOnly(aspectratioCB);
178 bc().addReadOnly(widthUnitCO);
179 bc().addReadOnly(widthED);
180 bc().addReadOnly(clipCB);
181 bc().addReadOnly(getbbPB);
182 bc().addReadOnly(ytED);
183 bc().addReadOnly(xlED);
184 bc().addReadOnly(xrED);
185 bc().addReadOnly(ybED);
186 bc().addReadOnly(extraFormatCO);
187 bc().addReadOnly(extraED);
189 bc().addCheckedLineEdit(angleED, angleLA);
190 bc().addCheckedLineEdit(displayscaleED, scaleLA);
191 bc().addCheckedLineEdit(heightED, heightLA);
192 bc().addCheckedLineEdit(widthED, widthLA);
193 bc().addCheckedLineEdit(xlED, lbLA);
194 bc().addCheckedLineEdit(ybED, lbLA);
195 bc().addCheckedLineEdit(xrED, rtLA);
196 bc().addCheckedLineEdit(ytED, rtLA);
197 bc().addCheckedLineEdit(fileED, fileLA);
199 external::TemplateManager::Templates::const_iterator i1, i2;
200 i1 = external::TemplateManager::get().getTemplates().begin();
201 i2 = external::TemplateManager::get().getTemplates().end();
202 for (; i1 != i2; ++i1)
203 externalCO->addItem(qt_(i1->second.guiName));
205 // Fill the origins combo
206 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
207 originCO->addItem(qt_(origin_gui_strs[i]));
210 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
214 bool GuiExternal::activateAspectratio() const
219 QString const wstr = widthED->text();
223 double val = wstr.trimmed().toDouble(&wIsDbl);
224 if (wIsDbl && float_equal(val, 0.0, 0.05))
227 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
230 QString const hstr = heightED->text();
234 val = hstr.trimmed().toDouble(&hIsDbl);
235 if (hIsDbl && float_equal(val, 0.0, 0.05))
237 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
244 bool GuiExternal::usingScale() const
246 return (widthUnitCO->itemData(
247 widthUnitCO->currentIndex()).toString() == "scale");
251 void GuiExternal::bbChanged()
258 void GuiExternal::browseClicked()
260 int const choice = externalCO->currentIndex();
261 QString const template_name = toqstr(getTemplate(choice).lyxName);
262 QString const str = browse(fileED->text(), template_name);
263 if (!str.isEmpty()) {
264 fileED->setText(str);
270 void GuiExternal::change_adaptor()
276 void GuiExternal::extraChanged(const QString & text)
278 extra_[extraFormatCO->currentText()] = text;
283 void GuiExternal::formatChanged(const QString & format)
285 extraED->setText(extra_[format]);
289 void GuiExternal::getbbClicked()
296 string const filename = fromqstr(fileED->text());
297 if (filename.empty())
300 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
302 // try to get it from the file, if possible
303 string bb = graphics::readBB_from_PSFile(abs_file);
305 // we don't, so ask the Graphics Cache if it has loaded the file
309 graphics::Cache & gc = graphics::Cache::get();
310 if (gc.inCache(abs_file)) {
311 graphics::Image const * image = gc.item(abs_file)->image();
314 width = image->width();
315 height = image->height();
318 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
321 xlED->setText(toqstr(token(bb, ' ', 0)));
322 ybED->setText(toqstr(token(bb, ' ', 1)));
323 xrED->setText(toqstr(token(bb, ' ', 2)));
324 ytED->setText(toqstr(token(bb, ' ', 3)));
330 void GuiExternal::sizeChanged()
332 aspectratioCB->setEnabled(activateAspectratio());
337 void GuiExternal::templateChanged()
344 void GuiExternal::widthUnitChanged()
347 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
349 widthED->setValidator(unsignedLengthValidator(widthED));
351 heightED->setEnabled(!usingScale());
352 heightUnitCO->setEnabled(!usingScale());
357 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
358 external::RotationData const & data)
360 originCO.setCurrentIndex(int(data.origin()));
361 doubleToWidget(&angleED, data.angle);
365 static void getRotation(external::RotationData & data,
366 QLineEdit const & angleED, QComboBox const & originCO)
368 typedef external::RotationData::OriginType OriginType;
370 data.origin(static_cast<OriginType>(originCO.currentIndex()));
371 data.angle = widgetToDoubleStr(&angleED);
375 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
376 QLineEdit & heightED, LengthCombo & heightUnitCO,
377 QCheckBox & aspectratioCB,
378 external::ResizeData const & data)
380 bool using_scale = data.usingScale();
381 string scale = data.scale;
382 if (data.no_resize()) {
383 // Everything is zero, so default to this!
389 doubleToWidget(&widthED, scale);
390 widthUnitCO.setCurrentItem("scale");
392 lengthToWidgets(&widthED, &widthUnitCO,
393 data.width.asString(), Length::defaultUnit());
395 string const h = data.height.zero() ? string() : data.height.asString();
396 Length::UNIT const default_unit = data.width.zero() ?
397 Length::defaultUnit() : data.width.unit();
398 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
400 heightED.setEnabled(!using_scale);
401 heightUnitCO.setEnabled(!using_scale);
403 aspectratioCB.setChecked(data.keepAspectRatio);
405 bool const disable_aspectRatio = using_scale ||
406 data.width.zero() || data.height.zero();
407 aspectratioCB.setEnabled(!disable_aspectRatio);
411 static void getSize(external::ResizeData & data,
412 QLineEdit const & widthED, QComboBox const & widthUnitCO,
413 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
414 QCheckBox const & aspectratioCB, bool const scaling)
417 // scaling instead of a width
418 data.scale = widgetToDoubleStr(&widthED);
419 data.width = Length();
421 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
422 data.scale = string();
424 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
425 data.keepAspectRatio = aspectratioCB.isChecked();
429 void setCrop(QCheckBox & clipCB,
430 QLineEdit & xlED, QLineEdit & ybED,
431 QLineEdit & xrED, QLineEdit & ytED,
432 external::ClipData const & data)
434 clipCB.setChecked(data.clip);
435 graphics::BoundingBox const & bbox = data.bbox;
436 xlED.setText(QString::number(bbox.xl));
437 ybED.setText(QString::number(bbox.yb));
438 xrED.setText(QString::number(bbox.xr));
439 ytED.setText(QString::number(bbox.yt));
443 static void getCrop(external::ClipData & data,
444 QCheckBox const & clipCB,
445 QLineEdit const & xlED, QLineEdit const & ybED,
446 QLineEdit const & xrED, QLineEdit const & ytED,
449 data.clip = clipCB.isChecked();
454 data.bbox.xl = xlED.text().toInt();
455 data.bbox.yb = ybED.text().toInt();
456 data.bbox.xr = xrED.text().toInt();
457 data.bbox.yt = ytED.text().toInt();
461 void GuiExternal::updateContents()
464 params_.filename.outputFileName(fromqstr(bufferFilePath()));
465 fileED->setText(toqstr(name));
468 external::TemplateManager::Templates::const_iterator i1, i2;
469 i1 = external::TemplateManager::get().getTemplates().begin();
470 i2 = external::TemplateManager::get().getTemplates().end();
471 for (int i = 0; i1 != i2; ++i1, ++i) {
472 if (i1->second.lyxName == params_.templatename()) {
478 externalCO->setCurrentIndex(index);
481 draftCB->setChecked(params_.draft);
483 displayGB->setChecked(params_.display);
484 displayscaleED->setText(QString::number(params_.lyxscale));
485 bool scaled = params_.display && !isBufferReadonly() &&
486 (params_.preview_mode != PREVIEW_INSTANT);
487 displayscaleED->setEnabled(scaled);
488 scaleLA->setEnabled(scaled);
489 displayGB->setEnabled(lyxrc.display_graphics);
492 setRotation(*angleED, *originCO, params_.rotationdata);
494 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
495 *aspectratioCB, params_.resizedata);
497 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
498 bbChanged_ = !params_.clipdata.bbox.empty();
504 void GuiExternal::updateTemplate()
506 external::Template templ = getTemplate(externalCO->currentIndex());
507 externalTB->setPlainText(qt_(templ.helpText));
509 // Ascertain which (if any) transformations the template supports
510 // and disable tabs and Group Boxes hosting unsupported transforms.
511 typedef vector<external::TransformID> TransformIDs;
512 TransformIDs const transformIds = templ.transformIds;
513 TransformIDs::const_iterator tr_begin = transformIds.begin();
514 TransformIDs::const_iterator const tr_end = transformIds.end();
516 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
517 rotationGB->setEnabled(rotate);
519 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
520 scaleGB->setEnabled(resize);
522 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
523 cropGB->setEnabled(clip);
525 sizetab->setEnabled(rotate || resize || clip);
526 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
528 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
529 optionsGB->setEnabled(found);
531 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
532 !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
533 displayscaleED->setEnabled(scaled);
534 scaleLA->setEnabled(scaled);
539 // Ascertain whether the template has any formats supporting
540 // the 'Extra' option
543 extraFormatCO->clear();
545 external::Template::Formats::const_iterator it = templ.formats.begin();
546 external::Template::Formats::const_iterator end = templ.formats.end();
547 for (; it != end; ++it) {
548 if (it->second.option_transformers.find(external::Extra) ==
549 it->second.option_transformers.end())
551 string const format = it->first;
552 string const opt = params_.extradata.get(format);
553 extraFormatCO->addItem(toqstr(format));
554 extra_[toqstr(format)] = toqstr(opt);
557 bool const enabled = extraFormatCO->count() > 0;
559 optionsGB->setEnabled(enabled);
560 extraED->setEnabled(enabled && !isBufferReadonly());
561 extraFormatCO->setEnabled(enabled);
564 extraFormatCO->setCurrentIndex(0);
565 extraED->setText(extra_[extraFormatCO->currentText()]);
570 void GuiExternal::applyView()
572 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
573 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
575 params_.draft = draftCB->isChecked();
576 params_.lyxscale = displayscaleED->text().toInt();
577 params_.display = displayGB->isChecked();
579 if (rotationGB->isEnabled())
580 getRotation(params_.rotationdata, *angleED, *originCO);
582 if (scaleGB->isEnabled())
583 getSize(params_.resizedata, *widthED, *widthUnitCO,
584 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
586 if (cropGB->isEnabled())
587 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
588 *xrED, *ytED, bbChanged_);
590 if (optionsGB->isEnabled()) {
591 MapType::const_iterator it = extra_.begin();
592 MapType::const_iterator const end = extra_.end();
593 for (; it != end; ++it)
594 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
599 bool GuiExternal::initialiseParams(string const & data)
601 InsetExternal::string2params(data, buffer(), params_);
606 void GuiExternal::clearParams()
608 params_ = InsetExternalParams();
612 void GuiExternal::dispatchParams()
614 string const lfun = InsetExternal::params2string(params_, buffer());
615 dispatch(FuncRequest(getLfun(), lfun));
619 static QStringList templateFilters(QString const & template_name)
621 /// Determine the template file extension
622 external::TemplateManager const & etm =
623 external::TemplateManager::get();
624 external::Template const * const et_ptr =
625 etm.getTemplateByName(fromqstr(template_name));
628 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
629 filter += et_ptr->guiName;
631 filter += et_ptr->fileRegExp;
634 return fileFilters(toqstr(filter));
638 QString GuiExternal::browse(QString const & input,
639 QString const & template_name) const
641 QString const title = qt_("Select external file");
642 QString const bufpath = bufferFilePath();
643 QStringList const filter = templateFilters(template_name);
645 QString const label1 = qt_("Documents|#o#O");
646 QString const dir1 = toqstr(lyxrc.document_path);
648 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
652 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
655 } // namespace frontend
658 #include "moc_GuiExternal.cpp"