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 if (external::TemplateManager::get().getTemplates().empty())
85 external::TemplateManager::Templates::const_iterator i1
86 = external::TemplateManager::get().getTemplates().begin();
94 GuiExternal::GuiExternal(GuiView & lv)
95 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
99 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
100 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
101 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
104 connect(displayGB, SIGNAL(toggled(bool)),
105 displayscaleED, SLOT(setEnabled(bool)));
107 connect(originCO, SIGNAL(activated(int)),
108 this, SLOT(change_adaptor()));
109 connect(aspectratioCB, SIGNAL(stateChanged(int)),
110 this, SLOT(change_adaptor()));
111 connect(browsePB, SIGNAL(clicked()),
112 this, SLOT(browseClicked()));
113 connect(externalCO, SIGNAL(activated(QString)),
114 this, SLOT(templateChanged()));
115 connect(extraED, SIGNAL(textChanged(QString)),
116 this, SLOT(extraChanged(QString)));
117 connect(extraFormatCO, SIGNAL(activated(QString)),
118 this, SLOT(formatChanged(QString)));
119 connect(widthUnitCO, SIGNAL(activated(int)),
120 this, SLOT(widthUnitChanged()));
121 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
122 this, SLOT(change_adaptor()));
123 connect(displayGB, SIGNAL(toggled(bool)),
124 this, SLOT(change_adaptor()));
125 connect(displayscaleED, SIGNAL(textChanged(QString)),
126 this, SLOT(change_adaptor()));
127 connect(angleED, SIGNAL(textChanged(QString)),
128 this, SLOT(change_adaptor()));
129 connect(widthED, SIGNAL(textChanged(QString)),
130 this, SLOT(sizeChanged()));
131 connect(heightED, SIGNAL(textChanged(QString)),
132 this, SLOT(sizeChanged()));
133 connect(fileED, SIGNAL(textChanged(QString)),
134 this, SLOT(change_adaptor()));
135 connect(clipCB, SIGNAL(stateChanged(int)),
136 this, SLOT(change_adaptor()));
137 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
138 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
139 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
140 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
144 QIntValidator * validator = new QIntValidator(displayscaleED);
145 validator->setBottom(1);
146 displayscaleED->setValidator(validator);
148 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
150 xlED->setValidator(new QIntValidator(xlED));
151 ybED->setValidator(new QIntValidator(ybED));
152 xrED->setValidator(new QIntValidator(xrED));
153 ytED->setValidator(new QIntValidator(ytED));
155 widthED->setValidator(unsignedLengthValidator(widthED));
156 heightED->setValidator(unsignedLengthValidator(heightED));
158 setFocusProxy(fileED);
160 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
163 bc().setApply(applyPB);
164 bc().setCancel(closePB);
166 bc().addReadOnly(fileED);
167 bc().addReadOnly(browsePB);
168 bc().addReadOnly(externalCO);
169 bc().addReadOnly(draftCB);
170 bc().addReadOnly(displayscaleED);
171 bc().addReadOnly(displayGB);
172 bc().addReadOnly(angleED);
173 bc().addReadOnly(originCO);
174 bc().addReadOnly(heightUnitCO);
175 bc().addReadOnly(heightED);
176 bc().addReadOnly(aspectratioCB);
177 bc().addReadOnly(widthUnitCO);
178 bc().addReadOnly(widthED);
179 bc().addReadOnly(clipCB);
180 bc().addReadOnly(getbbPB);
181 bc().addReadOnly(ytED);
182 bc().addReadOnly(xlED);
183 bc().addReadOnly(xrED);
184 bc().addReadOnly(ybED);
185 bc().addReadOnly(extraFormatCO);
186 bc().addReadOnly(extraED);
188 bc().addCheckedLineEdit(angleED, angleLA);
189 bc().addCheckedLineEdit(displayscaleED, scaleLA);
190 bc().addCheckedLineEdit(heightED, heightLA);
191 bc().addCheckedLineEdit(widthED, widthLA);
192 bc().addCheckedLineEdit(xlED, lbLA);
193 bc().addCheckedLineEdit(ybED, lbLA);
194 bc().addCheckedLineEdit(xrED, rtLA);
195 bc().addCheckedLineEdit(ytED, rtLA);
196 bc().addCheckedLineEdit(fileED, fileLA);
198 external::TemplateManager::Templates::const_iterator i1, i2;
199 i1 = external::TemplateManager::get().getTemplates().begin();
200 i2 = external::TemplateManager::get().getTemplates().end();
201 for (; i1 != i2; ++i1)
202 externalCO->addItem(qt_(i1->second.lyxName));
204 // Fill the origins combo
205 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
206 originCO->addItem(qt_(origin_gui_strs[i]));
209 widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
213 bool GuiExternal::activateAspectratio() const
218 QString const wstr = widthED->text();
222 double val = wstr.trimmed().toDouble(&wIsDbl);
223 if (wIsDbl && float_equal(val, 0.0, 0.05))
226 if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
229 QString const hstr = heightED->text();
233 val = hstr.trimmed().toDouble(&hIsDbl);
234 if (hIsDbl && float_equal(val, 0.0, 0.05))
236 if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
243 bool GuiExternal::usingScale() const
245 return (widthUnitCO->itemData(
246 widthUnitCO->currentIndex()).toString() == "scale");
250 void GuiExternal::bbChanged()
257 void GuiExternal::browseClicked()
259 int const choice = externalCO->currentIndex();
260 QString const template_name = toqstr(getTemplate(choice).lyxName);
261 QString const str = browse(fileED->text(), template_name);
262 if (!str.isEmpty()) {
263 fileED->setText(str);
269 void GuiExternal::change_adaptor()
275 void GuiExternal::extraChanged(const QString & text)
277 extra_[extraFormatCO->currentText()] = text;
282 void GuiExternal::formatChanged(const QString & format)
284 extraED->setText(extra_[format]);
288 void GuiExternal::getbbClicked()
295 string const filename = fromqstr(fileED->text());
296 if (filename.empty())
299 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilepath())));
301 // try to get it from the file, if possible
302 string bb = readBB_from_PSFile(abs_file);
304 // we don't, so ask the Graphics Cache if it has loaded the file
308 graphics::Cache & gc = graphics::Cache::get();
309 if (gc.inCache(abs_file)) {
310 graphics::Image const * image = gc.item(abs_file)->image();
313 width = image->width();
314 height = image->height();
317 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
320 xlED->setText(toqstr(token(bb, ' ', 0)));
321 ybED->setText(toqstr(token(bb, ' ', 1)));
322 xrED->setText(toqstr(token(bb, ' ', 2)));
323 ytED->setText(toqstr(token(bb, ' ', 3)));
329 void GuiExternal::sizeChanged()
331 aspectratioCB->setEnabled(activateAspectratio());
336 void GuiExternal::templateChanged()
343 void GuiExternal::widthUnitChanged()
346 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
348 widthED->setValidator(unsignedLengthValidator(widthED));
350 heightED->setEnabled(!usingScale());
351 heightUnitCO->setEnabled(!usingScale());
356 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
357 external::RotationData const & data)
359 originCO.setCurrentIndex(int(data.origin()));
360 doubleToWidget(&angleED, data.angle);
364 static void getRotation(external::RotationData & data,
365 QLineEdit const & angleED, QComboBox const & originCO)
367 typedef external::RotationData::OriginType OriginType;
369 data.origin(static_cast<OriginType>(originCO.currentIndex()));
370 data.angle = widgetToDoubleStr(&angleED);
374 static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
375 QLineEdit & heightED, LengthCombo & heightUnitCO,
376 QCheckBox & aspectratioCB,
377 external::ResizeData const & data)
379 bool using_scale = data.usingScale();
380 string scale = data.scale;
381 if (data.no_resize()) {
382 // Everything is zero, so default to this!
388 doubleToWidget(&widthED, scale);
389 widthUnitCO.setCurrentItem("scale");
391 lengthToWidgets(&widthED, &widthUnitCO,
392 data.width.asString(), Length::defaultUnit());
394 string const h = data.height.zero() ? string() : data.height.asString();
395 Length::UNIT const default_unit = data.width.zero() ?
396 Length::defaultUnit() : data.width.unit();
397 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
399 heightED.setEnabled(!using_scale);
400 heightUnitCO.setEnabled(!using_scale);
402 aspectratioCB.setChecked(data.keepAspectRatio);
404 bool const disable_aspectRatio = using_scale ||
405 data.width.zero() || data.height.zero();
406 aspectratioCB.setEnabled(!disable_aspectRatio);
410 static void getSize(external::ResizeData & data,
411 QLineEdit const & widthED, QComboBox const & widthUnitCO,
412 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
413 QCheckBox const & aspectratioCB, bool const scaling)
416 // scaling instead of a width
417 data.scale = widgetToDoubleStr(&widthED);
418 data.width = Length();
420 data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
421 data.scale = string();
423 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
424 data.keepAspectRatio = aspectratioCB.isChecked();
428 void setCrop(QCheckBox & clipCB,
429 QLineEdit & xlED, QLineEdit & ybED,
430 QLineEdit & xrED, QLineEdit & ytED,
431 external::ClipData const & data)
433 clipCB.setChecked(data.clip);
434 graphics::BoundingBox const & bbox = data.bbox;
435 xlED.setText(QString::number(bbox.xl));
436 ybED.setText(QString::number(bbox.yb));
437 xrED.setText(QString::number(bbox.xr));
438 ytED.setText(QString::number(bbox.yt));
442 static void getCrop(external::ClipData & data,
443 QCheckBox const & clipCB,
444 QLineEdit const & xlED, QLineEdit const & ybED,
445 QLineEdit const & xrED, QLineEdit const & ytED,
448 data.clip = clipCB.isChecked();
453 data.bbox.xl = xlED.text().toInt();
454 data.bbox.yb = ybED.text().toInt();
455 data.bbox.xr = xrED.text().toInt();
456 data.bbox.yt = ytED.text().toInt();
460 void GuiExternal::updateContents()
463 params_.filename.outputFilename(fromqstr(bufferFilepath()));
464 fileED->setText(toqstr(name));
467 external::TemplateManager::Templates::const_iterator i1, i2;
468 i1 = external::TemplateManager::get().getTemplates().begin();
469 i2 = external::TemplateManager::get().getTemplates().end();
470 for (int i = 0; i1 != i2; ++i1, ++i) {
471 if (i1->second.lyxName == params_.templatename()) {
477 externalCO->setCurrentIndex(index);
480 draftCB->setChecked(params_.draft);
482 displayGB->setChecked(params_.display);
483 displayscaleED->setText(QString::number(params_.lyxscale));
484 bool scaled = params_.display && !isBufferReadonly() &&
485 (params_.preview_mode != PREVIEW_INSTANT);
486 displayscaleED->setEnabled(scaled);
487 scaleLA->setEnabled(scaled);
488 displayGB->setEnabled(lyxrc.display_graphics);
491 setRotation(*angleED, *originCO, params_.rotationdata);
493 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
494 *aspectratioCB, params_.resizedata);
496 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
497 bbChanged_ = !params_.clipdata.bbox.empty();
503 void GuiExternal::updateTemplate()
505 external::Template templ = getTemplate(externalCO->currentIndex());
506 externalTB->setPlainText(qt_(templ.helpText));
508 // Ascertain which (if any) transformations the template supports
509 // and disable tabs and Group Boxes hosting unsupported transforms.
510 typedef vector<external::TransformID> TransformIDs;
511 TransformIDs const transformIds = templ.transformIds;
512 TransformIDs::const_iterator tr_begin = transformIds.begin();
513 TransformIDs::const_iterator const tr_end = transformIds.end();
515 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
516 rotationGB->setEnabled(found);
518 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
519 scaleGB->setEnabled(found);
521 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
522 cropGB->setEnabled(found);
524 tab->setTabEnabled(tab->indexOf(sizetab),
525 rotationGB->isEnabled()
526 || scaleGB->isEnabled()
527 || cropGB->isEnabled());
529 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
530 optionsGB->setEnabled(found);
532 bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
533 !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
534 displayscaleED->setEnabled(scaled);
535 scaleLA->setEnabled(scaled);
540 // Ascertain whether the template has any formats supporting
541 // the 'Extra' option
544 extraFormatCO->clear();
546 external::Template::Formats::const_iterator it = templ.formats.begin();
547 external::Template::Formats::const_iterator end = templ.formats.end();
548 for (; it != end; ++it) {
549 if (it->second.option_transformers.find(external::Extra) ==
550 it->second.option_transformers.end())
552 string const format = it->first;
553 string const opt = params_.extradata.get(format);
554 extraFormatCO->addItem(toqstr(format));
555 extra_[toqstr(format)] = toqstr(opt);
558 bool const enabled = extraFormatCO->count() > 0;
560 optionsGB->setEnabled(enabled);
561 extraED->setEnabled(enabled && !isBufferReadonly());
562 extraFormatCO->setEnabled(enabled);
565 extraFormatCO->setCurrentIndex(0);
566 extraED->setText(extra_[extraFormatCO->currentText()]);
571 void GuiExternal::applyView()
573 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
574 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
576 params_.draft = draftCB->isChecked();
577 params_.lyxscale = displayscaleED->text().toInt();
578 params_.display = displayGB->isChecked();
580 if (rotationGB->isEnabled())
581 getRotation(params_.rotationdata, *angleED, *originCO);
583 if (scaleGB->isEnabled())
584 getSize(params_.resizedata, *widthED, *widthUnitCO,
585 *heightED, *heightUnitCO, *aspectratioCB, usingScale());
587 if (cropGB->isEnabled())
588 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
589 *xrED, *ytED, bbChanged_);
591 if (optionsGB->isEnabled()) {
592 MapType::const_iterator it = extra_.begin();
593 MapType::const_iterator const end = extra_.end();
594 for (; it != end; ++it)
595 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
600 bool GuiExternal::initialiseParams(string const & data)
602 InsetExternal::string2params(data, buffer(), params_);
607 void GuiExternal::clearParams()
609 params_ = InsetExternalParams();
613 void GuiExternal::dispatchParams()
615 string const lfun = InsetExternal::params2string(params_, buffer());
616 dispatch(FuncRequest(getLfun(), lfun));
620 static QStringList templateFilters(QString const & template_name)
622 /// Determine the template file extension
623 external::TemplateManager const & etm =
624 external::TemplateManager::get();
625 external::Template const * const et_ptr =
626 etm.getTemplateByName(fromqstr(template_name));
628 return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
632 QString GuiExternal::browse(QString const & input,
633 QString const & template_name) const
635 QString const title = qt_("Select external file");
636 QString const bufpath = bufferFilepath();
637 QStringList const filter = templateFilters(template_name);
639 QString const label1 = qt_("Documents|#o#O");
640 QString const dir1 = toqstr(lyxrc.document_path);
642 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
646 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
649 } // namespace frontend
652 #include "moc_GuiExternal.cpp"