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]));
207 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
211 bool GuiExternal::activateAspectratio() const
216 string const wstr = fromqstr(widthED->text());
219 bool const wIsDbl = isStrDbl(wstr);
220 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
223 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
226 string const hstr = fromqstr(heightED->text());
229 bool const hIsDbl = isStrDbl(hstr);
230 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
232 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
239 bool GuiExternal::usingScale() const
241 return (widthUnitCO->itemData(
242 widthUnitCO->currentIndex()).toString() == "scale");
246 void GuiExternal::bbChanged()
253 void GuiExternal::browseClicked()
255 int const choice = externalCO->currentIndex();
256 QString const template_name = toqstr(getTemplate(choice).lyxName);
257 QString const str = browse(fileED->text(), template_name);
258 if (!str.isEmpty()) {
259 fileED->setText(str);
265 void GuiExternal::change_adaptor()
271 void GuiExternal::extraChanged(const QString & text)
273 extra_[extraFormatCO->currentText()] = text;
278 void GuiExternal::formatChanged(const QString & format)
280 extraED->setText(extra_[format]);
284 void GuiExternal::getbbClicked()
291 string const filename = fromqstr(fileED->text());
292 if (filename.empty())
295 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilepath())));
297 // try to get it from the file, if possible
298 string bb = readBB_from_PSFile(abs_file);
300 // we don't, so ask the Graphics Cache if it has loaded the file
304 graphics::Cache & gc = graphics::Cache::get();
305 if (gc.inCache(abs_file)) {
306 graphics::Image const * image = gc.item(abs_file)->image();
309 width = image->width();
310 height = image->height();
313 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
316 xlED->setText(toqstr(token(bb, ' ', 0)));
317 ybED->setText(toqstr(token(bb, ' ', 1)));
318 xrED->setText(toqstr(token(bb, ' ', 2)));
319 ytED->setText(toqstr(token(bb, ' ', 3)));
325 void GuiExternal::sizeChanged()
327 aspectratioCB->setEnabled(activateAspectratio());
332 void GuiExternal::templateChanged()
339 void GuiExternal::widthUnitChanged()
342 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
344 widthED->setValidator(unsignedLengthValidator(widthED));
346 heightED->setEnabled(!usingScale());
347 heightUnitCO->setEnabled(!usingScale());
352 static Length::UNIT defaultUnit()
354 Length::UNIT default_unit = Length::CM;
355 switch (lyxrc.default_papersize) {
358 case PAPER_USEXECUTIVE:
359 default_unit = Length::IN;
368 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
369 external::RotationData const & data)
371 originCO.setCurrentIndex(int(data.origin()));
372 angleED.setText(toqstr(data.angle));
376 static void getRotation(external::RotationData & data,
377 QLineEdit const & angleED, QComboBox const & originCO)
379 typedef external::RotationData::OriginType OriginType;
381 data.origin(static_cast<OriginType>(originCO.currentIndex()));
382 data.angle = fromqstr(angleED.text());
386 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
387 QLineEdit & heightED, LengthCombo & heightUnitCO,
388 QCheckBox & aspectratioCB,
389 external::ResizeData const & data)
391 bool using_scale = data.usingScale();
392 string scale = data.scale;
393 if (data.no_resize()) {
394 // Everything is zero, so default to this!
400 widthED.setText(toqstr(scale));
401 widthUnitCO.setCurrentItem("scale");
403 lengthToWidgets(&widthED, &widthUnitCO,
404 data.width.asString(), defaultUnit());
406 string const h = data.height.zero() ? string() : data.height.asString();
407 Length::UNIT default_unit = data.width.zero() ?
408 defaultUnit() : data.width.unit();
409 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
411 heightED.setEnabled(!using_scale);
412 heightUnitCO.setEnabled(!using_scale);
414 aspectratioCB.setChecked(data.keepAspectRatio);
416 bool const disable_aspectRatio = using_scale ||
417 data.width.zero() || data.height.zero();
418 aspectratioCB.setEnabled(!disable_aspectRatio);
422 static void getSize(external::ResizeData & data,
423 QLineEdit const & widthED, QComboBox const & widthUnitCO,
424 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
425 QCheckBox const & aspectratioCB, bool const scaling)
427 string const width = fromqstr(widthED.text());
430 // scaling instead of a width
432 data.width = Length();
434 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
435 data.scale = string();
437 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
438 data.keepAspectRatio = aspectratioCB.isChecked();
442 void setCrop(QCheckBox & clipCB,
443 QLineEdit & xlED, QLineEdit & ybED,
444 QLineEdit & xrED, QLineEdit & ytED,
445 external::ClipData const & data)
447 clipCB.setChecked(data.clip);
448 graphics::BoundingBox const & bbox = data.bbox;
449 xlED.setText(QString::number(bbox.xl));
450 ybED.setText(QString::number(bbox.yb));
451 xrED.setText(QString::number(bbox.xr));
452 ytED.setText(QString::number(bbox.yt));
456 static void getCrop(external::ClipData & data,
457 QCheckBox const & clipCB,
458 QLineEdit const & xlED, QLineEdit const & ybED,
459 QLineEdit const & xrED, QLineEdit const & ytED,
462 data.clip = clipCB.isChecked();
467 data.bbox.xl = xlED.text().toInt();
468 data.bbox.yb = ybED.text().toInt();
469 data.bbox.xr = xrED.text().toInt();
470 data.bbox.yt = ytED.text().toInt();
474 void GuiExternal::updateContents()
477 params_.filename.outputFilename(fromqstr(bufferFilepath()));
478 fileED->setText(toqstr(name));
481 external::TemplateManager::Templates::const_iterator i1, i2;
482 i1 = external::TemplateManager::get().getTemplates().begin();
483 i2 = external::TemplateManager::get().getTemplates().end();
484 for (int i = 0; i1 != i2; ++i1, ++i) {
485 if (i1->second.lyxName == params_.templatename()) {
491 externalCO->setCurrentIndex(index);
494 draftCB->setChecked(params_.draft);
496 displayGB->setChecked(params_.display);
497 displayscaleED->setText(QString::number(params_.lyxscale));
498 bool scaled = params_.display && !isBufferReadonly() &&
499 (params_.preview_mode != PREVIEW_INSTANT);
500 displayscaleED->setEnabled(scaled);
501 scaleLA->setEnabled(scaled);
502 displayGB->setEnabled(lyxrc.display_graphics);
505 setRotation(*angleED, *originCO, params_.rotationdata);
507 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
508 *aspectratioCB, params_.resizedata);
510 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
511 bbChanged_ = !params_.clipdata.bbox.empty();
517 void GuiExternal::updateTemplate()
519 external::Template templ = getTemplate(externalCO->currentIndex());
520 externalTB->setPlainText(qt_(templ.helpText));
522 // Ascertain which (if any) transformations the template supports
523 // and disable tabs and Group Boxes hosting unsupported transforms.
524 typedef vector<external::TransformID> TransformIDs;
525 TransformIDs const transformIds = templ.transformIds;
526 TransformIDs::const_iterator tr_begin = transformIds.begin();
527 TransformIDs::const_iterator const tr_end = transformIds.end();
529 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
530 rotationGB->setEnabled(found);
532 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
533 scaleGB->setEnabled(found);
535 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
536 cropGB->setEnabled(found);
538 tab->setTabEnabled(tab->indexOf(sizetab),
539 rotationGB->isEnabled()
540 || scaleGB->isEnabled()
541 || cropGB->isEnabled());
543 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
544 optionsGB->setEnabled(found);
546 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
547 !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
548 displayscaleED->setEnabled(scaled);
549 scaleLA->setEnabled(scaled);
554 // Ascertain whether the template has any formats supporting
555 // the 'Extra' option
558 extraFormatCO->clear();
560 external::Template::Formats::const_iterator it = templ.formats.begin();
561 external::Template::Formats::const_iterator end = templ.formats.end();
562 for (; it != end; ++it) {
563 if (it->second.option_transformers.find(external::Extra) ==
564 it->second.option_transformers.end())
566 string const format = it->first;
567 string const opt = params_.extradata.get(format);
568 extraFormatCO->addItem(toqstr(format));
569 extra_[toqstr(format)] = toqstr(opt);
572 bool const enabled = extraFormatCO->count() > 0;
574 optionsGB->setEnabled(enabled);
575 extraED->setEnabled(enabled && !isBufferReadonly());
576 extraFormatCO->setEnabled(enabled);
579 extraFormatCO->setCurrentIndex(0);
580 extraED->setText(extra_[extraFormatCO->currentText()]);
585 void GuiExternal::applyView()
587 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
588 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
590 params_.draft = draftCB->isChecked();
591 params_.lyxscale = displayscaleED->text().toInt();
592 params_.display = displayGB->isChecked();
594 if (rotationGB->isEnabled())
595 getRotation(params_.rotationdata, *angleED, *originCO);
597 if (scaleGB->isEnabled())
598 getSize(params_.resizedata, *widthED, *widthUnitCO,
599 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
601 if (cropGB->isEnabled())
602 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
603 *xrED, *ytED, bbChanged_);
605 if (optionsGB->isEnabled()) {
606 MapType::const_iterator it = extra_.begin();
607 MapType::const_iterator const end = extra_.end();
608 for (; it != end; ++it)
609 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
614 bool GuiExternal::initialiseParams(string const & data)
616 InsetExternal::string2params(data, buffer(), params_);
621 void GuiExternal::clearParams()
623 params_ = InsetExternalParams();
627 void GuiExternal::dispatchParams()
629 string const lfun = InsetExternal::params2string(params_, buffer());
630 dispatch(FuncRequest(getLfun(), lfun));
634 static QStringList templateFilters(QString const & template_name)
636 /// Determine the template file extension
637 external::TemplateManager const & etm =
638 external::TemplateManager::get();
639 external::Template const * const et_ptr =
640 etm.getTemplateByName(fromqstr(template_name));
642 return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
646 QString GuiExternal::browse(QString const & input,
647 QString const & template_name) const
649 QString const title = qt_("Select external file");
650 QString const bufpath = bufferFilepath();
651 QStringList const filter = templateFilters(template_name);
653 QString const label1 = qt_("Documents|#o#O");
654 QString const dir1 = toqstr(lyxrc.document_path);
656 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
660 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
663 } // namespace frontend
666 #include "moc_GuiExternal.cpp"