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::closeEvent(QCloseEvent * e)
281 void GuiExternal::editClicked()
288 void GuiExternal::extraChanged(const QString& text)
290 string const format = fromqstr(extraFormatCO->currentText());
291 extra_[format] = text;
296 void GuiExternal::formatChanged(const QString& format)
298 extraED->setText(extra_[fromqstr(format)]);
302 void GuiExternal::getbbClicked()
308 void GuiExternal::sizeChanged()
310 aspectratioCB->setEnabled(activateAspectratio());
315 void GuiExternal::templateChanged()
322 void GuiExternal::widthUnitChanged()
324 bool useHeight = (widthUnitCO->currentIndex() > 0);
327 widthED->setValidator(unsignedLengthValidator(widthED));
329 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
331 heightED->setEnabled(useHeight);
332 heightUnitCO->setEnabled(useHeight);
337 static Length::UNIT defaultUnit()
339 Length::UNIT default_unit = Length::CM;
340 switch (lyxrc.default_papersize) {
343 case PAPER_USEXECUTIVE:
344 default_unit = Length::IN;
353 static void setDisplay(
354 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
355 external::DisplayType display, unsigned int scale, bool read_only)
359 case external::DefaultDisplay:
362 case external::MonochromeDisplay:
365 case external::GrayscaleDisplay:
368 case external::ColorDisplay:
371 case external::PreviewDisplay:
374 case external::NoDisplay:
379 showCO.setCurrentIndex(item);
380 bool const no_display = display == external::NoDisplay;
381 showCO.setEnabled(!no_display && !read_only);
382 displayCB.setChecked(!no_display);
383 scaleED.setEnabled(!no_display && !read_only);
384 scaleED.setText(QString::number(scale));
388 static void getDisplay(external::DisplayType & display,
389 unsigned int & scale,
390 QCheckBox const & displayCB,
391 QComboBox const & showCO,
392 QLineEdit const & scaleED)
394 switch (showCO.currentIndex()) {
396 display = external::DefaultDisplay;
399 display = external::MonochromeDisplay;
402 display = external::GrayscaleDisplay;
405 display = external::ColorDisplay;
408 display = external::PreviewDisplay;
412 if (!displayCB.isChecked())
413 display = external::NoDisplay;
415 scale = scaleED.text().toInt();
419 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
420 external::RotationData const & data)
422 originCO.setCurrentIndex(int(data.origin()));
423 angleED.setText(toqstr(data.angle));
427 static void getRotation(external::RotationData & data,
428 QLineEdit const & angleED, QComboBox const & originCO)
430 typedef external::RotationData::OriginType OriginType;
432 data.origin(static_cast<OriginType>(originCO.currentIndex()));
433 data.angle = fromqstr(angleED.text());
437 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
438 QLineEdit & heightED, LengthCombo & heightUnitCO,
439 QCheckBox & aspectratioCB,
440 external::ResizeData const & data)
442 bool using_scale = data.usingScale();
443 string scale = data.scale;
444 if (data.no_resize()) {
445 // Everything is zero, so default to this!
451 widthED.setText(toqstr(scale));
452 widthUnitCO.setCurrentIndex(0);
454 widthED.setText(QString::number(data.width.value()));
455 // Because 'Scale' is position 0...
456 // Note also that width cannot be zero here, so
457 // we don't need to worry about the default unit.
458 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
461 string const h = data.height.zero() ? string() : data.height.asString();
462 Length::UNIT default_unit = data.width.zero() ?
463 defaultUnit() : data.width.unit();
464 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
466 heightED.setEnabled(!using_scale);
467 heightUnitCO.setEnabled(!using_scale);
469 aspectratioCB.setChecked(data.keepAspectRatio);
471 bool const disable_aspectRatio = using_scale ||
472 data.width.zero() || data.height.zero();
473 aspectratioCB.setEnabled(!disable_aspectRatio);
477 static void getSize(external::ResizeData & data,
478 QLineEdit const & widthED, QComboBox const & widthUnitCO,
479 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
480 QCheckBox const & aspectratioCB)
482 string const width = fromqstr(widthED.text());
484 if (widthUnitCO.currentIndex() > 0) {
485 // Subtract one, because scale is 0.
486 int const unit = widthUnitCO.currentIndex() - 1;
489 if (isValidLength(width, &w))
491 else if (isStrDbl(width))
492 data.width = Length(convert<double>(width),
493 static_cast<Length::UNIT>(unit));
495 data.width = Length();
497 data.scale = string();
500 // scaling instead of a width
502 data.width = Length();
505 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
507 data.keepAspectRatio = aspectratioCB.isChecked();
511 void setCrop(QCheckBox & clipCB,
512 QLineEdit & xlED, QLineEdit & ybED,
513 QLineEdit & xrED, QLineEdit & ytED,
514 external::ClipData const & data)
516 clipCB.setChecked(data.clip);
517 graphics::BoundingBox const & bbox = data.bbox;
518 xlED.setText(QString::number(bbox.xl));
519 ybED.setText(QString::number(bbox.yb));
520 xrED.setText(QString::number(bbox.xr));
521 ytED.setText(QString::number(bbox.yt));
525 static void getCrop(external::ClipData & data,
526 QCheckBox const & clipCB,
527 QLineEdit const & xlED, QLineEdit const & ybED,
528 QLineEdit const & xrED, QLineEdit const & ytED,
531 data.clip = clipCB.isChecked();
536 data.bbox.xl = xlED.text().toInt();
537 data.bbox.yb = ybED.text().toInt();
538 data.bbox.xr = xrED.text().toInt();
539 data.bbox.yt = ytED.text().toInt();
543 static void getExtra(external::ExtraData & data,
544 GuiExternal::MapType const & extra)
546 typedef GuiExternal::MapType MapType;
547 MapType::const_iterator it = extra.begin();
548 MapType::const_iterator const end = extra.end();
549 for (; it != end; ++it)
550 data.set(it->first, trim(fromqstr(it->second)));
554 void GuiExternal::updateContents()
556 tab->setCurrentIndex(0);
559 params_.filename.outputFilename(bufferFilepath());
560 fileED->setText(toqstr(name));
561 embedCB->setCheckState(params_.filename.embedded() ? Qt::Checked : Qt::Unchecked);
563 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
566 draftCB->setChecked(params_.draft);
568 setDisplay(*displayCB, *showCO, *displayscaleED,
569 params_.display, params_.lyxscale, isBufferReadonly());
571 setRotation(*angleED, *originCO, params_.rotationdata);
573 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
574 *aspectratioCB, params_.resizedata);
576 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
577 bbChanged_ = !params_.clipdata.bbox.empty();
583 void GuiExternal::updateTemplate()
585 external::Template templ = getTemplate(externalCO->currentIndex());
586 externalTB->setPlainText(qt_(templ.helpText));
588 // Ascertain which (if any) transformations the template supports
589 // and disable tabs hosting unsupported transforms.
590 typedef vector<external::TransformID> TransformIDs;
591 TransformIDs const transformIds = templ.transformIds;
592 TransformIDs::const_iterator tr_begin = transformIds.begin();
593 TransformIDs::const_iterator const tr_end = transformIds.end();
595 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
596 tab->setTabEnabled(tab->indexOf(rotatetab), found);
597 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
598 tab->setTabEnabled(tab->indexOf(scaletab), found);
600 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
601 tab->setTabEnabled(tab->indexOf(croptab), found);
603 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
604 tab->setTabEnabled(tab->indexOf(optionstab), found);
609 // Ascertain whether the template has any formats supporting
610 // the 'Extra' option
613 extraFormatCO->clear();
615 external::Template::Formats::const_iterator it = templ.formats.begin();
616 external::Template::Formats::const_iterator end = templ.formats.end();
617 for (; it != end; ++it) {
618 if (it->second.option_transformers.find(external::Extra) ==
619 it->second.option_transformers.end())
621 string const format = it->first;
622 string const opt = params_.extradata.get(format);
623 extraFormatCO->addItem(toqstr(format));
624 extra_[format] = toqstr(opt);
627 bool const enabled = extraFormatCO->count() > 0;
630 tab->indexOf(optionstab), enabled);
631 extraED->setEnabled(enabled && !isBufferReadonly());
632 extraFormatCO->setEnabled(enabled);
635 extraFormatCO->setCurrentIndex(0);
636 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
641 void GuiExternal::applyView()
643 params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
644 params_.filename.setEmbed(embedCB->checkState() == Qt::Checked);
646 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
648 params_.draft = draftCB->isChecked();
650 getDisplay(params_.display, params_.lyxscale,
654 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
655 getRotation(params_.rotationdata, *angleED, *originCO);
657 if (tab->isTabEnabled(tab->indexOf(scaletab)))
658 getSize(params_.resizedata, *widthED, *widthUnitCO,
659 *heightED, *heightUnitCO, *aspectratioCB);
661 if (tab->isTabEnabled(tab->indexOf(croptab)))
662 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
663 *xrED, *ytED, bbChanged_);
665 if (tab->isTabEnabled(tab->indexOf(optionstab)))
666 getExtra(params_.extradata, extra_);
670 void GuiExternal::getBB()
677 string const filename = fromqstr(fileED->text());
678 if (filename.empty())
681 string const bb = readBB(filename);
685 xlED->setText(toqstr(token(bb, ' ', 0)));
686 ybED->setText(toqstr(token(bb, ' ', 1)));
687 xrED->setText(toqstr(token(bb, ' ', 2)));
688 ytED->setText(toqstr(token(bb, ' ', 3)));
694 bool GuiExternal::initialiseParams(string const & data)
696 InsetExternalMailer::string2params(data, buffer(), params_);
701 void GuiExternal::clearParams()
703 params_ = InsetExternalParams();
707 void GuiExternal::dispatchParams()
709 string const lfun = InsetExternalMailer::params2string(params_, buffer());
710 dispatch(FuncRequest(getLfun(), lfun));
714 void GuiExternal::editExternal()
717 string const lfun = InsetExternalMailer::params2string(params_, buffer());
718 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
722 vector<string> const GuiExternal::getTemplates() const
724 vector<string> result;
726 external::TemplateManager::Templates::const_iterator i1, i2;
727 i1 = external::TemplateManager::get().getTemplates().begin();
728 i2 = external::TemplateManager::get().getTemplates().end();
730 for (; i1 != i2; ++i1) {
731 result.push_back(i1->second.lyxName);
737 int GuiExternal::getTemplateNumber(string const & name) const
739 external::TemplateManager::Templates::const_iterator i1, i2;
740 i1 = external::TemplateManager::get().getTemplates().begin();
741 i2 = external::TemplateManager::get().getTemplates().end();
742 for (int i = 0; i1 != i2; ++i1, ++i) {
743 if (i1->second.lyxName == name)
747 // we can get here if a LyX document has a template not installed
753 external::Template GuiExternal::getTemplate(int i) const
755 external::TemplateManager::Templates::const_iterator i1
756 = external::TemplateManager::get().getTemplates().begin();
765 GuiExternal::getTemplateFilters(string const & template_name) const
767 /// Determine the template file extension
768 external::TemplateManager const & etm =
769 external::TemplateManager::get();
770 external::Template const * const et_ptr =
771 etm.getTemplateByName(template_name);
774 return et_ptr->fileRegExp;
780 docstring const GuiExternal::browse(docstring const & input,
781 docstring const & template_name) const
783 docstring const title = _("Select external file");
785 docstring const bufpath = from_utf8(bufferFilepath());
786 FileFilterList const filter =
787 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
789 docstring const label1 = _("Documents|#o#O");
790 docstring const dir1 = from_utf8(lyxrc.document_path);
792 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
796 string const GuiExternal::readBB(string const & file)
798 FileName const abs_file(makeAbsPath(file, bufferFilepath()));
800 // try to get it from the file, if possible. Zipped files are
801 // unzipped in the readBB_from_PSFile-Function
802 string const bb = readBB_from_PSFile(abs_file);
806 // we don't, so ask the Graphics Cache if it has loaded the file
810 graphics::Cache & gc = graphics::Cache::get();
811 if (gc.inCache(abs_file)) {
812 graphics::Image const * image = gc.item(abs_file)->image();
815 width = image->width();
816 height = image->height();
820 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
824 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
827 } // namespace frontend
830 #include "GuiExternal_moc.cpp"