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"
43 #include <QPushButton>
45 #include <QTextBrowser>
48 using namespace lyx::support;
53 using namespace external;
57 RotationDataType origins[] = {
58 RotationData::DEFAULT,
59 RotationData::TOPLEFT,
60 RotationData::BOTTOMLEFT,
61 RotationData::BASELINELEFT,
63 RotationData::TOPCENTER,
64 RotationData::BOTTOMCENTER,
65 RotationData::BASELINECENTER,
66 RotationData::TOPRIGHT,
67 RotationData::BOTTOMRIGHT,
68 RotationData::BASELINERIGHT
72 // These are the strings, corresponding to the above, that the GUI should
73 // use. Note that they can/should be translated.
74 char const * const origin_gui_strs[] = {
76 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
77 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
78 N_("Top right"), N_("Bottom right"), N_("Baseline right")
81 external::Template getTemplate(int i)
83 external::TemplateManager::Templates::const_iterator i1
84 = external::TemplateManager::get().getTemplates().begin();
92 GuiExternal::GuiExternal(GuiView & lv)
93 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
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(editPB, SIGNAL(clicked()),
114 this, SLOT(editClicked()));
115 connect(externalCO, SIGNAL(activated(QString)),
116 this, SLOT(templateChanged()));
117 connect(extraED, SIGNAL(textChanged(QString)),
118 this, SLOT(extraChanged(QString)));
119 connect(extraFormatCO, SIGNAL(activated(QString)),
120 this, SLOT(formatChanged(QString)));
121 connect(widthUnitCO, SIGNAL(activated(int)),
122 this, SLOT(widthUnitChanged()));
123 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
124 this, SLOT(change_adaptor()));
125 connect(displayCB, SIGNAL(stateChanged(int)),
126 this, SLOT(change_adaptor()));
127 connect(displayscaleED, SIGNAL(textChanged(QString)),
128 this, SLOT(change_adaptor()));
129 connect(angleED, SIGNAL(textChanged(QString)),
130 this, SLOT(change_adaptor()));
131 connect(widthED, SIGNAL(textChanged(QString)),
132 this, SLOT(sizeChanged()));
133 connect(heightED, SIGNAL(textChanged(QString)),
134 this, SLOT(sizeChanged()));
135 connect(fileED, SIGNAL(textChanged(QString)),
136 this, SLOT(change_adaptor()));
137 connect(clipCB, SIGNAL(stateChanged(int)),
138 this, SLOT(change_adaptor()));
139 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
140 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
144 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
146 QIntValidator * validator = new QIntValidator(displayscaleED);
147 validator->setBottom(1);
148 displayscaleED->setValidator(validator);
150 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
152 xlED->setValidator(new QIntValidator(xlED));
153 ybED->setValidator(new QIntValidator(ybED));
154 xrED->setValidator(new QIntValidator(xrED));
155 ytED->setValidator(new QIntValidator(ytED));
157 widthED->setValidator(unsignedLengthValidator(widthED));
158 heightED->setValidator(unsignedLengthValidator(heightED));
160 setFocusProxy(fileED);
162 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
165 bc().setApply(applyPB);
166 bc().setCancel(closePB);
168 bc().addReadOnly(fileED);
169 bc().addReadOnly(browsePB);
170 bc().addReadOnly(editPB);
171 bc().addReadOnly(externalCO);
172 bc().addReadOnly(draftCB);
173 bc().addReadOnly(displayscaleED);
174 bc().addReadOnly(showCO);
175 bc().addReadOnly(displayCB);
176 bc().addReadOnly(angleED);
177 bc().addReadOnly(originCO);
178 bc().addReadOnly(heightUnitCO);
179 bc().addReadOnly(heightED);
180 bc().addReadOnly(aspectratioCB);
181 bc().addReadOnly(widthUnitCO);
182 bc().addReadOnly(widthED);
183 bc().addReadOnly(clipCB);
184 bc().addReadOnly(getbbPB);
185 bc().addReadOnly(ytED);
186 bc().addReadOnly(xlED);
187 bc().addReadOnly(xrED);
188 bc().addReadOnly(ybED);
189 bc().addReadOnly(extraFormatCO);
190 bc().addReadOnly(extraED);
192 bc().addCheckedLineEdit(angleED, angleLA);
193 bc().addCheckedLineEdit(displayscaleED, scaleLA);
194 bc().addCheckedLineEdit(heightED, heightLA);
195 bc().addCheckedLineEdit(widthED, widthLA);
196 bc().addCheckedLineEdit(xlED, lbLA);
197 bc().addCheckedLineEdit(ybED, lbLA);
198 bc().addCheckedLineEdit(xrED, rtLA);
199 bc().addCheckedLineEdit(ytED, rtLA);
200 bc().addCheckedLineEdit(fileED, fileLA);
202 external::TemplateManager::Templates::const_iterator i1, i2;
203 i1 = external::TemplateManager::get().getTemplates().begin();
204 i2 = external::TemplateManager::get().getTemplates().end();
205 for (; i1 != i2; ++i1)
206 externalCO->addItem(qt_(i1->second.lyxName));
208 // Fill the origins combo
209 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
210 originCO->addItem(qt_(origin_gui_strs[i]));
212 // Fill the width combo
213 widthUnitCO->addItem(qt_("Scale%"));
214 for (int i = 0; i < num_units; i++)
215 widthUnitCO->addItem(qt_(unit_name_gui[i]));
219 bool GuiExternal::activateAspectratio() const
221 if (widthUnitCO->currentIndex() == 0)
224 string const wstr = fromqstr(widthED->text());
227 bool const wIsDbl = isStrDbl(wstr);
228 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
231 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
234 string const hstr = fromqstr(heightED->text());
237 bool const hIsDbl = isStrDbl(hstr);
238 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
240 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
247 void GuiExternal::bbChanged()
254 void GuiExternal::browseClicked()
256 int const choice = externalCO->currentIndex();
257 QString const template_name = toqstr(getTemplate(choice).lyxName);
258 QString const str = browse(fileED->text(), template_name);
259 if (!str.isEmpty()) {
260 fileED->setText(str);
266 void GuiExternal::change_adaptor()
272 void GuiExternal::editClicked()
278 void GuiExternal::extraChanged(const QString & text)
280 extra_[extraFormatCO->currentText()] = text;
285 void GuiExternal::formatChanged(const QString & format)
287 extraED->setText(extra_[format]);
291 void GuiExternal::getbbClicked()
298 string const filename = fromqstr(fileED->text());
299 if (filename.empty())
302 FileName const abs_file(makeAbsPath(filename, fromqstr(bufferFilepath())));
304 // try to get it from the file, if possible
305 string bb = readBB_from_PSFile(abs_file);
307 // we don't, so ask the Graphics Cache if it has loaded the file
311 graphics::Cache & gc = graphics::Cache::get();
312 if (gc.inCache(abs_file)) {
313 graphics::Image const * image = gc.item(abs_file)->image();
316 width = image->width();
317 height = image->height();
320 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
323 xlED->setText(toqstr(token(bb, ' ', 0)));
324 ybED->setText(toqstr(token(bb, ' ', 1)));
325 xrED->setText(toqstr(token(bb, ' ', 2)));
326 ytED->setText(toqstr(token(bb, ' ', 3)));
332 void GuiExternal::sizeChanged()
334 aspectratioCB->setEnabled(activateAspectratio());
339 void GuiExternal::templateChanged()
346 void GuiExternal::widthUnitChanged()
348 bool useHeight = (widthUnitCO->currentIndex() > 0);
351 widthED->setValidator(unsignedLengthValidator(widthED));
353 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
355 heightED->setEnabled(useHeight);
356 heightUnitCO->setEnabled(useHeight);
361 static Length::UNIT defaultUnit()
363 Length::UNIT default_unit = Length::CM;
364 switch (lyxrc.default_papersize) {
367 case PAPER_USEXECUTIVE:
368 default_unit = Length::IN;
377 static void setDisplay(
378 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
379 external::DisplayType display, unsigned int scale, bool read_only)
383 case external::DefaultDisplay:
386 case external::MonochromeDisplay:
389 case external::GrayscaleDisplay:
392 case external::ColorDisplay:
395 case external::PreviewDisplay:
398 case external::NoDisplay:
403 showCO.setCurrentIndex(item);
404 bool const no_display = display == external::NoDisplay;
405 showCO.setEnabled(!no_display && !read_only);
406 displayCB.setChecked(!no_display);
407 scaleED.setEnabled(!no_display && !read_only);
408 scaleED.setText(QString::number(scale));
412 static external::DisplayType display(QCheckBox const & displayCB,
413 QComboBox const & showCO)
415 if (!displayCB.isChecked())
416 return external::NoDisplay;
417 switch (showCO.currentIndex()) {
420 return external::DefaultDisplay;
422 return external::MonochromeDisplay;
424 return external::GrayscaleDisplay;
426 return external::ColorDisplay;
428 return external::PreviewDisplay;
433 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
434 external::RotationData const & data)
436 originCO.setCurrentIndex(int(data.origin()));
437 angleED.setText(toqstr(data.angle));
441 static void getRotation(external::RotationData & data,
442 QLineEdit const & angleED, QComboBox const & originCO)
444 typedef external::RotationData::OriginType OriginType;
446 data.origin(static_cast<OriginType>(originCO.currentIndex()));
447 data.angle = fromqstr(angleED.text());
451 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
452 QLineEdit & heightED, LengthCombo & heightUnitCO,
453 QCheckBox & aspectratioCB,
454 external::ResizeData const & data)
456 bool using_scale = data.usingScale();
457 string scale = data.scale;
458 if (data.no_resize()) {
459 // Everything is zero, so default to this!
465 widthED.setText(toqstr(scale));
466 widthUnitCO.setCurrentIndex(0);
468 widthED.setText(QString::number(data.width.value()));
469 // Because 'Scale' is position 0...
470 // Note also that width cannot be zero here, so
471 // we don't need to worry about the default unit.
472 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
475 string const h = data.height.zero() ? string() : data.height.asString();
476 Length::UNIT default_unit = data.width.zero() ?
477 defaultUnit() : data.width.unit();
478 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
480 heightED.setEnabled(!using_scale);
481 heightUnitCO.setEnabled(!using_scale);
483 aspectratioCB.setChecked(data.keepAspectRatio);
485 bool const disable_aspectRatio = using_scale ||
486 data.width.zero() || data.height.zero();
487 aspectratioCB.setEnabled(!disable_aspectRatio);
491 static void getSize(external::ResizeData & data,
492 QLineEdit const & widthED, QComboBox const & widthUnitCO,
493 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
494 QCheckBox const & aspectratioCB)
496 string const width = fromqstr(widthED.text());
498 if (widthUnitCO.currentIndex() > 0) {
499 // Subtract one, because scale is 0.
500 int const unit = widthUnitCO.currentIndex() - 1;
503 if (isValidLength(width, &w))
505 else if (isStrDbl(width))
506 data.width = Length(convert<double>(width),
507 static_cast<Length::UNIT>(unit));
509 data.width = Length();
511 data.scale = string();
514 // scaling instead of a width
516 data.width = Length();
519 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
521 data.keepAspectRatio = aspectratioCB.isChecked();
525 void setCrop(QCheckBox & clipCB,
526 QLineEdit & xlED, QLineEdit & ybED,
527 QLineEdit & xrED, QLineEdit & ytED,
528 external::ClipData const & data)
530 clipCB.setChecked(data.clip);
531 graphics::BoundingBox const & bbox = data.bbox;
532 xlED.setText(QString::number(bbox.xl));
533 ybED.setText(QString::number(bbox.yb));
534 xrED.setText(QString::number(bbox.xr));
535 ytED.setText(QString::number(bbox.yt));
539 static void getCrop(external::ClipData & data,
540 QCheckBox const & clipCB,
541 QLineEdit const & xlED, QLineEdit const & ybED,
542 QLineEdit const & xrED, QLineEdit const & ytED,
545 data.clip = clipCB.isChecked();
550 data.bbox.xl = xlED.text().toInt();
551 data.bbox.yb = ybED.text().toInt();
552 data.bbox.xr = xrED.text().toInt();
553 data.bbox.yt = ytED.text().toInt();
557 void GuiExternal::updateContents()
559 tab->setCurrentIndex(0);
562 params_.filename.outputFilename(fromqstr(bufferFilepath()));
563 fileED->setText(toqstr(name));
566 external::TemplateManager::Templates::const_iterator i1, i2;
567 i1 = external::TemplateManager::get().getTemplates().begin();
568 i2 = external::TemplateManager::get().getTemplates().end();
569 for (int i = 0; i1 != i2; ++i1, ++i) {
570 if (i1->second.lyxName == params_.templatename()) {
576 externalCO->setCurrentIndex(index);
579 draftCB->setChecked(params_.draft);
581 setDisplay(*displayCB, *showCO, *displayscaleED,
582 params_.display, params_.lyxscale, isBufferReadonly());
584 setRotation(*angleED, *originCO, params_.rotationdata);
586 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
587 *aspectratioCB, params_.resizedata);
589 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
590 bbChanged_ = !params_.clipdata.bbox.empty();
596 void GuiExternal::updateTemplate()
598 external::Template templ = getTemplate(externalCO->currentIndex());
599 externalTB->setPlainText(qt_(templ.helpText));
601 // Ascertain which (if any) transformations the template supports
602 // and disable tabs hosting unsupported transforms.
603 typedef vector<external::TransformID> TransformIDs;
604 TransformIDs const transformIds = templ.transformIds;
605 TransformIDs::const_iterator tr_begin = transformIds.begin();
606 TransformIDs::const_iterator const tr_end = transformIds.end();
608 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
609 tab->setTabEnabled(tab->indexOf(rotatetab), found);
610 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
611 tab->setTabEnabled(tab->indexOf(scaletab), found);
613 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
614 tab->setTabEnabled(tab->indexOf(croptab), found);
616 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
617 tab->setTabEnabled(tab->indexOf(optionstab), found);
622 // Ascertain whether the template has any formats supporting
623 // the 'Extra' option
626 extraFormatCO->clear();
628 external::Template::Formats::const_iterator it = templ.formats.begin();
629 external::Template::Formats::const_iterator end = templ.formats.end();
630 for (; it != end; ++it) {
631 if (it->second.option_transformers.find(external::Extra) ==
632 it->second.option_transformers.end())
634 string const format = it->first;
635 string const opt = params_.extradata.get(format);
636 extraFormatCO->addItem(toqstr(format));
637 extra_[toqstr(format)] = toqstr(opt);
640 bool const enabled = extraFormatCO->count() > 0;
642 tab->setTabEnabled(tab->indexOf(optionstab), enabled);
643 extraED->setEnabled(enabled && !isBufferReadonly());
644 extraFormatCO->setEnabled(enabled);
647 extraFormatCO->setCurrentIndex(0);
648 extraED->setText(extra_[extraFormatCO->currentText()]);
653 void GuiExternal::applyView()
655 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
656 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
658 params_.draft = draftCB->isChecked();
659 params_.lyxscale = displayscaleED->text().toInt();
660 params_.display = display(*displayCB, *showCO);
662 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
663 getRotation(params_.rotationdata, *angleED, *originCO);
665 if (tab->isTabEnabled(tab->indexOf(scaletab)))
666 getSize(params_.resizedata, *widthED, *widthUnitCO,
667 *heightED, *heightUnitCO, *aspectratioCB);
669 if (tab->isTabEnabled(tab->indexOf(croptab)))
670 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
671 *xrED, *ytED, bbChanged_);
673 if (tab->isTabEnabled(tab->indexOf(optionstab))) {
674 MapType::const_iterator it = extra_.begin();
675 MapType::const_iterator const end = extra_.end();
676 for (; it != end; ++it)
677 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
682 bool GuiExternal::initialiseParams(string const & data)
684 InsetExternal::string2params(data, buffer(), params_);
689 void GuiExternal::clearParams()
691 params_ = InsetExternalParams();
695 void GuiExternal::dispatchParams()
697 string const lfun = InsetExternal::params2string(params_, buffer());
698 dispatch(FuncRequest(getLfun(), lfun));
702 void GuiExternal::editExternal()
705 string const lfun = InsetExternal::params2string(params_, buffer());
706 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
710 static string templateFilters(string const & template_name)
712 /// Determine the template file extension
713 external::TemplateManager const & etm =
714 external::TemplateManager::get();
715 external::Template const * const et_ptr =
716 etm.getTemplateByName(template_name);
717 return et_ptr ? et_ptr->fileRegExp : string();
721 QString GuiExternal::browse(QString const & input,
722 QString const & template_name) const
724 QString const title = qt_("Select external file");
725 QString const bufpath = bufferFilepath();
726 FileFilterList const filter =
727 FileFilterList(from_utf8(templateFilters(fromqstr(template_name))));
729 QString const label1 = qt_("Documents|#o#O");
730 QString const dir1 = toqstr(lyxrc.document_path);
732 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
736 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
739 } // namespace frontend
742 #include "GuiExternal_moc.cpp"