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/FileFilterList.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
37 #include "LengthCombo.h"
38 #include "qt_helpers.h"
39 #include "Validator.h"
43 #include <QPushButton>
45 #include <QTextBrowser>
48 using namespace lyx::support;
53 using namespace external;
57 RotationDataType origins_array[] = {
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 size_type const origins_array_size =
73 sizeof(origins_array) / sizeof(origins_array[0]);
75 vector<external::RotationDataType> const
76 all_origins(origins_array, origins_array + origins_array_size);
78 // These are the strings, corresponding to the above, that the GUI should
79 // use. Note that they can/should be translated.
80 char const * const origin_gui_strs[] = {
82 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
83 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
84 N_("Top right"), N_("Bottom right"), N_("Baseline right")
90 GuiExternal::GuiExternal(GuiView & lv)
91 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
95 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
96 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
97 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99 connect(displayCB, SIGNAL(toggled(bool)),
100 showCO, SLOT(setEnabled(bool)));
101 connect(displayCB, SIGNAL(toggled(bool)),
102 displayscaleED, SLOT(setEnabled(bool)));
103 connect(showCO, SIGNAL(activated(QString)),
104 this, SLOT(change_adaptor()));
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(embedCB, SIGNAL(toggled(bool)),
112 this, SLOT(change_adaptor()));
113 connect(editPB, SIGNAL(clicked()),
114 this, SLOT(editClicked()));
115 connect(externalCO, SIGNAL(activated(QString)),
116 this, SLOT(templateChanged()));
117 connect(extraED, SIGNAL(textChanged(QString)),
118 this, SLOT(extraChanged(QString)));
119 connect(extraFormatCO, SIGNAL(activated(QString)),
120 this, SLOT(formatChanged(QString)));
121 connect(widthUnitCO, SIGNAL(activated(int)),
122 this, SLOT(widthUnitChanged()));
123 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
124 this, SLOT(change_adaptor()));
125 connect(displayCB, SIGNAL(stateChanged(int)),
126 this, SLOT(change_adaptor()));
127 connect(displayscaleED, SIGNAL(textChanged(QString)),
128 this, SLOT(change_adaptor()));
129 connect(angleED, SIGNAL(textChanged(QString)),
130 this, SLOT(change_adaptor()));
131 connect(widthED, SIGNAL(textChanged(QString)),
132 this, SLOT(sizeChanged()));
133 connect(heightED, SIGNAL(textChanged(QString)),
134 this, SLOT(sizeChanged()));
135 connect(fileED, SIGNAL(textChanged(QString)),
136 this, SLOT(change_adaptor()));
137 connect(clipCB, SIGNAL(stateChanged(int)),
138 this, SLOT(change_adaptor()));
139 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
140 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
144 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
146 QIntValidator * validator = new QIntValidator(displayscaleED);
147 validator->setBottom(1);
148 displayscaleED->setValidator(validator);
150 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
152 xlED->setValidator(new QIntValidator(xlED));
153 ybED->setValidator(new QIntValidator(ybED));
154 xrED->setValidator(new QIntValidator(xrED));
155 ytED->setValidator(new QIntValidator(ytED));
157 widthED->setValidator(unsignedLengthValidator(widthED));
158 heightED->setValidator(unsignedLengthValidator(heightED));
160 setFocusProxy(fileED);
162 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
165 bc().setApply(applyPB);
166 bc().setCancel(closePB);
168 bc().addReadOnly(fileED);
169 bc().addReadOnly(browsePB);
170 bc().addReadOnly(editPB);
171 bc().addReadOnly(externalCO);
172 bc().addReadOnly(draftCB);
173 bc().addReadOnly(displayscaleED);
174 bc().addReadOnly(showCO);
175 bc().addReadOnly(displayCB);
176 bc().addReadOnly(angleED);
177 bc().addReadOnly(originCO);
178 bc().addReadOnly(heightUnitCO);
179 bc().addReadOnly(heightED);
180 bc().addReadOnly(aspectratioCB);
181 bc().addReadOnly(widthUnitCO);
182 bc().addReadOnly(widthED);
183 bc().addReadOnly(clipCB);
184 bc().addReadOnly(getbbPB);
185 bc().addReadOnly(ytED);
186 bc().addReadOnly(xlED);
187 bc().addReadOnly(xrED);
188 bc().addReadOnly(ybED);
189 bc().addReadOnly(extraFormatCO);
190 bc().addReadOnly(extraED);
192 bc().addCheckedLineEdit(angleED, angleLA);
193 bc().addCheckedLineEdit(displayscaleED, scaleLA);
194 bc().addCheckedLineEdit(heightED, heightLA);
195 bc().addCheckedLineEdit(widthED, widthLA);
196 bc().addCheckedLineEdit(xlED, lbLA);
197 bc().addCheckedLineEdit(ybED, lbLA);
198 bc().addCheckedLineEdit(xrED, rtLA);
199 bc().addCheckedLineEdit(ytED, rtLA);
200 bc().addCheckedLineEdit(fileED, fileLA);
202 vector<string> templates = getTemplates();
204 for (vector<string>::const_iterator cit = templates.begin();
205 cit != templates.end(); ++cit) {
206 externalCO->addItem(qt_(*cit));
209 // Fill the origins combo
210 for (size_t i = 0; i != all_origins.size(); ++i)
211 originCO->addItem(qt_(origin_gui_strs[i]));
213 // Fill the width combo
214 widthUnitCO->addItem(qt_("Scale%"));
215 for (int i = 0; i < num_units; i++)
216 widthUnitCO->addItem(qt_(unit_name_gui[i]));
220 bool GuiExternal::activateAspectratio() const
222 if (widthUnitCO->currentIndex() == 0)
225 string const wstr = fromqstr(widthED->text());
228 bool const wIsDbl = isStrDbl(wstr);
229 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
232 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
235 string const hstr = fromqstr(heightED->text());
238 bool const hIsDbl = isStrDbl(hstr);
239 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
241 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
248 void GuiExternal::bbChanged()
255 void GuiExternal::browseClicked()
257 int const choice = externalCO->currentIndex();
258 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
259 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
261 fileED->setText(toqstr(str));
267 void GuiExternal::change_adaptor()
273 void GuiExternal::editClicked()
280 void GuiExternal::extraChanged(const QString& text)
282 string const format = fromqstr(extraFormatCO->currentText());
283 extra_[format] = text;
288 void GuiExternal::formatChanged(const QString& format)
290 extraED->setText(extra_[fromqstr(format)]);
294 void GuiExternal::getbbClicked()
300 void GuiExternal::sizeChanged()
302 aspectratioCB->setEnabled(activateAspectratio());
307 void GuiExternal::templateChanged()
314 void GuiExternal::widthUnitChanged()
316 bool useHeight = (widthUnitCO->currentIndex() > 0);
319 widthED->setValidator(unsignedLengthValidator(widthED));
321 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
323 heightED->setEnabled(useHeight);
324 heightUnitCO->setEnabled(useHeight);
329 static Length::UNIT defaultUnit()
331 Length::UNIT default_unit = Length::CM;
332 switch (lyxrc.default_papersize) {
335 case PAPER_USEXECUTIVE:
336 default_unit = Length::IN;
345 static void setDisplay(
346 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
347 external::DisplayType display, unsigned int scale, bool read_only)
351 case external::DefaultDisplay:
354 case external::MonochromeDisplay:
357 case external::GrayscaleDisplay:
360 case external::ColorDisplay:
363 case external::PreviewDisplay:
366 case external::NoDisplay:
371 showCO.setCurrentIndex(item);
372 bool const no_display = display == external::NoDisplay;
373 showCO.setEnabled(!no_display && !read_only);
374 displayCB.setChecked(!no_display);
375 scaleED.setEnabled(!no_display && !read_only);
376 scaleED.setText(QString::number(scale));
380 static void getDisplay(external::DisplayType & display,
381 unsigned int & scale,
382 QCheckBox const & displayCB,
383 QComboBox const & showCO,
384 QLineEdit const & scaleED)
386 switch (showCO.currentIndex()) {
388 display = external::DefaultDisplay;
391 display = external::MonochromeDisplay;
394 display = external::GrayscaleDisplay;
397 display = external::ColorDisplay;
400 display = external::PreviewDisplay;
404 if (!displayCB.isChecked())
405 display = external::NoDisplay;
407 scale = scaleED.text().toInt();
411 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
412 external::RotationData const & data)
414 originCO.setCurrentIndex(int(data.origin()));
415 angleED.setText(toqstr(data.angle));
419 static void getRotation(external::RotationData & data,
420 QLineEdit const & angleED, QComboBox const & originCO)
422 typedef external::RotationData::OriginType OriginType;
424 data.origin(static_cast<OriginType>(originCO.currentIndex()));
425 data.angle = fromqstr(angleED.text());
429 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
430 QLineEdit & heightED, LengthCombo & heightUnitCO,
431 QCheckBox & aspectratioCB,
432 external::ResizeData const & data)
434 bool using_scale = data.usingScale();
435 string scale = data.scale;
436 if (data.no_resize()) {
437 // Everything is zero, so default to this!
443 widthED.setText(toqstr(scale));
444 widthUnitCO.setCurrentIndex(0);
446 widthED.setText(QString::number(data.width.value()));
447 // Because 'Scale' is position 0...
448 // Note also that width cannot be zero here, so
449 // we don't need to worry about the default unit.
450 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
453 string const h = data.height.zero() ? string() : data.height.asString();
454 Length::UNIT default_unit = data.width.zero() ?
455 defaultUnit() : data.width.unit();
456 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
458 heightED.setEnabled(!using_scale);
459 heightUnitCO.setEnabled(!using_scale);
461 aspectratioCB.setChecked(data.keepAspectRatio);
463 bool const disable_aspectRatio = using_scale ||
464 data.width.zero() || data.height.zero();
465 aspectratioCB.setEnabled(!disable_aspectRatio);
469 static void getSize(external::ResizeData & data,
470 QLineEdit const & widthED, QComboBox const & widthUnitCO,
471 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
472 QCheckBox const & aspectratioCB)
474 string const width = fromqstr(widthED.text());
476 if (widthUnitCO.currentIndex() > 0) {
477 // Subtract one, because scale is 0.
478 int const unit = widthUnitCO.currentIndex() - 1;
481 if (isValidLength(width, &w))
483 else if (isStrDbl(width))
484 data.width = Length(convert<double>(width),
485 static_cast<Length::UNIT>(unit));
487 data.width = Length();
489 data.scale = string();
492 // scaling instead of a width
494 data.width = Length();
497 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
499 data.keepAspectRatio = aspectratioCB.isChecked();
503 void setCrop(QCheckBox & clipCB,
504 QLineEdit & xlED, QLineEdit & ybED,
505 QLineEdit & xrED, QLineEdit & ytED,
506 external::ClipData const & data)
508 clipCB.setChecked(data.clip);
509 graphics::BoundingBox const & bbox = data.bbox;
510 xlED.setText(QString::number(bbox.xl));
511 ybED.setText(QString::number(bbox.yb));
512 xrED.setText(QString::number(bbox.xr));
513 ytED.setText(QString::number(bbox.yt));
517 static void getCrop(external::ClipData & data,
518 QCheckBox const & clipCB,
519 QLineEdit const & xlED, QLineEdit const & ybED,
520 QLineEdit const & xrED, QLineEdit const & ytED,
523 data.clip = clipCB.isChecked();
528 data.bbox.xl = xlED.text().toInt();
529 data.bbox.yb = ybED.text().toInt();
530 data.bbox.xr = xrED.text().toInt();
531 data.bbox.yt = ytED.text().toInt();
535 static void getExtra(external::ExtraData & data,
536 GuiExternal::MapType const & extra)
538 typedef GuiExternal::MapType MapType;
539 MapType::const_iterator it = extra.begin();
540 MapType::const_iterator const end = extra.end();
541 for (; it != end; ++it)
542 data.set(it->first, trim(fromqstr(it->second)));
546 void GuiExternal::updateContents()
548 tab->setCurrentIndex(0);
551 params_.filename.outputFilename(bufferFilepath());
552 fileED->setText(toqstr(name));
553 embedCB->setCheckState(params_.filename.embedded() ? Qt::Checked : Qt::Unchecked);
555 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
558 draftCB->setChecked(params_.draft);
560 setDisplay(*displayCB, *showCO, *displayscaleED,
561 params_.display, params_.lyxscale, isBufferReadonly());
563 setRotation(*angleED, *originCO, params_.rotationdata);
565 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
566 *aspectratioCB, params_.resizedata);
568 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
569 bbChanged_ = !params_.clipdata.bbox.empty();
575 void GuiExternal::updateTemplate()
577 external::Template templ = getTemplate(externalCO->currentIndex());
578 externalTB->setPlainText(qt_(templ.helpText));
580 // Ascertain which (if any) transformations the template supports
581 // and disable tabs hosting unsupported transforms.
582 typedef vector<external::TransformID> TransformIDs;
583 TransformIDs const transformIds = templ.transformIds;
584 TransformIDs::const_iterator tr_begin = transformIds.begin();
585 TransformIDs::const_iterator const tr_end = transformIds.end();
587 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
588 tab->setTabEnabled(tab->indexOf(rotatetab), found);
589 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
590 tab->setTabEnabled(tab->indexOf(scaletab), found);
592 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
593 tab->setTabEnabled(tab->indexOf(croptab), found);
595 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
596 tab->setTabEnabled(tab->indexOf(optionstab), found);
601 // Ascertain whether the template has any formats supporting
602 // the 'Extra' option
605 extraFormatCO->clear();
607 external::Template::Formats::const_iterator it = templ.formats.begin();
608 external::Template::Formats::const_iterator end = templ.formats.end();
609 for (; it != end; ++it) {
610 if (it->second.option_transformers.find(external::Extra) ==
611 it->second.option_transformers.end())
613 string const format = it->first;
614 string const opt = params_.extradata.get(format);
615 extraFormatCO->addItem(toqstr(format));
616 extra_[format] = toqstr(opt);
619 bool const enabled = extraFormatCO->count() > 0;
622 tab->indexOf(optionstab), enabled);
623 extraED->setEnabled(enabled && !isBufferReadonly());
624 extraFormatCO->setEnabled(enabled);
627 extraFormatCO->setCurrentIndex(0);
628 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
633 void GuiExternal::applyView()
635 params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
636 params_.filename.setEmbed(embedCB->checkState() == Qt::Checked);
638 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
640 params_.draft = draftCB->isChecked();
642 getDisplay(params_.display, params_.lyxscale,
646 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
647 getRotation(params_.rotationdata, *angleED, *originCO);
649 if (tab->isTabEnabled(tab->indexOf(scaletab)))
650 getSize(params_.resizedata, *widthED, *widthUnitCO,
651 *heightED, *heightUnitCO, *aspectratioCB);
653 if (tab->isTabEnabled(tab->indexOf(croptab)))
654 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
655 *xrED, *ytED, bbChanged_);
657 if (tab->isTabEnabled(tab->indexOf(optionstab)))
658 getExtra(params_.extradata, extra_);
662 void GuiExternal::getBB()
669 string const filename = fromqstr(fileED->text());
670 if (filename.empty())
673 string const bb = readBB(filename);
677 xlED->setText(toqstr(token(bb, ' ', 0)));
678 ybED->setText(toqstr(token(bb, ' ', 1)));
679 xrED->setText(toqstr(token(bb, ' ', 2)));
680 ytED->setText(toqstr(token(bb, ' ', 3)));
686 bool GuiExternal::initialiseParams(string const & data)
688 InsetExternalMailer::string2params(data, buffer(), params_);
693 void GuiExternal::clearParams()
695 params_ = InsetExternalParams();
699 void GuiExternal::dispatchParams()
701 string const lfun = InsetExternalMailer::params2string(params_, buffer());
702 dispatch(FuncRequest(getLfun(), lfun));
706 void GuiExternal::editExternal()
709 string const lfun = InsetExternalMailer::params2string(params_, buffer());
710 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
714 vector<string> const GuiExternal::getTemplates() const
716 vector<string> result;
718 external::TemplateManager::Templates::const_iterator i1, i2;
719 i1 = external::TemplateManager::get().getTemplates().begin();
720 i2 = external::TemplateManager::get().getTemplates().end();
722 for (; i1 != i2; ++i1) {
723 result.push_back(i1->second.lyxName);
729 int GuiExternal::getTemplateNumber(string const & name) const
731 external::TemplateManager::Templates::const_iterator i1, i2;
732 i1 = external::TemplateManager::get().getTemplates().begin();
733 i2 = external::TemplateManager::get().getTemplates().end();
734 for (int i = 0; i1 != i2; ++i1, ++i) {
735 if (i1->second.lyxName == name)
739 // we can get here if a LyX document has a template not installed
745 external::Template GuiExternal::getTemplate(int i) const
747 external::TemplateManager::Templates::const_iterator i1
748 = external::TemplateManager::get().getTemplates().begin();
757 GuiExternal::getTemplateFilters(string const & template_name) const
759 /// Determine the template file extension
760 external::TemplateManager const & etm =
761 external::TemplateManager::get();
762 external::Template const * const et_ptr =
763 etm.getTemplateByName(template_name);
766 return et_ptr->fileRegExp;
772 docstring const GuiExternal::browse(docstring const & input,
773 docstring const & template_name) const
775 docstring const title = _("Select external file");
777 docstring const bufpath = from_utf8(bufferFilepath());
778 FileFilterList const filter =
779 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
781 docstring const label1 = _("Documents|#o#O");
782 docstring const dir1 = from_utf8(lyxrc.document_path);
784 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
788 string const GuiExternal::readBB(string const & file)
790 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
792 // try to get it from the file, if possible. Zipped files are
793 // unzipped in the readBB_from_PSFile-Function
794 string const bb = readBB_from_PSFile(abs_file);
798 // we don't, so ask the Graphics Cache if it has loaded the file
802 graphics::Cache & gc = graphics::Cache::get();
803 if (gc.inCache(abs_file)) {
804 graphics::Image const * image = gc.item(abs_file)->image();
807 width = image->width();
808 height = image->height();
812 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
816 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
819 } // namespace frontend
822 #include "GuiExternal_moc.cpp"