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"), bbChanged_(false)
109 setViewTitle(_("External Material"));
111 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
112 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
113 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
115 connect(displayCB, SIGNAL(toggled(bool)),
116 showCO, SLOT(setEnabled(bool)));
117 connect(displayCB, SIGNAL(toggled(bool)),
118 displayscaleED, SLOT(setEnabled(bool)));
119 connect(showCO, SIGNAL(activated(QString)),
120 this, SLOT(change_adaptor()));
121 connect(originCO, SIGNAL(activated(int)),
122 this, SLOT(change_adaptor()));
123 connect(aspectratioCB, SIGNAL(stateChanged(int)),
124 this, SLOT(change_adaptor()));
125 connect(browsePB, SIGNAL(clicked()),
126 this, SLOT(browseClicked()));
127 connect(editPB, SIGNAL(clicked()),
128 this, SLOT(editClicked()));
129 connect(externalCO, SIGNAL(activated(QString)),
130 this, SLOT(templateChanged()));
131 connect(extraED, SIGNAL(textChanged(QString)),
132 this, SLOT(extraChanged(QString)));
133 connect(extraFormatCO, SIGNAL(activated(QString)),
134 this, SLOT(formatChanged(QString)));
135 connect(widthUnitCO, SIGNAL(activated(int)),
136 this, SLOT(widthUnitChanged()));
137 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138 this, SLOT(change_adaptor()));
139 connect(displayCB, SIGNAL(stateChanged(int)),
140 this, SLOT(change_adaptor()));
141 connect(displayscaleED, SIGNAL(textChanged(QString)),
142 this, SLOT(change_adaptor()));
143 connect(angleED, SIGNAL(textChanged(QString)),
144 this, SLOT(change_adaptor()));
145 connect(widthED, SIGNAL(textChanged(QString)),
146 this, SLOT(sizeChanged()));
147 connect(heightED, SIGNAL(textChanged(QString)),
148 this, SLOT(sizeChanged()));
149 connect(fileED, SIGNAL(textChanged(QString)),
150 this, SLOT(change_adaptor()));
151 connect(clipCB, SIGNAL(stateChanged(int)),
152 this, SLOT(change_adaptor()));
153 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
154 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
155 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
156 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
157 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
158 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
160 QIntValidator * validator = new QIntValidator(displayscaleED);
161 validator->setBottom(1);
162 displayscaleED->setValidator(validator);
164 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
166 xlED->setValidator(new QIntValidator(xlED));
167 ybED->setValidator(new QIntValidator(ybED));
168 xrED->setValidator(new QIntValidator(xrED));
169 ytED->setValidator(new QIntValidator(ytED));
171 widthED->setValidator(unsignedLengthValidator(widthED));
172 heightED->setValidator(unsignedLengthValidator(heightED));
174 setFocusProxy(fileED);
176 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
179 bc().setApply(applyPB);
180 bc().setCancel(closePB);
182 bc().addReadOnly(fileED);
183 bc().addReadOnly(browsePB);
184 bc().addReadOnly(editPB);
185 bc().addReadOnly(externalCO);
186 bc().addReadOnly(draftCB);
187 bc().addReadOnly(displayscaleED);
188 bc().addReadOnly(showCO);
189 bc().addReadOnly(displayCB);
190 bc().addReadOnly(angleED);
191 bc().addReadOnly(originCO);
192 bc().addReadOnly(heightUnitCO);
193 bc().addReadOnly(heightED);
194 bc().addReadOnly(aspectratioCB);
195 bc().addReadOnly(widthUnitCO);
196 bc().addReadOnly(widthED);
197 bc().addReadOnly(clipCB);
198 bc().addReadOnly(getbbPB);
199 bc().addReadOnly(ytED);
200 bc().addReadOnly(xlED);
201 bc().addReadOnly(xrED);
202 bc().addReadOnly(ybED);
203 bc().addReadOnly(extraFormatCO);
204 bc().addReadOnly(extraED);
206 bc().addCheckedLineEdit(angleED, angleLA);
207 bc().addCheckedLineEdit(displayscaleED, scaleLA);
208 bc().addCheckedLineEdit(heightED, heightLA);
209 bc().addCheckedLineEdit(widthED, widthLA);
210 bc().addCheckedLineEdit(xlED, lbLA);
211 bc().addCheckedLineEdit(ybED, lbLA);
212 bc().addCheckedLineEdit(xrED, rtLA);
213 bc().addCheckedLineEdit(ytED, rtLA);
214 bc().addCheckedLineEdit(fileED, fileLA);
216 std::vector<string> templates = getTemplates();
218 for (std::vector<string>::const_iterator cit = templates.begin();
219 cit != templates.end(); ++cit) {
220 externalCO->addItem(qt_(*cit));
223 // Fill the origins combo
224 for (size_t i = 0; i != all_origins.size(); ++i)
225 originCO->addItem(qt_(origin_gui_strs[i]));
227 // Fill the width combo
228 widthUnitCO->addItem(qt_("Scale%"));
229 for (int i = 0; i < num_units; i++)
230 widthUnitCO->addItem(qt_(unit_name_gui[i]));
234 bool GuiExternal::activateAspectratio() const
236 if (widthUnitCO->currentIndex() == 0)
239 string const wstr = fromqstr(widthED->text());
242 bool const wIsDbl = isStrDbl(wstr);
243 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
246 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
249 string const hstr = fromqstr(heightED->text());
252 bool const hIsDbl = isStrDbl(hstr);
253 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
255 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
262 void GuiExternal::bbChanged()
269 void GuiExternal::browseClicked()
271 int const choice = externalCO->currentIndex();
272 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
273 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
275 fileED->setText(toqstr(str));
281 void GuiExternal::change_adaptor()
287 void GuiExternal::closeEvent(QCloseEvent * e)
294 void GuiExternal::editClicked()
301 void GuiExternal::extraChanged(const QString& text)
303 std::string const format = fromqstr(extraFormatCO->currentText());
304 extra_[format] = text;
309 void GuiExternal::formatChanged(const QString& format)
311 extraED->setText(extra_[fromqstr(format)]);
315 void GuiExternal::getbbClicked()
321 void GuiExternal::sizeChanged()
323 aspectratioCB->setEnabled(activateAspectratio());
328 void GuiExternal::templateChanged()
335 void GuiExternal::widthUnitChanged()
337 bool useHeight = (widthUnitCO->currentIndex() > 0);
340 widthED->setValidator(unsignedLengthValidator(widthED));
342 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
344 heightED->setEnabled(useHeight);
345 heightUnitCO->setEnabled(useHeight);
350 static Length::UNIT defaultUnit()
352 Length::UNIT default_unit = Length::CM;
353 switch (lyxrc.default_papersize) {
356 case PAPER_USEXECUTIVE:
357 default_unit = Length::IN;
366 static void setDisplay(
367 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
368 external::DisplayType display, unsigned int scale, bool read_only)
372 case external::DefaultDisplay:
375 case external::MonochromeDisplay:
378 case external::GrayscaleDisplay:
381 case external::ColorDisplay:
384 case external::PreviewDisplay:
387 case external::NoDisplay:
392 showCO.setCurrentIndex(item);
393 bool const no_display = display == external::NoDisplay;
394 showCO.setEnabled(!no_display && !read_only);
395 displayCB.setChecked(!no_display);
396 scaleED.setEnabled(!no_display && !read_only);
397 scaleED.setText(QString::number(scale));
401 static void getDisplay(external::DisplayType & display,
402 unsigned int & scale,
403 QCheckBox const & displayCB,
404 QComboBox const & showCO,
405 QLineEdit const & scaleED)
407 switch (showCO.currentIndex()) {
409 display = external::DefaultDisplay;
412 display = external::MonochromeDisplay;
415 display = external::GrayscaleDisplay;
418 display = external::ColorDisplay;
421 display = external::PreviewDisplay;
425 if (!displayCB.isChecked())
426 display = external::NoDisplay;
428 scale = scaleED.text().toInt();
432 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
433 external::RotationData const & data)
435 originCO.setCurrentIndex(int(data.origin()));
436 angleED.setText(toqstr(data.angle));
440 static void getRotation(external::RotationData & data,
441 QLineEdit const & angleED, QComboBox const & originCO)
443 typedef external::RotationData::OriginType OriginType;
445 data.origin(static_cast<OriginType>(originCO.currentIndex()));
446 data.angle = fromqstr(angleED.text());
450 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
451 QLineEdit & heightED, LengthCombo & heightUnitCO,
452 QCheckBox & aspectratioCB,
453 external::ResizeData const & data)
455 bool using_scale = data.usingScale();
456 std::string scale = data.scale;
457 if (data.no_resize()) {
458 // Everything is zero, so default to this!
464 widthED.setText(toqstr(scale));
465 widthUnitCO.setCurrentIndex(0);
467 widthED.setText(QString::number(data.width.value()));
468 // Because 'Scale' is position 0...
469 // Note also that width cannot be zero here, so
470 // we don't need to worry about the default unit.
471 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
474 string const h = data.height.zero() ? string() : data.height.asString();
475 Length::UNIT default_unit = data.width.zero() ?
476 defaultUnit() : data.width.unit();
477 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
479 heightED.setEnabled(!using_scale);
480 heightUnitCO.setEnabled(!using_scale);
482 aspectratioCB.setChecked(data.keepAspectRatio);
484 bool const disable_aspectRatio = using_scale ||
485 data.width.zero() || data.height.zero();
486 aspectratioCB.setEnabled(!disable_aspectRatio);
490 static void getSize(external::ResizeData & data,
491 QLineEdit const & widthED, QComboBox const & widthUnitCO,
492 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
493 QCheckBox const & aspectratioCB)
495 string const width = fromqstr(widthED.text());
497 if (widthUnitCO.currentIndex() > 0) {
498 // Subtract one, because scale is 0.
499 int const unit = widthUnitCO.currentIndex() - 1;
502 if (isValidLength(width, &w))
504 else if (isStrDbl(width))
505 data.width = Length(convert<double>(width),
506 static_cast<Length::UNIT>(unit));
508 data.width = Length();
510 data.scale = string();
513 // scaling instead of a width
515 data.width = Length();
518 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
520 data.keepAspectRatio = aspectratioCB.isChecked();
524 void setCrop(QCheckBox & clipCB,
525 QLineEdit & xlED, QLineEdit & ybED,
526 QLineEdit & xrED, QLineEdit & ytED,
527 external::ClipData const & data)
529 clipCB.setChecked(data.clip);
530 graphics::BoundingBox const & bbox = data.bbox;
531 xlED.setText(QString::number(bbox.xl));
532 ybED.setText(QString::number(bbox.yb));
533 xrED.setText(QString::number(bbox.xr));
534 ytED.setText(QString::number(bbox.yt));
538 static void getCrop(external::ClipData & data,
539 QCheckBox const & clipCB,
540 QLineEdit const & xlED, QLineEdit const & ybED,
541 QLineEdit const & xrED, QLineEdit const & ytED,
544 data.clip = clipCB.isChecked();
549 data.bbox.xl = xlED.text().toInt();
550 data.bbox.yb = ybED.text().toInt();
551 data.bbox.xr = xrED.text().toInt();
552 data.bbox.yt = ytED.text().toInt();
556 static void getExtra(external::ExtraData & data,
557 GuiExternal::MapType const & extra)
559 typedef GuiExternal::MapType MapType;
560 MapType::const_iterator it = extra.begin();
561 MapType::const_iterator const end = extra.end();
562 for (; it != end; ++it)
563 data.set(it->first, trim(fromqstr(it->second)));
567 void GuiExternal::updateContents()
569 tab->setCurrentIndex(0);
572 params_.filename.outputFilename(bufferFilepath());
573 fileED->setText(toqstr(name));
575 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
578 draftCB->setChecked(params_.draft);
580 setDisplay(*displayCB, *showCO, *displayscaleED,
581 params_.display, params_.lyxscale, isBufferReadonly());
583 setRotation(*angleED, *originCO, params_.rotationdata);
585 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
586 *aspectratioCB, params_.resizedata);
588 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
589 bbChanged_ = !params_.clipdata.bbox.empty();
595 void GuiExternal::updateTemplate()
597 external::Template templ = getTemplate(externalCO->currentIndex());
598 externalTB->setPlainText(qt_(templ.helpText));
600 // Ascertain which (if any) transformations the template supports
601 // and disable tabs hosting unsupported transforms.
602 typedef vector<external::TransformID> TransformIDs;
603 TransformIDs const transformIds = templ.transformIds;
604 TransformIDs::const_iterator tr_begin = transformIds.begin();
605 TransformIDs::const_iterator const tr_end = transformIds.end();
607 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
608 tab->setTabEnabled(tab->indexOf(rotatetab), found);
609 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
610 tab->setTabEnabled(tab->indexOf(scaletab), found);
612 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
613 tab->setTabEnabled(tab->indexOf(croptab), found);
615 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
616 tab->setTabEnabled(tab->indexOf(optionstab), found);
621 // Ascertain whether the template has any formats supporting
622 // the 'Extra' option
625 extraFormatCO->clear();
627 external::Template::Formats::const_iterator it = templ.formats.begin();
628 external::Template::Formats::const_iterator end = templ.formats.end();
629 for (; it != end; ++it) {
630 if (it->second.option_transformers.find(external::Extra) ==
631 it->second.option_transformers.end())
633 string const format = it->first;
634 string const opt = params_.extradata.get(format);
635 extraFormatCO->addItem(toqstr(format));
636 extra_[format] = toqstr(opt);
639 bool const enabled = extraFormatCO->count() > 0;
642 tab->indexOf(optionstab), enabled);
643 extraED->setEnabled(enabled && !isBufferReadonly());
644 extraFormatCO->setEnabled(enabled);
647 extraFormatCO->setCurrentIndex(0);
648 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
653 void GuiExternal::applyView()
655 params_.filename.set(internal_path(fromqstr(fileED->text())),
656 controller().bufferFilepath());
658 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
660 params_.draft = draftCB->isChecked();
662 getDisplay(params_.display, params_.lyxscale,
666 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
667 getRotation(params_.rotationdata, *angleED, *originCO);
669 if (tab->isTabEnabled(tab->indexOf(scaletab)))
670 getSize(params_.resizedata, *widthED, *widthUnitCO,
671 *heightED, *heightUnitCO, *aspectratioCB);
673 if (tab->isTabEnabled(tab->indexOf(croptab)))
674 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
675 *xrED, *ytED, bbChanged_);
677 if (tab->isTabEnabled(tab->indexOf(optionstab)))
678 getExtra(params_.extradata, extra_);
682 void GuiExternal::getBB()
689 string const filename = fromqstr(fileED->text());
690 if (filename.empty())
693 string const bb = readBB(filename);
697 xlED->setText(toqstr(token(bb, ' ', 0)));
698 ybED->setText(toqstr(token(bb, ' ', 1)));
699 xrED->setText(toqstr(token(bb, ' ', 2)));
700 ytED->setText(toqstr(token(bb, ' ', 3)));
706 bool GuiExternal::initialiseParams(string const & data)
708 InsetExternalMailer::string2params(data, buffer(), params_);
713 void GuiExternal::clearParams()
715 params_ = InsetExternalParams();
719 void GuiExternal::dispatchParams()
721 string const lfun = InsetExternalMailer::params2string(params_, buffer());
722 dispatch(FuncRequest(getLfun(), lfun));
726 void GuiExternal::editExternal()
728 dialog().applyView();
729 string const lfun = InsetExternalMailer::params2string(params_, buffer());
730 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
734 vector<string> const GuiExternal::getTemplates() const
736 vector<string> result;
738 external::TemplateManager::Templates::const_iterator i1, i2;
739 i1 = external::TemplateManager::get().getTemplates().begin();
740 i2 = external::TemplateManager::get().getTemplates().end();
742 for (; i1 != i2; ++i1) {
743 result.push_back(i1->second.lyxName);
749 int GuiExternal::getTemplateNumber(string const & name) const
751 external::TemplateManager::Templates::const_iterator i1, i2;
752 i1 = external::TemplateManager::get().getTemplates().begin();
753 i2 = external::TemplateManager::get().getTemplates().end();
754 for (int i = 0; i1 != i2; ++i1, ++i) {
755 if (i1->second.lyxName == name)
759 // we can get here if a LyX document has a template not installed
765 external::Template GuiExternal::getTemplate(int i) const
767 external::TemplateManager::Templates::const_iterator i1
768 = external::TemplateManager::get().getTemplates().begin();
777 GuiExternal::getTemplateFilters(string const & template_name) const
779 /// Determine the template file extension
780 external::TemplateManager const & etm =
781 external::TemplateManager::get();
782 external::Template const * const et_ptr =
783 etm.getTemplateByName(template_name);
786 return et_ptr->fileRegExp;
792 docstring const GuiExternal::browse(docstring const & input,
793 docstring const & template_name) const
795 docstring const title = _("Select external file");
797 docstring const bufpath = lyx::from_utf8(bufferFilepath());
798 FileFilterList const filter =
799 FileFilterList(lyx::from_utf8(getTemplateFilters(lyx::to_utf8(template_name))));
801 std::pair<docstring, docstring> dir1(_("Documents|#o#O"),
802 lyx::from_utf8(lyxrc.document_path));
804 return browseRelFile(input, bufpath, title, filter, false, dir1);
808 string const GuiExternal::readBB(string const & file)
810 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
812 // try to get it from the file, if possible. Zipped files are
813 // unzipped in the readBB_from_PSFile-Function
814 string const bb = readBB_from_PSFile(abs_file);
818 // we don't, so ask the Graphics Cache if it has loaded the file
822 graphics::Cache & gc = graphics::Cache::get();
823 if (gc.inCache(abs_file)) {
824 graphics::Image const * image = gc.item(abs_file)->image();
827 width = image->getWidth();
828 height = image->getHeight();
832 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
836 Dialog * createGuiExternal(LyXView & lv) { return new GuiExternal(lv); }
839 } // namespace frontend
842 #include "GuiExternal_moc.cpp"