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>
53 using support::FileFilterList;
54 using support::FileName;
55 using support::float_equal;
56 using support::isStrDbl;
57 using support::makeAbsPath;
58 using support::readBB_from_PSFile;
61 using support::os::internal_path;
63 using namespace external;
67 RotationDataType origins_array[] = {
68 RotationData::DEFAULT,
69 RotationData::TOPLEFT,
70 RotationData::BOTTOMLEFT,
71 RotationData::BASELINELEFT,
73 RotationData::TOPCENTER,
74 RotationData::BOTTOMCENTER,
75 RotationData::BASELINECENTER,
76 RotationData::TOPRIGHT,
77 RotationData::BOTTOMRIGHT,
78 RotationData::BASELINERIGHT
82 size_type const origins_array_size =
83 sizeof(origins_array) / sizeof(origins_array[0]);
85 vector<external::RotationDataType> const
86 all_origins(origins_array, origins_array + origins_array_size);
88 // These are the strings, corresponding to the above, that the GUI should
89 // use. Note that they can/should be translated.
90 char const * const origin_gui_strs[] = {
92 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
93 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
94 N_("Top right"), N_("Bottom right"), N_("Baseline right")
100 GuiExternal::GuiExternal(GuiView & lv)
101 : GuiDialog(lv, "external"), bbChanged_(false)
104 setViewTitle(_("External Material"));
106 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
107 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
108 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
110 connect(displayCB, SIGNAL(toggled(bool)),
111 showCO, SLOT(setEnabled(bool)));
112 connect(displayCB, SIGNAL(toggled(bool)),
113 displayscaleED, SLOT(setEnabled(bool)));
114 connect(showCO, SIGNAL(activated(QString)),
115 this, SLOT(change_adaptor()));
116 connect(originCO, SIGNAL(activated(int)),
117 this, SLOT(change_adaptor()));
118 connect(aspectratioCB, SIGNAL(stateChanged(int)),
119 this, SLOT(change_adaptor()));
120 connect(browsePB, SIGNAL(clicked()),
121 this, SLOT(browseClicked()));
122 connect(editPB, SIGNAL(clicked()),
123 this, SLOT(editClicked()));
124 connect(externalCO, SIGNAL(activated(QString)),
125 this, SLOT(templateChanged()));
126 connect(extraED, SIGNAL(textChanged(QString)),
127 this, SLOT(extraChanged(QString)));
128 connect(extraFormatCO, SIGNAL(activated(QString)),
129 this, SLOT(formatChanged(QString)));
130 connect(widthUnitCO, SIGNAL(activated(int)),
131 this, SLOT(widthUnitChanged()));
132 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
133 this, SLOT(change_adaptor()));
134 connect(displayCB, SIGNAL(stateChanged(int)),
135 this, SLOT(change_adaptor()));
136 connect(displayscaleED, SIGNAL(textChanged(QString)),
137 this, SLOT(change_adaptor()));
138 connect(angleED, SIGNAL(textChanged(QString)),
139 this, SLOT(change_adaptor()));
140 connect(widthED, SIGNAL(textChanged(QString)),
141 this, SLOT(sizeChanged()));
142 connect(heightED, SIGNAL(textChanged(QString)),
143 this, SLOT(sizeChanged()));
144 connect(fileED, SIGNAL(textChanged(QString)),
145 this, SLOT(change_adaptor()));
146 connect(clipCB, SIGNAL(stateChanged(int)),
147 this, SLOT(change_adaptor()));
148 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
149 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
150 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
151 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
152 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
153 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
155 QIntValidator * validator = new QIntValidator(displayscaleED);
156 validator->setBottom(1);
157 displayscaleED->setValidator(validator);
159 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
161 xlED->setValidator(new QIntValidator(xlED));
162 ybED->setValidator(new QIntValidator(ybED));
163 xrED->setValidator(new QIntValidator(xrED));
164 ytED->setValidator(new QIntValidator(ytED));
166 widthED->setValidator(unsignedLengthValidator(widthED));
167 heightED->setValidator(unsignedLengthValidator(heightED));
169 setFocusProxy(fileED);
171 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
174 bc().setApply(applyPB);
175 bc().setCancel(closePB);
177 bc().addReadOnly(fileED);
178 bc().addReadOnly(browsePB);
179 bc().addReadOnly(editPB);
180 bc().addReadOnly(externalCO);
181 bc().addReadOnly(draftCB);
182 bc().addReadOnly(displayscaleED);
183 bc().addReadOnly(showCO);
184 bc().addReadOnly(displayCB);
185 bc().addReadOnly(angleED);
186 bc().addReadOnly(originCO);
187 bc().addReadOnly(heightUnitCO);
188 bc().addReadOnly(heightED);
189 bc().addReadOnly(aspectratioCB);
190 bc().addReadOnly(widthUnitCO);
191 bc().addReadOnly(widthED);
192 bc().addReadOnly(clipCB);
193 bc().addReadOnly(getbbPB);
194 bc().addReadOnly(ytED);
195 bc().addReadOnly(xlED);
196 bc().addReadOnly(xrED);
197 bc().addReadOnly(ybED);
198 bc().addReadOnly(extraFormatCO);
199 bc().addReadOnly(extraED);
201 bc().addCheckedLineEdit(angleED, angleLA);
202 bc().addCheckedLineEdit(displayscaleED, scaleLA);
203 bc().addCheckedLineEdit(heightED, heightLA);
204 bc().addCheckedLineEdit(widthED, widthLA);
205 bc().addCheckedLineEdit(xlED, lbLA);
206 bc().addCheckedLineEdit(ybED, lbLA);
207 bc().addCheckedLineEdit(xrED, rtLA);
208 bc().addCheckedLineEdit(ytED, rtLA);
209 bc().addCheckedLineEdit(fileED, fileLA);
211 std::vector<string> templates = getTemplates();
213 for (std::vector<string>::const_iterator cit = templates.begin();
214 cit != templates.end(); ++cit) {
215 externalCO->addItem(qt_(*cit));
218 // Fill the origins combo
219 for (size_t i = 0; i != all_origins.size(); ++i)
220 originCO->addItem(qt_(origin_gui_strs[i]));
222 // Fill the width combo
223 widthUnitCO->addItem(qt_("Scale%"));
224 for (int i = 0; i < num_units; i++)
225 widthUnitCO->addItem(qt_(unit_name_gui[i]));
229 bool GuiExternal::activateAspectratio() const
231 if (widthUnitCO->currentIndex() == 0)
234 string const wstr = fromqstr(widthED->text());
237 bool const wIsDbl = isStrDbl(wstr);
238 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
241 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
244 string const hstr = fromqstr(heightED->text());
247 bool const hIsDbl = isStrDbl(hstr);
248 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
250 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
257 void GuiExternal::bbChanged()
264 void GuiExternal::browseClicked()
266 int const choice = externalCO->currentIndex();
267 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
268 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
270 fileED->setText(toqstr(str));
276 void GuiExternal::change_adaptor()
282 void GuiExternal::closeEvent(QCloseEvent * e)
289 void GuiExternal::editClicked()
296 void GuiExternal::extraChanged(const QString& text)
298 std::string const format = fromqstr(extraFormatCO->currentText());
299 extra_[format] = text;
304 void GuiExternal::formatChanged(const QString& format)
306 extraED->setText(extra_[fromqstr(format)]);
310 void GuiExternal::getbbClicked()
316 void GuiExternal::sizeChanged()
318 aspectratioCB->setEnabled(activateAspectratio());
323 void GuiExternal::templateChanged()
330 void GuiExternal::widthUnitChanged()
332 bool useHeight = (widthUnitCO->currentIndex() > 0);
335 widthED->setValidator(unsignedLengthValidator(widthED));
337 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
339 heightED->setEnabled(useHeight);
340 heightUnitCO->setEnabled(useHeight);
345 static Length::UNIT defaultUnit()
347 Length::UNIT default_unit = Length::CM;
348 switch (lyxrc.default_papersize) {
351 case PAPER_USEXECUTIVE:
352 default_unit = Length::IN;
361 static void setDisplay(
362 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
363 external::DisplayType display, unsigned int scale, bool read_only)
367 case external::DefaultDisplay:
370 case external::MonochromeDisplay:
373 case external::GrayscaleDisplay:
376 case external::ColorDisplay:
379 case external::PreviewDisplay:
382 case external::NoDisplay:
387 showCO.setCurrentIndex(item);
388 bool const no_display = display == external::NoDisplay;
389 showCO.setEnabled(!no_display && !read_only);
390 displayCB.setChecked(!no_display);
391 scaleED.setEnabled(!no_display && !read_only);
392 scaleED.setText(QString::number(scale));
396 static void getDisplay(external::DisplayType & display,
397 unsigned int & scale,
398 QCheckBox const & displayCB,
399 QComboBox const & showCO,
400 QLineEdit const & scaleED)
402 switch (showCO.currentIndex()) {
404 display = external::DefaultDisplay;
407 display = external::MonochromeDisplay;
410 display = external::GrayscaleDisplay;
413 display = external::ColorDisplay;
416 display = external::PreviewDisplay;
420 if (!displayCB.isChecked())
421 display = external::NoDisplay;
423 scale = scaleED.text().toInt();
427 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
428 external::RotationData const & data)
430 originCO.setCurrentIndex(int(data.origin()));
431 angleED.setText(toqstr(data.angle));
435 static void getRotation(external::RotationData & data,
436 QLineEdit const & angleED, QComboBox const & originCO)
438 typedef external::RotationData::OriginType OriginType;
440 data.origin(static_cast<OriginType>(originCO.currentIndex()));
441 data.angle = fromqstr(angleED.text());
445 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
446 QLineEdit & heightED, LengthCombo & heightUnitCO,
447 QCheckBox & aspectratioCB,
448 external::ResizeData const & data)
450 bool using_scale = data.usingScale();
451 std::string scale = data.scale;
452 if (data.no_resize()) {
453 // Everything is zero, so default to this!
459 widthED.setText(toqstr(scale));
460 widthUnitCO.setCurrentIndex(0);
462 widthED.setText(QString::number(data.width.value()));
463 // Because 'Scale' is position 0...
464 // Note also that width cannot be zero here, so
465 // we don't need to worry about the default unit.
466 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
469 string const h = data.height.zero() ? string() : data.height.asString();
470 Length::UNIT default_unit = data.width.zero() ?
471 defaultUnit() : data.width.unit();
472 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
474 heightED.setEnabled(!using_scale);
475 heightUnitCO.setEnabled(!using_scale);
477 aspectratioCB.setChecked(data.keepAspectRatio);
479 bool const disable_aspectRatio = using_scale ||
480 data.width.zero() || data.height.zero();
481 aspectratioCB.setEnabled(!disable_aspectRatio);
485 static void getSize(external::ResizeData & data,
486 QLineEdit const & widthED, QComboBox const & widthUnitCO,
487 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
488 QCheckBox const & aspectratioCB)
490 string const width = fromqstr(widthED.text());
492 if (widthUnitCO.currentIndex() > 0) {
493 // Subtract one, because scale is 0.
494 int const unit = widthUnitCO.currentIndex() - 1;
497 if (isValidLength(width, &w))
499 else if (isStrDbl(width))
500 data.width = Length(convert<double>(width),
501 static_cast<Length::UNIT>(unit));
503 data.width = Length();
505 data.scale = string();
508 // scaling instead of a width
510 data.width = Length();
513 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
515 data.keepAspectRatio = aspectratioCB.isChecked();
519 void setCrop(QCheckBox & clipCB,
520 QLineEdit & xlED, QLineEdit & ybED,
521 QLineEdit & xrED, QLineEdit & ytED,
522 external::ClipData const & data)
524 clipCB.setChecked(data.clip);
525 graphics::BoundingBox const & bbox = data.bbox;
526 xlED.setText(QString::number(bbox.xl));
527 ybED.setText(QString::number(bbox.yb));
528 xrED.setText(QString::number(bbox.xr));
529 ytED.setText(QString::number(bbox.yt));
533 static void getCrop(external::ClipData & data,
534 QCheckBox const & clipCB,
535 QLineEdit const & xlED, QLineEdit const & ybED,
536 QLineEdit const & xrED, QLineEdit const & ytED,
539 data.clip = clipCB.isChecked();
544 data.bbox.xl = xlED.text().toInt();
545 data.bbox.yb = ybED.text().toInt();
546 data.bbox.xr = xrED.text().toInt();
547 data.bbox.yt = ytED.text().toInt();
551 static void getExtra(external::ExtraData & data,
552 GuiExternal::MapType const & extra)
554 typedef GuiExternal::MapType MapType;
555 MapType::const_iterator it = extra.begin();
556 MapType::const_iterator const end = extra.end();
557 for (; it != end; ++it)
558 data.set(it->first, trim(fromqstr(it->second)));
562 void GuiExternal::updateContents()
564 tab->setCurrentIndex(0);
567 params_.filename.outputFilename(bufferFilepath());
568 fileED->setText(toqstr(name));
570 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
573 draftCB->setChecked(params_.draft);
575 setDisplay(*displayCB, *showCO, *displayscaleED,
576 params_.display, params_.lyxscale, isBufferReadonly());
578 setRotation(*angleED, *originCO, params_.rotationdata);
580 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
581 *aspectratioCB, params_.resizedata);
583 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
584 bbChanged_ = !params_.clipdata.bbox.empty();
590 void GuiExternal::updateTemplate()
592 external::Template templ = getTemplate(externalCO->currentIndex());
593 externalTB->setPlainText(qt_(templ.helpText));
595 // Ascertain which (if any) transformations the template supports
596 // and disable tabs hosting unsupported transforms.
597 typedef vector<external::TransformID> TransformIDs;
598 TransformIDs const transformIds = templ.transformIds;
599 TransformIDs::const_iterator tr_begin = transformIds.begin();
600 TransformIDs::const_iterator const tr_end = transformIds.end();
602 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
603 tab->setTabEnabled(tab->indexOf(rotatetab), found);
604 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
605 tab->setTabEnabled(tab->indexOf(scaletab), found);
607 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
608 tab->setTabEnabled(tab->indexOf(croptab), found);
610 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
611 tab->setTabEnabled(tab->indexOf(optionstab), found);
616 // Ascertain whether the template has any formats supporting
617 // the 'Extra' option
620 extraFormatCO->clear();
622 external::Template::Formats::const_iterator it = templ.formats.begin();
623 external::Template::Formats::const_iterator end = templ.formats.end();
624 for (; it != end; ++it) {
625 if (it->second.option_transformers.find(external::Extra) ==
626 it->second.option_transformers.end())
628 string const format = it->first;
629 string const opt = params_.extradata.get(format);
630 extraFormatCO->addItem(toqstr(format));
631 extra_[format] = toqstr(opt);
634 bool const enabled = extraFormatCO->count() > 0;
637 tab->indexOf(optionstab), enabled);
638 extraED->setEnabled(enabled && !isBufferReadonly());
639 extraFormatCO->setEnabled(enabled);
642 extraFormatCO->setCurrentIndex(0);
643 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
648 void GuiExternal::applyView()
650 params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
652 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
654 params_.draft = draftCB->isChecked();
656 getDisplay(params_.display, params_.lyxscale,
660 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
661 getRotation(params_.rotationdata, *angleED, *originCO);
663 if (tab->isTabEnabled(tab->indexOf(scaletab)))
664 getSize(params_.resizedata, *widthED, *widthUnitCO,
665 *heightED, *heightUnitCO, *aspectratioCB);
667 if (tab->isTabEnabled(tab->indexOf(croptab)))
668 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
669 *xrED, *ytED, bbChanged_);
671 if (tab->isTabEnabled(tab->indexOf(optionstab)))
672 getExtra(params_.extradata, extra_);
676 void GuiExternal::getBB()
683 string const filename = fromqstr(fileED->text());
684 if (filename.empty())
687 string const bb = readBB(filename);
691 xlED->setText(toqstr(token(bb, ' ', 0)));
692 ybED->setText(toqstr(token(bb, ' ', 1)));
693 xrED->setText(toqstr(token(bb, ' ', 2)));
694 ytED->setText(toqstr(token(bb, ' ', 3)));
700 bool GuiExternal::initialiseParams(string const & data)
702 InsetExternalMailer::string2params(data, buffer(), params_);
707 void GuiExternal::clearParams()
709 params_ = InsetExternalParams();
713 void GuiExternal::dispatchParams()
715 string const lfun = InsetExternalMailer::params2string(params_, buffer());
716 dispatch(FuncRequest(getLfun(), lfun));
720 void GuiExternal::editExternal()
723 string const lfun = InsetExternalMailer::params2string(params_, buffer());
724 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
728 vector<string> const GuiExternal::getTemplates() const
730 vector<string> result;
732 external::TemplateManager::Templates::const_iterator i1, i2;
733 i1 = external::TemplateManager::get().getTemplates().begin();
734 i2 = external::TemplateManager::get().getTemplates().end();
736 for (; i1 != i2; ++i1) {
737 result.push_back(i1->second.lyxName);
743 int GuiExternal::getTemplateNumber(string const & name) const
745 external::TemplateManager::Templates::const_iterator i1, i2;
746 i1 = external::TemplateManager::get().getTemplates().begin();
747 i2 = external::TemplateManager::get().getTemplates().end();
748 for (int i = 0; i1 != i2; ++i1, ++i) {
749 if (i1->second.lyxName == name)
753 // we can get here if a LyX document has a template not installed
759 external::Template GuiExternal::getTemplate(int i) const
761 external::TemplateManager::Templates::const_iterator i1
762 = external::TemplateManager::get().getTemplates().begin();
771 GuiExternal::getTemplateFilters(string const & template_name) const
773 /// Determine the template file extension
774 external::TemplateManager const & etm =
775 external::TemplateManager::get();
776 external::Template const * const et_ptr =
777 etm.getTemplateByName(template_name);
780 return et_ptr->fileRegExp;
786 docstring const GuiExternal::browse(docstring const & input,
787 docstring const & template_name) const
789 docstring const title = _("Select external file");
791 docstring const bufpath = from_utf8(bufferFilepath());
792 FileFilterList const filter =
793 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
795 docstring const label1 = _("Documents|#o#O");
796 docstring const dir1 = from_utf8(lyxrc.document_path);
798 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
802 string const GuiExternal::readBB(string const & file)
804 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
806 // try to get it from the file, if possible. Zipped files are
807 // unzipped in the readBB_from_PSFile-Function
808 string const bb = readBB_from_PSFile(abs_file);
812 // we don't, so ask the Graphics Cache if it has loaded the file
816 graphics::Cache & gc = graphics::Cache::get();
817 if (gc.inCache(abs_file)) {
818 graphics::Image const * image = gc.item(abs_file)->image();
821 width = image->width();
822 height = image->height();
826 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
830 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
833 } // namespace frontend
836 #include "GuiExternal_moc.cpp"