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/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/lstrings.h"
33 #include "support/lyxlib.h"
34 #include "support/os.h"
36 #include "LengthCombo.h"
37 #include "qt_helpers.h"
38 #include "Validator.h"
43 #include <QPushButton>
45 #include <QTextBrowser>
48 using namespace lyx::support;
53 using namespace external;
57 RotationDataType origins[] = {
58 RotationData::DEFAULT,
59 RotationData::TOPLEFT,
60 RotationData::BOTTOMLEFT,
61 RotationData::BASELINELEFT,
63 RotationData::TOPCENTER,
64 RotationData::BOTTOMCENTER,
65 RotationData::BASELINECENTER,
66 RotationData::TOPRIGHT,
67 RotationData::BOTTOMRIGHT,
68 RotationData::BASELINERIGHT
72 // These are the strings, corresponding to the above, that the GUI should
73 // use. Note that they can/should be translated.
74 char const * const origin_gui_strs[] = {
76 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
77 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
78 N_("Top right"), N_("Bottom right"), N_("Baseline right")
81 external::Template getTemplate(int i)
83 external::TemplateManager::Templates::const_iterator i1
84 = external::TemplateManager::get().getTemplates().begin();
92 GuiExternal::GuiExternal(GuiView & lv)
93 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
97 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
98 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
99 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
102 connect(displayGB, SIGNAL(toggled(bool)),
103 displayscaleED, SLOT(setEnabled(bool)));
105 connect(originCO, SIGNAL(activated(int)),
106 this, SLOT(change_adaptor()));
107 connect(aspectratioCB, SIGNAL(stateChanged(int)),
108 this, SLOT(change_adaptor()));
109 connect(browsePB, SIGNAL(clicked()),
110 this, SLOT(browseClicked()));
111 connect(externalCO, SIGNAL(activated(QString)),
112 this, SLOT(templateChanged()));
113 connect(extraED, SIGNAL(textChanged(QString)),
114 this, SLOT(extraChanged(QString)));
115 connect(extraFormatCO, SIGNAL(activated(QString)),
116 this, SLOT(formatChanged(QString)));
117 connect(widthUnitCO, SIGNAL(activated(int)),
118 this, SLOT(widthUnitChanged()));
119 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
120 this, SLOT(change_adaptor()));
121 connect(displayGB, SIGNAL(toggled(bool)),
122 this, SLOT(change_adaptor()));
123 connect(displayscaleED, SIGNAL(textChanged(QString)),
124 this, SLOT(change_adaptor()));
125 connect(angleED, SIGNAL(textChanged(QString)),
126 this, SLOT(change_adaptor()));
127 connect(widthED, SIGNAL(textChanged(QString)),
128 this, SLOT(sizeChanged()));
129 connect(heightED, SIGNAL(textChanged(QString)),
130 this, SLOT(sizeChanged()));
131 connect(fileED, SIGNAL(textChanged(QString)),
132 this, SLOT(change_adaptor()));
133 connect(clipCB, SIGNAL(stateChanged(int)),
134 this, SLOT(change_adaptor()));
135 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
136 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
137 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
138 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
139 connect(ybED, SIGNAL(textChanged(QString)), 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(new QIntValidator(xlED));
149 ybED->setValidator(new QIntValidator(ybED));
150 xrED->setValidator(new QIntValidator(xrED));
151 ytED->setValidator(new QIntValidator(ytED));
153 widthED->setValidator(unsignedLengthValidator(widthED));
154 heightED->setValidator(unsignedLengthValidator(heightED));
156 setFocusProxy(fileED);
158 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
161 bc().setApply(applyPB);
162 bc().setCancel(closePB);
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(extraFormatCO);
184 bc().addReadOnly(extraED);
186 bc().addCheckedLineEdit(angleED, angleLA);
187 bc().addCheckedLineEdit(displayscaleED, scaleLA);
188 bc().addCheckedLineEdit(heightED, heightLA);
189 bc().addCheckedLineEdit(widthED, widthLA);
190 bc().addCheckedLineEdit(xlED, lbLA);
191 bc().addCheckedLineEdit(ybED, lbLA);
192 bc().addCheckedLineEdit(xrED, rtLA);
193 bc().addCheckedLineEdit(ytED, rtLA);
194 bc().addCheckedLineEdit(fileED, fileLA);
196 external::TemplateManager::Templates::const_iterator i1, i2;
197 i1 = external::TemplateManager::get().getTemplates().begin();
198 i2 = external::TemplateManager::get().getTemplates().end();
199 for (; i1 != i2; ++i1)
200 externalCO->addItem(qt_(i1->second.lyxName));
202 // Fill the origins combo
203 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
204 originCO->addItem(qt_(origin_gui_strs[i]));
206 // Fill the width combo
207 widthUnitCO->addItem(qt_("Scale%"));
208 for (int i = 0; i < num_units; i++)
209 widthUnitCO->addItem(qt_(unit_name_gui[i]));
213 bool GuiExternal::activateAspectratio() const
215 if (widthUnitCO->currentIndex() == 0)
218 string const wstr = fromqstr(widthED->text());
221 bool const wIsDbl = isStrDbl(wstr);
222 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
225 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
228 string const hstr = fromqstr(heightED->text());
231 bool const hIsDbl = isStrDbl(hstr);
232 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
234 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
241 void GuiExternal::bbChanged()
248 void GuiExternal::browseClicked()
250 int const choice = externalCO->currentIndex();
251 QString const template_name = toqstr(getTemplate(choice).lyxName);
252 QString const str = browse(fileED->text(), template_name);
253 if (!str.isEmpty()) {
254 fileED->setText(str);
260 void GuiExternal::change_adaptor()
266 void GuiExternal::extraChanged(const QString & text)
268 extra_[extraFormatCO->currentText()] = text;
273 void GuiExternal::formatChanged(const QString & format)
275 extraED->setText(extra_[format]);
279 void GuiExternal::getbbClicked()
286 string const filename = fromqstr(fileED->text());
287 if (filename.empty())
290 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilepath())));
292 // try to get it from the file, if possible
293 string bb = readBB_from_PSFile(abs_file);
295 // we don't, so ask the Graphics Cache if it has loaded the file
299 graphics::Cache & gc = graphics::Cache::get();
300 if (gc.inCache(abs_file)) {
301 graphics::Image const * image = gc.item(abs_file)->image();
304 width = image->width();
305 height = image->height();
308 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
311 xlED->setText(toqstr(token(bb, ' ', 0)));
312 ybED->setText(toqstr(token(bb, ' ', 1)));
313 xrED->setText(toqstr(token(bb, ' ', 2)));
314 ytED->setText(toqstr(token(bb, ' ', 3)));
320 void GuiExternal::sizeChanged()
322 aspectratioCB->setEnabled(activateAspectratio());
327 void GuiExternal::templateChanged()
334 void GuiExternal::widthUnitChanged()
336 bool useHeight = (widthUnitCO->currentIndex() > 0);
339 widthED->setValidator(unsignedLengthValidator(widthED));
341 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
343 heightED->setEnabled(useHeight);
344 heightUnitCO->setEnabled(useHeight);
349 static Length::UNIT defaultUnit()
351 Length::UNIT default_unit = Length::CM;
352 switch (lyxrc.default_papersize) {
355 case PAPER_USEXECUTIVE:
356 default_unit = Length::IN;
365 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
366 external::RotationData const & data)
368 originCO.setCurrentIndex(int(data.origin()));
369 angleED.setText(toqstr(data.angle));
373 static void getRotation(external::RotationData & data,
374 QLineEdit const & angleED, QComboBox const & originCO)
376 typedef external::RotationData::OriginType OriginType;
378 data.origin(static_cast<OriginType>(originCO.currentIndex()));
379 data.angle = fromqstr(angleED.text());
383 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
384 QLineEdit & heightED, LengthCombo & heightUnitCO,
385 QCheckBox & aspectratioCB,
386 external::ResizeData const & data)
388 bool using_scale = data.usingScale();
389 string scale = data.scale;
390 if (data.no_resize()) {
391 // Everything is zero, so default to this!
397 widthED.setText(toqstr(scale));
398 widthUnitCO.setCurrentIndex(0);
400 widthED.setText(QString::number(data.width.value()));
401 // Because 'Scale' is position 0...
402 // Note also that width cannot be zero here, so
403 // we don't need to worry about the default unit.
404 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
407 string const h = data.height.zero() ? string() : data.height.asString();
408 Length::UNIT default_unit = data.width.zero() ?
409 defaultUnit() : data.width.unit();
410 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
412 heightED.setEnabled(!using_scale);
413 heightUnitCO.setEnabled(!using_scale);
415 aspectratioCB.setChecked(data.keepAspectRatio);
417 bool const disable_aspectRatio = using_scale ||
418 data.width.zero() || data.height.zero();
419 aspectratioCB.setEnabled(!disable_aspectRatio);
423 static void getSize(external::ResizeData & data,
424 QLineEdit const & widthED, QComboBox const & widthUnitCO,
425 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
426 QCheckBox const & aspectratioCB)
428 string const width = fromqstr(widthED.text());
430 if (widthUnitCO.currentIndex() > 0) {
431 // Subtract one, because scale is 0.
432 int const unit = widthUnitCO.currentIndex() - 1;
435 if (isValidLength(width, &w))
437 else if (isStrDbl(width))
438 data.width = Length(convert<double>(width),
439 static_cast<Length::UNIT>(unit));
441 data.width = Length();
443 data.scale = string();
446 // scaling instead of a width
448 data.width = Length();
451 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
453 data.keepAspectRatio = aspectratioCB.isChecked();
457 void setCrop(QCheckBox & clipCB,
458 QLineEdit & xlED, QLineEdit & ybED,
459 QLineEdit & xrED, QLineEdit & ytED,
460 external::ClipData const & data)
462 clipCB.setChecked(data.clip);
463 graphics::BoundingBox const & bbox = data.bbox;
464 xlED.setText(QString::number(bbox.xl));
465 ybED.setText(QString::number(bbox.yb));
466 xrED.setText(QString::number(bbox.xr));
467 ytED.setText(QString::number(bbox.yt));
471 static void getCrop(external::ClipData & data,
472 QCheckBox const & clipCB,
473 QLineEdit const & xlED, QLineEdit const & ybED,
474 QLineEdit const & xrED, QLineEdit const & ytED,
477 data.clip = clipCB.isChecked();
482 data.bbox.xl = xlED.text().toInt();
483 data.bbox.yb = ybED.text().toInt();
484 data.bbox.xr = xrED.text().toInt();
485 data.bbox.yt = ytED.text().toInt();
489 void GuiExternal::updateContents()
491 tab->setCurrentIndex(0);
494 params_.filename.outputFilename(fromqstr(bufferFilepath()));
495 fileED->setText(toqstr(name));
498 external::TemplateManager::Templates::const_iterator i1, i2;
499 i1 = external::TemplateManager::get().getTemplates().begin();
500 i2 = external::TemplateManager::get().getTemplates().end();
501 for (int i = 0; i1 != i2; ++i1, ++i) {
502 if (i1->second.lyxName == params_.templatename()) {
508 externalCO->setCurrentIndex(index);
511 draftCB->setChecked(params_.draft);
513 displayGB->setChecked(params_.display);
514 displayscaleED->setEnabled(params_.display && !isBufferReadonly());
515 displayscaleED->setText(QString::number(params_.lyxscale));
516 displayGB->setEnabled(lyxrc.display_graphics);
517 displayscaleED->setEnabled(params_.preview_mode != PREVIEW_INSTANT);
519 setRotation(*angleED, *originCO, params_.rotationdata);
521 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
522 *aspectratioCB, params_.resizedata);
524 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
525 bbChanged_ = !params_.clipdata.bbox.empty();
531 void GuiExternal::updateTemplate()
533 external::Template templ = getTemplate(externalCO->currentIndex());
534 externalTB->setPlainText(qt_(templ.helpText));
536 // Ascertain which (if any) transformations the template supports
537 // and disable tabs and Group Boxes hosting unsupported transforms.
538 typedef vector<external::TransformID> TransformIDs;
539 TransformIDs const transformIds = templ.transformIds;
540 TransformIDs::const_iterator tr_begin = transformIds.begin();
541 TransformIDs::const_iterator const tr_end = transformIds.end();
543 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
544 rotationGB->setEnabled(found);
546 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
547 scaleGB->setEnabled(found);
549 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
550 cropGB->setEnabled(found);
552 tab->setTabEnabled(tab->indexOf(sizetab),
553 rotationGB->isEnabled()
554 || scaleGB->isEnabled()
555 || cropGB->isEnabled());
557 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
558 optionsGB->setEnabled(found);
559 displayscaleED->setEnabled(templ.preview_mode != PREVIEW_INSTANT);
564 // Ascertain whether the template has any formats supporting
565 // the 'Extra' option
568 extraFormatCO->clear();
570 external::Template::Formats::const_iterator it = templ.formats.begin();
571 external::Template::Formats::const_iterator end = templ.formats.end();
572 for (; it != end; ++it) {
573 if (it->second.option_transformers.find(external::Extra) ==
574 it->second.option_transformers.end())
576 string const format = it->first;
577 string const opt = params_.extradata.get(format);
578 extraFormatCO->addItem(toqstr(format));
579 extra_[toqstr(format)] = toqstr(opt);
582 bool const enabled = extraFormatCO->count() > 0;
584 optionsGB->setEnabled(enabled);
585 extraED->setEnabled(enabled && !isBufferReadonly());
586 extraFormatCO->setEnabled(enabled);
589 extraFormatCO->setCurrentIndex(0);
590 extraED->setText(extra_[extraFormatCO->currentText()]);
595 void GuiExternal::applyView()
597 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
598 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
600 params_.draft = draftCB->isChecked();
601 params_.lyxscale = displayscaleED->text().toInt();
602 params_.display = displayGB->isChecked();
604 if (rotationGB->isEnabled())
605 getRotation(params_.rotationdata, *angleED, *originCO);
607 if (scaleGB->isEnabled())
608 getSize(params_.resizedata, *widthED, *widthUnitCO,
609 *heightED, *heightUnitCO, *aspectratioCB);
611 if (cropGB->isEnabled())
612 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
613 *xrED, *ytED, bbChanged_);
615 if (optionsGB->isEnabled()) {
616 MapType::const_iterator it = extra_.begin();
617 MapType::const_iterator const end = extra_.end();
618 for (; it != end; ++it)
619 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
624 bool GuiExternal::initialiseParams(string const & data)
626 InsetExternal::string2params(data, buffer(), params_);
631 void GuiExternal::clearParams()
633 params_ = InsetExternalParams();
637 void GuiExternal::dispatchParams()
639 string const lfun = InsetExternal::params2string(params_, buffer());
640 dispatch(FuncRequest(getLfun(), lfun));
644 static QStringList templateFilters(QString const & template_name)
646 /// Determine the template file extension
647 external::TemplateManager const & etm =
648 external::TemplateManager::get();
649 external::Template const * const et_ptr =
650 etm.getTemplateByName(fromqstr(template_name));
652 return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
656 QString GuiExternal::browse(QString const & input,
657 QString const & template_name) const
659 QString const title = qt_("Select external file");
660 QString const bufpath = bufferFilepath();
661 QStringList const filter = templateFilters(template_name);
663 QString const label1 = qt_("Documents|#o#O");
664 QString const dir1 = toqstr(lyxrc.document_path);
666 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
670 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
673 } // namespace frontend
676 #include "moc_GuiExternal.cpp"