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")
83 external::Template getTemplate(int i)
85 if (external::TemplateManager::get().getTemplates().empty())
87 external::TemplateManager::Templates::const_iterator i1
88 = external::TemplateManager::get().getTemplates().begin();
96 GuiExternal::GuiExternal(GuiView & lv)
97 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
101 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
102 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
103 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
106 connect(displayGB, SIGNAL(toggled(bool)),
107 displayscaleED, SLOT(setEnabled(bool)));
109 connect(originCO, SIGNAL(activated(int)),
110 this, SLOT(change_adaptor()));
111 connect(aspectratioCB, SIGNAL(stateChanged(int)),
112 this, SLOT(change_adaptor()));
113 connect(browsePB, SIGNAL(clicked()),
114 this, SLOT(browseClicked()));
115 connect(externalCO, SIGNAL(activated(QString)),
116 this, SLOT(templateChanged()));
117 connect(extraED, SIGNAL(textChanged(QString)),
118 this, SLOT(extraChanged(QString)));
119 connect(extraFormatCO, SIGNAL(activated(QString)),
120 this, SLOT(formatChanged(QString)));
121 connect(widthUnitCO, SIGNAL(activated(int)),
122 this, SLOT(widthUnitChanged()));
123 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
124 this, SLOT(change_adaptor()));
125 connect(displayGB, SIGNAL(toggled(bool)),
126 this, SLOT(change_adaptor()));
127 connect(displayscaleED, SIGNAL(textChanged(QString)),
128 this, SLOT(change_adaptor()));
129 connect(angleED, SIGNAL(textChanged(QString)),
130 this, SLOT(change_adaptor()));
131 connect(widthED, SIGNAL(textChanged(QString)),
132 this, SLOT(sizeChanged()));
133 connect(heightED, SIGNAL(textChanged(QString)),
134 this, SLOT(sizeChanged()));
135 connect(fileED, SIGNAL(textChanged(QString)),
136 this, SLOT(change_adaptor()));
137 connect(clipCB, SIGNAL(stateChanged(int)),
138 this, SLOT(change_adaptor()));
139 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
140 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
144 connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
145 this, SLOT(bbChanged()));
146 connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
147 this, SLOT(bbChanged()));
148 connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
149 this, SLOT(bbChanged()));
150 connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
151 this, SLOT(bbChanged()));
152 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
154 QIntValidator * validator = new QIntValidator(displayscaleED);
155 validator->setBottom(1);
156 displayscaleED->setValidator(validator);
158 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
160 xlED->setValidator(unsignedLengthValidator(xlED));
161 ybED->setValidator(unsignedLengthValidator(ybED));
162 xrED->setValidator(unsignedLengthValidator(xrED));
163 ytED->setValidator(unsignedLengthValidator(ytED));
165 widthED->setValidator(unsignedLengthValidator(widthED));
166 heightED->setValidator(unsignedLengthValidator(heightED));
168 setFocusProxy(fileED);
170 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
173 bc().setApply(applyPB);
174 bc().setCancel(closePB);
176 bc().addReadOnly(fileED);
177 bc().addReadOnly(browsePB);
178 bc().addReadOnly(externalCO);
179 bc().addReadOnly(draftCB);
180 bc().addReadOnly(displayscaleED);
181 bc().addReadOnly(displayGB);
182 bc().addReadOnly(angleED);
183 bc().addReadOnly(originCO);
184 bc().addReadOnly(heightUnitCO);
185 bc().addReadOnly(heightED);
186 bc().addReadOnly(aspectratioCB);
187 bc().addReadOnly(widthUnitCO);
188 bc().addReadOnly(widthED);
189 bc().addReadOnly(clipCB);
190 bc().addReadOnly(getbbPB);
191 bc().addReadOnly(ytED);
192 bc().addReadOnly(xlED);
193 bc().addReadOnly(xrED);
194 bc().addReadOnly(ybED);
195 bc().addReadOnly(ytUnitCO);
196 bc().addReadOnly(xlUnitCO);
197 bc().addReadOnly(xrUnitCO);
198 bc().addReadOnly(ybUnitCO);
199 bc().addReadOnly(extraFormatCO);
200 bc().addReadOnly(extraED);
202 bc().addCheckedLineEdit(angleED, angleLA);
203 bc().addCheckedLineEdit(displayscaleED, scaleLA);
204 bc().addCheckedLineEdit(heightED, heightLA);
205 bc().addCheckedLineEdit(widthED, widthLA);
206 bc().addCheckedLineEdit(xlED, lbLA);
207 bc().addCheckedLineEdit(ybED, lbLA);
208 bc().addCheckedLineEdit(xrED, rtLA);
209 bc().addCheckedLineEdit(ytED, rtLA);
210 bc().addCheckedLineEdit(fileED, fileLA);
212 external::TemplateManager::Templates::const_iterator i1, i2;
213 i1 = external::TemplateManager::get().getTemplates().begin();
214 i2 = external::TemplateManager::get().getTemplates().end();
215 for (; i1 != i2; ++i1)
216 externalCO->addItem(qt_(i1->second.guiName));
218 // Fill the origins combo
219 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
220 originCO->addItem(qt_(origin_gui_strs[i]));
223 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
225 // remove all units from bb that depend on font or other dimensions
226 // we cannot use these, since we need to compare against absolute
227 // values from the image file.
228 xlUnitCO->noPercents();
229 xlUnitCO->removeFontDependent();
230 xrUnitCO->noPercents();
231 xrUnitCO->removeFontDependent();
232 ytUnitCO->noPercents();
233 ytUnitCO->removeFontDependent();
234 ybUnitCO->noPercents();
235 ybUnitCO->removeFontDependent();
239 bool GuiExternal::activateAspectratio() const
244 QString const wstr = widthED->text();
248 double val = wstr.trimmed().toDouble(&wIsDbl);
249 if (wIsDbl && float_equal(val, 0.0, 0.05))
252 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
255 QString const hstr = heightED->text();
259 val = hstr.trimmed().toDouble(&hIsDbl);
260 if (hIsDbl && float_equal(val, 0.0, 0.05))
262 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
269 bool GuiExternal::usingScale() const
271 return (widthUnitCO->itemData(
272 widthUnitCO->currentIndex()).toString() == "scale");
276 void GuiExternal::bbChanged()
283 void GuiExternal::browseClicked()
285 int const choice = externalCO->currentIndex();
286 QString const template_name = toqstr(getTemplate(choice).lyxName);
287 QString const str = browse(fileED->text(), template_name);
288 if (!str.isEmpty()) {
289 fileED->setText(str);
295 void GuiExternal::change_adaptor()
301 void GuiExternal::extraChanged(const QString & text)
303 extra_[extraFormatCO->currentText()] = text;
308 void GuiExternal::formatChanged(const QString & format)
310 extraED->setText(extra_[format]);
314 void GuiExternal::getbbClicked()
321 string const filename = fromqstr(fileED->text());
322 if (filename.empty())
325 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
327 // try to get it from the file, if possible
328 string bb = graphics::readBB_from_PSFile(abs_file);
330 // we don't, so ask the Graphics Cache if it has loaded the file
334 graphics::Cache & gc = graphics::Cache::get();
335 if (gc.inCache(abs_file)) {
336 graphics::Image const * image = gc.item(abs_file)->image();
339 width = image->width();
340 height = image->height();
343 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
346 doubleToWidget(xlED, token(bb, ' ', 0));
347 doubleToWidget(ybED, token(bb, ' ', 1));
348 doubleToWidget(xrED, token(bb, ' ', 2));
349 doubleToWidget(ytED, token(bb, ' ', 3));
350 // the values from the file always have the bigpoint-unit bp
351 xlUnitCO->setCurrentIndex(0);
352 ybUnitCO->setCurrentIndex(0);
353 xrUnitCO->setCurrentIndex(0);
354 ytUnitCO->setCurrentIndex(0);
360 void GuiExternal::sizeChanged()
362 aspectratioCB->setEnabled(activateAspectratio());
367 void GuiExternal::templateChanged()
374 void GuiExternal::widthUnitChanged()
377 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
379 widthED->setValidator(unsignedLengthValidator(widthED));
381 heightED->setEnabled(!usingScale());
382 heightUnitCO->setEnabled(!usingScale());
387 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
388 external::RotationData const & data)
390 originCO.setCurrentIndex(int(data.origin()));
391 doubleToWidget(&angleED, data.angle);
395 static void getRotation(external::RotationData & data,
396 QLineEdit const & angleED, QComboBox const & originCO)
398 typedef external::RotationData::OriginType OriginType;
400 data.origin(static_cast<OriginType>(originCO.currentIndex()));
401 data.angle = widgetToDoubleStr(&angleED);
405 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
406 QLineEdit & heightED, LengthCombo & heightUnitCO,
407 QCheckBox & aspectratioCB,
408 external::ResizeData const & data)
410 bool using_scale = data.usingScale();
411 string scale = data.scale;
412 if (data.no_resize()) {
413 // Everything is zero, so default to this!
419 doubleToWidget(&widthED, scale);
420 widthUnitCO.setCurrentItem("scale");
422 lengthToWidgets(&widthED, &widthUnitCO,
423 data.width.asString(), Length::defaultUnit());
425 string const h = data.height.zero() ? string() : data.height.asString();
426 Length::UNIT const default_unit = data.width.zero() ?
427 Length::defaultUnit() : data.width.unit();
428 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
430 heightED.setEnabled(!using_scale);
431 heightUnitCO.setEnabled(!using_scale);
433 aspectratioCB.setChecked(data.keepAspectRatio);
435 bool const disable_aspectRatio = using_scale ||
436 data.width.zero() || data.height.zero();
437 aspectratioCB.setEnabled(!disable_aspectRatio);
441 static void getSize(external::ResizeData & data,
442 QLineEdit const & widthED, QComboBox const & widthUnitCO,
443 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
444 QCheckBox const & aspectratioCB, bool const scaling)
447 // scaling instead of a width
448 data.scale = widgetToDoubleStr(&widthED);
449 data.width = Length();
451 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
452 data.scale = string();
454 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
455 data.keepAspectRatio = aspectratioCB.isChecked();
459 void setCrop(QCheckBox & clipCB,
460 QLineEdit & xlED, QLineEdit & ybED,
461 QLineEdit & xrED, QLineEdit & ytED,
462 LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
463 LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
464 external::ClipData const & data)
466 clipCB.setChecked(data.clip);
467 Length::UNIT const default_unit = data.bbox.xl.zero() ?
468 Length::defaultUnit() : data.bbox.xl.unit();
469 lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
470 lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
471 lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
472 lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
476 static void getCrop(external::ClipData & data,
477 QCheckBox const & clipCB,
478 QLineEdit const & xlED, QLineEdit const & ybED,
479 QLineEdit const & xrED, QLineEdit const & ytED,
480 LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
481 LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
484 data.clip = clipCB.isChecked();
489 data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
490 data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
491 data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
492 data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
496 void GuiExternal::updateContents()
499 params_.filename.outputFileName(fromqstr(bufferFilePath()));
500 fileED->setText(toqstr(name));
503 external::TemplateManager::Templates::const_iterator i1, i2;
504 i1 = external::TemplateManager::get().getTemplates().begin();
505 i2 = external::TemplateManager::get().getTemplates().end();
506 for (int i = 0; i1 != i2; ++i1, ++i) {
507 if (i1->second.lyxName == params_.templatename()) {
513 externalCO->setCurrentIndex(index);
516 draftCB->setChecked(params_.draft);
518 displayGB->setChecked(params_.display);
519 displayscaleED->setText(QString::number(params_.lyxscale));
520 bool scaled = params_.display && !isBufferReadonly() &&
521 (params_.preview_mode != PREVIEW_INSTANT);
522 displayscaleED->setEnabled(scaled);
523 scaleLA->setEnabled(scaled);
524 displayGB->setEnabled(lyxrc.display_graphics);
527 setRotation(*angleED, *originCO, params_.rotationdata);
529 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
530 *aspectratioCB, params_.resizedata);
532 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
533 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
534 bbChanged_ = !params_.clipdata.bbox.empty();
540 void GuiExternal::updateTemplate()
542 external::Template templ = getTemplate(externalCO->currentIndex());
543 externalTB->setPlainText(toqstr(translateIfPossible(templ.helpText)));
545 // Ascertain which (if any) transformations the template supports
546 // and disable tabs and Group Boxes hosting unsupported transforms.
547 typedef vector<external::TransformID> TransformIDs;
548 TransformIDs const transformIds = templ.transformIds;
549 TransformIDs::const_iterator tr_begin = transformIds.begin();
550 TransformIDs::const_iterator const tr_end = transformIds.end();
552 bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
553 rotationGB->setEnabled(rotate);
555 bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
556 scaleGB->setEnabled(resize);
558 bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
559 cropGB->setEnabled(clip);
561 sizetab->setEnabled(rotate || resize || clip);
562 tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
564 bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
565 optionsGB->setEnabled(found);
567 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
568 !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
569 displayscaleED->setEnabled(scaled);
570 scaleLA->setEnabled(scaled);
575 // Ascertain whether the template has any formats supporting
576 // the 'Extra' option
579 extraFormatCO->clear();
581 external::Template::Formats::const_iterator it = templ.formats.begin();
582 external::Template::Formats::const_iterator end = templ.formats.end();
583 for (; it != end; ++it) {
584 if (it->second.option_transformers.find(external::Extra) ==
585 it->second.option_transformers.end())
587 string const format = it->first;
588 string const opt = params_.extradata.get(format);
589 extraFormatCO->addItem(toqstr(format));
590 extra_[toqstr(format)] = toqstr(opt);
593 bool const enabled = extraFormatCO->count() > 0;
595 optionsGB->setEnabled(enabled);
596 extraED->setEnabled(enabled && !isBufferReadonly());
597 extraFormatCO->setEnabled(enabled);
600 extraFormatCO->setCurrentIndex(0);
601 extraED->setText(extra_[extraFormatCO->currentText()]);
606 void GuiExternal::applyView()
608 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
609 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
611 params_.draft = draftCB->isChecked();
612 params_.lyxscale = displayscaleED->text().toInt();
613 params_.display = displayGB->isChecked();
615 if (rotationGB->isEnabled())
616 getRotation(params_.rotationdata, *angleED, *originCO);
618 if (scaleGB->isEnabled())
619 getSize(params_.resizedata, *widthED, *widthUnitCO,
620 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
622 if (cropGB->isEnabled())
623 getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
624 *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
626 if (optionsGB->isEnabled()) {
627 MapType::const_iterator it = extra_.begin();
628 MapType::const_iterator const end = extra_.end();
629 for (; it != end; ++it)
630 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
635 bool GuiExternal::initialiseParams(string const & data)
637 InsetExternal::string2params(data, buffer(), params_);
642 void GuiExternal::clearParams()
644 params_ = InsetExternalParams();
648 void GuiExternal::dispatchParams()
650 string const lfun = InsetExternal::params2string(params_, buffer());
651 dispatch(FuncRequest(getLfun(), lfun));
655 static QStringList templateFilters(QString const & template_name)
657 /// Determine the template file extension
658 external::TemplateManager const & etm =
659 external::TemplateManager::get();
660 external::Template const * const et_ptr =
661 etm.getTemplateByName(fromqstr(template_name));
664 if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
665 filter += et_ptr->guiName;
667 filter += et_ptr->fileRegExp;
670 return fileFilters(toqstr(filter));
674 QString GuiExternal::browse(QString const & input,
675 QString const & template_name) const
677 QString const title = qt_("Select external file");
678 QString const bufpath = bufferFilePath();
679 QStringList const filter = templateFilters(template_name);
681 QString const label1 = qt_("Documents|#o#O");
682 QString const dir1 = toqstr(lyxrc.document_path);
684 return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
688 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
691 } // namespace frontend
694 #include "moc_GuiExternal.cpp"