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/filetools.h"
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
34 #include "support/os.h"
36 #include "LengthCombo.h"
37 #include "qt_helpers.h"
38 #include "Validator.h"
42 #include <QPushButton>
44 #include <QTextBrowser>
47 using namespace lyx::support;
52 using namespace external;
56 RotationDataType origins[] = {
57 RotationData::DEFAULT,
58 RotationData::TOPLEFT,
59 RotationData::BOTTOMLEFT,
60 RotationData::BASELINELEFT,
62 RotationData::TOPCENTER,
63 RotationData::BOTTOMCENTER,
64 RotationData::BASELINECENTER,
65 RotationData::TOPRIGHT,
66 RotationData::BOTTOMRIGHT,
67 RotationData::BASELINERIGHT
71 // These are the strings, corresponding to the above, that the GUI should
72 // use. Note that they can/should be translated.
73 char const * const origin_gui_strs[] = {
75 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
76 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
77 N_("Top right"), N_("Bottom right"), N_("Baseline right")
80 external::Template getTemplate(int i)
82 external::TemplateManager::Templates::const_iterator i1
83 = external::TemplateManager::get().getTemplates().begin();
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(editPB, SIGNAL(clicked()),
113 this, SLOT(editClicked()));
114 connect(externalCO, SIGNAL(activated(QString)),
115 this, SLOT(templateChanged()));
116 connect(extraED, SIGNAL(textChanged(QString)),
117 this, SLOT(extraChanged(QString)));
118 connect(extraFormatCO, SIGNAL(activated(QString)),
119 this, SLOT(formatChanged(QString)));
120 connect(widthUnitCO, SIGNAL(activated(int)),
121 this, SLOT(widthUnitChanged()));
122 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
123 this, SLOT(change_adaptor()));
124 connect(displayCB, SIGNAL(stateChanged(int)),
125 this, SLOT(change_adaptor()));
126 connect(displayscaleED, SIGNAL(textChanged(QString)),
127 this, SLOT(change_adaptor()));
128 connect(angleED, SIGNAL(textChanged(QString)),
129 this, SLOT(change_adaptor()));
130 connect(widthED, SIGNAL(textChanged(QString)),
131 this, SLOT(sizeChanged()));
132 connect(heightED, SIGNAL(textChanged(QString)),
133 this, SLOT(sizeChanged()));
134 connect(fileED, SIGNAL(textChanged(QString)),
135 this, SLOT(change_adaptor()));
136 connect(clipCB, SIGNAL(stateChanged(int)),
137 this, SLOT(change_adaptor()));
138 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
139 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
140 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
143 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
145 QIntValidator * validator = new QIntValidator(displayscaleED);
146 validator->setBottom(1);
147 displayscaleED->setValidator(validator);
149 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
151 xlED->setValidator(new QIntValidator(xlED));
152 ybED->setValidator(new QIntValidator(ybED));
153 xrED->setValidator(new QIntValidator(xrED));
154 ytED->setValidator(new QIntValidator(ytED));
156 widthED->setValidator(unsignedLengthValidator(widthED));
157 heightED->setValidator(unsignedLengthValidator(heightED));
159 setFocusProxy(fileED);
161 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
164 bc().setApply(applyPB);
165 bc().setCancel(closePB);
167 bc().addReadOnly(fileED);
168 bc().addReadOnly(browsePB);
169 bc().addReadOnly(editPB);
170 bc().addReadOnly(externalCO);
171 bc().addReadOnly(draftCB);
172 bc().addReadOnly(displayscaleED);
173 bc().addReadOnly(showCO);
174 bc().addReadOnly(displayCB);
175 bc().addReadOnly(angleED);
176 bc().addReadOnly(originCO);
177 bc().addReadOnly(heightUnitCO);
178 bc().addReadOnly(heightED);
179 bc().addReadOnly(aspectratioCB);
180 bc().addReadOnly(widthUnitCO);
181 bc().addReadOnly(widthED);
182 bc().addReadOnly(clipCB);
183 bc().addReadOnly(getbbPB);
184 bc().addReadOnly(ytED);
185 bc().addReadOnly(xlED);
186 bc().addReadOnly(xrED);
187 bc().addReadOnly(ybED);
188 bc().addReadOnly(extraFormatCO);
189 bc().addReadOnly(extraED);
191 bc().addCheckedLineEdit(angleED, angleLA);
192 bc().addCheckedLineEdit(displayscaleED, scaleLA);
193 bc().addCheckedLineEdit(heightED, heightLA);
194 bc().addCheckedLineEdit(widthED, widthLA);
195 bc().addCheckedLineEdit(xlED, lbLA);
196 bc().addCheckedLineEdit(ybED, lbLA);
197 bc().addCheckedLineEdit(xrED, rtLA);
198 bc().addCheckedLineEdit(ytED, rtLA);
199 bc().addCheckedLineEdit(fileED, fileLA);
201 external::TemplateManager::Templates::const_iterator i1, i2;
202 i1 = external::TemplateManager::get().getTemplates().begin();
203 i2 = external::TemplateManager::get().getTemplates().end();
204 for (; i1 != i2; ++i1)
205 externalCO->addItem(qt_(i1->second.lyxName));
207 // Fill the origins combo
208 for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
209 originCO->addItem(qt_(origin_gui_strs[i]));
211 // Fill the width combo
212 widthUnitCO->addItem(qt_("Scale%"));
213 for (int i = 0; i < num_units; i++)
214 widthUnitCO->addItem(qt_(unit_name_gui[i]));
218 bool GuiExternal::activateAspectratio() const
220 if (widthUnitCO->currentIndex() == 0)
223 string const wstr = fromqstr(widthED->text());
226 bool const wIsDbl = isStrDbl(wstr);
227 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
230 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
233 string const hstr = fromqstr(heightED->text());
236 bool const hIsDbl = isStrDbl(hstr);
237 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
239 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
246 void GuiExternal::bbChanged()
253 void GuiExternal::browseClicked()
255 int const choice = externalCO->currentIndex();
256 QString const template_name = toqstr(getTemplate(choice).lyxName);
257 QString const str = browse(fileED->text(), template_name);
258 if (!str.isEmpty()) {
259 fileED->setText(str);
265 void GuiExternal::change_adaptor()
271 void GuiExternal::editClicked()
277 void GuiExternal::extraChanged(const QString & text)
279 extra_[extraFormatCO->currentText()] = text;
284 void GuiExternal::formatChanged(const QString & format)
286 extraED->setText(extra_[format]);
290 void GuiExternal::getbbClicked()
297 string const filename = fromqstr(fileED->text());
298 if (filename.empty())
301 FileName const abs_file(makeAbsPath(filename, fromqstr(bufferFilepath())));
303 // try to get it from the file, if possible
304 string bb = readBB_from_PSFile(abs_file);
306 // we don't, so ask the Graphics Cache if it has loaded the file
310 graphics::Cache & gc = graphics::Cache::get();
311 if (gc.inCache(abs_file)) {
312 graphics::Image const * image = gc.item(abs_file)->image();
315 width = image->width();
316 height = image->height();
319 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
322 xlED->setText(toqstr(token(bb, ' ', 0)));
323 ybED->setText(toqstr(token(bb, ' ', 1)));
324 xrED->setText(toqstr(token(bb, ' ', 2)));
325 ytED->setText(toqstr(token(bb, ' ', 3)));
331 void GuiExternal::sizeChanged()
333 aspectratioCB->setEnabled(activateAspectratio());
338 void GuiExternal::templateChanged()
345 void GuiExternal::widthUnitChanged()
347 bool useHeight = (widthUnitCO->currentIndex() > 0);
350 widthED->setValidator(unsignedLengthValidator(widthED));
352 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
354 heightED->setEnabled(useHeight);
355 heightUnitCO->setEnabled(useHeight);
360 static Length::UNIT defaultUnit()
362 Length::UNIT default_unit = Length::CM;
363 switch (lyxrc.default_papersize) {
366 case PAPER_USEXECUTIVE:
367 default_unit = Length::IN;
376 static void setDisplay(
377 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
378 external::DisplayType display, unsigned int scale, bool read_only)
382 case external::DefaultDisplay:
385 case external::MonochromeDisplay:
388 case external::GrayscaleDisplay:
391 case external::ColorDisplay:
394 case external::PreviewDisplay:
397 case external::NoDisplay:
402 showCO.setCurrentIndex(item);
403 bool const no_display = display == external::NoDisplay;
404 showCO.setEnabled(!no_display && !read_only);
405 displayCB.setChecked(!no_display);
406 scaleED.setEnabled(!no_display && !read_only);
407 scaleED.setText(QString::number(scale));
411 static external::DisplayType display(QCheckBox const & displayCB,
412 QComboBox const & showCO)
414 if (!displayCB.isChecked())
415 return external::NoDisplay;
416 switch (showCO.currentIndex()) {
419 return external::DefaultDisplay;
421 return external::MonochromeDisplay;
423 return external::GrayscaleDisplay;
425 return external::ColorDisplay;
427 return external::PreviewDisplay;
432 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
433 external::RotationData const & data)
435 originCO.setCurrentIndex(int(data.origin()));
436 angleED.setText(toqstr(data.angle));
440 static void getRotation(external::RotationData & data,
441 QLineEdit const & angleED, QComboBox const & originCO)
443 typedef external::RotationData::OriginType OriginType;
445 data.origin(static_cast<OriginType>(originCO.currentIndex()));
446 data.angle = fromqstr(angleED.text());
450 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
451 QLineEdit & heightED, LengthCombo & heightUnitCO,
452 QCheckBox & aspectratioCB,
453 external::ResizeData const & data)
455 bool using_scale = data.usingScale();
456 string scale = data.scale;
457 if (data.no_resize()) {
458 // Everything is zero, so default to this!
464 widthED.setText(toqstr(scale));
465 widthUnitCO.setCurrentIndex(0);
467 widthED.setText(QString::number(data.width.value()));
468 // Because 'Scale' is position 0...
469 // Note also that width cannot be zero here, so
470 // we don't need to worry about the default unit.
471 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
474 string const h = data.height.zero() ? string() : data.height.asString();
475 Length::UNIT default_unit = data.width.zero() ?
476 defaultUnit() : data.width.unit();
477 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
479 heightED.setEnabled(!using_scale);
480 heightUnitCO.setEnabled(!using_scale);
482 aspectratioCB.setChecked(data.keepAspectRatio);
484 bool const disable_aspectRatio = using_scale ||
485 data.width.zero() || data.height.zero();
486 aspectratioCB.setEnabled(!disable_aspectRatio);
490 static void getSize(external::ResizeData & data,
491 QLineEdit const & widthED, QComboBox const & widthUnitCO,
492 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
493 QCheckBox const & aspectratioCB)
495 string const width = fromqstr(widthED.text());
497 if (widthUnitCO.currentIndex() > 0) {
498 // Subtract one, because scale is 0.
499 int const unit = widthUnitCO.currentIndex() - 1;
502 if (isValidLength(width, &w))
504 else if (isStrDbl(width))
505 data.width = Length(convert<double>(width),
506 static_cast<Length::UNIT>(unit));
508 data.width = Length();
510 data.scale = string();
513 // scaling instead of a width
515 data.width = Length();
518 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
520 data.keepAspectRatio = aspectratioCB.isChecked();
524 void setCrop(QCheckBox & clipCB,
525 QLineEdit & xlED, QLineEdit & ybED,
526 QLineEdit & xrED, QLineEdit & ytED,
527 external::ClipData const & data)
529 clipCB.setChecked(data.clip);
530 graphics::BoundingBox const & bbox = data.bbox;
531 xlED.setText(QString::number(bbox.xl));
532 ybED.setText(QString::number(bbox.yb));
533 xrED.setText(QString::number(bbox.xr));
534 ytED.setText(QString::number(bbox.yt));
538 static void getCrop(external::ClipData & data,
539 QCheckBox const & clipCB,
540 QLineEdit const & xlED, QLineEdit const & ybED,
541 QLineEdit const & xrED, QLineEdit const & ytED,
544 data.clip = clipCB.isChecked();
549 data.bbox.xl = xlED.text().toInt();
550 data.bbox.yb = ybED.text().toInt();
551 data.bbox.xr = xrED.text().toInt();
552 data.bbox.yt = ytED.text().toInt();
556 void GuiExternal::updateContents()
558 tab->setCurrentIndex(0);
561 params_.filename.outputFilename(fromqstr(bufferFilepath()));
562 fileED->setText(toqstr(name));
565 external::TemplateManager::Templates::const_iterator i1, i2;
566 i1 = external::TemplateManager::get().getTemplates().begin();
567 i2 = external::TemplateManager::get().getTemplates().end();
568 for (int i = 0; i1 != i2; ++i1, ++i) {
569 if (i1->second.lyxName == params_.templatename()) {
575 externalCO->setCurrentIndex(index);
578 draftCB->setChecked(params_.draft);
580 setDisplay(*displayCB, *showCO, *displayscaleED,
581 params_.display, params_.lyxscale, isBufferReadonly());
583 setRotation(*angleED, *originCO, params_.rotationdata);
585 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
586 *aspectratioCB, params_.resizedata);
588 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
589 bbChanged_ = !params_.clipdata.bbox.empty();
595 void GuiExternal::updateTemplate()
597 external::Template templ = getTemplate(externalCO->currentIndex());
598 externalTB->setPlainText(qt_(templ.helpText));
600 // Ascertain which (if any) transformations the template supports
601 // and disable tabs hosting unsupported transforms.
602 typedef vector<external::TransformID> TransformIDs;
603 TransformIDs const transformIds = templ.transformIds;
604 TransformIDs::const_iterator tr_begin = transformIds.begin();
605 TransformIDs::const_iterator const tr_end = transformIds.end();
607 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
608 tab->setTabEnabled(tab->indexOf(rotatetab), found);
609 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
610 tab->setTabEnabled(tab->indexOf(scaletab), found);
612 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
613 tab->setTabEnabled(tab->indexOf(croptab), found);
615 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
616 tab->setTabEnabled(tab->indexOf(optionstab), found);
621 // Ascertain whether the template has any formats supporting
622 // the 'Extra' option
625 extraFormatCO->clear();
627 external::Template::Formats::const_iterator it = templ.formats.begin();
628 external::Template::Formats::const_iterator end = templ.formats.end();
629 for (; it != end; ++it) {
630 if (it->second.option_transformers.find(external::Extra) ==
631 it->second.option_transformers.end())
633 string const format = it->first;
634 string const opt = params_.extradata.get(format);
635 extraFormatCO->addItem(toqstr(format));
636 extra_[toqstr(format)] = toqstr(opt);
639 bool const enabled = extraFormatCO->count() > 0;
641 tab->setTabEnabled(tab->indexOf(optionstab), enabled);
642 extraED->setEnabled(enabled && !isBufferReadonly());
643 extraFormatCO->setEnabled(enabled);
646 extraFormatCO->setCurrentIndex(0);
647 extraED->setText(extra_[extraFormatCO->currentText()]);
652 void GuiExternal::applyView()
654 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
655 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
657 params_.draft = draftCB->isChecked();
658 params_.lyxscale = displayscaleED->text().toInt();
659 params_.display = display(*displayCB, *showCO);
661 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
662 getRotation(params_.rotationdata, *angleED, *originCO);
664 if (tab->isTabEnabled(tab->indexOf(scaletab)))
665 getSize(params_.resizedata, *widthED, *widthUnitCO,
666 *heightED, *heightUnitCO, *aspectratioCB);
668 if (tab->isTabEnabled(tab->indexOf(croptab)))
669 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
670 *xrED, *ytED, bbChanged_);
672 if (tab->isTabEnabled(tab->indexOf(optionstab))) {
673 MapType::const_iterator it = extra_.begin();
674 MapType::const_iterator const end = extra_.end();
675 for (; it != end; ++it)
676 params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
681 bool GuiExternal::initialiseParams(string const & data)
683 InsetExternal::string2params(data, buffer(), params_);
688 void GuiExternal::clearParams()
690 params_ = InsetExternalParams();
694 void GuiExternal::dispatchParams()
696 string const lfun = InsetExternal::params2string(params_, buffer());
697 dispatch(FuncRequest(getLfun(), lfun));
701 void GuiExternal::editExternal()
704 string const lfun = InsetExternal::params2string(params_, buffer());
705 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
709 static QStringList templateFilters(QString const & template_name)
711 /// Determine the template file extension
712 external::TemplateManager const & etm =
713 external::TemplateManager::get();
714 external::Template const * const et_ptr =
715 etm.getTemplateByName(fromqstr(template_name));
717 return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
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 QStringList const filter = templateFilters(template_name);
728 QString const label1 = qt_("Documents|#o#O");
729 QString const dir1 = toqstr(lyxrc.document_path);
731 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
735 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
738 } // namespace frontend
741 #include "GuiExternal_moc.cpp"