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"
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>
56 using support::FileFilterList;
57 using support::FileName;
58 using support::float_equal;
59 using support::isStrDbl;
60 using support::makeAbsPath;
61 using support::readBB_from_PSFile;
64 using support::os::internal_path;
66 using namespace external;
70 RotationDataType origins_array[] = {
71 RotationData::DEFAULT,
72 RotationData::TOPLEFT,
73 RotationData::BOTTOMLEFT,
74 RotationData::BASELINELEFT,
76 RotationData::TOPCENTER,
77 RotationData::BOTTOMCENTER,
78 RotationData::BASELINECENTER,
79 RotationData::TOPRIGHT,
80 RotationData::BOTTOMRIGHT,
81 RotationData::BASELINERIGHT
85 size_type const origins_array_size =
86 sizeof(origins_array) / sizeof(origins_array[0]);
88 vector<external::RotationDataType> const
89 all_origins(origins_array, origins_array + origins_array_size);
91 // These are the strings, corresponding to the above, that the GUI should
92 // use. Note that they can/should be translated.
93 char const * const origin_gui_strs[] = {
95 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
96 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
97 N_("Top right"), N_("Bottom right"), N_("Baseline right")
103 GuiExternal::GuiExternal(GuiView & lv)
104 : GuiDialog(lv, "external"), bbChanged_(false)
107 setViewTitle(_("External Material"));
109 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
110 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
111 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
113 connect(displayCB, SIGNAL(toggled(bool)),
114 showCO, SLOT(setEnabled(bool)));
115 connect(displayCB, SIGNAL(toggled(bool)),
116 displayscaleED, SLOT(setEnabled(bool)));
117 connect(showCO, SIGNAL(activated(QString)),
118 this, SLOT(change_adaptor()));
119 connect(originCO, SIGNAL(activated(int)),
120 this, SLOT(change_adaptor()));
121 connect(aspectratioCB, SIGNAL(stateChanged(int)),
122 this, SLOT(change_adaptor()));
123 connect(browsePB, SIGNAL(clicked()),
124 this, SLOT(browseClicked()));
125 connect(editPB, SIGNAL(clicked()),
126 this, SLOT(editClicked()));
127 connect(externalCO, SIGNAL(activated(QString)),
128 this, SLOT(templateChanged()));
129 connect(extraED, SIGNAL(textChanged(QString)),
130 this, SLOT(extraChanged(QString)));
131 connect(extraFormatCO, SIGNAL(activated(QString)),
132 this, SLOT(formatChanged(QString)));
133 connect(widthUnitCO, SIGNAL(activated(int)),
134 this, SLOT(widthUnitChanged()));
135 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
136 this, SLOT(change_adaptor()));
137 connect(displayCB, SIGNAL(stateChanged(int)),
138 this, SLOT(change_adaptor()));
139 connect(displayscaleED, SIGNAL(textChanged(QString)),
140 this, SLOT(change_adaptor()));
141 connect(angleED, SIGNAL(textChanged(QString)),
142 this, SLOT(change_adaptor()));
143 connect(widthED, SIGNAL(textChanged(QString)),
144 this, SLOT(sizeChanged()));
145 connect(heightED, SIGNAL(textChanged(QString)),
146 this, SLOT(sizeChanged()));
147 connect(fileED, SIGNAL(textChanged(QString)),
148 this, SLOT(change_adaptor()));
149 connect(clipCB, SIGNAL(stateChanged(int)),
150 this, SLOT(change_adaptor()));
151 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
152 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
153 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
154 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
155 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
156 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
158 QIntValidator * validator = new QIntValidator(displayscaleED);
159 validator->setBottom(1);
160 displayscaleED->setValidator(validator);
162 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
164 xlED->setValidator(new QIntValidator(xlED));
165 ybED->setValidator(new QIntValidator(ybED));
166 xrED->setValidator(new QIntValidator(xrED));
167 ytED->setValidator(new QIntValidator(ytED));
169 widthED->setValidator(unsignedLengthValidator(widthED));
170 heightED->setValidator(unsignedLengthValidator(heightED));
172 setFocusProxy(fileED);
174 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
177 bc().setApply(applyPB);
178 bc().setCancel(closePB);
180 bc().addReadOnly(fileED);
181 bc().addReadOnly(browsePB);
182 bc().addReadOnly(editPB);
183 bc().addReadOnly(externalCO);
184 bc().addReadOnly(draftCB);
185 bc().addReadOnly(displayscaleED);
186 bc().addReadOnly(showCO);
187 bc().addReadOnly(displayCB);
188 bc().addReadOnly(angleED);
189 bc().addReadOnly(originCO);
190 bc().addReadOnly(heightUnitCO);
191 bc().addReadOnly(heightED);
192 bc().addReadOnly(aspectratioCB);
193 bc().addReadOnly(widthUnitCO);
194 bc().addReadOnly(widthED);
195 bc().addReadOnly(clipCB);
196 bc().addReadOnly(getbbPB);
197 bc().addReadOnly(ytED);
198 bc().addReadOnly(xlED);
199 bc().addReadOnly(xrED);
200 bc().addReadOnly(ybED);
201 bc().addReadOnly(extraFormatCO);
202 bc().addReadOnly(extraED);
204 bc().addCheckedLineEdit(angleED, angleLA);
205 bc().addCheckedLineEdit(displayscaleED, scaleLA);
206 bc().addCheckedLineEdit(heightED, heightLA);
207 bc().addCheckedLineEdit(widthED, widthLA);
208 bc().addCheckedLineEdit(xlED, lbLA);
209 bc().addCheckedLineEdit(ybED, lbLA);
210 bc().addCheckedLineEdit(xrED, rtLA);
211 bc().addCheckedLineEdit(ytED, rtLA);
212 bc().addCheckedLineEdit(fileED, fileLA);
214 std::vector<string> templates = getTemplates();
216 for (std::vector<string>::const_iterator cit = templates.begin();
217 cit != templates.end(); ++cit) {
218 externalCO->addItem(qt_(*cit));
221 // Fill the origins combo
222 for (size_t i = 0; i != all_origins.size(); ++i)
223 originCO->addItem(qt_(origin_gui_strs[i]));
225 // Fill the width combo
226 widthUnitCO->addItem(qt_("Scale%"));
227 for (int i = 0; i < num_units; i++)
228 widthUnitCO->addItem(qt_(unit_name_gui[i]));
232 bool GuiExternal::activateAspectratio() const
234 if (widthUnitCO->currentIndex() == 0)
237 string const wstr = fromqstr(widthED->text());
240 bool const wIsDbl = isStrDbl(wstr);
241 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
244 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
247 string const hstr = fromqstr(heightED->text());
250 bool const hIsDbl = isStrDbl(hstr);
251 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
253 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
260 void GuiExternal::bbChanged()
267 void GuiExternal::browseClicked()
269 int const choice = externalCO->currentIndex();
270 docstring const template_name = from_utf8(getTemplate(choice).lyxName);
271 docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
273 fileED->setText(toqstr(str));
279 void GuiExternal::change_adaptor()
285 void GuiExternal::closeEvent(QCloseEvent * e)
292 void GuiExternal::editClicked()
299 void GuiExternal::extraChanged(const QString& text)
301 std::string const format = fromqstr(extraFormatCO->currentText());
302 extra_[format] = text;
307 void GuiExternal::formatChanged(const QString& format)
309 extraED->setText(extra_[fromqstr(format)]);
313 void GuiExternal::getbbClicked()
319 void GuiExternal::sizeChanged()
321 aspectratioCB->setEnabled(activateAspectratio());
326 void GuiExternal::templateChanged()
333 void GuiExternal::widthUnitChanged()
335 bool useHeight = (widthUnitCO->currentIndex() > 0);
338 widthED->setValidator(unsignedLengthValidator(widthED));
340 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
342 heightED->setEnabled(useHeight);
343 heightUnitCO->setEnabled(useHeight);
348 static Length::UNIT defaultUnit()
350 Length::UNIT default_unit = Length::CM;
351 switch (lyxrc.default_papersize) {
354 case PAPER_USEXECUTIVE:
355 default_unit = Length::IN;
364 static void setDisplay(
365 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
366 external::DisplayType display, unsigned int scale, bool read_only)
370 case external::DefaultDisplay:
373 case external::MonochromeDisplay:
376 case external::GrayscaleDisplay:
379 case external::ColorDisplay:
382 case external::PreviewDisplay:
385 case external::NoDisplay:
390 showCO.setCurrentIndex(item);
391 bool const no_display = display == external::NoDisplay;
392 showCO.setEnabled(!no_display && !read_only);
393 displayCB.setChecked(!no_display);
394 scaleED.setEnabled(!no_display && !read_only);
395 scaleED.setText(QString::number(scale));
399 static void getDisplay(external::DisplayType & display,
400 unsigned int & scale,
401 QCheckBox const & displayCB,
402 QComboBox const & showCO,
403 QLineEdit const & scaleED)
405 switch (showCO.currentIndex()) {
407 display = external::DefaultDisplay;
410 display = external::MonochromeDisplay;
413 display = external::GrayscaleDisplay;
416 display = external::ColorDisplay;
419 display = external::PreviewDisplay;
423 if (!displayCB.isChecked())
424 display = external::NoDisplay;
426 scale = scaleED.text().toInt();
430 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
431 external::RotationData const & data)
433 originCO.setCurrentIndex(int(data.origin()));
434 angleED.setText(toqstr(data.angle));
438 static void getRotation(external::RotationData & data,
439 QLineEdit const & angleED, QComboBox const & originCO)
441 typedef external::RotationData::OriginType OriginType;
443 data.origin(static_cast<OriginType>(originCO.currentIndex()));
444 data.angle = fromqstr(angleED.text());
448 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
449 QLineEdit & heightED, LengthCombo & heightUnitCO,
450 QCheckBox & aspectratioCB,
451 external::ResizeData const & data)
453 bool using_scale = data.usingScale();
454 std::string scale = data.scale;
455 if (data.no_resize()) {
456 // Everything is zero, so default to this!
462 widthED.setText(toqstr(scale));
463 widthUnitCO.setCurrentIndex(0);
465 widthED.setText(QString::number(data.width.value()));
466 // Because 'Scale' is position 0...
467 // Note also that width cannot be zero here, so
468 // we don't need to worry about the default unit.
469 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
472 string const h = data.height.zero() ? string() : data.height.asString();
473 Length::UNIT default_unit = data.width.zero() ?
474 defaultUnit() : data.width.unit();
475 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
477 heightED.setEnabled(!using_scale);
478 heightUnitCO.setEnabled(!using_scale);
480 aspectratioCB.setChecked(data.keepAspectRatio);
482 bool const disable_aspectRatio = using_scale ||
483 data.width.zero() || data.height.zero();
484 aspectratioCB.setEnabled(!disable_aspectRatio);
488 static void getSize(external::ResizeData & data,
489 QLineEdit const & widthED, QComboBox const & widthUnitCO,
490 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
491 QCheckBox const & aspectratioCB)
493 string const width = fromqstr(widthED.text());
495 if (widthUnitCO.currentIndex() > 0) {
496 // Subtract one, because scale is 0.
497 int const unit = widthUnitCO.currentIndex() - 1;
500 if (isValidLength(width, &w))
502 else if (isStrDbl(width))
503 data.width = Length(convert<double>(width),
504 static_cast<Length::UNIT>(unit));
506 data.width = Length();
508 data.scale = string();
511 // scaling instead of a width
513 data.width = Length();
516 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
518 data.keepAspectRatio = aspectratioCB.isChecked();
522 void setCrop(QCheckBox & clipCB,
523 QLineEdit & xlED, QLineEdit & ybED,
524 QLineEdit & xrED, QLineEdit & ytED,
525 external::ClipData const & data)
527 clipCB.setChecked(data.clip);
528 graphics::BoundingBox const & bbox = data.bbox;
529 xlED.setText(QString::number(bbox.xl));
530 ybED.setText(QString::number(bbox.yb));
531 xrED.setText(QString::number(bbox.xr));
532 ytED.setText(QString::number(bbox.yt));
536 static void getCrop(external::ClipData & data,
537 QCheckBox const & clipCB,
538 QLineEdit const & xlED, QLineEdit const & ybED,
539 QLineEdit const & xrED, QLineEdit const & ytED,
542 data.clip = clipCB.isChecked();
547 data.bbox.xl = xlED.text().toInt();
548 data.bbox.yb = ybED.text().toInt();
549 data.bbox.xr = xrED.text().toInt();
550 data.bbox.yt = ytED.text().toInt();
554 static void getExtra(external::ExtraData & data,
555 GuiExternal::MapType const & extra)
557 typedef GuiExternal::MapType MapType;
558 MapType::const_iterator it = extra.begin();
559 MapType::const_iterator const end = extra.end();
560 for (; it != end; ++it)
561 data.set(it->first, trim(fromqstr(it->second)));
565 void GuiExternal::updateContents()
567 tab->setCurrentIndex(0);
570 params_.filename.outputFilename(bufferFilepath());
571 fileED->setText(toqstr(name));
573 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
576 draftCB->setChecked(params_.draft);
578 setDisplay(*displayCB, *showCO, *displayscaleED,
579 params_.display, params_.lyxscale, isBufferReadonly());
581 setRotation(*angleED, *originCO, params_.rotationdata);
583 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
584 *aspectratioCB, params_.resizedata);
586 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
587 bbChanged_ = !params_.clipdata.bbox.empty();
593 void GuiExternal::updateTemplate()
595 external::Template templ = getTemplate(externalCO->currentIndex());
596 externalTB->setPlainText(qt_(templ.helpText));
598 // Ascertain which (if any) transformations the template supports
599 // and disable tabs hosting unsupported transforms.
600 typedef vector<external::TransformID> TransformIDs;
601 TransformIDs const transformIds = templ.transformIds;
602 TransformIDs::const_iterator tr_begin = transformIds.begin();
603 TransformIDs::const_iterator const tr_end = transformIds.end();
605 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
606 tab->setTabEnabled(tab->indexOf(rotatetab), found);
607 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
608 tab->setTabEnabled(tab->indexOf(scaletab), found);
610 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
611 tab->setTabEnabled(tab->indexOf(croptab), found);
613 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
614 tab->setTabEnabled(tab->indexOf(optionstab), found);
619 // Ascertain whether the template has any formats supporting
620 // the 'Extra' option
623 extraFormatCO->clear();
625 external::Template::Formats::const_iterator it = templ.formats.begin();
626 external::Template::Formats::const_iterator end = templ.formats.end();
627 for (; it != end; ++it) {
628 if (it->second.option_transformers.find(external::Extra) ==
629 it->second.option_transformers.end())
631 string const format = it->first;
632 string const opt = params_.extradata.get(format);
633 extraFormatCO->addItem(toqstr(format));
634 extra_[format] = toqstr(opt);
637 bool const enabled = extraFormatCO->count() > 0;
640 tab->indexOf(optionstab), enabled);
641 extraED->setEnabled(enabled && !isBufferReadonly());
642 extraFormatCO->setEnabled(enabled);
645 extraFormatCO->setCurrentIndex(0);
646 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
651 void GuiExternal::applyView()
653 params_.filename.set(internal_path(fromqstr(fileED->text())),
656 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
658 params_.draft = draftCB->isChecked();
660 getDisplay(params_.display, params_.lyxscale,
664 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
665 getRotation(params_.rotationdata, *angleED, *originCO);
667 if (tab->isTabEnabled(tab->indexOf(scaletab)))
668 getSize(params_.resizedata, *widthED, *widthUnitCO,
669 *heightED, *heightUnitCO, *aspectratioCB);
671 if (tab->isTabEnabled(tab->indexOf(croptab)))
672 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
673 *xrED, *ytED, bbChanged_);
675 if (tab->isTabEnabled(tab->indexOf(optionstab)))
676 getExtra(params_.extradata, extra_);
680 void GuiExternal::getBB()
687 string const filename = fromqstr(fileED->text());
688 if (filename.empty())
691 string const bb = readBB(filename);
695 xlED->setText(toqstr(token(bb, ' ', 0)));
696 ybED->setText(toqstr(token(bb, ' ', 1)));
697 xrED->setText(toqstr(token(bb, ' ', 2)));
698 ytED->setText(toqstr(token(bb, ' ', 3)));
704 bool GuiExternal::initialiseParams(string const & data)
706 InsetExternalMailer::string2params(data, buffer(), params_);
711 void GuiExternal::clearParams()
713 params_ = InsetExternalParams();
717 void GuiExternal::dispatchParams()
719 string const lfun = InsetExternalMailer::params2string(params_, buffer());
720 dispatch(FuncRequest(getLfun(), lfun));
724 void GuiExternal::editExternal()
727 string const lfun = InsetExternalMailer::params2string(params_, buffer());
728 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
732 vector<string> const GuiExternal::getTemplates() const
734 vector<string> result;
736 external::TemplateManager::Templates::const_iterator i1, i2;
737 i1 = external::TemplateManager::get().getTemplates().begin();
738 i2 = external::TemplateManager::get().getTemplates().end();
740 for (; i1 != i2; ++i1) {
741 result.push_back(i1->second.lyxName);
747 int GuiExternal::getTemplateNumber(string const & name) const
749 external::TemplateManager::Templates::const_iterator i1, i2;
750 i1 = external::TemplateManager::get().getTemplates().begin();
751 i2 = external::TemplateManager::get().getTemplates().end();
752 for (int i = 0; i1 != i2; ++i1, ++i) {
753 if (i1->second.lyxName == name)
757 // we can get here if a LyX document has a template not installed
763 external::Template GuiExternal::getTemplate(int i) const
765 external::TemplateManager::Templates::const_iterator i1
766 = external::TemplateManager::get().getTemplates().begin();
775 GuiExternal::getTemplateFilters(string const & template_name) const
777 /// Determine the template file extension
778 external::TemplateManager const & etm =
779 external::TemplateManager::get();
780 external::Template const * const et_ptr =
781 etm.getTemplateByName(template_name);
784 return et_ptr->fileRegExp;
790 docstring const GuiExternal::browse(docstring const & input,
791 docstring const & template_name) const
793 docstring const title = _("Select external file");
795 docstring const bufpath = from_utf8(bufferFilepath());
796 FileFilterList const filter =
797 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
799 docstring const label1 = _("Documents|#o#O");
800 docstring const dir1 = from_utf8(lyxrc.document_path);
802 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
806 string const GuiExternal::readBB(string const & file)
808 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
810 // try to get it from the file, if possible. Zipped files are
811 // unzipped in the readBB_from_PSFile-Function
812 string const bb = readBB_from_PSFile(abs_file);
816 // we don't, so ask the Graphics Cache if it has loaded the file
820 graphics::Cache & gc = graphics::Cache::get();
821 if (gc.inCache(abs_file)) {
822 graphics::Image const * image = gc.item(abs_file)->image();
825 width = image->width();
826 height = image->height();
830 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
834 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
837 } // namespace frontend
840 #include "GuiExternal_moc.cpp"