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")
87 external::Template getTemplate(int i)
89 external::TemplateManager::Templates::const_iterator i1
90 = external::TemplateManager::get().getTemplates().begin();
100 GuiExternal::GuiExternal(GuiView & lv)
101 : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
105 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
106 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
107 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
109 connect(displayCB, SIGNAL(toggled(bool)),
110 showCO, SLOT(setEnabled(bool)));
111 connect(displayCB, SIGNAL(toggled(bool)),
112 displayscaleED, SLOT(setEnabled(bool)));
113 connect(showCO, SIGNAL(activated(QString)),
114 this, SLOT(change_adaptor()));
115 connect(originCO, SIGNAL(activated(int)),
116 this, SLOT(change_adaptor()));
117 connect(aspectratioCB, SIGNAL(stateChanged(int)),
118 this, SLOT(change_adaptor()));
119 connect(browsePB, SIGNAL(clicked()),
120 this, SLOT(browseClicked()));
121 connect(editPB, SIGNAL(clicked()),
122 this, SLOT(editClicked()));
123 connect(externalCO, SIGNAL(activated(QString)),
124 this, SLOT(templateChanged()));
125 connect(extraED, SIGNAL(textChanged(QString)),
126 this, SLOT(extraChanged(QString)));
127 connect(extraFormatCO, SIGNAL(activated(QString)),
128 this, SLOT(formatChanged(QString)));
129 connect(widthUnitCO, SIGNAL(activated(int)),
130 this, SLOT(widthUnitChanged()));
131 connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
132 this, SLOT(change_adaptor()));
133 connect(displayCB, SIGNAL(stateChanged(int)),
134 this, SLOT(change_adaptor()));
135 connect(displayscaleED, SIGNAL(textChanged(QString)),
136 this, SLOT(change_adaptor()));
137 connect(angleED, SIGNAL(textChanged(QString)),
138 this, SLOT(change_adaptor()));
139 connect(widthED, SIGNAL(textChanged(QString)),
140 this, SLOT(sizeChanged()));
141 connect(heightED, SIGNAL(textChanged(QString)),
142 this, SLOT(sizeChanged()));
143 connect(fileED, SIGNAL(textChanged(QString)),
144 this, SLOT(change_adaptor()));
145 connect(clipCB, SIGNAL(stateChanged(int)),
146 this, SLOT(change_adaptor()));
147 connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
148 connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
149 connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
150 connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
151 connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
152 connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
154 QIntValidator * validator = new QIntValidator(displayscaleED);
155 validator->setBottom(1);
156 displayscaleED->setValidator(validator);
158 angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
160 xlED->setValidator(new QIntValidator(xlED));
161 ybED->setValidator(new QIntValidator(ybED));
162 xrED->setValidator(new QIntValidator(xrED));
163 ytED->setValidator(new QIntValidator(ytED));
165 widthED->setValidator(unsignedLengthValidator(widthED));
166 heightED->setValidator(unsignedLengthValidator(heightED));
168 setFocusProxy(fileED);
170 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
173 bc().setApply(applyPB);
174 bc().setCancel(closePB);
176 bc().addReadOnly(fileED);
177 bc().addReadOnly(browsePB);
178 bc().addReadOnly(editPB);
179 bc().addReadOnly(externalCO);
180 bc().addReadOnly(draftCB);
181 bc().addReadOnly(displayscaleED);
182 bc().addReadOnly(showCO);
183 bc().addReadOnly(displayCB);
184 bc().addReadOnly(angleED);
185 bc().addReadOnly(originCO);
186 bc().addReadOnly(heightUnitCO);
187 bc().addReadOnly(heightED);
188 bc().addReadOnly(aspectratioCB);
189 bc().addReadOnly(widthUnitCO);
190 bc().addReadOnly(widthED);
191 bc().addReadOnly(clipCB);
192 bc().addReadOnly(getbbPB);
193 bc().addReadOnly(ytED);
194 bc().addReadOnly(xlED);
195 bc().addReadOnly(xrED);
196 bc().addReadOnly(ybED);
197 bc().addReadOnly(extraFormatCO);
198 bc().addReadOnly(extraED);
200 bc().addCheckedLineEdit(angleED, angleLA);
201 bc().addCheckedLineEdit(displayscaleED, scaleLA);
202 bc().addCheckedLineEdit(heightED, heightLA);
203 bc().addCheckedLineEdit(widthED, widthLA);
204 bc().addCheckedLineEdit(xlED, lbLA);
205 bc().addCheckedLineEdit(ybED, lbLA);
206 bc().addCheckedLineEdit(xrED, rtLA);
207 bc().addCheckedLineEdit(ytED, rtLA);
208 bc().addCheckedLineEdit(fileED, fileLA);
210 external::TemplateManager::Templates::const_iterator i1, i2;
211 i1 = external::TemplateManager::get().getTemplates().begin();
212 i2 = external::TemplateManager::get().getTemplates().end();
213 for (; i1 != i2; ++i1)
214 externalCO->addItem(qt_(i1->second.lyxName));
216 // Fill the origins combo
217 for (size_t i = 0; i != all_origins.size(); ++i)
218 originCO->addItem(qt_(origin_gui_strs[i]));
220 // Fill the width combo
221 widthUnitCO->addItem(qt_("Scale%"));
222 for (int i = 0; i < num_units; i++)
223 widthUnitCO->addItem(qt_(unit_name_gui[i]));
227 bool GuiExternal::activateAspectratio() const
229 if (widthUnitCO->currentIndex() == 0)
232 string const wstr = fromqstr(widthED->text());
235 bool const wIsDbl = isStrDbl(wstr);
236 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
239 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
242 string const hstr = fromqstr(heightED->text());
245 bool const hIsDbl = isStrDbl(hstr);
246 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
248 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
255 void GuiExternal::bbChanged()
262 void GuiExternal::browseClicked()
264 int const choice = externalCO->currentIndex();
265 QString const template_name = toqstr(getTemplate(choice).lyxName);
266 QString const str = browse(fileED->text(), template_name);
267 if (!str.isEmpty()) {
268 fileED->setText(str);
274 void GuiExternal::change_adaptor()
280 void GuiExternal::editClicked()
286 void GuiExternal::extraChanged(const QString & text)
288 extra_[extraFormatCO->currentText()] = text;
293 void GuiExternal::formatChanged(const QString & format)
295 extraED->setText(extra_[format]);
299 void GuiExternal::getbbClicked()
305 void GuiExternal::sizeChanged()
307 aspectratioCB->setEnabled(activateAspectratio());
312 void GuiExternal::templateChanged()
319 void GuiExternal::widthUnitChanged()
321 bool useHeight = (widthUnitCO->currentIndex() > 0);
324 widthED->setValidator(unsignedLengthValidator(widthED));
326 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
328 heightED->setEnabled(useHeight);
329 heightUnitCO->setEnabled(useHeight);
334 static Length::UNIT defaultUnit()
336 Length::UNIT default_unit = Length::CM;
337 switch (lyxrc.default_papersize) {
340 case PAPER_USEXECUTIVE:
341 default_unit = Length::IN;
350 static void setDisplay(
351 QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
352 external::DisplayType display, unsigned int scale, bool read_only)
356 case external::DefaultDisplay:
359 case external::MonochromeDisplay:
362 case external::GrayscaleDisplay:
365 case external::ColorDisplay:
368 case external::PreviewDisplay:
371 case external::NoDisplay:
376 showCO.setCurrentIndex(item);
377 bool const no_display = display == external::NoDisplay;
378 showCO.setEnabled(!no_display && !read_only);
379 displayCB.setChecked(!no_display);
380 scaleED.setEnabled(!no_display && !read_only);
381 scaleED.setText(QString::number(scale));
385 static void getDisplay(external::DisplayType & display,
386 unsigned int & scale,
387 QCheckBox const & displayCB,
388 QComboBox const & showCO,
389 QLineEdit const & scaleED)
391 switch (showCO.currentIndex()) {
393 display = external::DefaultDisplay;
396 display = external::MonochromeDisplay;
399 display = external::GrayscaleDisplay;
402 display = external::ColorDisplay;
405 display = external::PreviewDisplay;
409 if (!displayCB.isChecked())
410 display = external::NoDisplay;
412 scale = scaleED.text().toInt();
416 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
417 external::RotationData const & data)
419 originCO.setCurrentIndex(int(data.origin()));
420 angleED.setText(toqstr(data.angle));
424 static void getRotation(external::RotationData & data,
425 QLineEdit const & angleED, QComboBox const & originCO)
427 typedef external::RotationData::OriginType OriginType;
429 data.origin(static_cast<OriginType>(originCO.currentIndex()));
430 data.angle = fromqstr(angleED.text());
434 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
435 QLineEdit & heightED, LengthCombo & heightUnitCO,
436 QCheckBox & aspectratioCB,
437 external::ResizeData const & data)
439 bool using_scale = data.usingScale();
440 string scale = data.scale;
441 if (data.no_resize()) {
442 // Everything is zero, so default to this!
448 widthED.setText(toqstr(scale));
449 widthUnitCO.setCurrentIndex(0);
451 widthED.setText(QString::number(data.width.value()));
452 // Because 'Scale' is position 0...
453 // Note also that width cannot be zero here, so
454 // we don't need to worry about the default unit.
455 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
458 string const h = data.height.zero() ? string() : data.height.asString();
459 Length::UNIT default_unit = data.width.zero() ?
460 defaultUnit() : data.width.unit();
461 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
463 heightED.setEnabled(!using_scale);
464 heightUnitCO.setEnabled(!using_scale);
466 aspectratioCB.setChecked(data.keepAspectRatio);
468 bool const disable_aspectRatio = using_scale ||
469 data.width.zero() || data.height.zero();
470 aspectratioCB.setEnabled(!disable_aspectRatio);
474 static void getSize(external::ResizeData & data,
475 QLineEdit const & widthED, QComboBox const & widthUnitCO,
476 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
477 QCheckBox const & aspectratioCB)
479 string const width = fromqstr(widthED.text());
481 if (widthUnitCO.currentIndex() > 0) {
482 // Subtract one, because scale is 0.
483 int const unit = widthUnitCO.currentIndex() - 1;
486 if (isValidLength(width, &w))
488 else if (isStrDbl(width))
489 data.width = Length(convert<double>(width),
490 static_cast<Length::UNIT>(unit));
492 data.width = Length();
494 data.scale = string();
497 // scaling instead of a width
499 data.width = Length();
502 data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
504 data.keepAspectRatio = aspectratioCB.isChecked();
508 void setCrop(QCheckBox & clipCB,
509 QLineEdit & xlED, QLineEdit & ybED,
510 QLineEdit & xrED, QLineEdit & ytED,
511 external::ClipData const & data)
513 clipCB.setChecked(data.clip);
514 graphics::BoundingBox const & bbox = data.bbox;
515 xlED.setText(QString::number(bbox.xl));
516 ybED.setText(QString::number(bbox.yb));
517 xrED.setText(QString::number(bbox.xr));
518 ytED.setText(QString::number(bbox.yt));
522 static void getCrop(external::ClipData & data,
523 QCheckBox const & clipCB,
524 QLineEdit const & xlED, QLineEdit const & ybED,
525 QLineEdit const & xrED, QLineEdit const & ytED,
528 data.clip = clipCB.isChecked();
533 data.bbox.xl = xlED.text().toInt();
534 data.bbox.yb = ybED.text().toInt();
535 data.bbox.xr = xrED.text().toInt();
536 data.bbox.yt = ytED.text().toInt();
540 static void getExtra(external::ExtraData & data,
541 GuiExternal::MapType const & extra)
543 typedef GuiExternal::MapType MapType;
544 MapType::const_iterator it = extra.begin();
545 MapType::const_iterator const end = extra.end();
546 for (; it != end; ++it)
547 data.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
551 void GuiExternal::updateContents()
553 tab->setCurrentIndex(0);
556 params_.filename.outputFilename(fromqstr(bufferFilepath()));
557 fileED->setText(toqstr(name));
560 external::TemplateManager::Templates::const_iterator i1, i2;
561 i1 = external::TemplateManager::get().getTemplates().begin();
562 i2 = external::TemplateManager::get().getTemplates().end();
563 for (int i = 0; i1 != i2; ++i1, ++i) {
564 if (i1->second.lyxName == params_.templatename()) {
570 externalCO->setCurrentIndex(index);
573 draftCB->setChecked(params_.draft);
575 setDisplay(*displayCB, *showCO, *displayscaleED,
576 params_.display, params_.lyxscale, isBufferReadonly());
578 setRotation(*angleED, *originCO, params_.rotationdata);
580 setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
581 *aspectratioCB, params_.resizedata);
583 setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
584 bbChanged_ = !params_.clipdata.bbox.empty();
590 void GuiExternal::updateTemplate()
592 external::Template templ = getTemplate(externalCO->currentIndex());
593 externalTB->setPlainText(qt_(templ.helpText));
595 // Ascertain which (if any) transformations the template supports
596 // and disable tabs hosting unsupported transforms.
597 typedef vector<external::TransformID> TransformIDs;
598 TransformIDs const transformIds = templ.transformIds;
599 TransformIDs::const_iterator tr_begin = transformIds.begin();
600 TransformIDs::const_iterator const tr_end = transformIds.end();
602 bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
603 tab->setTabEnabled(tab->indexOf(rotatetab), found);
604 found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
605 tab->setTabEnabled(tab->indexOf(scaletab), found);
607 found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
608 tab->setTabEnabled(tab->indexOf(croptab), found);
610 found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
611 tab->setTabEnabled(tab->indexOf(optionstab), found);
616 // Ascertain whether the template has any formats supporting
617 // the 'Extra' option
620 extraFormatCO->clear();
622 external::Template::Formats::const_iterator it = templ.formats.begin();
623 external::Template::Formats::const_iterator end = templ.formats.end();
624 for (; it != end; ++it) {
625 if (it->second.option_transformers.find(external::Extra) ==
626 it->second.option_transformers.end())
628 string const format = it->first;
629 string const opt = params_.extradata.get(format);
630 extraFormatCO->addItem(toqstr(format));
631 extra_[toqstr(format)] = toqstr(opt);
634 bool const enabled = extraFormatCO->count() > 0;
636 tab->setTabEnabled(tab->indexOf(optionstab), enabled);
637 extraED->setEnabled(enabled && !isBufferReadonly());
638 extraFormatCO->setEnabled(enabled);
641 extraFormatCO->setCurrentIndex(0);
642 extraED->setText(extra_[extraFormatCO->currentText()]);
647 void GuiExternal::applyView()
649 params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
650 params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
652 params_.draft = draftCB->isChecked();
654 getDisplay(params_.display, params_.lyxscale,
658 if (tab->isTabEnabled(tab->indexOf(rotatetab)))
659 getRotation(params_.rotationdata, *angleED, *originCO);
661 if (tab->isTabEnabled(tab->indexOf(scaletab)))
662 getSize(params_.resizedata, *widthED, *widthUnitCO,
663 *heightED, *heightUnitCO, *aspectratioCB);
665 if (tab->isTabEnabled(tab->indexOf(croptab)))
666 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
667 *xrED, *ytED, bbChanged_);
669 if (tab->isTabEnabled(tab->indexOf(optionstab)))
670 getExtra(params_.extradata, extra_);
674 void GuiExternal::getBB()
681 string const filename = fromqstr(fileED->text());
682 if (filename.empty())
685 string const bb = readBB(filename);
689 xlED->setText(toqstr(token(bb, ' ', 0)));
690 ybED->setText(toqstr(token(bb, ' ', 1)));
691 xrED->setText(toqstr(token(bb, ' ', 2)));
692 ytED->setText(toqstr(token(bb, ' ', 3)));
698 bool GuiExternal::initialiseParams(string const & data)
700 InsetExternal::string2params(data, buffer(), params_);
705 void GuiExternal::clearParams()
707 params_ = InsetExternalParams();
711 void GuiExternal::dispatchParams()
713 string const lfun = InsetExternal::params2string(params_, buffer());
714 dispatch(FuncRequest(getLfun(), lfun));
718 void GuiExternal::editExternal()
721 string const lfun = InsetExternal::params2string(params_, buffer());
722 dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
726 static string templateFilters(string const & template_name)
728 /// Determine the template file extension
729 external::TemplateManager const & etm =
730 external::TemplateManager::get();
731 external::Template const * const et_ptr =
732 etm.getTemplateByName(template_name);
733 return et_ptr ? et_ptr->fileRegExp : string();
737 QString GuiExternal::browse(QString const & input,
738 QString const & template_name) const
740 QString const title = qt_("Select external file");
741 QString const bufpath = bufferFilepath();
742 FileFilterList const filter =
743 FileFilterList(from_utf8(templateFilters(fromqstr(template_name))));
745 QString const label1 = qt_("Documents|#o#O");
746 QString const dir1 = toqstr(lyxrc.document_path);
748 return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
752 string const GuiExternal::readBB(string const & file)
754 FileName const abs_file(makeAbsPath(file, fromqstr(bufferFilepath())));
756 // try to get it from the file, if possible. Zipped files are
757 // unzipped in the readBB_from_PSFile-Function
758 string const bb = readBB_from_PSFile(abs_file);
762 // we don't, so ask the Graphics Cache if it has loaded the file
766 graphics::Cache & gc = graphics::Cache::get();
767 if (gc.inCache(abs_file)) {
768 graphics::Image const * image = gc.item(abs_file)->image();
771 width = image->width();
772 height = image->height();
776 return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
780 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
783 } // namespace frontend
786 #include "GuiExternal_moc.cpp"