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 "frontend_helpers.h"
18 #include "FuncRequest.h"
23 #include "insets/ExternalSupport.h"
24 #include "insets/ExternalTemplate.h"
25 #include "insets/InsetExternal.h"
27 #include "graphics/GraphicsCache.h"
28 #include "graphics/GraphicsCacheItem.h"
29 #include "graphics/GraphicsImage.h"
31 #include "support/convert.h"
32 #include "support/FileFilterList.h"
33 #include "support/filetools.h"
34 #include "support/lstrings.h"
35 #include "support/lyxlib.h"
36 #include "support/os.h"
38 #include "LengthCombo.h"
39 #include "qt_helpers.h"
40 #include "Validator.h"
42 #include <QCloseEvent>
45 #include <QPushButton>
47 #include <QTextBrowser>
57 using support::FileFilterList;
58 using support::FileName;
59 using support::float_equal;
60 using support::isStrDbl;
61 using support::makeAbsPath;
62 using support::readBB_from_PSFile;
65 using support::os::internal_path;
67 using namespace external;
71 RotationDataType origins_array[] = {
72 RotationData::DEFAULT,
73 RotationData::TOPLEFT,
74 RotationData::BOTTOMLEFT,
75 RotationData::BASELINELEFT,
77 RotationData::TOPCENTER,
78 RotationData::BOTTOMCENTER,
79 RotationData::BASELINECENTER,
80 RotationData::TOPRIGHT,
81 RotationData::BOTTOMRIGHT,
82 RotationData::BASELINERIGHT
86 size_type const origins_array_size =
87 sizeof(origins_array) / sizeof(origins_array[0]);
89 vector<external::RotationDataType> const
90 all_origins(origins_array, origins_array + origins_array_size);
92 // These are the strings, corresponding to the above, that the GUI should
93 // use. Note that they can/should be translated.
94 char const * const origin_gui_strs[] = {
96 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
97 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
98 N_("Top right"), N_("Bottom right"), N_("Baseline right")
104 GuiExternal::GuiExternal(LyXView & lv)
105 : GuiDialog(lv, "external"), bbChanged_(false)
108 setViewTitle(_("External Material"));
110 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
111 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
112 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
114 connect(displayCB, SIGNAL(toggled(bool)),
115 showCO, SLOT(setEnabled(bool)));
116 connect(displayCB, SIGNAL(toggled(bool)),
117 displayscaleED, SLOT(setEnabled(bool)));
118 connect(showCO, SIGNAL(activated(QString)),
119 this, SLOT(change_adaptor()));
120 connect(originCO, SIGNAL(activated(int)),
121 this, SLOT(change_adaptor()));
122 connect(aspectratioCB, SIGNAL(stateChanged(int)),
123 this, SLOT(change_adaptor()));
124 connect(browsePB, SIGNAL(clicked()),
125 this, SLOT(browseClicked()));
126 connect(editPB, SIGNAL(clicked()),
127 this, SLOT(editClicked()));
128 connect(externalCO, SIGNAL(activated(QString)),
129 this, SLOT(templateChanged()));
130 connect(extraED, SIGNAL(textChanged(QString)),
131 this, SLOT(extraChanged(QString)));
132 connect(extraFormatCO, SIGNAL(activated(QString)),
133 this, SLOT(formatChanged(QString)));
134 connect(widthUnitCO, SIGNAL(activated(int)),
135 this, SLOT(widthUnitChanged()));
136 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
137 this, SLOT(change_adaptor()));
138 connect(displayCB, SIGNAL(stateChanged(int)),
139 this, SLOT(change_adaptor()));
140 connect(displayscaleED, SIGNAL(textChanged(QString)),
141 this, SLOT(change_adaptor()));
142 connect(angleED, SIGNAL(textChanged(QString)),
143 this, SLOT(change_adaptor()));
144 connect(widthED, SIGNAL(textChanged(QString)),
145 this, SLOT(sizeChanged()));
146 connect(heightED, SIGNAL(textChanged(QString)),
147 this, SLOT(sizeChanged()));
148 connect(fileED, SIGNAL(textChanged(QString)),
149 this, SLOT(change_adaptor()));
150 connect(clipCB, SIGNAL(stateChanged(int)),
151 this, SLOT(change_adaptor()));
152 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
153 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
154 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
155 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
156 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
157 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
159 QIntValidator * validator = new QIntValidator(displayscaleED);
160 validator->setBottom(1);
161 displayscaleED->setValidator(validator);
163 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
165 xlED->setValidator(new QIntValidator(xlED));
166 ybED->setValidator(new QIntValidator(ybED));
167 xrED->setValidator(new QIntValidator(xrED));
168 ytED->setValidator(new QIntValidator(ytED));
170 widthED->setValidator(unsignedLengthValidator(widthED));
171 heightED->setValidator(unsignedLengthValidator(heightED));
173 setFocusProxy(fileED);
175 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
178 bc().setApply(applyPB);
179 bc().setCancel(closePB);
181 bc().addReadOnly(fileED);
182 bc().addReadOnly(browsePB);
183 bc().addReadOnly(editPB);
184 bc().addReadOnly(externalCO);
185 bc().addReadOnly(draftCB);
186 bc().addReadOnly(displayscaleED);
187 bc().addReadOnly(showCO);
188 bc().addReadOnly(displayCB);
189 bc().addReadOnly(angleED);
190 bc().addReadOnly(originCO);
191 bc().addReadOnly(heightUnitCO);
192 bc().addReadOnly(heightED);
193 bc().addReadOnly(aspectratioCB);
194 bc().addReadOnly(widthUnitCO);
195 bc().addReadOnly(widthED);
196 bc().addReadOnly(clipCB);
197 bc().addReadOnly(getbbPB);
198 bc().addReadOnly(ytED);
199 bc().addReadOnly(xlED);
200 bc().addReadOnly(xrED);
201 bc().addReadOnly(ybED);
202 bc().addReadOnly(extraFormatCO);
203 bc().addReadOnly(extraED);
205 bc().addCheckedLineEdit(angleED, angleLA);
206 bc().addCheckedLineEdit(displayscaleED, scaleLA);
207 bc().addCheckedLineEdit(heightED, heightLA);
208 bc().addCheckedLineEdit(widthED, widthLA);
209 bc().addCheckedLineEdit(xlED, lbLA);
210 bc().addCheckedLineEdit(ybED, lbLA);
211 bc().addCheckedLineEdit(xrED, rtLA);
212 bc().addCheckedLineEdit(ytED, rtLA);
213 bc().addCheckedLineEdit(fileED, fileLA);
215 std::vector<string> templates = getTemplates();
217 for (std::vector<string>::const_iterator cit = templates.begin();
218 cit != templates.end(); ++cit) {
219 externalCO->addItem(qt_(*cit));
222 // Fill the origins combo
223 for (size_t i = 0; i != all_origins.size(); ++i)
224 originCO->addItem(qt_(origin_gui_strs[i]));
226 // Fill the width combo
227 widthUnitCO->addItem(qt_("Scale%"));
228 for (int i = 0; i < num_units; i++)
229 widthUnitCO->addItem(qt_(unit_name_gui[i]));
233 bool GuiExternal::activateAspectratio() const
235 if (widthUnitCO->currentIndex() == 0)
238 string const wstr = fromqstr(widthED->text());
241 bool const wIsDbl = isStrDbl(wstr);
242 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
245 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
248 string const hstr = fromqstr(heightED->text());
251 bool const hIsDbl = isStrDbl(hstr);
252 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
254 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
261 void GuiExternal::bbChanged()
268 void GuiExternal::browseClicked()
270 int const choice = externalCO->currentIndex();
271 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
272 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
274 fileED->setText(toqstr(str));
280 void GuiExternal::change_adaptor()
286 void GuiExternal::closeEvent(QCloseEvent * e)
293 void GuiExternal::editClicked()
300 void GuiExternal::extraChanged(const QString& text)
302 std::string const format = fromqstr(extraFormatCO->currentText());
303 extra_[format] = text;
308 void GuiExternal::formatChanged(const QString& format)
310 extraED->setText(extra_[fromqstr(format)]);
314 void GuiExternal::getbbClicked()
320 void GuiExternal::sizeChanged()
322 aspectratioCB->setEnabled(activateAspectratio());
327 void GuiExternal::templateChanged()
334 void GuiExternal::widthUnitChanged()
336 bool useHeight = (widthUnitCO->currentIndex() > 0);
339 widthED->setValidator(unsignedLengthValidator(widthED));
341 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
343 heightED->setEnabled(useHeight);
344 heightUnitCO->setEnabled(useHeight);
349 static Length::UNIT defaultUnit()
351 Length::UNIT default_unit = Length::CM;
352 switch (lyxrc.default_papersize) {
355 case PAPER_USEXECUTIVE:
356 default_unit = Length::IN;
365 static void setDisplay(
366 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
367 external::DisplayType display, unsigned int scale, bool read_only)
371 case external::DefaultDisplay:
374 case external::MonochromeDisplay:
377 case external::GrayscaleDisplay:
380 case external::ColorDisplay:
383 case external::PreviewDisplay:
386 case external::NoDisplay:
391 showCO.setCurrentIndex(item);
392 bool const no_display = display == external::NoDisplay;
393 showCO.setEnabled(!no_display && !read_only);
394 displayCB.setChecked(!no_display);
395 scaleED.setEnabled(!no_display && !read_only);
396 scaleED.setText(QString::number(scale));
400 static void getDisplay(external::DisplayType & display,
401 unsigned int & scale,
402 QCheckBox const & displayCB,
403 QComboBox const & showCO,
404 QLineEdit const & scaleED)
406 switch (showCO.currentIndex()) {
408 display = external::DefaultDisplay;
411 display = external::MonochromeDisplay;
414 display = external::GrayscaleDisplay;
417 display = external::ColorDisplay;
420 display = external::PreviewDisplay;
424 if (!displayCB.isChecked())
425 display = external::NoDisplay;
427 scale = scaleED.text().toInt();
431 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
432 external::RotationData const & data)
434 originCO.setCurrentIndex(int(data.origin()));
435 angleED.setText(toqstr(data.angle));
439 static void getRotation(external::RotationData & data,
440 QLineEdit const & angleED, QComboBox const & originCO)
442 typedef external::RotationData::OriginType OriginType;
444 data.origin(static_cast<OriginType>(originCO.currentIndex()));
445 data.angle = fromqstr(angleED.text());
449 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
450 QLineEdit & heightED, LengthCombo & heightUnitCO,
451 QCheckBox & aspectratioCB,
452 external::ResizeData const & data)
454 bool using_scale = data.usingScale();
455 std::string scale = data.scale;
456 if (data.no_resize()) {
457 // Everything is zero, so default to this!
463 widthED.setText(toqstr(scale));
464 widthUnitCO.setCurrentIndex(0);
466 widthED.setText(QString::number(data.width.value()));
467 // Because 'Scale' is position 0...
468 // Note also that width cannot be zero here, so
469 // we don't need to worry about the default unit.
470 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
473 string const h = data.height.zero() ? string() : data.height.asString();
474 Length::UNIT default_unit = data.width.zero() ?
475 defaultUnit() : data.width.unit();
476 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
478 heightED.setEnabled(!using_scale);
479 heightUnitCO.setEnabled(!using_scale);
481 aspectratioCB.setChecked(data.keepAspectRatio);
483 bool const disable_aspectRatio = using_scale ||
484 data.width.zero() || data.height.zero();
485 aspectratioCB.setEnabled(!disable_aspectRatio);
489 static void getSize(external::ResizeData & data,
490 QLineEdit const & widthED, QComboBox const & widthUnitCO,
491 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
492 QCheckBox const & aspectratioCB)
494 string const width = fromqstr(widthED.text());
496 if (widthUnitCO.currentIndex() > 0) {
497 // Subtract one, because scale is 0.
498 int const unit = widthUnitCO.currentIndex() - 1;
501 if (isValidLength(width, &w))
503 else if (isStrDbl(width))
504 data.width = Length(convert<double>(width),
505 static_cast<Length::UNIT>(unit));
507 data.width = Length();
509 data.scale = string();
512 // scaling instead of a width
514 data.width = Length();
517 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
519 data.keepAspectRatio = aspectratioCB.isChecked();
523 void setCrop(QCheckBox & clipCB,
524 QLineEdit & xlED, QLineEdit & ybED,
525 QLineEdit & xrED, QLineEdit & ytED,
526 external::ClipData const & data)
528 clipCB.setChecked(data.clip);
529 graphics::BoundingBox const & bbox = data.bbox;
530 xlED.setText(QString::number(bbox.xl));
531 ybED.setText(QString::number(bbox.yb));
532 xrED.setText(QString::number(bbox.xr));
533 ytED.setText(QString::number(bbox.yt));
537 static void getCrop(external::ClipData & data,
538 QCheckBox const & clipCB,
539 QLineEdit const & xlED, QLineEdit const & ybED,
540 QLineEdit const & xrED, QLineEdit const & ytED,
543 data.clip = clipCB.isChecked();
548 data.bbox.xl = xlED.text().toInt();
549 data.bbox.yb = ybED.text().toInt();
550 data.bbox.xr = xrED.text().toInt();
551 data.bbox.yt = ytED.text().toInt();
555 static void getExtra(external::ExtraData & data,
556 GuiExternal::MapType const & extra)
558 typedef GuiExternal::MapType MapType;
559 MapType::const_iterator it = extra.begin();
560 MapType::const_iterator const end = extra.end();
561 for (; it != end; ++it)
562 data.set(it->first, trim(fromqstr(it->second)));
566 void GuiExternal::updateContents()
568 tab->setCurrentIndex(0);
571 params_.filename.outputFilename(bufferFilepath());
572 fileED->setText(toqstr(name));
574 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
577 draftCB->setChecked(params_.draft);
579 setDisplay(*displayCB, *showCO, *displayscaleED,
580 params_.display, params_.lyxscale, isBufferReadonly());
582 setRotation(*angleED, *originCO, params_.rotationdata);
584 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
585 *aspectratioCB, params_.resizedata);
587 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
588 bbChanged_ = !params_.clipdata.bbox.empty();
594 void GuiExternal::updateTemplate()
596 external::Template templ = getTemplate(externalCO->currentIndex());
597 externalTB->setPlainText(qt_(templ.helpText));
599 // Ascertain which (if any) transformations the template supports
600 // and disable tabs hosting unsupported transforms.
601 typedef vector<external::TransformID> TransformIDs;
602 TransformIDs const transformIds = templ.transformIds;
603 TransformIDs::const_iterator tr_begin = transformIds.begin();
604 TransformIDs::const_iterator const tr_end = transformIds.end();
606 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
607 tab->setTabEnabled(tab->indexOf(rotatetab), found);
608 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
609 tab->setTabEnabled(tab->indexOf(scaletab), found);
611 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
612 tab->setTabEnabled(tab->indexOf(croptab), found);
614 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
615 tab->setTabEnabled(tab->indexOf(optionstab), found);
620 // Ascertain whether the template has any formats supporting
621 // the 'Extra' option
624 extraFormatCO->clear();
626 external::Template::Formats::const_iterator it = templ.formats.begin();
627 external::Template::Formats::const_iterator end = templ.formats.end();
628 for (; it != end; ++it) {
629 if (it->second.option_transformers.find(external::Extra) ==
630 it->second.option_transformers.end())
632 string const format = it->first;
633 string const opt = params_.extradata.get(format);
634 extraFormatCO->addItem(toqstr(format));
635 extra_[format] = toqstr(opt);
638 bool const enabled = extraFormatCO->count() > 0;
641 tab->indexOf(optionstab), enabled);
642 extraED->setEnabled(enabled && !isBufferReadonly());
643 extraFormatCO->setEnabled(enabled);
646 extraFormatCO->setCurrentIndex(0);
647 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
652 void GuiExternal::applyView()
654 params_.filename.set(internal_path(fromqstr(fileED->text())),
657 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
659 params_.draft = draftCB->isChecked();
661 getDisplay(params_.display, params_.lyxscale,
665 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
666 getRotation(params_.rotationdata, *angleED, *originCO);
668 if (tab->isTabEnabled(tab->indexOf(scaletab)))
669 getSize(params_.resizedata, *widthED, *widthUnitCO,
670 *heightED, *heightUnitCO, *aspectratioCB);
672 if (tab->isTabEnabled(tab->indexOf(croptab)))
673 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
674 *xrED, *ytED, bbChanged_);
676 if (tab->isTabEnabled(tab->indexOf(optionstab)))
677 getExtra(params_.extradata, extra_);
681 void GuiExternal::getBB()
688 string const filename = fromqstr(fileED->text());
689 if (filename.empty())
692 string const bb = readBB(filename);
696 xlED->setText(toqstr(token(bb, ' ', 0)));
697 ybED->setText(toqstr(token(bb, ' ', 1)));
698 xrED->setText(toqstr(token(bb, ' ', 2)));
699 ytED->setText(toqstr(token(bb, ' ', 3)));
705 bool GuiExternal::initialiseParams(string const & data)
707 InsetExternalMailer::string2params(data, buffer(), params_);
712 void GuiExternal::clearParams()
714 params_ = InsetExternalParams();
718 void GuiExternal::dispatchParams()
720 string const lfun = InsetExternalMailer::params2string(params_, buffer());
721 dispatch(FuncRequest(getLfun(), lfun));
725 void GuiExternal::editExternal()
728 string const lfun = InsetExternalMailer::params2string(params_, buffer());
729 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
733 vector<string> const GuiExternal::getTemplates() const
735 vector<string> result;
737 external::TemplateManager::Templates::const_iterator i1, i2;
738 i1 = external::TemplateManager::get().getTemplates().begin();
739 i2 = external::TemplateManager::get().getTemplates().end();
741 for (; i1 != i2; ++i1) {
742 result.push_back(i1->second.lyxName);
748 int GuiExternal::getTemplateNumber(string const & name) const
750 external::TemplateManager::Templates::const_iterator i1, i2;
751 i1 = external::TemplateManager::get().getTemplates().begin();
752 i2 = external::TemplateManager::get().getTemplates().end();
753 for (int i = 0; i1 != i2; ++i1, ++i) {
754 if (i1->second.lyxName == name)
758 // we can get here if a LyX document has a template not installed
764 external::Template GuiExternal::getTemplate(int i) const
766 external::TemplateManager::Templates::const_iterator i1
767 = external::TemplateManager::get().getTemplates().begin();
776 GuiExternal::getTemplateFilters(string const & template_name) const
778 /// Determine the template file extension
779 external::TemplateManager const & etm =
780 external::TemplateManager::get();
781 external::Template const * const et_ptr =
782 etm.getTemplateByName(template_name);
785 return et_ptr->fileRegExp;
791 docstring const GuiExternal::browse(docstring const & input,
792 docstring const & template_name) const
794 docstring const title = _("Select external file");
796 docstring const bufpath = lyx::from_utf8(bufferFilepath());
797 FileFilterList const filter =
798 FileFilterList(lyx::from_utf8(getTemplateFilters(lyx::to_utf8(template_name))));
800 std::pair<docstring, docstring> dir1(_("Documents|#o#O"),
801 lyx::from_utf8(lyxrc.document_path));
803 return browseRelFile(input, bufpath, title, filter, false, dir1);
807 string const GuiExternal::readBB(string const & file)
809 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
811 // try to get it from the file, if possible. Zipped files are
812 // unzipped in the readBB_from_PSFile-Function
813 string const bb = readBB_from_PSFile(abs_file);
817 // we don't, so ask the Graphics Cache if it has loaded the file
821 graphics::Cache & gc = graphics::Cache::get();
822 if (gc.inCache(abs_file)) {
823 graphics::Image const * image = gc.item(abs_file)->image();
826 width = image->getWidth();
827 height = image->getHeight();
831 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
835 Dialog * createGuiExternal(LyXView & lv) { return new GuiExternal(lv); }
838 } // namespace frontend
841 #include "GuiExternal_moc.cpp"