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(embedCB, SIGNAL(toggled(bool)),
114 this, SLOT(change_adaptor()));
115 connect(editPB, SIGNAL(clicked()),
116 this, SLOT(editClicked()));
117 connect(externalCO, SIGNAL(activated(QString)),
118 this, SLOT(templateChanged()));
119 connect(extraED, SIGNAL(textChanged(QString)),
120 this, SLOT(extraChanged(QString)));
121 connect(extraFormatCO, SIGNAL(activated(QString)),
122 this, SLOT(formatChanged(QString)));
123 connect(widthUnitCO, SIGNAL(activated(int)),
124 this, SLOT(widthUnitChanged()));
125 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
126 this, SLOT(change_adaptor()));
127 connect(displayCB, SIGNAL(stateChanged(int)),
128 this, SLOT(change_adaptor()));
129 connect(displayscaleED, SIGNAL(textChanged(QString)),
130 this, SLOT(change_adaptor()));
131 connect(angleED, SIGNAL(textChanged(QString)),
132 this, SLOT(change_adaptor()));
133 connect(widthED, SIGNAL(textChanged(QString)),
134 this, SLOT(sizeChanged()));
135 connect(heightED, SIGNAL(textChanged(QString)),
136 this, SLOT(sizeChanged()));
137 connect(fileED, SIGNAL(textChanged(QString)),
138 this, SLOT(change_adaptor()));
139 connect(clipCB, SIGNAL(stateChanged(int)),
140 this, SLOT(change_adaptor()));
141 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
142 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
144 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
145 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
146 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
148 QIntValidator * validator = new QIntValidator(displayscaleED);
149 validator->setBottom(1);
150 displayscaleED->setValidator(validator);
152 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
154 xlED->setValidator(new QIntValidator(xlED));
155 ybED->setValidator(new QIntValidator(ybED));
156 xrED->setValidator(new QIntValidator(xrED));
157 ytED->setValidator(new QIntValidator(ytED));
159 widthED->setValidator(unsignedLengthValidator(widthED));
160 heightED->setValidator(unsignedLengthValidator(heightED));
162 setFocusProxy(fileED);
164 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
167 bc().setApply(applyPB);
168 bc().setCancel(closePB);
170 bc().addReadOnly(fileED);
171 bc().addReadOnly(browsePB);
172 bc().addReadOnly(editPB);
173 bc().addReadOnly(externalCO);
174 bc().addReadOnly(draftCB);
175 bc().addReadOnly(displayscaleED);
176 bc().addReadOnly(showCO);
177 bc().addReadOnly(displayCB);
178 bc().addReadOnly(angleED);
179 bc().addReadOnly(originCO);
180 bc().addReadOnly(heightUnitCO);
181 bc().addReadOnly(heightED);
182 bc().addReadOnly(aspectratioCB);
183 bc().addReadOnly(widthUnitCO);
184 bc().addReadOnly(widthED);
185 bc().addReadOnly(clipCB);
186 bc().addReadOnly(getbbPB);
187 bc().addReadOnly(ytED);
188 bc().addReadOnly(xlED);
189 bc().addReadOnly(xrED);
190 bc().addReadOnly(ybED);
191 bc().addReadOnly(extraFormatCO);
192 bc().addReadOnly(extraED);
194 bc().addCheckedLineEdit(angleED, angleLA);
195 bc().addCheckedLineEdit(displayscaleED, scaleLA);
196 bc().addCheckedLineEdit(heightED, heightLA);
197 bc().addCheckedLineEdit(widthED, widthLA);
198 bc().addCheckedLineEdit(xlED, lbLA);
199 bc().addCheckedLineEdit(ybED, lbLA);
200 bc().addCheckedLineEdit(xrED, rtLA);
201 bc().addCheckedLineEdit(ytED, rtLA);
202 bc().addCheckedLineEdit(fileED, fileLA);
204 vector<string> templates = getTemplates();
206 for (vector<string>::const_iterator cit = templates.begin();
207 cit != templates.end(); ++cit) {
208 externalCO->addItem(qt_(*cit));
211 // Fill the origins combo
212 for (size_t i = 0; i != all_origins.size(); ++i)
213 originCO->addItem(qt_(origin_gui_strs[i]));
215 // Fill the width combo
216 widthUnitCO->addItem(qt_("Scale%"));
217 for (int i = 0; i < num_units; i++)
218 widthUnitCO->addItem(qt_(unit_name_gui[i]));
222 bool GuiExternal::activateAspectratio() const
224 if (widthUnitCO->currentIndex() == 0)
227 string const wstr = fromqstr(widthED->text());
230 bool const wIsDbl = isStrDbl(wstr);
231 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
234 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
237 string const hstr = fromqstr(heightED->text());
240 bool const hIsDbl = isStrDbl(hstr);
241 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
243 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
250 void GuiExternal::bbChanged()
257 void GuiExternal::browseClicked()
259 int const choice = externalCO->currentIndex();
260 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
261 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
263 fileED->setText(toqstr(str));
269 void GuiExternal::change_adaptor()
275 void GuiExternal::closeEvent(QCloseEvent * e)
282 void GuiExternal::editClicked()
289 void GuiExternal::extraChanged(const QString& text)
291 string const format = fromqstr(extraFormatCO->currentText());
292 extra_[format] = text;
297 void GuiExternal::formatChanged(const QString& format)
299 extraED->setText(extra_[fromqstr(format)]);
303 void GuiExternal::getbbClicked()
309 void GuiExternal::sizeChanged()
311 aspectratioCB->setEnabled(activateAspectratio());
316 void GuiExternal::templateChanged()
323 void GuiExternal::widthUnitChanged()
325 bool useHeight = (widthUnitCO->currentIndex() > 0);
328 widthED->setValidator(unsignedLengthValidator(widthED));
330 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
332 heightED->setEnabled(useHeight);
333 heightUnitCO->setEnabled(useHeight);
338 static Length::UNIT defaultUnit()
340 Length::UNIT default_unit = Length::CM;
341 switch (lyxrc.default_papersize) {
344 case PAPER_USEXECUTIVE:
345 default_unit = Length::IN;
354 static void setDisplay(
355 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
356 external::DisplayType display, unsigned int scale, bool read_only)
360 case external::DefaultDisplay:
363 case external::MonochromeDisplay:
366 case external::GrayscaleDisplay:
369 case external::ColorDisplay:
372 case external::PreviewDisplay:
375 case external::NoDisplay:
380 showCO.setCurrentIndex(item);
381 bool const no_display = display == external::NoDisplay;
382 showCO.setEnabled(!no_display && !read_only);
383 displayCB.setChecked(!no_display);
384 scaleED.setEnabled(!no_display && !read_only);
385 scaleED.setText(QString::number(scale));
389 static void getDisplay(external::DisplayType & display,
390 unsigned int & scale,
391 QCheckBox const & displayCB,
392 QComboBox const & showCO,
393 QLineEdit const & scaleED)
395 switch (showCO.currentIndex()) {
397 display = external::DefaultDisplay;
400 display = external::MonochromeDisplay;
403 display = external::GrayscaleDisplay;
406 display = external::ColorDisplay;
409 display = external::PreviewDisplay;
413 if (!displayCB.isChecked())
414 display = external::NoDisplay;
416 scale = scaleED.text().toInt();
420 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
421 external::RotationData const & data)
423 originCO.setCurrentIndex(int(data.origin()));
424 angleED.setText(toqstr(data.angle));
428 static void getRotation(external::RotationData & data,
429 QLineEdit const & angleED, QComboBox const & originCO)
431 typedef external::RotationData::OriginType OriginType;
433 data.origin(static_cast<OriginType>(originCO.currentIndex()));
434 data.angle = fromqstr(angleED.text());
438 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
439 QLineEdit & heightED, LengthCombo & heightUnitCO,
440 QCheckBox & aspectratioCB,
441 external::ResizeData const & data)
443 bool using_scale = data.usingScale();
444 string scale = data.scale;
445 if (data.no_resize()) {
446 // Everything is zero, so default to this!
452 widthED.setText(toqstr(scale));
453 widthUnitCO.setCurrentIndex(0);
455 widthED.setText(QString::number(data.width.value()));
456 // Because 'Scale' is position 0...
457 // Note also that width cannot be zero here, so
458 // we don't need to worry about the default unit.
459 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
462 string const h = data.height.zero() ? string() : data.height.asString();
463 Length::UNIT default_unit = data.width.zero() ?
464 defaultUnit() : data.width.unit();
465 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
467 heightED.setEnabled(!using_scale);
468 heightUnitCO.setEnabled(!using_scale);
470 aspectratioCB.setChecked(data.keepAspectRatio);
472 bool const disable_aspectRatio = using_scale ||
473 data.width.zero() || data.height.zero();
474 aspectratioCB.setEnabled(!disable_aspectRatio);
478 static void getSize(external::ResizeData & data,
479 QLineEdit const & widthED, QComboBox const & widthUnitCO,
480 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
481 QCheckBox const & aspectratioCB)
483 string const width = fromqstr(widthED.text());
485 if (widthUnitCO.currentIndex() > 0) {
486 // Subtract one, because scale is 0.
487 int const unit = widthUnitCO.currentIndex() - 1;
490 if (isValidLength(width, &w))
492 else if (isStrDbl(width))
493 data.width = Length(convert<double>(width),
494 static_cast<Length::UNIT>(unit));
496 data.width = Length();
498 data.scale = string();
501 // scaling instead of a width
503 data.width = Length();
506 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
508 data.keepAspectRatio = aspectratioCB.isChecked();
512 void setCrop(QCheckBox & clipCB,
513 QLineEdit & xlED, QLineEdit & ybED,
514 QLineEdit & xrED, QLineEdit & ytED,
515 external::ClipData const & data)
517 clipCB.setChecked(data.clip);
518 graphics::BoundingBox const & bbox = data.bbox;
519 xlED.setText(QString::number(bbox.xl));
520 ybED.setText(QString::number(bbox.yb));
521 xrED.setText(QString::number(bbox.xr));
522 ytED.setText(QString::number(bbox.yt));
526 static void getCrop(external::ClipData & data,
527 QCheckBox const & clipCB,
528 QLineEdit const & xlED, QLineEdit const & ybED,
529 QLineEdit const & xrED, QLineEdit const & ytED,
532 data.clip = clipCB.isChecked();
537 data.bbox.xl = xlED.text().toInt();
538 data.bbox.yb = ybED.text().toInt();
539 data.bbox.xr = xrED.text().toInt();
540 data.bbox.yt = ytED.text().toInt();
544 static void getExtra(external::ExtraData & data,
545 GuiExternal::MapType const & extra)
547 typedef GuiExternal::MapType MapType;
548 MapType::const_iterator it = extra.begin();
549 MapType::const_iterator const end = extra.end();
550 for (; it != end; ++it)
551 data.set(it->first, trim(fromqstr(it->second)));
555 void GuiExternal::updateContents()
557 tab->setCurrentIndex(0);
560 params_.filename.outputFilename(bufferFilepath());
561 fileED->setText(toqstr(name));
562 embedCB->setCheckState(params_.filename.embedded() ? Qt::Checked : Qt::Unchecked);
564 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
567 draftCB->setChecked(params_.draft);
569 setDisplay(*displayCB, *showCO, *displayscaleED,
570 params_.display, params_.lyxscale, isBufferReadonly());
572 setRotation(*angleED, *originCO, params_.rotationdata);
574 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
575 *aspectratioCB, params_.resizedata);
577 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
578 bbChanged_ = !params_.clipdata.bbox.empty();
584 void GuiExternal::updateTemplate()
586 external::Template templ = getTemplate(externalCO->currentIndex());
587 externalTB->setPlainText(qt_(templ.helpText));
589 // Ascertain which (if any) transformations the template supports
590 // and disable tabs hosting unsupported transforms.
591 typedef vector<external::TransformID> TransformIDs;
592 TransformIDs const transformIds = templ.transformIds;
593 TransformIDs::const_iterator tr_begin = transformIds.begin();
594 TransformIDs::const_iterator const tr_end = transformIds.end();
596 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
597 tab->setTabEnabled(tab->indexOf(rotatetab), found);
598 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
599 tab->setTabEnabled(tab->indexOf(scaletab), found);
601 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
602 tab->setTabEnabled(tab->indexOf(croptab), found);
604 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
605 tab->setTabEnabled(tab->indexOf(optionstab), found);
610 // Ascertain whether the template has any formats supporting
611 // the 'Extra' option
614 extraFormatCO->clear();
616 external::Template::Formats::const_iterator it = templ.formats.begin();
617 external::Template::Formats::const_iterator end = templ.formats.end();
618 for (; it != end; ++it) {
619 if (it->second.option_transformers.find(external::Extra) ==
620 it->second.option_transformers.end())
622 string const format = it->first;
623 string const opt = params_.extradata.get(format);
624 extraFormatCO->addItem(toqstr(format));
625 extra_[format] = toqstr(opt);
628 bool const enabled = extraFormatCO->count() > 0;
631 tab->indexOf(optionstab), enabled);
632 extraED->setEnabled(enabled && !isBufferReadonly());
633 extraFormatCO->setEnabled(enabled);
636 extraFormatCO->setCurrentIndex(0);
637 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
642 void GuiExternal::applyView()
644 params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
645 params_.filename.setEmbed(embedCB->checkState() == Qt::Checked);
647 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
649 params_.draft = draftCB->isChecked();
651 getDisplay(params_.display, params_.lyxscale,
655 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
656 getRotation(params_.rotationdata, *angleED, *originCO);
658 if (tab->isTabEnabled(tab->indexOf(scaletab)))
659 getSize(params_.resizedata, *widthED, *widthUnitCO,
660 *heightED, *heightUnitCO, *aspectratioCB);
662 if (tab->isTabEnabled(tab->indexOf(croptab)))
663 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
664 *xrED, *ytED, bbChanged_);
666 if (tab->isTabEnabled(tab->indexOf(optionstab)))
667 getExtra(params_.extradata, extra_);
671 void GuiExternal::getBB()
678 string const filename = fromqstr(fileED->text());
679 if (filename.empty())
682 string const bb = readBB(filename);
686 xlED->setText(toqstr(token(bb, ' ', 0)));
687 ybED->setText(toqstr(token(bb, ' ', 1)));
688 xrED->setText(toqstr(token(bb, ' ', 2)));
689 ytED->setText(toqstr(token(bb, ' ', 3)));
695 bool GuiExternal::initialiseParams(string const & data)
697 InsetExternalMailer::string2params(data, buffer(), params_);
702 void GuiExternal::clearParams()
704 params_ = InsetExternalParams();
708 void GuiExternal::dispatchParams()
710 string const lfun = InsetExternalMailer::params2string(params_, buffer());
711 dispatch(FuncRequest(getLfun(), lfun));
715 void GuiExternal::editExternal()
718 string const lfun = InsetExternalMailer::params2string(params_, buffer());
719 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
723 vector<string> const GuiExternal::getTemplates() const
725 vector<string> result;
727 external::TemplateManager::Templates::const_iterator i1, i2;
728 i1 = external::TemplateManager::get().getTemplates().begin();
729 i2 = external::TemplateManager::get().getTemplates().end();
731 for (; i1 != i2; ++i1) {
732 result.push_back(i1->second.lyxName);
738 int GuiExternal::getTemplateNumber(string const & name) const
740 external::TemplateManager::Templates::const_iterator i1, i2;
741 i1 = external::TemplateManager::get().getTemplates().begin();
742 i2 = external::TemplateManager::get().getTemplates().end();
743 for (int i = 0; i1 != i2; ++i1, ++i) {
744 if (i1->second.lyxName == name)
748 // we can get here if a LyX document has a template not installed
754 external::Template GuiExternal::getTemplate(int i) const
756 external::TemplateManager::Templates::const_iterator i1
757 = external::TemplateManager::get().getTemplates().begin();
766 GuiExternal::getTemplateFilters(string const & template_name) const
768 /// Determine the template file extension
769 external::TemplateManager const & etm =
770 external::TemplateManager::get();
771 external::Template const * const et_ptr =
772 etm.getTemplateByName(template_name);
775 return et_ptr->fileRegExp;
781 docstring const GuiExternal::browse(docstring const & input,
782 docstring const & template_name) const
784 docstring const title = _("Select external file");
786 docstring const bufpath = from_utf8(bufferFilepath());
787 FileFilterList const filter =
788 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
790 docstring const label1 = _("Documents|#o#O");
791 docstring const dir1 = from_utf8(lyxrc.document_path);
793 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
797 string const GuiExternal::readBB(string const & file)
799 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
801 // try to get it from the file, if possible. Zipped files are
802 // unzipped in the readBB_from_PSFile-Function
803 string const bb = readBB_from_PSFile(abs_file);
807 // we don't, so ask the Graphics Cache if it has loaded the file
811 graphics::Cache & gc = graphics::Cache::get();
812 if (gc.inCache(abs_file)) {
813 graphics::Image const * image = gc.item(abs_file)->image();
816 width = image->width();
817 height = image->height();
821 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
825 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
828 } // namespace frontend
831 #include "GuiExternal_moc.cpp"