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 "GuiExternal.h"
18 #include "frontend_helpers.h"
19 #include "FuncRequest.h"
21 #include "lengthcommon.h"
24 #include "insets/ExternalSupport.h"
25 #include "insets/ExternalTemplate.h"
26 #include "insets/InsetExternal.h"
28 #include "graphics/GraphicsCache.h"
29 #include "graphics/GraphicsCacheItem.h"
30 #include "graphics/GraphicsImage.h"
32 #include "support/convert.h"
33 #include "support/FileFilterList.h"
34 #include "support/filetools.h"
35 #include "support/lstrings.h"
36 #include "support/lyxlib.h"
37 #include "support/os.h"
39 #include "LengthCombo.h"
40 #include "qt_helpers.h"
41 #include "Validator.h"
43 #include <QCloseEvent>
46 #include <QPushButton>
48 #include <QTextBrowser>
58 using support::FileFilterList;
59 using support::FileName;
60 using support::float_equal;
61 using support::isStrDbl;
62 using support::makeAbsPath;
63 using support::readBB_from_PSFile;
66 using support::os::internal_path;
68 using namespace external;
72 RotationDataType origins_array[] = {
73 RotationData::DEFAULT,
74 RotationData::TOPLEFT,
75 RotationData::BOTTOMLEFT,
76 RotationData::BASELINELEFT,
78 RotationData::TOPCENTER,
79 RotationData::BOTTOMCENTER,
80 RotationData::BASELINECENTER,
81 RotationData::TOPRIGHT,
82 RotationData::BOTTOMRIGHT,
83 RotationData::BASELINERIGHT
87 size_type const origins_array_size =
88 sizeof(origins_array) / sizeof(origins_array[0]);
90 vector<external::RotationDataType> const
91 all_origins(origins_array, origins_array + origins_array_size);
93 // These are the strings, corresponding to the above, that the GUI should
94 // use. Note that they can/should be translated.
95 char const * const origin_gui_strs[] = {
97 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
98 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
99 N_("Top right"), N_("Bottom right"), N_("Baseline right")
105 GuiExternal::GuiExternal(LyXView & lv)
106 : GuiDialog(lv, "external"), Controller(this), bbChanged_(false)
109 setViewTitle(_("External Material"));
110 setController(this, false);
112 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
113 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
114 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
116 connect(displayCB, SIGNAL(toggled(bool)),
117 showCO, SLOT(setEnabled(bool)));
118 connect(displayCB, SIGNAL(toggled(bool)),
119 displayscaleED, SLOT(setEnabled(bool)));
120 connect(showCO, SIGNAL(activated(const QString&)),
121 this, SLOT(change_adaptor()));
122 connect(originCO, SIGNAL(activated(int)),
123 this, SLOT(change_adaptor()));
124 connect(aspectratioCB, SIGNAL(stateChanged(int)),
125 this, SLOT(change_adaptor()));
126 connect(browsePB, SIGNAL(clicked()),
127 this, SLOT(browseClicked()));
128 connect(editPB, SIGNAL(clicked()),
129 this, SLOT(editClicked()));
130 connect(externalCO, SIGNAL(activated(const QString &)),
131 this, SLOT(templateChanged()));
132 connect(extraED, SIGNAL(textChanged(const QString &)),
133 this, SLOT(extraChanged(const QString&)));
134 connect(extraFormatCO, SIGNAL(activated(const QString &)),
135 this, SLOT(formatChanged(const QString&)));
136 connect(widthUnitCO, SIGNAL(activated(int)),
137 this, SLOT(widthUnitChanged()));
138 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
139 this, SLOT(change_adaptor()));
140 connect(displayCB, SIGNAL(stateChanged(int)),
141 this, SLOT(change_adaptor()));
142 connect(displayscaleED, SIGNAL(textChanged(const QString &)),
143 this, SLOT(change_adaptor()));
144 connect(angleED, SIGNAL(textChanged(const QString &)),
145 this, SLOT(change_adaptor()));
146 connect(widthED, SIGNAL(textChanged(const QString &)),
147 this, SLOT(sizeChanged()));
148 connect(heightED, SIGNAL(textChanged(const QString &)),
149 this, SLOT(sizeChanged()));
150 connect(fileED, SIGNAL(textChanged(const QString &)),
151 this, SLOT(change_adaptor()));
152 connect(clipCB, SIGNAL(stateChanged(int)),
153 this, SLOT(change_adaptor()));
154 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
155 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
156 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
157 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
158 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
159 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
161 QIntValidator * validator = new QIntValidator(displayscaleED);
162 validator->setBottom(1);
163 displayscaleED->setValidator(validator);
165 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
167 xlED->setValidator(new QIntValidator(xlED));
168 ybED->setValidator(new QIntValidator(ybED));
169 xrED->setValidator(new QIntValidator(xrED));
170 ytED->setValidator(new QIntValidator(ytED));
172 widthED->setValidator(unsignedLengthValidator(widthED));
173 heightED->setValidator(unsignedLengthValidator(heightED));
175 setFocusProxy(fileED);
177 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
180 bc().setApply(applyPB);
181 bc().setCancel(closePB);
183 bc().addReadOnly(fileED);
184 bc().addReadOnly(browsePB);
185 bc().addReadOnly(editPB);
186 bc().addReadOnly(externalCO);
187 bc().addReadOnly(draftCB);
188 bc().addReadOnly(displayscaleED);
189 bc().addReadOnly(showCO);
190 bc().addReadOnly(displayCB);
191 bc().addReadOnly(angleED);
192 bc().addReadOnly(originCO);
193 bc().addReadOnly(heightUnitCO);
194 bc().addReadOnly(heightED);
195 bc().addReadOnly(aspectratioCB);
196 bc().addReadOnly(widthUnitCO);
197 bc().addReadOnly(widthED);
198 bc().addReadOnly(clipCB);
199 bc().addReadOnly(getbbPB);
200 bc().addReadOnly(ytED);
201 bc().addReadOnly(xlED);
202 bc().addReadOnly(xrED);
203 bc().addReadOnly(ybED);
204 bc().addReadOnly(extraFormatCO);
205 bc().addReadOnly(extraED);
207 bc().addCheckedLineEdit(angleED, angleLA);
208 bc().addCheckedLineEdit(displayscaleED, scaleLA);
209 bc().addCheckedLineEdit(heightED, heightLA);
210 bc().addCheckedLineEdit(widthED, widthLA);
211 bc().addCheckedLineEdit(xlED, lbLA);
212 bc().addCheckedLineEdit(ybED, lbLA);
213 bc().addCheckedLineEdit(xrED, rtLA);
214 bc().addCheckedLineEdit(ytED, rtLA);
215 bc().addCheckedLineEdit(fileED, fileLA);
217 std::vector<string> templates = getTemplates();
219 for (std::vector<string>::const_iterator cit = templates.begin();
220 cit != templates.end(); ++cit) {
221 externalCO->addItem(qt_(*cit));
224 // Fill the origins combo
225 for (size_t i = 0; i != all_origins.size(); ++i)
226 originCO->addItem(qt_(origin_gui_strs[i]));
228 // Fill the width combo
229 widthUnitCO->addItem(qt_("Scale%"));
230 for (int i = 0; i < num_units; i++)
231 widthUnitCO->addItem(qt_(unit_name_gui[i]));
235 bool GuiExternal::activateAspectratio() const
237 if (widthUnitCO->currentIndex() == 0)
240 string const wstr = fromqstr(widthED->text());
243 bool const wIsDbl = isStrDbl(wstr);
244 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
247 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
250 string const hstr = fromqstr(heightED->text());
253 bool const hIsDbl = isStrDbl(hstr);
254 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
256 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
263 void GuiExternal::bbChanged()
270 void GuiExternal::browseClicked()
272 int const choice = externalCO->currentIndex();
273 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
274 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
276 fileED->setText(toqstr(str));
282 void GuiExternal::change_adaptor()
288 void GuiExternal::closeEvent(QCloseEvent * e)
295 void GuiExternal::editClicked()
302 void GuiExternal::extraChanged(const QString& text)
304 std::string const format = fromqstr(extraFormatCO->currentText());
305 extra_[format] = text;
310 void GuiExternal::formatChanged(const QString& format)
312 extraED->setText(extra_[fromqstr(format)]);
316 void GuiExternal::getbbClicked()
322 void GuiExternal::sizeChanged()
324 aspectratioCB->setEnabled(activateAspectratio());
329 void GuiExternal::templateChanged()
336 void GuiExternal::widthUnitChanged()
338 bool useHeight = (widthUnitCO->currentIndex() > 0);
341 widthED->setValidator(unsignedLengthValidator(widthED));
343 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
345 heightED->setEnabled(useHeight);
346 heightUnitCO->setEnabled(useHeight);
351 static Length::UNIT defaultUnit()
353 Length::UNIT default_unit = Length::CM;
354 switch (lyxrc.default_papersize) {
357 case PAPER_USEXECUTIVE:
358 default_unit = Length::IN;
367 static void setDisplay(
368 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
369 external::DisplayType display, unsigned int scale, bool read_only)
373 case external::DefaultDisplay:
376 case external::MonochromeDisplay:
379 case external::GrayscaleDisplay:
382 case external::ColorDisplay:
385 case external::PreviewDisplay:
388 case external::NoDisplay:
393 showCO.setCurrentIndex(item);
394 bool const no_display = display == external::NoDisplay;
395 showCO.setEnabled(!no_display && !read_only);
396 displayCB.setChecked(!no_display);
397 scaleED.setEnabled(!no_display && !read_only);
398 scaleED.setText(QString::number(scale));
402 static void getDisplay(external::DisplayType & display,
403 unsigned int & scale,
404 QCheckBox const & displayCB,
405 QComboBox const & showCO,
406 QLineEdit const & scaleED)
408 switch (showCO.currentIndex()) {
410 display = external::DefaultDisplay;
413 display = external::MonochromeDisplay;
416 display = external::GrayscaleDisplay;
419 display = external::ColorDisplay;
422 display = external::PreviewDisplay;
426 if (!displayCB.isChecked())
427 display = external::NoDisplay;
429 scale = scaleED.text().toInt();
433 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
434 external::RotationData const & data)
436 originCO.setCurrentIndex(int(data.origin()));
437 angleED.setText(toqstr(data.angle));
441 static void getRotation(external::RotationData & data,
442 QLineEdit const & angleED, QComboBox const & originCO)
444 typedef external::RotationData::OriginType OriginType;
446 data.origin(static_cast<OriginType>(originCO.currentIndex()));
447 data.angle = fromqstr(angleED.text());
451 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
452 QLineEdit & heightED, LengthCombo & heightUnitCO,
453 QCheckBox & aspectratioCB,
454 external::ResizeData const & data)
456 bool using_scale = data.usingScale();
457 std::string scale = data.scale;
458 if (data.no_resize()) {
459 // Everything is zero, so default to this!
465 widthED.setText(toqstr(scale));
466 widthUnitCO.setCurrentIndex(0);
468 widthED.setText(QString::number(data.width.value()));
469 // Because 'Scale' is position 0...
470 // Note also that width cannot be zero here, so
471 // we don't need to worry about the default unit.
472 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
475 string const h = data.height.zero() ? string() : data.height.asString();
476 Length::UNIT default_unit = data.width.zero() ?
477 defaultUnit() : data.width.unit();
478 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
480 heightED.setEnabled(!using_scale);
481 heightUnitCO.setEnabled(!using_scale);
483 aspectratioCB.setChecked(data.keepAspectRatio);
485 bool const disable_aspectRatio = using_scale ||
486 data.width.zero() || data.height.zero();
487 aspectratioCB.setEnabled(!disable_aspectRatio);
491 static void getSize(external::ResizeData & data,
492 QLineEdit const & widthED, QComboBox const & widthUnitCO,
493 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
494 QCheckBox const & aspectratioCB)
496 string const width = fromqstr(widthED.text());
498 if (widthUnitCO.currentIndex() > 0) {
499 // Subtract one, because scale is 0.
500 int const unit = widthUnitCO.currentIndex() - 1;
503 if (isValidLength(width, &w))
505 else if (isStrDbl(width))
506 data.width = Length(convert<double>(width),
507 static_cast<Length::UNIT>(unit));
509 data.width = Length();
511 data.scale = string();
514 // scaling instead of a width
516 data.width = Length();
519 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
521 data.keepAspectRatio = aspectratioCB.isChecked();
525 void setCrop(QCheckBox & clipCB,
526 QLineEdit & xlED, QLineEdit & ybED,
527 QLineEdit & xrED, QLineEdit & ytED,
528 external::ClipData const & data)
530 clipCB.setChecked(data.clip);
531 graphics::BoundingBox const & bbox = data.bbox;
532 xlED.setText(QString::number(bbox.xl));
533 ybED.setText(QString::number(bbox.yb));
534 xrED.setText(QString::number(bbox.xr));
535 ytED.setText(QString::number(bbox.yt));
539 static void getCrop(external::ClipData & data,
540 QCheckBox const & clipCB,
541 QLineEdit const & xlED, QLineEdit const & ybED,
542 QLineEdit const & xrED, QLineEdit const & ytED,
545 data.clip = clipCB.isChecked();
550 data.bbox.xl = xlED.text().toInt();
551 data.bbox.yb = ybED.text().toInt();
552 data.bbox.xr = xrED.text().toInt();
553 data.bbox.yt = ytED.text().toInt();
557 static void getExtra(external::ExtraData & data,
558 GuiExternal::MapType const & extra)
560 typedef GuiExternal::MapType MapType;
561 MapType::const_iterator it = extra.begin();
562 MapType::const_iterator const end = extra.end();
563 for (; it != end; ++it)
564 data.set(it->first, trim(fromqstr(it->second)));
568 void GuiExternal::updateContents()
570 tab->setCurrentIndex(0);
573 params_.filename.outputFilename(bufferFilepath());
574 fileED->setText(toqstr(name));
576 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
579 draftCB->setChecked(params_.draft);
581 setDisplay(*displayCB, *showCO, *displayscaleED,
582 params_.display, params_.lyxscale, isBufferReadonly());
584 setRotation(*angleED, *originCO, params_.rotationdata);
586 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
587 *aspectratioCB, params_.resizedata);
589 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
590 bbChanged_ = !params_.clipdata.bbox.empty();
596 void GuiExternal::updateTemplate()
598 external::Template templ = getTemplate(externalCO->currentIndex());
599 externalTB->setPlainText(qt_(templ.helpText));
601 // Ascertain which (if any) transformations the template supports
602 // and disable tabs hosting unsupported transforms.
603 typedef vector<external::TransformID> TransformIDs;
604 TransformIDs const transformIds = templ.transformIds;
605 TransformIDs::const_iterator tr_begin = transformIds.begin();
606 TransformIDs::const_iterator const tr_end = transformIds.end();
608 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
609 tab->setTabEnabled(tab->indexOf(rotatetab), found);
610 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
611 tab->setTabEnabled(tab->indexOf(scaletab), found);
613 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
614 tab->setTabEnabled(tab->indexOf(croptab), found);
616 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
617 tab->setTabEnabled(tab->indexOf(optionstab), found);
622 // Ascertain whether the template has any formats supporting
623 // the 'Extra' option
626 extraFormatCO->clear();
628 external::Template::Formats::const_iterator it = templ.formats.begin();
629 external::Template::Formats::const_iterator end = templ.formats.end();
630 for (; it != end; ++it) {
631 if (it->second.option_transformers.find(external::Extra) ==
632 it->second.option_transformers.end())
634 string const format = it->first;
635 string const opt = params_.extradata.get(format);
636 extraFormatCO->addItem(toqstr(format));
637 extra_[format] = toqstr(opt);
640 bool const enabled = extraFormatCO->count() > 0;
643 tab->indexOf(optionstab), enabled);
644 extraED->setEnabled(enabled && !isBufferReadonly());
645 extraFormatCO->setEnabled(enabled);
648 extraFormatCO->setCurrentIndex(0);
649 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
654 void GuiExternal::applyView()
656 params_.filename.set(internal_path(fromqstr(fileED->text())),
657 controller().bufferFilepath());
659 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
661 params_.draft = draftCB->isChecked();
663 getDisplay(params_.display, params_.lyxscale,
667 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
668 getRotation(params_.rotationdata, *angleED, *originCO);
670 if (tab->isTabEnabled(tab->indexOf(scaletab)))
671 getSize(params_.resizedata, *widthED, *widthUnitCO,
672 *heightED, *heightUnitCO, *aspectratioCB);
674 if (tab->isTabEnabled(tab->indexOf(croptab)))
675 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
676 *xrED, *ytED, bbChanged_);
678 if (tab->isTabEnabled(tab->indexOf(optionstab)))
679 getExtra(params_.extradata, extra_);
683 void GuiExternal::getBB()
690 string const filename = fromqstr(fileED->text());
691 if (filename.empty())
694 string const bb = readBB(filename);
698 xlED->setText(toqstr(token(bb, ' ', 0)));
699 ybED->setText(toqstr(token(bb, ' ', 1)));
700 xrED->setText(toqstr(token(bb, ' ', 2)));
701 ytED->setText(toqstr(token(bb, ' ', 3)));
707 bool GuiExternal::initialiseParams(string const & data)
709 InsetExternalMailer::string2params(data, buffer(), params_);
714 void GuiExternal::clearParams()
716 params_ = InsetExternalParams();
720 void GuiExternal::dispatchParams()
722 string const lfun = InsetExternalMailer::params2string(params_, buffer());
723 dispatch(FuncRequest(getLfun(), lfun));
727 void GuiExternal::editExternal()
729 dialog().applyView();
730 string const lfun = InsetExternalMailer::params2string(params_, buffer());
731 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
735 vector<string> const GuiExternal::getTemplates() const
737 vector<string> result;
739 external::TemplateManager::Templates::const_iterator i1, i2;
740 i1 = external::TemplateManager::get().getTemplates().begin();
741 i2 = external::TemplateManager::get().getTemplates().end();
743 for (; i1 != i2; ++i1) {
744 result.push_back(i1->second.lyxName);
750 int GuiExternal::getTemplateNumber(string const & name) const
752 external::TemplateManager::Templates::const_iterator i1, i2;
753 i1 = external::TemplateManager::get().getTemplates().begin();
754 i2 = external::TemplateManager::get().getTemplates().end();
755 for (int i = 0; i1 != i2; ++i1, ++i) {
756 if (i1->second.lyxName == name)
760 // we can get here if a LyX document has a template not installed
766 external::Template GuiExternal::getTemplate(int i) const
768 external::TemplateManager::Templates::const_iterator i1
769 = external::TemplateManager::get().getTemplates().begin();
778 GuiExternal::getTemplateFilters(string const & template_name) const
780 /// Determine the template file extension
781 external::TemplateManager const & etm =
782 external::TemplateManager::get();
783 external::Template const * const et_ptr =
784 etm.getTemplateByName(template_name);
787 return et_ptr->fileRegExp;
793 docstring const GuiExternal::browse(docstring const & input,
794 docstring const & template_name) const
796 docstring const title = _("Select external file");
798 docstring const bufpath = lyx::from_utf8(bufferFilepath());
799 FileFilterList const filter =
800 FileFilterList(lyx::from_utf8(getTemplateFilters(lyx::to_utf8(template_name))));
802 std::pair<docstring, docstring> dir1(_("Documents|#o#O"),
803 lyx::from_utf8(lyxrc.document_path));
805 return browseRelFile(input, bufpath, title, filter, false, dir1);
809 string const GuiExternal::readBB(string const & file)
811 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
813 // try to get it from the file, if possible. Zipped files are
814 // unzipped in the readBB_from_PSFile-Function
815 string const bb = readBB_from_PSFile(abs_file);
819 // we don't, so ask the Graphics Cache if it has loaded the file
823 graphics::Cache & gc = graphics::Cache::get();
824 if (gc.inCache(abs_file)) {
825 graphics::Image const * image = gc.item(abs_file)->image();
828 width = image->getWidth();
829 height = image->getHeight();
833 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
837 Dialog * createGuiExternal(LyXView & lv) { return new GuiExternal(lv); }
840 } // namespace frontend
843 #include "GuiExternal_moc.cpp"