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()));
101 connect(displayGB, SIGNAL(toggled(bool)),
102 showCO, SLOT(setEnabled(bool)));
103 connect(displayGB, SIGNAL(toggled(bool)),
104 displayscaleED, SLOT(setEnabled(bool)));
105 connect(showCO, SIGNAL(activated(QString)),
106 this, SLOT(change_adaptor()));
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(showCO);
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.lyxName));
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]));
209 // Fill the width combo
210 widthUnitCO->addItem(qt_("Scale%"));
211 for (int i = 0; i < num_units; i++)
212 widthUnitCO->addItem(qt_(unit_name_gui[i]));
216 bool GuiExternal::activateAspectratio() const
218 if (widthUnitCO->currentIndex() == 0)
221 string const wstr = fromqstr(widthED->text());
224 bool const wIsDbl = isStrDbl(wstr);
225 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
228 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
231 string const hstr = fromqstr(heightED->text());
234 bool const hIsDbl = isStrDbl(hstr);
235 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
237 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
244 void GuiExternal::bbChanged()
251 void GuiExternal::browseClicked()
253 int const choice = externalCO->currentIndex();
254 QString const template_name = toqstr(getTemplate(choice).lyxName);
255 QString const str = browse(fileED->text(), template_name);
256 if (!str.isEmpty()) {
257 fileED->setText(str);
263 void GuiExternal::change_adaptor()
269 void GuiExternal::extraChanged(const QString & text)
271 extra_[extraFormatCO->currentText()] = text;
276 void GuiExternal::formatChanged(const QString & format)
278 extraED->setText(extra_[format]);
282 void GuiExternal::getbbClicked()
289 string const filename = fromqstr(fileED->text());
290 if (filename.empty())
293 FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilepath())));
295 // try to get it from the file, if possible
296 string bb = readBB_from_PSFile(abs_file);
298 // we don't, so ask the Graphics Cache if it has loaded the file
302 graphics::Cache & gc = graphics::Cache::get();
303 if (gc.inCache(abs_file)) {
304 graphics::Image const * image = gc.item(abs_file)->image();
307 width = image->width();
308 height = image->height();
311 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
314 xlED->setText(toqstr(token(bb, ' ', 0)));
315 ybED->setText(toqstr(token(bb, ' ', 1)));
316 xrED->setText(toqstr(token(bb, ' ', 2)));
317 ytED->setText(toqstr(token(bb, ' ', 3)));
323 void GuiExternal::sizeChanged()
325 aspectratioCB->setEnabled(activateAspectratio());
330 void GuiExternal::templateChanged()
337 void GuiExternal::widthUnitChanged()
339 bool useHeight = (widthUnitCO->currentIndex() > 0);
342 widthED->setValidator(unsignedLengthValidator(widthED));
344 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
346 heightED->setEnabled(useHeight);
347 heightUnitCO->setEnabled(useHeight);
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 setDisplay(
369 QGroupBox & displayGB, QComboBox & showCO, QLineEdit & scaleED,
370 external::DisplayType display, unsigned int scale, bool read_only)
374 case external::DefaultDisplay:
377 case external::MonochromeDisplay:
380 case external::GrayscaleDisplay:
383 case external::ColorDisplay:
386 case external::PreviewDisplay:
389 case external::NoDisplay:
394 showCO.setCurrentIndex(item);
395 bool const no_display = display == external::NoDisplay;
396 showCO.setEnabled(!no_display && !read_only);
397 displayGB.setChecked(!no_display);
398 scaleED.setEnabled(!no_display && !read_only);
399 scaleED.setText(QString::number(scale));
403 static external::DisplayType display(QGroupBox const & displayGB,
404 QComboBox const & showCO)
406 if (!displayGB.isChecked())
407 return external::NoDisplay;
408 switch (showCO.currentIndex()) {
411 return external::DefaultDisplay;
413 return external::MonochromeDisplay;
415 return external::GrayscaleDisplay;
417 return external::ColorDisplay;
419 return external::PreviewDisplay;
424 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
425 external::RotationData const & data)
427 originCO.setCurrentIndex(int(data.origin()));
428 angleED.setText(toqstr(data.angle));
432 static void getRotation(external::RotationData & data,
433 QLineEdit const & angleED, QComboBox const & originCO)
435 typedef external::RotationData::OriginType OriginType;
437 data.origin(static_cast<OriginType>(originCO.currentIndex()));
438 data.angle = fromqstr(angleED.text());
442 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
443 QLineEdit & heightED, LengthCombo & heightUnitCO,
444 QCheckBox & aspectratioCB,
445 external::ResizeData const & data)
447 bool using_scale = data.usingScale();
448 string scale = data.scale;
449 if (data.no_resize()) {
450 // Everything is zero, so default to this!
456 widthED.setText(toqstr(scale));
457 widthUnitCO.setCurrentIndex(0);
459 widthED.setText(QString::number(data.width.value()));
460 // Because 'Scale' is position 0...
461 // Note also that width cannot be zero here, so
462 // we don't need to worry about the default unit.
463 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
466 string const h = data.height.zero() ? string() : data.height.asString();
467 Length::UNIT default_unit = data.width.zero() ?
468 defaultUnit() : data.width.unit();
469 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
471 heightED.setEnabled(!using_scale);
472 heightUnitCO.setEnabled(!using_scale);
474 aspectratioCB.setChecked(data.keepAspectRatio);
476 bool const disable_aspectRatio = using_scale ||
477 data.width.zero() || data.height.zero();
478 aspectratioCB.setEnabled(!disable_aspectRatio);
482 static void getSize(external::ResizeData & data,
483 QLineEdit const & widthED, QComboBox const & widthUnitCO,
484 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
485 QCheckBox const & aspectratioCB)
487 string const width = fromqstr(widthED.text());
489 if (widthUnitCO.currentIndex() > 0) {
490 // Subtract one, because scale is 0.
491 int const unit = widthUnitCO.currentIndex() - 1;
494 if (isValidLength(width, &w))
496 else if (isStrDbl(width))
497 data.width = Length(convert<double>(width),
498 static_cast<Length::UNIT>(unit));
500 data.width = Length();
502 data.scale = string();
505 // scaling instead of a width
507 data.width = Length();
510 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
512 data.keepAspectRatio = aspectratioCB.isChecked();
516 void setCrop(QCheckBox & clipCB,
517 QLineEdit & xlED, QLineEdit & ybED,
518 QLineEdit & xrED, QLineEdit & ytED,
519 external::ClipData const & data)
521 clipCB.setChecked(data.clip);
522 graphics::BoundingBox const & bbox = data.bbox;
523 xlED.setText(QString::number(bbox.xl));
524 ybED.setText(QString::number(bbox.yb));
525 xrED.setText(QString::number(bbox.xr));
526 ytED.setText(QString::number(bbox.yt));
530 static void getCrop(external::ClipData & data,
531 QCheckBox const & clipCB,
532 QLineEdit const & xlED, QLineEdit const & ybED,
533 QLineEdit const & xrED, QLineEdit const & ytED,
536 data.clip = clipCB.isChecked();
541 data.bbox.xl = xlED.text().toInt();
542 data.bbox.yb = ybED.text().toInt();
543 data.bbox.xr = xrED.text().toInt();
544 data.bbox.yt = ytED.text().toInt();
548 void GuiExternal::updateContents()
550 tab->setCurrentIndex(0);
553 params_.filename.outputFilename(fromqstr(bufferFilepath()));
554 fileED->setText(toqstr(name));
557 external::TemplateManager::Templates::const_iterator i1, i2;
558 i1 = external::TemplateManager::get().getTemplates().begin();
559 i2 = external::TemplateManager::get().getTemplates().end();
560 for (int i = 0; i1 != i2; ++i1, ++i) {
561 if (i1->second.lyxName == params_.templatename()) {
567 externalCO->setCurrentIndex(index);
570 draftCB->setChecked(params_.draft);
572 setDisplay(*displayGB, *showCO, *displayscaleED,
573 params_.display, params_.lyxscale, isBufferReadonly());
575 setRotation(*angleED, *originCO, params_.rotationdata);
577 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
578 *aspectratioCB, params_.resizedata);
580 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
581 bbChanged_ = !params_.clipdata.bbox.empty();
587 void GuiExternal::updateTemplate()
589 external::Template templ = getTemplate(externalCO->currentIndex());
590 externalTB->setPlainText(qt_(templ.helpText));
592 // Ascertain which (if any) transformations the template supports
593 // and disable tabs and Group Boxes hosting unsupported transforms.
594 typedef vector<external::TransformID> TransformIDs;
595 TransformIDs const transformIds = templ.transformIds;
596 TransformIDs::const_iterator tr_begin = transformIds.begin();
597 TransformIDs::const_iterator const tr_end = transformIds.end();
599 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
600 rotationGB->setEnabled(found);
602 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
603 scaleGB->setEnabled(found);
605 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
606 cropGB->setEnabled(found);
608 tab->setTabEnabled(tab->indexOf(sizetab),
609 rotationGB->isEnabled()
610 || scaleGB->isEnabled()
611 || cropGB->isEnabled());
613 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
614 optionsGB->setEnabled(found);
619 // Ascertain whether the template has any formats supporting
620 // the 'Extra' option
623 extraFormatCO->clear();
625 external::Template::Formats::const_iterator it = templ.formats.begin();
626 external::Template::Formats::const_iterator end = templ.formats.end();
627 for (; it != end; ++it) {
628 if (it->second.option_transformers.find(external::Extra) ==
629 it->second.option_transformers.end())
631 string const format = it->first;
632 string const opt = params_.extradata.get(format);
633 extraFormatCO->addItem(toqstr(format));
634 extra_[toqstr(format)] = toqstr(opt);
637 bool const enabled = extraFormatCO->count() > 0;
639 optionsGB->setEnabled(enabled);
640 extraED->setEnabled(enabled && !isBufferReadonly());
641 extraFormatCO->setEnabled(enabled);
644 extraFormatCO->setCurrentIndex(0);
645 extraED->setText(extra_[extraFormatCO->currentText()]);
650 void GuiExternal::applyView()
652 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
653 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
655 params_.draft = draftCB->isChecked();
656 params_.lyxscale = displayscaleED->text().toInt();
657 params_.display = display(*displayGB, *showCO);
659 if (rotationGB->isEnabled())
660 getRotation(params_.rotationdata, *angleED, *originCO);
662 if (scaleGB->isEnabled())
663 getSize(params_.resizedata, *widthED, *widthUnitCO,
664 *heightED, *heightUnitCO, *aspectratioCB);
666 if (cropGB->isEnabled())
667 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
668 *xrED, *ytED, bbChanged_);
670 if (optionsGB->isEnabled()) {
671 MapType::const_iterator it = extra_.begin();
672 MapType::const_iterator const end = extra_.end();
673 for (; it != end; ++it)
674 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
679 bool GuiExternal::initialiseParams(string const & data)
681 InsetExternal::string2params(data, buffer(), params_);
686 void GuiExternal::clearParams()
688 params_ = InsetExternalParams();
692 void GuiExternal::dispatchParams()
694 string const lfun = InsetExternal::params2string(params_, buffer());
695 dispatch(FuncRequest(getLfun(), lfun));
699 static QStringList templateFilters(QString const & template_name)
701 /// Determine the template file extension
702 external::TemplateManager const & etm =
703 external::TemplateManager::get();
704 external::Template const * const et_ptr =
705 etm.getTemplateByName(fromqstr(template_name));
707 return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
711 QString GuiExternal::browse(QString const & input,
712 QString const & template_name) const
714 QString const title = qt_("Select external file");
715 QString const bufpath = bufferFilepath();
716 QStringList const filter = templateFilters(template_name);
718 QString const label1 = qt_("Documents|#o#O");
719 QString const dir1 = toqstr(lyxrc.document_path);
721 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
725 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
728 } // namespace frontend
731 #include "GuiExternal_moc.cpp"