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"
18 #include "FuncRequest.h"
19 #include "support/gettext.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 "frontends/alert.h"
33 #include "support/convert.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/FileFilterList.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
38 #include "support/lyxlib.h"
39 #include "support/os.h"
41 #include "LengthCombo.h"
42 #include "qt_helpers.h"
43 #include "Validator.h"
47 #include <QPushButton>
49 #include <QTextBrowser>
52 using namespace lyx::support;
57 using namespace external;
61 RotationDataType origins_array[] = {
62 RotationData::DEFAULT,
63 RotationData::TOPLEFT,
64 RotationData::BOTTOMLEFT,
65 RotationData::BASELINELEFT,
67 RotationData::TOPCENTER,
68 RotationData::BOTTOMCENTER,
69 RotationData::BASELINECENTER,
70 RotationData::TOPRIGHT,
71 RotationData::BOTTOMRIGHT,
72 RotationData::BASELINERIGHT
76 size_type const origins_array_size =
77 sizeof(origins_array) / sizeof(origins_array[0]);
79 vector<external::RotationDataType> const
80 all_origins(origins_array, origins_array + origins_array_size);
82 // These are the strings, corresponding to the above, that the GUI should
83 // use. Note that they can/should be translated.
84 char const * const origin_gui_strs[] = {
86 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
87 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
88 N_("Top right"), N_("Bottom right"), N_("Baseline right")
94 GuiExternal::GuiExternal(GuiView & lv)
95 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
99 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
100 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
101 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
103 connect(displayCB, SIGNAL(toggled(bool)),
104 showCO, SLOT(setEnabled(bool)));
105 connect(displayCB, SIGNAL(toggled(bool)),
106 displayscaleED, SLOT(setEnabled(bool)));
107 connect(showCO, SIGNAL(activated(QString)),
108 this, SLOT(change_adaptor()));
109 connect(originCO, SIGNAL(activated(int)),
110 this, SLOT(change_adaptor()));
111 connect(aspectratioCB, SIGNAL(stateChanged(int)),
112 this, SLOT(change_adaptor()));
113 connect(browsePB, SIGNAL(clicked()),
114 this, SLOT(browseClicked()));
115 connect(embedCB, SIGNAL(toggled(bool)),
116 this, SLOT(change_adaptor()));
117 connect(editPB, SIGNAL(clicked()),
118 this, SLOT(editClicked()));
119 connect(externalCO, SIGNAL(activated(QString)),
120 this, SLOT(templateChanged()));
121 connect(extraED, SIGNAL(textChanged(QString)),
122 this, SLOT(extraChanged(QString)));
123 connect(extraFormatCO, SIGNAL(activated(QString)),
124 this, SLOT(formatChanged(QString)));
125 connect(widthUnitCO, SIGNAL(activated(int)),
126 this, SLOT(widthUnitChanged()));
127 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
128 this, SLOT(change_adaptor()));
129 connect(displayCB, SIGNAL(stateChanged(int)),
130 this, SLOT(change_adaptor()));
131 connect(displayscaleED, SIGNAL(textChanged(QString)),
132 this, SLOT(change_adaptor()));
133 connect(angleED, SIGNAL(textChanged(QString)),
134 this, SLOT(change_adaptor()));
135 connect(widthED, SIGNAL(textChanged(QString)),
136 this, SLOT(sizeChanged()));
137 connect(heightED, SIGNAL(textChanged(QString)),
138 this, SLOT(sizeChanged()));
139 connect(fileED, SIGNAL(textChanged(QString)),
140 this, SLOT(change_adaptor()));
141 connect(clipCB, SIGNAL(stateChanged(int)),
142 this, SLOT(change_adaptor()));
143 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
144 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
145 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
146 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
147 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
148 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
150 QIntValidator * validator = new QIntValidator(displayscaleED);
151 validator->setBottom(1);
152 displayscaleED->setValidator(validator);
154 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
156 xlED->setValidator(new QIntValidator(xlED));
157 ybED->setValidator(new QIntValidator(ybED));
158 xrED->setValidator(new QIntValidator(xrED));
159 ytED->setValidator(new QIntValidator(ytED));
161 widthED->setValidator(unsignedLengthValidator(widthED));
162 heightED->setValidator(unsignedLengthValidator(heightED));
164 setFocusProxy(fileED);
166 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
169 bc().setApply(applyPB);
170 bc().setCancel(closePB);
172 bc().addReadOnly(fileED);
173 bc().addReadOnly(browsePB);
174 bc().addReadOnly(editPB);
175 bc().addReadOnly(externalCO);
176 bc().addReadOnly(draftCB);
177 bc().addReadOnly(displayscaleED);
178 bc().addReadOnly(showCO);
179 bc().addReadOnly(displayCB);
180 bc().addReadOnly(angleED);
181 bc().addReadOnly(originCO);
182 bc().addReadOnly(heightUnitCO);
183 bc().addReadOnly(heightED);
184 bc().addReadOnly(aspectratioCB);
185 bc().addReadOnly(widthUnitCO);
186 bc().addReadOnly(widthED);
187 bc().addReadOnly(clipCB);
188 bc().addReadOnly(getbbPB);
189 bc().addReadOnly(ytED);
190 bc().addReadOnly(xlED);
191 bc().addReadOnly(xrED);
192 bc().addReadOnly(ybED);
193 bc().addReadOnly(extraFormatCO);
194 bc().addReadOnly(extraED);
196 bc().addCheckedLineEdit(angleED, angleLA);
197 bc().addCheckedLineEdit(displayscaleED, scaleLA);
198 bc().addCheckedLineEdit(heightED, heightLA);
199 bc().addCheckedLineEdit(widthED, widthLA);
200 bc().addCheckedLineEdit(xlED, lbLA);
201 bc().addCheckedLineEdit(ybED, lbLA);
202 bc().addCheckedLineEdit(xrED, rtLA);
203 bc().addCheckedLineEdit(ytED, rtLA);
204 bc().addCheckedLineEdit(fileED, fileLA);
206 vector<string> templates = getTemplates();
208 for (vector<string>::const_iterator cit = templates.begin();
209 cit != templates.end(); ++cit) {
210 externalCO->addItem(qt_(*cit));
213 // Fill the origins combo
214 for (size_t i = 0; i != all_origins.size(); ++i)
215 originCO->addItem(qt_(origin_gui_strs[i]));
217 // Fill the width combo
218 widthUnitCO->addItem(qt_("Scale%"));
219 for (int i = 0; i < num_units; i++)
220 widthUnitCO->addItem(qt_(unit_name_gui[i]));
224 bool GuiExternal::activateAspectratio() const
226 if (widthUnitCO->currentIndex() == 0)
229 string const wstr = fromqstr(widthED->text());
232 bool const wIsDbl = isStrDbl(wstr);
233 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
236 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
239 string const hstr = fromqstr(heightED->text());
242 bool const hIsDbl = isStrDbl(hstr);
243 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
245 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
252 void GuiExternal::bbChanged()
259 void GuiExternal::browseClicked()
261 int const choice = externalCO->currentIndex();
262 QString const template_name = toqstr(getTemplate(choice).lyxName);
263 QString const str = browse(fileED->text(), template_name);
264 if (!str.isEmpty()) {
265 fileED->setText(str);
271 void GuiExternal::change_adaptor()
277 void GuiExternal::editClicked()
284 void GuiExternal::extraChanged(const QString& text)
286 string const format = fromqstr(extraFormatCO->currentText());
287 extra_[format] = text;
292 void GuiExternal::formatChanged(const QString& format)
294 extraED->setText(extra_[fromqstr(format)]);
298 void GuiExternal::getbbClicked()
304 void GuiExternal::sizeChanged()
306 aspectratioCB->setEnabled(activateAspectratio());
311 void GuiExternal::templateChanged()
318 void GuiExternal::widthUnitChanged()
320 bool useHeight = (widthUnitCO->currentIndex() > 0);
323 widthED->setValidator(unsignedLengthValidator(widthED));
325 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
327 heightED->setEnabled(useHeight);
328 heightUnitCO->setEnabled(useHeight);
333 static Length::UNIT defaultUnit()
335 Length::UNIT default_unit = Length::CM;
336 switch (lyxrc.default_papersize) {
339 case PAPER_USEXECUTIVE:
340 default_unit = Length::IN;
349 static void setDisplay(
350 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
351 external::DisplayType display, unsigned int scale, bool read_only)
355 case external::DefaultDisplay:
358 case external::MonochromeDisplay:
361 case external::GrayscaleDisplay:
364 case external::ColorDisplay:
367 case external::PreviewDisplay:
370 case external::NoDisplay:
375 showCO.setCurrentIndex(item);
376 bool const no_display = display == external::NoDisplay;
377 showCO.setEnabled(!no_display && !read_only);
378 displayCB.setChecked(!no_display);
379 scaleED.setEnabled(!no_display && !read_only);
380 scaleED.setText(QString::number(scale));
384 static void getDisplay(external::DisplayType & display,
385 unsigned int & scale,
386 QCheckBox const & displayCB,
387 QComboBox const & showCO,
388 QLineEdit const & scaleED)
390 switch (showCO.currentIndex()) {
392 display = external::DefaultDisplay;
395 display = external::MonochromeDisplay;
398 display = external::GrayscaleDisplay;
401 display = external::ColorDisplay;
404 display = external::PreviewDisplay;
408 if (!displayCB.isChecked())
409 display = external::NoDisplay;
411 scale = scaleED.text().toInt();
415 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
416 external::RotationData const & data)
418 originCO.setCurrentIndex(int(data.origin()));
419 angleED.setText(toqstr(data.angle));
423 static void getRotation(external::RotationData & data,
424 QLineEdit const & angleED, QComboBox const & originCO)
426 typedef external::RotationData::OriginType OriginType;
428 data.origin(static_cast<OriginType>(originCO.currentIndex()));
429 data.angle = fromqstr(angleED.text());
433 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
434 QLineEdit & heightED, LengthCombo & heightUnitCO,
435 QCheckBox & aspectratioCB,
436 external::ResizeData const & data)
438 bool using_scale = data.usingScale();
439 string scale = data.scale;
440 if (data.no_resize()) {
441 // Everything is zero, so default to this!
447 widthED.setText(toqstr(scale));
448 widthUnitCO.setCurrentIndex(0);
450 widthED.setText(QString::number(data.width.value()));
451 // Because 'Scale' is position 0...
452 // Note also that width cannot be zero here, so
453 // we don't need to worry about the default unit.
454 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
457 string const h = data.height.zero() ? string() : data.height.asString();
458 Length::UNIT default_unit = data.width.zero() ?
459 defaultUnit() : data.width.unit();
460 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
462 heightED.setEnabled(!using_scale);
463 heightUnitCO.setEnabled(!using_scale);
465 aspectratioCB.setChecked(data.keepAspectRatio);
467 bool const disable_aspectRatio = using_scale ||
468 data.width.zero() || data.height.zero();
469 aspectratioCB.setEnabled(!disable_aspectRatio);
473 static void getSize(external::ResizeData & data,
474 QLineEdit const & widthED, QComboBox const & widthUnitCO,
475 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
476 QCheckBox const & aspectratioCB)
478 string const width = fromqstr(widthED.text());
480 if (widthUnitCO.currentIndex() > 0) {
481 // Subtract one, because scale is 0.
482 int const unit = widthUnitCO.currentIndex() - 1;
485 if (isValidLength(width, &w))
487 else if (isStrDbl(width))
488 data.width = Length(convert<double>(width),
489 static_cast<Length::UNIT>(unit));
491 data.width = Length();
493 data.scale = string();
496 // scaling instead of a width
498 data.width = Length();
501 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
503 data.keepAspectRatio = aspectratioCB.isChecked();
507 void setCrop(QCheckBox & clipCB,
508 QLineEdit & xlED, QLineEdit & ybED,
509 QLineEdit & xrED, QLineEdit & ytED,
510 external::ClipData const & data)
512 clipCB.setChecked(data.clip);
513 graphics::BoundingBox const & bbox = data.bbox;
514 xlED.setText(QString::number(bbox.xl));
515 ybED.setText(QString::number(bbox.yb));
516 xrED.setText(QString::number(bbox.xr));
517 ytED.setText(QString::number(bbox.yt));
521 static void getCrop(external::ClipData & data,
522 QCheckBox const & clipCB,
523 QLineEdit const & xlED, QLineEdit const & ybED,
524 QLineEdit const & xrED, QLineEdit const & ytED,
527 data.clip = clipCB.isChecked();
532 data.bbox.xl = xlED.text().toInt();
533 data.bbox.yb = ybED.text().toInt();
534 data.bbox.xr = xrED.text().toInt();
535 data.bbox.yt = ytED.text().toInt();
539 static void getExtra(external::ExtraData & data,
540 GuiExternal::MapType const & extra)
542 typedef GuiExternal::MapType MapType;
543 MapType::const_iterator it = extra.begin();
544 MapType::const_iterator const end = extra.end();
545 for (; it != end; ++it)
546 data.set(it->first, trim(fromqstr(it->second)));
550 void GuiExternal::updateContents()
552 tab->setCurrentIndex(0);
555 params_.filename.outputFilename(fromqstr(bufferFilepath()));
556 fileED->setText(toqstr(name));
557 embedCB->setCheckState(params_.filename.embedded() ? Qt::Checked : Qt::Unchecked);
559 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
562 draftCB->setChecked(params_.draft);
564 setDisplay(*displayCB, *showCO, *displayscaleED,
565 params_.display, params_.lyxscale, isBufferReadonly());
567 setRotation(*angleED, *originCO, params_.rotationdata);
569 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
570 *aspectratioCB, params_.resizedata);
572 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
573 bbChanged_ = !params_.clipdata.bbox.empty();
579 void GuiExternal::updateTemplate()
581 external::Template templ = getTemplate(externalCO->currentIndex());
582 externalTB->setPlainText(qt_(templ.helpText));
584 // Ascertain which (if any) transformations the template supports
585 // and disable tabs hosting unsupported transforms.
586 typedef vector<external::TransformID> TransformIDs;
587 TransformIDs const transformIds = templ.transformIds;
588 TransformIDs::const_iterator tr_begin = transformIds.begin();
589 TransformIDs::const_iterator const tr_end = transformIds.end();
591 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
592 tab->setTabEnabled(tab->indexOf(rotatetab), found);
593 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
594 tab->setTabEnabled(tab->indexOf(scaletab), found);
596 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
597 tab->setTabEnabled(tab->indexOf(croptab), found);
599 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
600 tab->setTabEnabled(tab->indexOf(optionstab), found);
605 // Ascertain whether the template has any formats supporting
606 // the 'Extra' option
609 extraFormatCO->clear();
611 external::Template::Formats::const_iterator it = templ.formats.begin();
612 external::Template::Formats::const_iterator end = templ.formats.end();
613 for (; it != end; ++it) {
614 if (it->second.option_transformers.find(external::Extra) ==
615 it->second.option_transformers.end())
617 string const format = it->first;
618 string const opt = params_.extradata.get(format);
619 extraFormatCO->addItem(toqstr(format));
620 extra_[format] = toqstr(opt);
623 bool const enabled = extraFormatCO->count() > 0;
626 tab->indexOf(optionstab), enabled);
627 extraED->setEnabled(enabled && !isBufferReadonly());
628 extraFormatCO->setEnabled(enabled);
631 extraFormatCO->setCurrentIndex(0);
632 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
637 void GuiExternal::applyView()
639 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
642 Buffer & buf = buffer();
643 EmbeddedFile file(fromqstr(fileED->text()), buf.filePath());
644 file.setEmbed(embedCB->checkState() == Qt::Checked);
645 // move file around if needed, an exception may be raised.
646 file.enable(buf.embedded(), &buf, true);
647 // if things are OK..., embed params_.filename
648 params_.filename.setEmbed(file.embedded());
649 } catch (ExceptionMessage const & message) {
650 Alert::error(message.title_, message.details_);
652 params_.filename.setEmbed(false);
655 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
657 params_.draft = draftCB->isChecked();
659 getDisplay(params_.display, params_.lyxscale,
663 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
664 getRotation(params_.rotationdata, *angleED, *originCO);
666 if (tab->isTabEnabled(tab->indexOf(scaletab)))
667 getSize(params_.resizedata, *widthED, *widthUnitCO,
668 *heightED, *heightUnitCO, *aspectratioCB);
670 if (tab->isTabEnabled(tab->indexOf(croptab)))
671 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
672 *xrED, *ytED, bbChanged_);
674 if (tab->isTabEnabled(tab->indexOf(optionstab)))
675 getExtra(params_.extradata, extra_);
679 void GuiExternal::getBB()
686 string const filename = fromqstr(fileED->text());
687 if (filename.empty())
690 string const bb = readBB(filename);
694 xlED->setText(toqstr(token(bb, ' ', 0)));
695 ybED->setText(toqstr(token(bb, ' ', 1)));
696 xrED->setText(toqstr(token(bb, ' ', 2)));
697 ytED->setText(toqstr(token(bb, ' ', 3)));
703 bool GuiExternal::initialiseParams(string const & data)
705 InsetExternalMailer::string2params(data, buffer(), params_);
710 void GuiExternal::clearParams()
712 params_ = InsetExternalParams();
716 void GuiExternal::dispatchParams()
718 string const lfun = InsetExternalMailer::params2string(params_, buffer());
719 dispatch(FuncRequest(getLfun(), lfun));
723 void GuiExternal::editExternal()
726 string const lfun = InsetExternalMailer::params2string(params_, buffer());
727 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
731 vector<string> const GuiExternal::getTemplates() const
733 vector<string> result;
735 external::TemplateManager::Templates::const_iterator i1, i2;
736 i1 = external::TemplateManager::get().getTemplates().begin();
737 i2 = external::TemplateManager::get().getTemplates().end();
739 for (; i1 != i2; ++i1) {
740 result.push_back(i1->second.lyxName);
746 int GuiExternal::getTemplateNumber(string const & name) const
748 external::TemplateManager::Templates::const_iterator i1, i2;
749 i1 = external::TemplateManager::get().getTemplates().begin();
750 i2 = external::TemplateManager::get().getTemplates().end();
751 for (int i = 0; i1 != i2; ++i1, ++i) {
752 if (i1->second.lyxName == name)
756 // we can get here if a LyX document has a template not installed
762 external::Template GuiExternal::getTemplate(int i) const
764 external::TemplateManager::Templates::const_iterator i1
765 = external::TemplateManager::get().getTemplates().begin();
774 GuiExternal::getTemplateFilters(string const & template_name) const
776 /// Determine the template file extension
777 external::TemplateManager const & etm =
778 external::TemplateManager::get();
779 external::Template const * const et_ptr =
780 etm.getTemplateByName(template_name);
783 return et_ptr->fileRegExp;
789 QString GuiExternal::browse(QString const & input,
790 QString const & template_name) const
792 QString const title = qt_("Select external file");
793 QString const bufpath = bufferFilepath();
794 FileFilterList const filter =
795 FileFilterList(from_utf8(getTemplateFilters(fromqstr(template_name))));
797 QString const label1 = qt_("Documents|#o#O");
798 QString const dir1 = toqstr(lyxrc.document_path);
800 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
804 string const GuiExternal::readBB(string const & file)
806 FileName const abs_file(makeAbsPath(file, fromqstr(bufferFilepath())));
808 // try to get it from the file, if possible. Zipped files are
809 // unzipped in the readBB_from_PSFile-Function
810 string const bb = readBB_from_PSFile(abs_file);
814 // we don't, so ask the Graphics Cache if it has loaded the file
818 graphics::Cache & gc = graphics::Cache::get();
819 if (gc.inCache(abs_file)) {
820 graphics::Image const * image = gc.item(abs_file)->image();
823 width = image->width();
824 height = image->height();
828 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
832 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
835 } // namespace frontend
838 #include "GuiExternal_moc.cpp"