3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 // Qt defines a macro 'signals' that clashes with a boost namespace.
15 // All is well if the namespace is visible first.
16 #include "lengthcommon.h"
19 #include "controllers/ControlExternal.h"
21 #include "insets/ExternalTemplate.h"
22 #include "insets/insetexternal.h"
24 #include "support/lstrings.h"
25 #include "support/convert.h"
26 #include "support/os.h"
28 #include "QExternal.h"
29 #include "QExternalDialog.h"
32 #include "checkedwidgets.h"
33 #include "lengthcombo.h"
34 #include "qt_helpers.h"
35 #include "validators.h"
38 #include <QPushButton>
41 #include <QTextBrowser>
43 namespace external = lyx::external;
45 using lyx::support::isStrDbl;
46 using lyx::support::token;
47 using lyx::support::trim;
49 using lyx::support::os::internal_path;
60 LyXLength::UNIT defaultUnit()
62 LyXLength::UNIT default_unit = LyXLength::CM;
63 switch (lyxrc.default_papersize) {
66 case PAPER_USEXECUTIVE:
67 default_unit = LyXLength::IN;
76 void setDisplay(QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
77 external::DisplayType display, unsigned int scale,
82 case external::DefaultDisplay:
85 case external::MonochromeDisplay:
88 case external::GrayscaleDisplay:
91 case external::ColorDisplay:
94 case external::PreviewDisplay:
97 case external::NoDisplay:
102 showCO.setCurrentIndex(item);
103 bool const no_display = display == external::NoDisplay;
104 showCO.setEnabled(!no_display && !read_only);
105 displayCB.setChecked(!no_display);
106 scaleED.setEnabled(!no_display && !read_only);
107 scaleED.setText(toqstr(convert<string>(scale)));
111 void getDisplay(external::DisplayType & display,
112 unsigned int & scale,
113 QCheckBox const & displayCB,
114 QComboBox const & showCO,
115 QLineEdit const & scaleED)
117 switch (showCO.currentIndex()) {
119 display = external::DefaultDisplay;
122 display = external::MonochromeDisplay;
125 display = external::GrayscaleDisplay;
128 display = external::ColorDisplay;
131 display = external::PreviewDisplay;
135 if (!displayCB.isChecked())
136 display = external::NoDisplay;
138 scale = convert<int>(fromqstr(scaleED.text()));
142 void setRotation(QLineEdit & angleED, QComboBox & originCO,
143 external::RotationData const & data)
145 originCO.setCurrentIndex(int(data.origin()));
146 angleED.setText(toqstr(data.angle));
150 void getRotation(external::RotationData & data,
151 QLineEdit const & angleED, QComboBox const & originCO)
153 typedef external::RotationData::OriginType OriginType;
155 data.origin(static_cast<OriginType>(originCO.currentIndex()));
156 data.angle = fromqstr(angleED.text());
160 void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
161 QLineEdit & heightED, LengthCombo & heightUnitCO,
162 QCheckBox & aspectratioCB,
163 external::ResizeData const & data)
165 bool using_scale = data.usingScale();
166 std::string scale = data.scale;
167 if (data.no_resize()) {
168 // Everything is zero, so default to this!
174 widthED.setText(toqstr(scale));
175 widthUnitCO.setCurrentIndex(0);
177 widthED.setText(toqstr(convert<string>(data.width.value())));
178 // Because 'Scale' is position 0...
179 // Note also that width cannot be zero here, so
180 // we don't need to worry about the default unit.
181 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
184 string const h = data.height.zero() ? string() : data.height.asString();
185 LyXLength::UNIT default_unit = data.width.zero() ?
186 defaultUnit() : data.width.unit();
187 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
189 heightED.setEnabled(!using_scale);
190 heightUnitCO.setEnabled(!using_scale);
192 aspectratioCB.setChecked(data.keepAspectRatio);
194 bool const disable_aspectRatio = using_scale ||
195 data.width.zero() || data.height.zero();
196 aspectratioCB.setEnabled(!disable_aspectRatio);
200 void getSize(external::ResizeData & data,
201 QLineEdit const & widthED, QComboBox const & widthUnitCO,
202 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
203 QCheckBox const & aspectratioCB)
205 string const width = fromqstr(widthED.text());
207 if (widthUnitCO.currentIndex() > 0) {
208 // Subtract one, because scale is 0.
209 int const unit = widthUnitCO.currentIndex() - 1;
212 if (isValidLength(width, &w))
214 else if (isStrDbl(width))
215 data.width = LyXLength(convert<double>(width),
216 static_cast<LyXLength::UNIT>(unit));
218 data.width = LyXLength();
220 data.scale = string();
223 // scaling instead of a width
225 data.width = LyXLength();
228 data.height = LyXLength(widgetsToLength(&heightED, &heightUnitCO));
230 data.keepAspectRatio = aspectratioCB.isChecked();
234 void setCrop(QCheckBox & clipCB,
235 QLineEdit & xlED, QLineEdit & ybED,
236 QLineEdit & xrED, QLineEdit & ytED,
237 external::ClipData const & data)
239 clipCB.setChecked(data.clip);
240 graphics::BoundingBox const & bbox = data.bbox;
241 xlED.setText(toqstr(convert<string>(bbox.xl)));
242 ybED.setText(toqstr(convert<string>(bbox.yb)));
243 xrED.setText(toqstr(convert<string>(bbox.xr)));
244 ytED.setText(toqstr(convert<string>(bbox.yt)));
248 void getCrop(external::ClipData & data,
249 QCheckBox const & clipCB,
250 QLineEdit const & xlED, QLineEdit const & ybED,
251 QLineEdit const & xrED, QLineEdit const & ytED,
254 data.clip = clipCB.isChecked();
259 data.bbox.xl = convert<int>(fromqstr(xlED.text()));
260 data.bbox.yb = convert<int>(fromqstr(ybED.text()));
261 data.bbox.xr = convert<int>(fromqstr(xrED.text()));
262 data.bbox.yt = convert<int>(fromqstr(ytED.text()));
266 void getExtra(external::ExtraData & data,
267 QExternal::MapType const & extra)
269 typedef QExternal::MapType MapType;
270 MapType::const_iterator it = extra.begin();
271 MapType::const_iterator const end = extra.end();
272 for (; it != end; ++it)
273 data.set(it->first, trim(fromqstr(it->second)));
279 typedef QController<ControlExternal, QView<QExternalDialog> > base_class;
281 QExternal::QExternal(Dialog & parent)
282 : base_class(parent, _("External Material"))
286 void QExternal::build_dialog()
288 dialog_.reset(new QExternalDialog(this));
290 bcview().setOK(dialog_->okPB);
291 bcview().setApply(dialog_->applyPB);
292 bcview().setCancel(dialog_->closePB);
294 bcview().addReadOnly(dialog_->fileED);
295 bcview().addReadOnly(dialog_->browsePB);
296 bcview().addReadOnly(dialog_->editPB);
297 bcview().addReadOnly(dialog_->externalCO);
298 bcview().addReadOnly(dialog_->draftCB);
299 bcview().addReadOnly(dialog_->displayscaleED);
300 bcview().addReadOnly(dialog_->showCO);
301 bcview().addReadOnly(dialog_->displayCB);
302 bcview().addReadOnly(dialog_->angleED);
303 bcview().addReadOnly(dialog_->originCO);
304 bcview().addReadOnly(dialog_->heightUnitCO);
305 bcview().addReadOnly(dialog_->heightED);
306 bcview().addReadOnly(dialog_->aspectratioCB);
307 bcview().addReadOnly(dialog_->widthUnitCO);
308 bcview().addReadOnly(dialog_->widthED);
309 bcview().addReadOnly(dialog_->clipCB);
310 bcview().addReadOnly(dialog_->getbbPB);
311 bcview().addReadOnly(dialog_->ytED);
312 bcview().addReadOnly(dialog_->xlED);
313 bcview().addReadOnly(dialog_->xrED);
314 bcview().addReadOnly(dialog_->ybED);
315 bcview().addReadOnly(dialog_->extraFormatCO);
316 bcview().addReadOnly(dialog_->extraED);
318 addCheckedLineEdit(bcview(), dialog_->angleED, dialog_->angleLA);
319 addCheckedLineEdit(bcview(), dialog_->displayscaleED, dialog_->scaleLA);
320 addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
321 addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
322 addCheckedLineEdit(bcview(), dialog_->xlED, dialog_->lbLA);
323 addCheckedLineEdit(bcview(), dialog_->ybED, dialog_->lbLA);
324 addCheckedLineEdit(bcview(), dialog_->xrED, dialog_->rtLA);
325 addCheckedLineEdit(bcview(), dialog_->ytED, dialog_->rtLA);
326 addCheckedLineEdit(bcview(), dialog_->fileED, dialog_->fileLA);
328 std::vector<string> templates(controller().getTemplates());
330 for (std::vector<string>::const_iterator cit = templates.begin();
331 cit != templates.end(); ++cit) {
332 dialog_->externalCO->addItem(toqstr(*cit));
335 // Fill the origins combo
336 typedef vector<external::RotationDataType> Origins;
337 Origins const & all_origins = external::all_origins();
338 for (Origins::size_type i = 0; i != all_origins.size(); ++i)
339 dialog_->originCO->addItem(toqstr(external::origin_gui_str(i)));
341 // Fill the width combo
342 dialog_->widthUnitCO->addItem(qt_("Scale%"));
343 for (int i = 0; i < num_units; i++)
344 dialog_->widthUnitCO->addItem(qt_(unit_name_gui[i]));
348 void QExternal::update_contents()
350 PathValidator * path_validator = getPathValidator(dialog_->fileED);
352 path_validator->setChecker(kernel().docType(), lyxrc);
354 dialog_->tab->setCurrentIndex(0);
355 InsetExternalParams const & params = controller().params();
358 params.filename.outputFilename(kernel().bufferFilepath());
359 dialog_->fileED->setText(toqstr(name));
361 dialog_->externalCO->setCurrentIndex(
362 controller().getTemplateNumber(params.templatename()));
365 dialog_->draftCB->setChecked(params.draft);
367 setDisplay(*dialog_->displayCB, *dialog_->showCO,
368 *dialog_->displayscaleED,
369 params.display, params.lyxscale, readOnly());
371 setRotation(*dialog_->angleED, *dialog_->originCO, params.rotationdata);
373 setSize(*dialog_->widthED, *dialog_->widthUnitCO,
374 *dialog_->heightED, *dialog_->heightUnitCO,
375 *dialog_->aspectratioCB,
378 setCrop(*dialog_->clipCB,
379 *dialog_->xlED, *dialog_->ybED,
380 *dialog_->xrED, *dialog_->ytED,
382 controller().bbChanged(!params.clipdata.bbox.empty());
388 void QExternal::updateTemplate()
390 external::Template templ =
391 controller().getTemplate(dialog_->externalCO->currentIndex());
392 dialog_->externalTB->setPlainText(toqstr(templ.helpText));
394 // Ascertain which (if any) transformations the template supports
395 // and disable tabs hosting unsupported transforms.
396 typedef vector<external::TransformID> TransformIDs;
397 TransformIDs const transformIds = templ.transformIds;
398 TransformIDs::const_iterator tr_begin = transformIds.begin();
399 TransformIDs::const_iterator const tr_end = transformIds.end();
401 bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
402 dialog_->tab->setTabEnabled(
403 dialog_->tab->indexOf(dialog_->rotatetab), found);
404 found = find(tr_begin, tr_end, external::Resize) != tr_end;
405 dialog_->tab->setTabEnabled(
406 dialog_->tab->indexOf(dialog_->scaletab), found);
408 found = find(tr_begin, tr_end, external::Clip) != tr_end;
409 dialog_->tab->setTabEnabled(
410 dialog_->tab->indexOf(dialog_->croptab), found);
412 found = find(tr_begin, tr_end, external::Extra) != tr_end;
413 dialog_->tab->setTabEnabled(
414 dialog_->tab->indexOf(dialog_->optionstab), found);
419 // Ascertain whether the template has any formats supporting
420 // the 'Extra' option
421 QLineEdit * const extraED = dialog_->extraED;
422 QComboBox * const extraCB = dialog_->extraFormatCO;
428 external::Template::Formats::const_iterator it = templ.formats.begin();
429 external::Template::Formats::const_iterator end = templ.formats.end();
430 for (; it != end; ++it) {
431 if (it->second.option_transformers.find(external::Extra) ==
432 it->second.option_transformers.end())
434 string const format = it->first;
435 string const opt = controller().params().extradata.get(format);
436 extraCB->addItem(toqstr(format));
437 extra_[format] = toqstr(opt);
440 bool const enabled = extraCB->count() > 0;
442 dialog_->tab->setTabEnabled(
443 dialog_->tab->indexOf(dialog_->optionstab), enabled);
444 extraED->setEnabled(enabled && !kernel().isBufferReadonly());
445 extraCB->setEnabled(enabled);
448 extraCB->setCurrentIndex(0);
449 extraED->setText(extra_[fromqstr(extraCB->currentText())]);
454 void QExternal::apply()
456 InsetExternalParams params = controller().params();
458 params.filename.set(internal_path(fromqstr(dialog_->fileED->text())),
459 kernel().bufferFilepath());
461 params.settemplate(controller().getTemplate(
462 dialog_->externalCO->currentIndex()).lyxName);
464 params.draft = dialog_->draftCB->isChecked();
466 getDisplay(params.display, params.lyxscale,
467 *dialog_->displayCB, *dialog_->showCO,
468 *dialog_->displayscaleED);
470 if (dialog_->tab->isTabEnabled(
471 dialog_->tab->indexOf(dialog_->rotatetab)))
472 getRotation(params.rotationdata,
473 *dialog_->angleED, *dialog_->originCO);
475 if (dialog_->tab->isTabEnabled(
476 dialog_->tab->indexOf(dialog_->scaletab)))
477 getSize(params.resizedata,
478 *dialog_->widthED, *dialog_->widthUnitCO,
479 *dialog_->heightED, *dialog_->heightUnitCO,
480 *dialog_->aspectratioCB);
482 if (dialog_->tab->isTabEnabled(
483 dialog_->tab->indexOf(dialog_->croptab)))
484 getCrop(params.clipdata,
486 *dialog_->xlED, *dialog_->ybED,
487 *dialog_->xrED, *dialog_->ytED,
488 controller().bbChanged());
490 if (dialog_->tab->isTabEnabled(
491 dialog_->tab->indexOf(dialog_->optionstab)))
492 getExtra(params.extradata, extra_);
494 controller().setParams(params);
498 void QExternal::getBB()
500 dialog_->xlED->setText("0");
501 dialog_->ybED->setText("0");
502 dialog_->xrED->setText("0");
503 dialog_->ytED->setText("0");
505 string const filename = fromqstr(dialog_->fileED->text());
506 if (filename.empty())
509 string const bb = controller().readBB(filename);
513 dialog_->xlED->setText(toqstr(token(bb, ' ', 0)));
514 dialog_->ybED->setText(toqstr(token(bb, ' ', 1)));
515 dialog_->xrED->setText(toqstr(token(bb, ' ', 2)));
516 dialog_->ytED->setText(toqstr(token(bb, ' ', 3)));
518 controller().bbChanged(false);
521 } // namespace frontend