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"
41 #include <QCloseEvent>
44 #include <QPushButton>
46 #include <QTextBrowser>
49 using namespace lyx::support;
54 using namespace external;
58 RotationDataType origins_array[] = {
59 RotationData::DEFAULT,
60 RotationData::TOPLEFT,
61 RotationData::BOTTOMLEFT,
62 RotationData::BASELINELEFT,
64 RotationData::TOPCENTER,
65 RotationData::BOTTOMCENTER,
66 RotationData::BASELINECENTER,
67 RotationData::TOPRIGHT,
68 RotationData::BOTTOMRIGHT,
69 RotationData::BASELINERIGHT
73 size_type const origins_array_size =
74 sizeof(origins_array) / sizeof(origins_array[0]);
76 vector<external::RotationDataType> const
77 all_origins(origins_array, origins_array + origins_array_size);
79 // These are the strings, corresponding to the above, that the GUI should
80 // use. Note that they can/should be translated.
81 char const * const origin_gui_strs[] = {
83 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
84 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
85 N_("Top right"), N_("Bottom right"), N_("Baseline right")
91 GuiExternal::GuiExternal(GuiView & lv)
92 : GuiDialog(lv, "external"), bbChanged_(false)
95 setViewTitle(_("External Material"));
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(displayCB, SIGNAL(toggled(bool)),
102 showCO, SLOT(setEnabled(bool)));
103 connect(displayCB, 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(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 std::vector<string> templates = getTemplates();
204 for (std::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::closeEvent(QCloseEvent * e)
280 void GuiExternal::editClicked()
287 void GuiExternal::extraChanged(const QString& text)
289 std::string const format = fromqstr(extraFormatCO->currentText());
290 extra_[format] = text;
295 void GuiExternal::formatChanged(const QString& format)
297 extraED->setText(extra_[fromqstr(format)]);
301 void GuiExternal::getbbClicked()
307 void GuiExternal::sizeChanged()
309 aspectratioCB->setEnabled(activateAspectratio());
314 void GuiExternal::templateChanged()
321 void GuiExternal::widthUnitChanged()
323 bool useHeight = (widthUnitCO->currentIndex() > 0);
326 widthED->setValidator(unsignedLengthValidator(widthED));
328 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
330 heightED->setEnabled(useHeight);
331 heightUnitCO->setEnabled(useHeight);
336 static Length::UNIT defaultUnit()
338 Length::UNIT default_unit = Length::CM;
339 switch (lyxrc.default_papersize) {
342 case PAPER_USEXECUTIVE:
343 default_unit = Length::IN;
352 static void setDisplay(
353 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
354 external::DisplayType display, unsigned int scale, bool read_only)
358 case external::DefaultDisplay:
361 case external::MonochromeDisplay:
364 case external::GrayscaleDisplay:
367 case external::ColorDisplay:
370 case external::PreviewDisplay:
373 case external::NoDisplay:
378 showCO.setCurrentIndex(item);
379 bool const no_display = display == external::NoDisplay;
380 showCO.setEnabled(!no_display && !read_only);
381 displayCB.setChecked(!no_display);
382 scaleED.setEnabled(!no_display && !read_only);
383 scaleED.setText(QString::number(scale));
387 static void getDisplay(external::DisplayType & display,
388 unsigned int & scale,
389 QCheckBox const & displayCB,
390 QComboBox const & showCO,
391 QLineEdit const & scaleED)
393 switch (showCO.currentIndex()) {
395 display = external::DefaultDisplay;
398 display = external::MonochromeDisplay;
401 display = external::GrayscaleDisplay;
404 display = external::ColorDisplay;
407 display = external::PreviewDisplay;
411 if (!displayCB.isChecked())
412 display = external::NoDisplay;
414 scale = scaleED.text().toInt();
418 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
419 external::RotationData const & data)
421 originCO.setCurrentIndex(int(data.origin()));
422 angleED.setText(toqstr(data.angle));
426 static void getRotation(external::RotationData & data,
427 QLineEdit const & angleED, QComboBox const & originCO)
429 typedef external::RotationData::OriginType OriginType;
431 data.origin(static_cast<OriginType>(originCO.currentIndex()));
432 data.angle = fromqstr(angleED.text());
436 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
437 QLineEdit & heightED, LengthCombo & heightUnitCO,
438 QCheckBox & aspectratioCB,
439 external::ResizeData const & data)
441 bool using_scale = data.usingScale();
442 std::string scale = data.scale;
443 if (data.no_resize()) {
444 // Everything is zero, so default to this!
450 widthED.setText(toqstr(scale));
451 widthUnitCO.setCurrentIndex(0);
453 widthED.setText(QString::number(data.width.value()));
454 // Because 'Scale' is position 0...
455 // Note also that width cannot be zero here, so
456 // we don't need to worry about the default unit.
457 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
460 string const h = data.height.zero() ? string() : data.height.asString();
461 Length::UNIT default_unit = data.width.zero() ?
462 defaultUnit() : data.width.unit();
463 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
465 heightED.setEnabled(!using_scale);
466 heightUnitCO.setEnabled(!using_scale);
468 aspectratioCB.setChecked(data.keepAspectRatio);
470 bool const disable_aspectRatio = using_scale ||
471 data.width.zero() || data.height.zero();
472 aspectratioCB.setEnabled(!disable_aspectRatio);
476 static void getSize(external::ResizeData & data,
477 QLineEdit const & widthED, QComboBox const & widthUnitCO,
478 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
479 QCheckBox const & aspectratioCB)
481 string const width = fromqstr(widthED.text());
483 if (widthUnitCO.currentIndex() > 0) {
484 // Subtract one, because scale is 0.
485 int const unit = widthUnitCO.currentIndex() - 1;
488 if (isValidLength(width, &w))
490 else if (isStrDbl(width))
491 data.width = Length(convert<double>(width),
492 static_cast<Length::UNIT>(unit));
494 data.width = Length();
496 data.scale = string();
499 // scaling instead of a width
501 data.width = Length();
504 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
506 data.keepAspectRatio = aspectratioCB.isChecked();
510 void setCrop(QCheckBox & clipCB,
511 QLineEdit & xlED, QLineEdit & ybED,
512 QLineEdit & xrED, QLineEdit & ytED,
513 external::ClipData const & data)
515 clipCB.setChecked(data.clip);
516 graphics::BoundingBox const & bbox = data.bbox;
517 xlED.setText(QString::number(bbox.xl));
518 ybED.setText(QString::number(bbox.yb));
519 xrED.setText(QString::number(bbox.xr));
520 ytED.setText(QString::number(bbox.yt));
524 static void getCrop(external::ClipData & data,
525 QCheckBox const & clipCB,
526 QLineEdit const & xlED, QLineEdit const & ybED,
527 QLineEdit const & xrED, QLineEdit const & ytED,
530 data.clip = clipCB.isChecked();
535 data.bbox.xl = xlED.text().toInt();
536 data.bbox.yb = ybED.text().toInt();
537 data.bbox.xr = xrED.text().toInt();
538 data.bbox.yt = ytED.text().toInt();
542 static void getExtra(external::ExtraData & data,
543 GuiExternal::MapType const & extra)
545 typedef GuiExternal::MapType MapType;
546 MapType::const_iterator it = extra.begin();
547 MapType::const_iterator const end = extra.end();
548 for (; it != end; ++it)
549 data.set(it->first, trim(fromqstr(it->second)));
553 void GuiExternal::updateContents()
555 tab->setCurrentIndex(0);
558 params_.filename.outputFilename(bufferFilepath());
559 fileED->setText(toqstr(name));
561 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
564 draftCB->setChecked(params_.draft);
566 setDisplay(*displayCB, *showCO, *displayscaleED,
567 params_.display, params_.lyxscale, isBufferReadonly());
569 setRotation(*angleED, *originCO, params_.rotationdata);
571 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
572 *aspectratioCB, params_.resizedata);
574 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
575 bbChanged_ = !params_.clipdata.bbox.empty();
581 void GuiExternal::updateTemplate()
583 external::Template templ = getTemplate(externalCO->currentIndex());
584 externalTB->setPlainText(qt_(templ.helpText));
586 // Ascertain which (if any) transformations the template supports
587 // and disable tabs hosting unsupported transforms.
588 typedef vector<external::TransformID> TransformIDs;
589 TransformIDs const transformIds = templ.transformIds;
590 TransformIDs::const_iterator tr_begin = transformIds.begin();
591 TransformIDs::const_iterator const tr_end = transformIds.end();
593 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
594 tab->setTabEnabled(tab->indexOf(rotatetab), found);
595 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
596 tab->setTabEnabled(tab->indexOf(scaletab), found);
598 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
599 tab->setTabEnabled(tab->indexOf(croptab), found);
601 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
602 tab->setTabEnabled(tab->indexOf(optionstab), found);
607 // Ascertain whether the template has any formats supporting
608 // the 'Extra' option
611 extraFormatCO->clear();
613 external::Template::Formats::const_iterator it = templ.formats.begin();
614 external::Template::Formats::const_iterator end = templ.formats.end();
615 for (; it != end; ++it) {
616 if (it->second.option_transformers.find(external::Extra) ==
617 it->second.option_transformers.end())
619 string const format = it->first;
620 string const opt = params_.extradata.get(format);
621 extraFormatCO->addItem(toqstr(format));
622 extra_[format] = toqstr(opt);
625 bool const enabled = extraFormatCO->count() > 0;
628 tab->indexOf(optionstab), enabled);
629 extraED->setEnabled(enabled && !isBufferReadonly());
630 extraFormatCO->setEnabled(enabled);
633 extraFormatCO->setCurrentIndex(0);
634 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
639 void GuiExternal::applyView()
641 params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
643 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
645 params_.draft = draftCB->isChecked();
647 getDisplay(params_.display, params_.lyxscale,
651 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
652 getRotation(params_.rotationdata, *angleED, *originCO);
654 if (tab->isTabEnabled(tab->indexOf(scaletab)))
655 getSize(params_.resizedata, *widthED, *widthUnitCO,
656 *heightED, *heightUnitCO, *aspectratioCB);
658 if (tab->isTabEnabled(tab->indexOf(croptab)))
659 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
660 *xrED, *ytED, bbChanged_);
662 if (tab->isTabEnabled(tab->indexOf(optionstab)))
663 getExtra(params_.extradata, extra_);
667 void GuiExternal::getBB()
674 string const filename = fromqstr(fileED->text());
675 if (filename.empty())
678 string const bb = readBB(filename);
682 xlED->setText(toqstr(token(bb, ' ', 0)));
683 ybED->setText(toqstr(token(bb, ' ', 1)));
684 xrED->setText(toqstr(token(bb, ' ', 2)));
685 ytED->setText(toqstr(token(bb, ' ', 3)));
691 bool GuiExternal::initialiseParams(string const & data)
693 InsetExternalMailer::string2params(data, buffer(), params_);
698 void GuiExternal::clearParams()
700 params_ = InsetExternalParams();
704 void GuiExternal::dispatchParams()
706 string const lfun = InsetExternalMailer::params2string(params_, buffer());
707 dispatch(FuncRequest(getLfun(), lfun));
711 void GuiExternal::editExternal()
714 string const lfun = InsetExternalMailer::params2string(params_, buffer());
715 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
719 vector<string> const GuiExternal::getTemplates() const
721 vector<string> result;
723 external::TemplateManager::Templates::const_iterator i1, i2;
724 i1 = external::TemplateManager::get().getTemplates().begin();
725 i2 = external::TemplateManager::get().getTemplates().end();
727 for (; i1 != i2; ++i1) {
728 result.push_back(i1->second.lyxName);
734 int GuiExternal::getTemplateNumber(string const & name) const
736 external::TemplateManager::Templates::const_iterator i1, i2;
737 i1 = external::TemplateManager::get().getTemplates().begin();
738 i2 = external::TemplateManager::get().getTemplates().end();
739 for (int i = 0; i1 != i2; ++i1, ++i) {
740 if (i1->second.lyxName == name)
744 // we can get here if a LyX document has a template not installed
750 external::Template GuiExternal::getTemplate(int i) const
752 external::TemplateManager::Templates::const_iterator i1
753 = external::TemplateManager::get().getTemplates().begin();
762 GuiExternal::getTemplateFilters(string const & template_name) const
764 /// Determine the template file extension
765 external::TemplateManager const & etm =
766 external::TemplateManager::get();
767 external::Template const * const et_ptr =
768 etm.getTemplateByName(template_name);
771 return et_ptr->fileRegExp;
777 docstring const GuiExternal::browse(docstring const & input,
778 docstring const & template_name) const
780 docstring const title = _("Select external file");
782 docstring const bufpath = from_utf8(bufferFilepath());
783 FileFilterList const filter =
784 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
786 docstring const label1 = _("Documents|#o#O");
787 docstring const dir1 = from_utf8(lyxrc.document_path);
789 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
793 string const GuiExternal::readBB(string const & file)
795 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
797 // try to get it from the file, if possible. Zipped files are
798 // unzipped in the readBB_from_PSFile-Function
799 string const bb = readBB_from_PSFile(abs_file);
803 // we don't, so ask the Graphics Cache if it has loaded the file
807 graphics::Cache & gc = graphics::Cache::get();
808 if (gc.inCache(abs_file)) {
809 graphics::Image const * image = gc.item(abs_file)->image();
812 width = image->width();
813 height = image->height();
817 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
821 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
824 } // namespace frontend
827 #include "GuiExternal_moc.cpp"