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> t = templates();
202 for (vector<string>::const_iterator cit = t.begin();
203 cit != t.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()
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()
296 void GuiExternal::sizeChanged()
298 aspectratioCB->setEnabled(activateAspectratio());
303 void GuiExternal::templateChanged()
310 void GuiExternal::widthUnitChanged()
312 bool useHeight = (widthUnitCO->currentIndex() > 0);
315 widthED->setValidator(unsignedLengthValidator(widthED));
317 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
319 heightED->setEnabled(useHeight);
320 heightUnitCO->setEnabled(useHeight);
325 static Length::UNIT defaultUnit()
327 Length::UNIT default_unit = Length::CM;
328 switch (lyxrc.default_papersize) {
331 case PAPER_USEXECUTIVE:
332 default_unit = Length::IN;
341 static void setDisplay(
342 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
343 external::DisplayType display, unsigned int scale, bool read_only)
347 case external::DefaultDisplay:
350 case external::MonochromeDisplay:
353 case external::GrayscaleDisplay:
356 case external::ColorDisplay:
359 case external::PreviewDisplay:
362 case external::NoDisplay:
367 showCO.setCurrentIndex(item);
368 bool const no_display = display == external::NoDisplay;
369 showCO.setEnabled(!no_display && !read_only);
370 displayCB.setChecked(!no_display);
371 scaleED.setEnabled(!no_display && !read_only);
372 scaleED.setText(QString::number(scale));
376 static void getDisplay(external::DisplayType & display,
377 unsigned int & scale,
378 QCheckBox const & displayCB,
379 QComboBox const & showCO,
380 QLineEdit const & scaleED)
382 switch (showCO.currentIndex()) {
384 display = external::DefaultDisplay;
387 display = external::MonochromeDisplay;
390 display = external::GrayscaleDisplay;
393 display = external::ColorDisplay;
396 display = external::PreviewDisplay;
400 if (!displayCB.isChecked())
401 display = external::NoDisplay;
403 scale = scaleED.text().toInt();
407 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
408 external::RotationData const & data)
410 originCO.setCurrentIndex(int(data.origin()));
411 angleED.setText(toqstr(data.angle));
415 static void getRotation(external::RotationData & data,
416 QLineEdit const & angleED, QComboBox const & originCO)
418 typedef external::RotationData::OriginType OriginType;
420 data.origin(static_cast<OriginType>(originCO.currentIndex()));
421 data.angle = fromqstr(angleED.text());
425 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
426 QLineEdit & heightED, LengthCombo & heightUnitCO,
427 QCheckBox & aspectratioCB,
428 external::ResizeData const & data)
430 bool using_scale = data.usingScale();
431 string scale = data.scale;
432 if (data.no_resize()) {
433 // Everything is zero, so default to this!
439 widthED.setText(toqstr(scale));
440 widthUnitCO.setCurrentIndex(0);
442 widthED.setText(QString::number(data.width.value()));
443 // Because 'Scale' is position 0...
444 // Note also that width cannot be zero here, so
445 // we don't need to worry about the default unit.
446 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
449 string const h = data.height.zero() ? string() : data.height.asString();
450 Length::UNIT default_unit = data.width.zero() ?
451 defaultUnit() : data.width.unit();
452 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
454 heightED.setEnabled(!using_scale);
455 heightUnitCO.setEnabled(!using_scale);
457 aspectratioCB.setChecked(data.keepAspectRatio);
459 bool const disable_aspectRatio = using_scale ||
460 data.width.zero() || data.height.zero();
461 aspectratioCB.setEnabled(!disable_aspectRatio);
465 static void getSize(external::ResizeData & data,
466 QLineEdit const & widthED, QComboBox const & widthUnitCO,
467 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
468 QCheckBox const & aspectratioCB)
470 string const width = fromqstr(widthED.text());
472 if (widthUnitCO.currentIndex() > 0) {
473 // Subtract one, because scale is 0.
474 int const unit = widthUnitCO.currentIndex() - 1;
477 if (isValidLength(width, &w))
479 else if (isStrDbl(width))
480 data.width = Length(convert<double>(width),
481 static_cast<Length::UNIT>(unit));
483 data.width = Length();
485 data.scale = string();
488 // scaling instead of a width
490 data.width = Length();
493 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
495 data.keepAspectRatio = aspectratioCB.isChecked();
499 void setCrop(QCheckBox & clipCB,
500 QLineEdit & xlED, QLineEdit & ybED,
501 QLineEdit & xrED, QLineEdit & ytED,
502 external::ClipData const & data)
504 clipCB.setChecked(data.clip);
505 graphics::BoundingBox const & bbox = data.bbox;
506 xlED.setText(QString::number(bbox.xl));
507 ybED.setText(QString::number(bbox.yb));
508 xrED.setText(QString::number(bbox.xr));
509 ytED.setText(QString::number(bbox.yt));
513 static void getCrop(external::ClipData & data,
514 QCheckBox const & clipCB,
515 QLineEdit const & xlED, QLineEdit const & ybED,
516 QLineEdit const & xrED, QLineEdit const & ytED,
519 data.clip = clipCB.isChecked();
524 data.bbox.xl = xlED.text().toInt();
525 data.bbox.yb = ybED.text().toInt();
526 data.bbox.xr = xrED.text().toInt();
527 data.bbox.yt = ytED.text().toInt();
531 static void getExtra(external::ExtraData & data,
532 GuiExternal::MapType const & extra)
534 typedef GuiExternal::MapType MapType;
535 MapType::const_iterator it = extra.begin();
536 MapType::const_iterator const end = extra.end();
537 for (; it != end; ++it)
538 data.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
542 void GuiExternal::updateContents()
544 tab->setCurrentIndex(0);
547 params_.filename.outputFilename(fromqstr(bufferFilepath()));
548 fileED->setText(toqstr(name));
550 externalCO->setCurrentIndex(templateNumber(params_.templatename()));
553 draftCB->setChecked(params_.draft);
555 setDisplay(*displayCB, *showCO, *displayscaleED,
556 params_.display, params_.lyxscale, isBufferReadonly());
558 setRotation(*angleED, *originCO, params_.rotationdata);
560 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
561 *aspectratioCB, params_.resizedata);
563 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
564 bbChanged_ = !params_.clipdata.bbox.empty();
570 void GuiExternal::updateTemplate()
572 external::Template templ = getTemplate(externalCO->currentIndex());
573 externalTB->setPlainText(qt_(templ.helpText));
575 // Ascertain which (if any) transformations the template supports
576 // and disable tabs hosting unsupported transforms.
577 typedef vector<external::TransformID> TransformIDs;
578 TransformIDs const transformIds = templ.transformIds;
579 TransformIDs::const_iterator tr_begin = transformIds.begin();
580 TransformIDs::const_iterator const tr_end = transformIds.end();
582 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
583 tab->setTabEnabled(tab->indexOf(rotatetab), found);
584 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
585 tab->setTabEnabled(tab->indexOf(scaletab), found);
587 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
588 tab->setTabEnabled(tab->indexOf(croptab), found);
590 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
591 tab->setTabEnabled(tab->indexOf(optionstab), found);
596 // Ascertain whether the template has any formats supporting
597 // the 'Extra' option
600 extraFormatCO->clear();
602 external::Template::Formats::const_iterator it = templ.formats.begin();
603 external::Template::Formats::const_iterator end = templ.formats.end();
604 for (; it != end; ++it) {
605 if (it->second.option_transformers.find(external::Extra) ==
606 it->second.option_transformers.end())
608 string const format = it->first;
609 string const opt = params_.extradata.get(format);
610 extraFormatCO->addItem(toqstr(format));
611 extra_[toqstr(format)] = toqstr(opt);
614 bool const enabled = extraFormatCO->count() > 0;
616 tab->setTabEnabled(tab->indexOf(optionstab), enabled);
617 extraED->setEnabled(enabled && !isBufferReadonly());
618 extraFormatCO->setEnabled(enabled);
621 extraFormatCO->setCurrentIndex(0);
622 extraED->setText(extra_[extraFormatCO->currentText()]);
627 void GuiExternal::applyView()
629 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
630 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
632 params_.draft = draftCB->isChecked();
634 getDisplay(params_.display, params_.lyxscale,
638 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
639 getRotation(params_.rotationdata, *angleED, *originCO);
641 if (tab->isTabEnabled(tab->indexOf(scaletab)))
642 getSize(params_.resizedata, *widthED, *widthUnitCO,
643 *heightED, *heightUnitCO, *aspectratioCB);
645 if (tab->isTabEnabled(tab->indexOf(croptab)))
646 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
647 *xrED, *ytED, bbChanged_);
649 if (tab->isTabEnabled(tab->indexOf(optionstab)))
650 getExtra(params_.extradata, extra_);
654 void GuiExternal::getBB()
661 string const filename = fromqstr(fileED->text());
662 if (filename.empty())
665 string const bb = readBB(filename);
669 xlED->setText(toqstr(token(bb, ' ', 0)));
670 ybED->setText(toqstr(token(bb, ' ', 1)));
671 xrED->setText(toqstr(token(bb, ' ', 2)));
672 ytED->setText(toqstr(token(bb, ' ', 3)));
678 bool GuiExternal::initialiseParams(string const & data)
680 InsetExternal::string2params(data, buffer(), params_);
685 void GuiExternal::clearParams()
687 params_ = InsetExternalParams();
691 void GuiExternal::dispatchParams()
693 string const lfun = InsetExternal::params2string(params_, buffer());
694 dispatch(FuncRequest(getLfun(), lfun));
698 void GuiExternal::editExternal()
701 string const lfun = InsetExternal::params2string(params_, buffer());
702 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
706 vector<string> GuiExternal::templates() const
708 vector<string> result;
710 external::TemplateManager::Templates::const_iterator i1, i2;
711 i1 = external::TemplateManager::get().getTemplates().begin();
712 i2 = external::TemplateManager::get().getTemplates().end();
714 for (; i1 != i2; ++i1)
715 result.push_back(i1->second.lyxName);
721 int GuiExternal::templateNumber(string const & name) const
723 external::TemplateManager::Templates::const_iterator i1, i2;
724 i1 = external::TemplateManager::get().getTemplates().begin();
725 i2 = external::TemplateManager::get().getTemplates().end();
726 for (int i = 0; i1 != i2; ++i1, ++i) {
727 if (i1->second.lyxName == name)
731 // we can get here if a LyX document has a template not installed
737 external::Template GuiExternal::getTemplate(int i) const
739 external::TemplateManager::Templates::const_iterator i1
740 = external::TemplateManager::get().getTemplates().begin();
748 string GuiExternal::templateFilters(string const & template_name) const
750 /// Determine the template file extension
751 external::TemplateManager const & etm =
752 external::TemplateManager::get();
753 external::Template const * const et_ptr =
754 etm.getTemplateByName(template_name);
757 return et_ptr->fileRegExp;
763 QString GuiExternal::browse(QString const & input,
764 QString const & template_name) const
766 QString const title = qt_("Select external file");
767 QString const bufpath = bufferFilepath();
768 FileFilterList const filter =
769 FileFilterList(from_utf8(templateFilters(fromqstr(template_name))));
771 QString const label1 = qt_("Documents|#o#O");
772 QString const dir1 = toqstr(lyxrc.document_path);
774 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
778 string const GuiExternal::readBB(string const & file)
780 FileName const abs_file(makeAbsPath(file, fromqstr(bufferFilepath())));
782 // try to get it from the file, if possible. Zipped files are
783 // unzipped in the readBB_from_PSFile-Function
784 string const bb = readBB_from_PSFile(abs_file);
788 // we don't, so ask the Graphics Cache if it has loaded the file
792 graphics::Cache & gc = graphics::Cache::get();
793 if (gc.inCache(abs_file)) {
794 graphics::Image const * image = gc.item(abs_file)->image();
797 width = image->width();
798 height = image->height();
802 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
806 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
809 } // namespace frontend
812 #include "GuiExternal_moc.cpp"