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_array[] = {
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 size_type const origins_array_size =
73 sizeof(origins_array) / sizeof(origins_array[0]);
75 vector<external::RotationDataType> const
76 all_origins(origins_array, origins_array + origins_array_size);
78 // These are the strings, corresponding to the above, that the GUI should
79 // use. Note that they can/should be translated.
80 char const * const origin_gui_strs[] = {
82 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
83 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
84 N_("Top right"), N_("Bottom right"), N_("Baseline right")
90 GuiExternal::GuiExternal(GuiView & lv)
91 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
95 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
96 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
97 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99 connect(displayCB, SIGNAL(toggled(bool)),
100 showCO, SLOT(setEnabled(bool)));
101 connect(displayCB, SIGNAL(toggled(bool)),
102 displayscaleED, SLOT(setEnabled(bool)));
103 connect(showCO, SIGNAL(activated(QString)),
104 this, SLOT(change_adaptor()));
105 connect(originCO, SIGNAL(activated(int)),
106 this, SLOT(change_adaptor()));
107 connect(aspectratioCB, SIGNAL(stateChanged(int)),
108 this, SLOT(change_adaptor()));
109 connect(browsePB, SIGNAL(clicked()),
110 this, SLOT(browseClicked()));
111 connect(editPB, SIGNAL(clicked()),
112 this, SLOT(editClicked()));
113 connect(externalCO, SIGNAL(activated(QString)),
114 this, SLOT(templateChanged()));
115 connect(extraED, SIGNAL(textChanged(QString)),
116 this, SLOT(extraChanged(QString)));
117 connect(extraFormatCO, SIGNAL(activated(QString)),
118 this, SLOT(formatChanged(QString)));
119 connect(widthUnitCO, SIGNAL(activated(int)),
120 this, SLOT(widthUnitChanged()));
121 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
122 this, SLOT(change_adaptor()));
123 connect(displayCB, SIGNAL(stateChanged(int)),
124 this, SLOT(change_adaptor()));
125 connect(displayscaleED, SIGNAL(textChanged(QString)),
126 this, SLOT(change_adaptor()));
127 connect(angleED, SIGNAL(textChanged(QString)),
128 this, SLOT(change_adaptor()));
129 connect(widthED, SIGNAL(textChanged(QString)),
130 this, SLOT(sizeChanged()));
131 connect(heightED, SIGNAL(textChanged(QString)),
132 this, SLOT(sizeChanged()));
133 connect(fileED, SIGNAL(textChanged(QString)),
134 this, SLOT(change_adaptor()));
135 connect(clipCB, SIGNAL(stateChanged(int)),
136 this, SLOT(change_adaptor()));
137 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
138 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
139 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
140 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
141 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
142 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
144 QIntValidator * validator = new QIntValidator(displayscaleED);
145 validator->setBottom(1);
146 displayscaleED->setValidator(validator);
148 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
150 xlED->setValidator(new QIntValidator(xlED));
151 ybED->setValidator(new QIntValidator(ybED));
152 xrED->setValidator(new QIntValidator(xrED));
153 ytED->setValidator(new QIntValidator(ytED));
155 widthED->setValidator(unsignedLengthValidator(widthED));
156 heightED->setValidator(unsignedLengthValidator(heightED));
158 setFocusProxy(fileED);
160 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
163 bc().setApply(applyPB);
164 bc().setCancel(closePB);
166 bc().addReadOnly(fileED);
167 bc().addReadOnly(browsePB);
168 bc().addReadOnly(editPB);
169 bc().addReadOnly(externalCO);
170 bc().addReadOnly(draftCB);
171 bc().addReadOnly(displayscaleED);
172 bc().addReadOnly(showCO);
173 bc().addReadOnly(displayCB);
174 bc().addReadOnly(angleED);
175 bc().addReadOnly(originCO);
176 bc().addReadOnly(heightUnitCO);
177 bc().addReadOnly(heightED);
178 bc().addReadOnly(aspectratioCB);
179 bc().addReadOnly(widthUnitCO);
180 bc().addReadOnly(widthED);
181 bc().addReadOnly(clipCB);
182 bc().addReadOnly(getbbPB);
183 bc().addReadOnly(ytED);
184 bc().addReadOnly(xlED);
185 bc().addReadOnly(xrED);
186 bc().addReadOnly(ybED);
187 bc().addReadOnly(extraFormatCO);
188 bc().addReadOnly(extraED);
190 bc().addCheckedLineEdit(angleED, angleLA);
191 bc().addCheckedLineEdit(displayscaleED, scaleLA);
192 bc().addCheckedLineEdit(heightED, heightLA);
193 bc().addCheckedLineEdit(widthED, widthLA);
194 bc().addCheckedLineEdit(xlED, lbLA);
195 bc().addCheckedLineEdit(ybED, lbLA);
196 bc().addCheckedLineEdit(xrED, rtLA);
197 bc().addCheckedLineEdit(ytED, rtLA);
198 bc().addCheckedLineEdit(fileED, fileLA);
200 vector<string> templates = getTemplates();
202 for (vector<string>::const_iterator cit = templates.begin();
203 cit != templates.end(); ++cit) {
204 externalCO->addItem(qt_(*cit));
207 // Fill the origins combo
208 for (size_t i = 0; i != all_origins.size(); ++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()
278 void GuiExternal::extraChanged(const QString& text)
280 string const format = fromqstr(extraFormatCO->currentText());
281 extra_[format] = text;
286 void GuiExternal::formatChanged(const QString& format)
288 extraED->setText(extra_[fromqstr(format)]);
292 void GuiExternal::getbbClicked()
298 void GuiExternal::sizeChanged()
300 aspectratioCB->setEnabled(activateAspectratio());
305 void GuiExternal::templateChanged()
312 void GuiExternal::widthUnitChanged()
314 bool useHeight = (widthUnitCO->currentIndex() > 0);
317 widthED->setValidator(unsignedLengthValidator(widthED));
319 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
321 heightED->setEnabled(useHeight);
322 heightUnitCO->setEnabled(useHeight);
327 static Length::UNIT defaultUnit()
329 Length::UNIT default_unit = Length::CM;
330 switch (lyxrc.default_papersize) {
333 case PAPER_USEXECUTIVE:
334 default_unit = Length::IN;
343 static void setDisplay(
344 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
345 external::DisplayType display, unsigned int scale, bool read_only)
349 case external::DefaultDisplay:
352 case external::MonochromeDisplay:
355 case external::GrayscaleDisplay:
358 case external::ColorDisplay:
361 case external::PreviewDisplay:
364 case external::NoDisplay:
369 showCO.setCurrentIndex(item);
370 bool const no_display = display == external::NoDisplay;
371 showCO.setEnabled(!no_display && !read_only);
372 displayCB.setChecked(!no_display);
373 scaleED.setEnabled(!no_display && !read_only);
374 scaleED.setText(QString::number(scale));
378 static void getDisplay(external::DisplayType & display,
379 unsigned int & scale,
380 QCheckBox const & displayCB,
381 QComboBox const & showCO,
382 QLineEdit const & scaleED)
384 switch (showCO.currentIndex()) {
386 display = external::DefaultDisplay;
389 display = external::MonochromeDisplay;
392 display = external::GrayscaleDisplay;
395 display = external::ColorDisplay;
398 display = external::PreviewDisplay;
402 if (!displayCB.isChecked())
403 display = external::NoDisplay;
405 scale = scaleED.text().toInt();
409 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
410 external::RotationData const & data)
412 originCO.setCurrentIndex(int(data.origin()));
413 angleED.setText(toqstr(data.angle));
417 static void getRotation(external::RotationData & data,
418 QLineEdit const & angleED, QComboBox const & originCO)
420 typedef external::RotationData::OriginType OriginType;
422 data.origin(static_cast<OriginType>(originCO.currentIndex()));
423 data.angle = fromqstr(angleED.text());
427 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
428 QLineEdit & heightED, LengthCombo & heightUnitCO,
429 QCheckBox & aspectratioCB,
430 external::ResizeData const & data)
432 bool using_scale = data.usingScale();
433 string scale = data.scale;
434 if (data.no_resize()) {
435 // Everything is zero, so default to this!
441 widthED.setText(toqstr(scale));
442 widthUnitCO.setCurrentIndex(0);
444 widthED.setText(QString::number(data.width.value()));
445 // Because 'Scale' is position 0...
446 // Note also that width cannot be zero here, so
447 // we don't need to worry about the default unit.
448 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
451 string const h = data.height.zero() ? string() : data.height.asString();
452 Length::UNIT default_unit = data.width.zero() ?
453 defaultUnit() : data.width.unit();
454 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
456 heightED.setEnabled(!using_scale);
457 heightUnitCO.setEnabled(!using_scale);
459 aspectratioCB.setChecked(data.keepAspectRatio);
461 bool const disable_aspectRatio = using_scale ||
462 data.width.zero() || data.height.zero();
463 aspectratioCB.setEnabled(!disable_aspectRatio);
467 static void getSize(external::ResizeData & data,
468 QLineEdit const & widthED, QComboBox const & widthUnitCO,
469 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
470 QCheckBox const & aspectratioCB)
472 string const width = fromqstr(widthED.text());
474 if (widthUnitCO.currentIndex() > 0) {
475 // Subtract one, because scale is 0.
476 int const unit = widthUnitCO.currentIndex() - 1;
479 if (isValidLength(width, &w))
481 else if (isStrDbl(width))
482 data.width = Length(convert<double>(width),
483 static_cast<Length::UNIT>(unit));
485 data.width = Length();
487 data.scale = string();
490 // scaling instead of a width
492 data.width = Length();
495 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
497 data.keepAspectRatio = aspectratioCB.isChecked();
501 void setCrop(QCheckBox & clipCB,
502 QLineEdit & xlED, QLineEdit & ybED,
503 QLineEdit & xrED, QLineEdit & ytED,
504 external::ClipData const & data)
506 clipCB.setChecked(data.clip);
507 graphics::BoundingBox const & bbox = data.bbox;
508 xlED.setText(QString::number(bbox.xl));
509 ybED.setText(QString::number(bbox.yb));
510 xrED.setText(QString::number(bbox.xr));
511 ytED.setText(QString::number(bbox.yt));
515 static void getCrop(external::ClipData & data,
516 QCheckBox const & clipCB,
517 QLineEdit const & xlED, QLineEdit const & ybED,
518 QLineEdit const & xrED, QLineEdit const & ytED,
521 data.clip = clipCB.isChecked();
526 data.bbox.xl = xlED.text().toInt();
527 data.bbox.yb = ybED.text().toInt();
528 data.bbox.xr = xrED.text().toInt();
529 data.bbox.yt = ytED.text().toInt();
533 static void getExtra(external::ExtraData & data,
534 GuiExternal::MapType const & extra)
536 typedef GuiExternal::MapType MapType;
537 MapType::const_iterator it = extra.begin();
538 MapType::const_iterator const end = extra.end();
539 for (; it != end; ++it)
540 data.set(it->first, trim(fromqstr(it->second)));
544 void GuiExternal::updateContents()
546 tab->setCurrentIndex(0);
549 params_.filename.outputFilename(fromqstr(bufferFilepath()));
550 fileED->setText(toqstr(name));
552 externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
555 draftCB->setChecked(params_.draft);
557 setDisplay(*displayCB, *showCO, *displayscaleED,
558 params_.display, params_.lyxscale, isBufferReadonly());
560 setRotation(*angleED, *originCO, params_.rotationdata);
562 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
563 *aspectratioCB, params_.resizedata);
565 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
566 bbChanged_ = !params_.clipdata.bbox.empty();
572 void GuiExternal::updateTemplate()
574 external::Template templ = getTemplate(externalCO->currentIndex());
575 externalTB->setPlainText(qt_(templ.helpText));
577 // Ascertain which (if any) transformations the template supports
578 // and disable tabs hosting unsupported transforms.
579 typedef vector<external::TransformID> TransformIDs;
580 TransformIDs const transformIds = templ.transformIds;
581 TransformIDs::const_iterator tr_begin = transformIds.begin();
582 TransformIDs::const_iterator const tr_end = transformIds.end();
584 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
585 tab->setTabEnabled(tab->indexOf(rotatetab), found);
586 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
587 tab->setTabEnabled(tab->indexOf(scaletab), found);
589 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
590 tab->setTabEnabled(tab->indexOf(croptab), found);
592 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
593 tab->setTabEnabled(tab->indexOf(optionstab), found);
598 // Ascertain whether the template has any formats supporting
599 // the 'Extra' option
602 extraFormatCO->clear();
604 external::Template::Formats::const_iterator it = templ.formats.begin();
605 external::Template::Formats::const_iterator end = templ.formats.end();
606 for (; it != end; ++it) {
607 if (it->second.option_transformers.find(external::Extra) ==
608 it->second.option_transformers.end())
610 string const format = it->first;
611 string const opt = params_.extradata.get(format);
612 extraFormatCO->addItem(toqstr(format));
613 extra_[format] = toqstr(opt);
616 bool const enabled = extraFormatCO->count() > 0;
619 tab->indexOf(optionstab), enabled);
620 extraED->setEnabled(enabled && !isBufferReadonly());
621 extraFormatCO->setEnabled(enabled);
624 extraFormatCO->setCurrentIndex(0);
625 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
630 void GuiExternal::applyView()
632 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
634 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
636 params_.draft = draftCB->isChecked();
638 getDisplay(params_.display, params_.lyxscale,
642 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
643 getRotation(params_.rotationdata, *angleED, *originCO);
645 if (tab->isTabEnabled(tab->indexOf(scaletab)))
646 getSize(params_.resizedata, *widthED, *widthUnitCO,
647 *heightED, *heightUnitCO, *aspectratioCB);
649 if (tab->isTabEnabled(tab->indexOf(croptab)))
650 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
651 *xrED, *ytED, bbChanged_);
653 if (tab->isTabEnabled(tab->indexOf(optionstab)))
654 getExtra(params_.extradata, extra_);
658 void GuiExternal::getBB()
665 string const filename = fromqstr(fileED->text());
666 if (filename.empty())
669 string const bb = readBB(filename);
673 xlED->setText(toqstr(token(bb, ' ', 0)));
674 ybED->setText(toqstr(token(bb, ' ', 1)));
675 xrED->setText(toqstr(token(bb, ' ', 2)));
676 ytED->setText(toqstr(token(bb, ' ', 3)));
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 vector<string> const GuiExternal::getTemplates() const
712 vector<string> result;
714 external::TemplateManager::Templates::const_iterator i1, i2;
715 i1 = external::TemplateManager::get().getTemplates().begin();
716 i2 = external::TemplateManager::get().getTemplates().end();
718 for (; i1 != i2; ++i1) {
719 result.push_back(i1->second.lyxName);
725 int GuiExternal::getTemplateNumber(string const & name) const
727 external::TemplateManager::Templates::const_iterator i1, i2;
728 i1 = external::TemplateManager::get().getTemplates().begin();
729 i2 = external::TemplateManager::get().getTemplates().end();
730 for (int i = 0; i1 != i2; ++i1, ++i) {
731 if (i1->second.lyxName == name)
735 // we can get here if a LyX document has a template not installed
741 external::Template GuiExternal::getTemplate(int i) const
743 external::TemplateManager::Templates::const_iterator i1
744 = external::TemplateManager::get().getTemplates().begin();
753 GuiExternal::getTemplateFilters(string const & template_name) const
755 /// Determine the template file extension
756 external::TemplateManager const & etm =
757 external::TemplateManager::get();
758 external::Template const * const et_ptr =
759 etm.getTemplateByName(template_name);
762 return et_ptr->fileRegExp;
768 QString GuiExternal::browse(QString const & input,
769 QString const & template_name) const
771 QString const title = qt_("Select external file");
772 QString const bufpath = bufferFilepath();
773 FileFilterList const filter =
774 FileFilterList(from_utf8(getTemplateFilters(fromqstr(template_name))));
776 QString const label1 = qt_("Documents|#o#O");
777 QString const dir1 = toqstr(lyxrc.document_path);
779 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
783 string const GuiExternal::readBB(string const & file)
785 FileName const abs_file(makeAbsPath(file, fromqstr(bufferFilepath())));
787 // try to get it from the file, if possible. Zipped files are
788 // unzipped in the readBB_from_PSFile-Function
789 string const bb = readBB_from_PSFile(abs_file);
793 // we don't, so ask the Graphics Cache if it has loaded the file
797 graphics::Cache & gc = graphics::Cache::get();
798 if (gc.inCache(abs_file)) {
799 graphics::Image const * image = gc.item(abs_file)->image();
802 width = image->width();
803 height = image->height();
807 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
811 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
814 } // namespace frontend
817 #include "GuiExternal_moc.cpp"