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", qt_("External Material")), bbChanged_(false)
96 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
97 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
98 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
100 connect(displayCB, SIGNAL(toggled(bool)),
101 showCO, SLOT(setEnabled(bool)));
102 connect(displayCB, SIGNAL(toggled(bool)),
103 displayscaleED, SLOT(setEnabled(bool)));
104 connect(showCO, SIGNAL(activated(QString)),
105 this, SLOT(change_adaptor()));
106 connect(originCO, SIGNAL(activated(int)),
107 this, SLOT(change_adaptor()));
108 connect(aspectratioCB, SIGNAL(stateChanged(int)),
109 this, SLOT(change_adaptor()));
110 connect(browsePB, SIGNAL(clicked()),
111 this, SLOT(browseClicked()));
112 connect(embedCB, SIGNAL(toggled(bool)),
113 this, SLOT(change_adaptor()));
114 connect(editPB, SIGNAL(clicked()),
115 this, SLOT(editClicked()));
116 connect(externalCO, SIGNAL(activated(QString)),
117 this, SLOT(templateChanged()));
118 connect(extraED, SIGNAL(textChanged(QString)),
119 this, SLOT(extraChanged(QString)));
120 connect(extraFormatCO, SIGNAL(activated(QString)),
121 this, SLOT(formatChanged(QString)));
122 connect(widthUnitCO, SIGNAL(activated(int)),
123 this, SLOT(widthUnitChanged()));
124 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
125 this, SLOT(change_adaptor()));
126 connect(displayCB, SIGNAL(stateChanged(int)),
127 this, SLOT(change_adaptor()));
128 connect(displayscaleED, SIGNAL(textChanged(QString)),
129 this, SLOT(change_adaptor()));
130 connect(angleED, SIGNAL(textChanged(QString)),
131 this, SLOT(change_adaptor()));
132 connect(widthED, SIGNAL(textChanged(QString)),
133 this, SLOT(sizeChanged()));
134 connect(heightED, SIGNAL(textChanged(QString)),
135 this, SLOT(sizeChanged()));
136 connect(fileED, SIGNAL(textChanged(QString)),
137 this, SLOT(change_adaptor()));
138 connect(clipCB, SIGNAL(stateChanged(int)),
139 this, SLOT(change_adaptor()));
140 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
141 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
144 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
145 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
147 QIntValidator * validator = new QIntValidator(displayscaleED);
148 validator->setBottom(1);
149 displayscaleED->setValidator(validator);
151 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
153 xlED->setValidator(new QIntValidator(xlED));
154 ybED->setValidator(new QIntValidator(ybED));
155 xrED->setValidator(new QIntValidator(xrED));
156 ytED->setValidator(new QIntValidator(ytED));
158 widthED->setValidator(unsignedLengthValidator(widthED));
159 heightED->setValidator(unsignedLengthValidator(heightED));
161 setFocusProxy(fileED);
163 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
166 bc().setApply(applyPB);
167 bc().setCancel(closePB);
169 bc().addReadOnly(fileED);
170 bc().addReadOnly(browsePB);
171 bc().addReadOnly(editPB);
172 bc().addReadOnly(externalCO);
173 bc().addReadOnly(draftCB);
174 bc().addReadOnly(displayscaleED);
175 bc().addReadOnly(showCO);
176 bc().addReadOnly(displayCB);
177 bc().addReadOnly(angleED);
178 bc().addReadOnly(originCO);
179 bc().addReadOnly(heightUnitCO);
180 bc().addReadOnly(heightED);
181 bc().addReadOnly(aspectratioCB);
182 bc().addReadOnly(widthUnitCO);
183 bc().addReadOnly(widthED);
184 bc().addReadOnly(clipCB);
185 bc().addReadOnly(getbbPB);
186 bc().addReadOnly(ytED);
187 bc().addReadOnly(xlED);
188 bc().addReadOnly(xrED);
189 bc().addReadOnly(ybED);
190 bc().addReadOnly(extraFormatCO);
191 bc().addReadOnly(extraED);
193 bc().addCheckedLineEdit(angleED, angleLA);
194 bc().addCheckedLineEdit(displayscaleED, scaleLA);
195 bc().addCheckedLineEdit(heightED, heightLA);
196 bc().addCheckedLineEdit(widthED, widthLA);
197 bc().addCheckedLineEdit(xlED, lbLA);
198 bc().addCheckedLineEdit(ybED, lbLA);
199 bc().addCheckedLineEdit(xrED, rtLA);
200 bc().addCheckedLineEdit(ytED, rtLA);
201 bc().addCheckedLineEdit(fileED, fileLA);
203 vector<string> templates = getTemplates();
205 for (vector<string>::const_iterator cit = templates.begin();
206 cit != templates.end(); ++cit) {
207 externalCO->addItem(qt_(*cit));
210 // Fill the origins combo
211 for (size_t i = 0; i != all_origins.size(); ++i)
212 originCO->addItem(qt_(origin_gui_strs[i]));
214 // Fill the width combo
215 widthUnitCO->addItem(qt_("Scale%"));
216 for (int i = 0; i < num_units; i++)
217 widthUnitCO->addItem(qt_(unit_name_gui[i]));
221 bool GuiExternal::activateAspectratio() const
223 if (widthUnitCO->currentIndex() == 0)
226 string const wstr = fromqstr(widthED->text());
229 bool const wIsDbl = isStrDbl(wstr);
230 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
233 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
236 string const hstr = fromqstr(heightED->text());
239 bool const hIsDbl = isStrDbl(hstr);
240 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
242 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
249 void GuiExternal::bbChanged()
256 void GuiExternal::browseClicked()
258 int const choice = externalCO->currentIndex();
259 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
260 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
262 fileED->setText(toqstr(str));
268 void GuiExternal::change_adaptor()
274 void GuiExternal::editClicked()
281 void GuiExternal::extraChanged(const QString& text)
283 string const format = fromqstr(extraFormatCO->currentText());
284 extra_[format] = text;
289 void GuiExternal::formatChanged(const QString& format)
291 extraED->setText(extra_[fromqstr(format)]);
295 void GuiExternal::getbbClicked()
301 void GuiExternal::sizeChanged()
303 aspectratioCB->setEnabled(activateAspectratio());
308 void GuiExternal::templateChanged()
315 void GuiExternal::widthUnitChanged()
317 bool useHeight = (widthUnitCO->currentIndex() > 0);
320 widthED->setValidator(unsignedLengthValidator(widthED));
322 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
324 heightED->setEnabled(useHeight);
325 heightUnitCO->setEnabled(useHeight);
330 static Length::UNIT defaultUnit()
332 Length::UNIT default_unit = Length::CM;
333 switch (lyxrc.default_papersize) {
336 case PAPER_USEXECUTIVE:
337 default_unit = Length::IN;
346 static void setDisplay(
347 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
348 external::DisplayType display, unsigned int scale, bool read_only)
352 case external::DefaultDisplay:
355 case external::MonochromeDisplay:
358 case external::GrayscaleDisplay:
361 case external::ColorDisplay:
364 case external::PreviewDisplay:
367 case external::NoDisplay:
372 showCO.setCurrentIndex(item);
373 bool const no_display = display == external::NoDisplay;
374 showCO.setEnabled(!no_display && !read_only);
375 displayCB.setChecked(!no_display);
376 scaleED.setEnabled(!no_display && !read_only);
377 scaleED.setText(QString::number(scale));
381 static void getDisplay(external::DisplayType & display,
382 unsigned int & scale,
383 QCheckBox const & displayCB,
384 QComboBox const & showCO,
385 QLineEdit const & scaleED)
387 switch (showCO.currentIndex()) {
389 display = external::DefaultDisplay;
392 display = external::MonochromeDisplay;
395 display = external::GrayscaleDisplay;
398 display = external::ColorDisplay;
401 display = external::PreviewDisplay;
405 if (!displayCB.isChecked())
406 display = external::NoDisplay;
408 scale = scaleED.text().toInt();
412 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
413 external::RotationData const & data)
415 originCO.setCurrentIndex(int(data.origin()));
416 angleED.setText(toqstr(data.angle));
420 static void getRotation(external::RotationData & data,
421 QLineEdit const & angleED, QComboBox const & originCO)
423 typedef external::RotationData::OriginType OriginType;
425 data.origin(static_cast<OriginType>(originCO.currentIndex()));
426 data.angle = fromqstr(angleED.text());
430 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
431 QLineEdit & heightED, LengthCombo & heightUnitCO,
432 QCheckBox & aspectratioCB,
433 external::ResizeData const & data)
435 bool using_scale = data.usingScale();
436 string scale = data.scale;
437 if (data.no_resize()) {
438 // Everything is zero, so default to this!
444 widthED.setText(toqstr(scale));
445 widthUnitCO.setCurrentIndex(0);
447 widthED.setText(QString::number(data.width.value()));
448 // Because 'Scale' is position 0...
449 // Note also that width cannot be zero here, so
450 // we don't need to worry about the default unit.
451 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
454 string const h = data.height.zero() ? string() : data.height.asString();
455 Length::UNIT default_unit = data.width.zero() ?
456 defaultUnit() : data.width.unit();
457 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
459 heightED.setEnabled(!using_scale);
460 heightUnitCO.setEnabled(!using_scale);
462 aspectratioCB.setChecked(data.keepAspectRatio);
464 bool const disable_aspectRatio = using_scale ||
465 data.width.zero() || data.height.zero();
466 aspectratioCB.setEnabled(!disable_aspectRatio);
470 static void getSize(external::ResizeData & data,
471 QLineEdit const & widthED, QComboBox const & widthUnitCO,
472 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
473 QCheckBox const & aspectratioCB)
475 string const width = fromqstr(widthED.text());
477 if (widthUnitCO.currentIndex() > 0) {
478 // Subtract one, because scale is 0.
479 int const unit = widthUnitCO.currentIndex() - 1;
482 if (isValidLength(width, &w))
484 else if (isStrDbl(width))
485 data.width = Length(convert<double>(width),
486 static_cast<Length::UNIT>(unit));
488 data.width = Length();
490 data.scale = string();
493 // scaling instead of a width
495 data.width = Length();
498 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
500 data.keepAspectRatio = aspectratioCB.isChecked();
504 void setCrop(QCheckBox & clipCB,
505 QLineEdit & xlED, QLineEdit & ybED,
506 QLineEdit & xrED, QLineEdit & ytED,
507 external::ClipData const & data)
509 clipCB.setChecked(data.clip);
510 graphics::BoundingBox const & bbox = data.bbox;
511 xlED.setText(QString::number(bbox.xl));
512 ybED.setText(QString::number(bbox.yb));
513 xrED.setText(QString::number(bbox.xr));
514 ytED.setText(QString::number(bbox.yt));
518 static void getCrop(external::ClipData & data,
519 QCheckBox const & clipCB,
520 QLineEdit const & xlED, QLineEdit const & ybED,
521 QLineEdit const & xrED, QLineEdit const & ytED,
524 data.clip = clipCB.isChecked();
529 data.bbox.xl = xlED.text().toInt();
530 data.bbox.yb = ybED.text().toInt();
531 data.bbox.xr = xrED.text().toInt();
532 data.bbox.yt = ytED.text().toInt();
536 static void getExtra(external::ExtraData & data,
537 GuiExternal::MapType const & extra)
539 typedef GuiExternal::MapType MapType;
540 MapType::const_iterator it = extra.begin();
541 MapType::const_iterator const end = extra.end();
542 for (; it != end; ++it)
543 data.set(it->first, trim(fromqstr(it->second)));
547 void GuiExternal::updateContents()
549 tab->setCurrentIndex(0);
552 params_.filename.outputFilename(bufferFilepath());
553 fileED->setText(toqstr(name));
554 embedCB->setCheckState(params_.filename.embedded() ? Qt::Checked : Qt::Unchecked);
556 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
559 draftCB->setChecked(params_.draft);
561 setDisplay(*displayCB, *showCO, *displayscaleED,
562 params_.display, params_.lyxscale, isBufferReadonly());
564 setRotation(*angleED, *originCO, params_.rotationdata);
566 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
567 *aspectratioCB, params_.resizedata);
569 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
570 bbChanged_ = !params_.clipdata.bbox.empty();
576 void GuiExternal::updateTemplate()
578 external::Template templ = getTemplate(externalCO->currentIndex());
579 externalTB->setPlainText(qt_(templ.helpText));
581 // Ascertain which (if any) transformations the template supports
582 // and disable tabs hosting unsupported transforms.
583 typedef vector<external::TransformID> TransformIDs;
584 TransformIDs const transformIds = templ.transformIds;
585 TransformIDs::const_iterator tr_begin = transformIds.begin();
586 TransformIDs::const_iterator const tr_end = transformIds.end();
588 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
589 tab->setTabEnabled(tab->indexOf(rotatetab), found);
590 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
591 tab->setTabEnabled(tab->indexOf(scaletab), found);
593 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
594 tab->setTabEnabled(tab->indexOf(croptab), found);
596 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
597 tab->setTabEnabled(tab->indexOf(optionstab), found);
602 // Ascertain whether the template has any formats supporting
603 // the 'Extra' option
606 extraFormatCO->clear();
608 external::Template::Formats::const_iterator it = templ.formats.begin();
609 external::Template::Formats::const_iterator end = templ.formats.end();
610 for (; it != end; ++it) {
611 if (it->second.option_transformers.find(external::Extra) ==
612 it->second.option_transformers.end())
614 string const format = it->first;
615 string const opt = params_.extradata.get(format);
616 extraFormatCO->addItem(toqstr(format));
617 extra_[format] = toqstr(opt);
620 bool const enabled = extraFormatCO->count() > 0;
623 tab->indexOf(optionstab), enabled);
624 extraED->setEnabled(enabled && !isBufferReadonly());
625 extraFormatCO->setEnabled(enabled);
628 extraFormatCO->setCurrentIndex(0);
629 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
634 void GuiExternal::applyView()
636 params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
637 params_.filename.setEmbed(embedCB->checkState() == Qt::Checked);
639 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
641 params_.draft = draftCB->isChecked();
643 getDisplay(params_.display, params_.lyxscale,
647 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
648 getRotation(params_.rotationdata, *angleED, *originCO);
650 if (tab->isTabEnabled(tab->indexOf(scaletab)))
651 getSize(params_.resizedata, *widthED, *widthUnitCO,
652 *heightED, *heightUnitCO, *aspectratioCB);
654 if (tab->isTabEnabled(tab->indexOf(croptab)))
655 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
656 *xrED, *ytED, bbChanged_);
658 if (tab->isTabEnabled(tab->indexOf(optionstab)))
659 getExtra(params_.extradata, extra_);
663 void GuiExternal::getBB()
670 string const filename = fromqstr(fileED->text());
671 if (filename.empty())
674 string const bb = readBB(filename);
678 xlED->setText(toqstr(token(bb, ' ', 0)));
679 ybED->setText(toqstr(token(bb, ' ', 1)));
680 xrED->setText(toqstr(token(bb, ' ', 2)));
681 ytED->setText(toqstr(token(bb, ' ', 3)));
687 bool GuiExternal::initialiseParams(string const & data)
689 InsetExternalMailer::string2params(data, buffer(), params_);
694 void GuiExternal::clearParams()
696 params_ = InsetExternalParams();
700 void GuiExternal::dispatchParams()
702 string const lfun = InsetExternalMailer::params2string(params_, buffer());
703 dispatch(FuncRequest(getLfun(), lfun));
707 void GuiExternal::editExternal()
710 string const lfun = InsetExternalMailer::params2string(params_, buffer());
711 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
715 vector<string> const GuiExternal::getTemplates() const
717 vector<string> result;
719 external::TemplateManager::Templates::const_iterator i1, i2;
720 i1 = external::TemplateManager::get().getTemplates().begin();
721 i2 = external::TemplateManager::get().getTemplates().end();
723 for (; i1 != i2; ++i1) {
724 result.push_back(i1->second.lyxName);
730 int GuiExternal::getTemplateNumber(string const & name) const
732 external::TemplateManager::Templates::const_iterator i1, i2;
733 i1 = external::TemplateManager::get().getTemplates().begin();
734 i2 = external::TemplateManager::get().getTemplates().end();
735 for (int i = 0; i1 != i2; ++i1, ++i) {
736 if (i1->second.lyxName == name)
740 // we can get here if a LyX document has a template not installed
746 external::Template GuiExternal::getTemplate(int i) const
748 external::TemplateManager::Templates::const_iterator i1
749 = external::TemplateManager::get().getTemplates().begin();
758 GuiExternal::getTemplateFilters(string const & template_name) const
760 /// Determine the template file extension
761 external::TemplateManager const & etm =
762 external::TemplateManager::get();
763 external::Template const * const et_ptr =
764 etm.getTemplateByName(template_name);
767 return et_ptr->fileRegExp;
773 docstring const GuiExternal::browse(docstring const & input,
774 docstring const & template_name) const
776 docstring const title = _("Select external file");
778 docstring const bufpath = from_utf8(bufferFilepath());
779 FileFilterList const filter =
780 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
782 docstring const label1 = _("Documents|#o#O");
783 docstring const dir1 = from_utf8(lyxrc.document_path);
785 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
789 string const GuiExternal::readBB(string const & file)
791 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
793 // try to get it from the file, if possible. Zipped files are
794 // unzipped in the readBB_from_PSFile-Function
795 string const bb = readBB_from_PSFile(abs_file);
799 // we don't, so ask the Graphics Cache if it has loaded the file
803 graphics::Cache & gc = graphics::Cache::get();
804 if (gc.inCache(abs_file)) {
805 graphics::Image const * image = gc.item(abs_file)->image();
808 width = image->width();
809 height = image->height();
813 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
817 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
820 } // namespace frontend
823 #include "GuiExternal_moc.cpp"