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/tostr.h"
27 #include "QExternal.h"
28 #include "QExternalDialog.h"
31 #include "checkedwidgets.h"
32 #include "lengthcombo.h"
33 #include "qt_helpers.h"
35 #include <qlineedit.h>
36 #include <qpushbutton.h>
37 #include <qcheckbox.h>
38 #include <qcombobox.h>
39 #include <qtabwidget.h>
40 #include <qtextview.h>
42 namespace external = lyx::external;
44 using lyx::support::isStrDbl;
45 using lyx::support::strToDbl;
46 using lyx::support::strToInt;
47 using lyx::support::token;
48 using lyx::support::trim;
59 LyXLength::UNIT defaultUnit()
61 LyXLength::UNIT default_unit = LyXLength::CM;
62 switch (lyxrc.default_papersize) {
64 case PAPER_LEGALPAPER:
65 case PAPER_EXECUTIVEPAPER:
66 default_unit = LyXLength::IN;
75 void setDisplay(QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
76 external::DisplayType display, unsigned int scale,
81 case external::DefaultDisplay:
84 case external::MonochromeDisplay:
87 case external::GrayscaleDisplay:
90 case external::ColorDisplay:
93 case external::PreviewDisplay:
96 case external::NoDisplay:
101 showCO.setCurrentItem(item);
102 bool const no_display = display == external::NoDisplay;
103 showCO.setEnabled(!no_display && !read_only);
104 displayCB.setChecked(!no_display);
105 scaleED.setEnabled(!no_display && !read_only);
106 scaleED.setText(toqstr(tostr(scale)));
110 void getDisplay(external::DisplayType & display,
111 unsigned int & scale,
112 QCheckBox const & displayCB,
113 QComboBox const & showCO,
114 QLineEdit const & scaleED)
116 switch (showCO.currentItem()) {
118 display = external::DefaultDisplay;
121 display = external::MonochromeDisplay;
124 display = external::GrayscaleDisplay;
127 display = external::ColorDisplay;
130 display = external::PreviewDisplay;
134 if (!displayCB.isChecked())
135 display = external::NoDisplay;
137 scale = strToInt(fromqstr(scaleED.text()));
141 void setRotation(QLineEdit & angleED, QComboBox & originCO,
142 external::RotationData const & data)
144 originCO.setCurrentItem(int(data.origin()));
145 angleED.setText(toqstr(data.angle));
149 void getRotation(external::RotationData & data,
150 QLineEdit const & angleED, QComboBox const & originCO)
152 typedef external::RotationData::OriginType OriginType;
154 data.origin(static_cast<OriginType>(originCO.currentItem()));
155 data.angle = fromqstr(angleED.text());
159 void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
160 QLineEdit & heightED, LengthCombo & heightUnitCO,
161 QCheckBox & aspectratioCB,
162 external::ResizeData const & data)
164 bool using_scale = data.usingScale();
165 std::string scale = data.scale;
166 if (data.no_resize()) {
167 // Everything is zero, so default to this!
173 widthED.setText(toqstr(scale));
174 widthUnitCO.setCurrentItem(0);
176 widthED.setText(toqstr(tostr(data.width.value())));
177 // Because 'Scale' is position 0...
178 // Note also that width cannot be zero here, so
179 // we don't need to worry about the default unit.
180 widthUnitCO.setCurrentItem(data.width.unit() + 1);
183 string const h = data.height.zero() ? string() : data.height.asString();
184 LyXLength::UNIT default_unit = data.width.zero() ?
185 defaultUnit() : data.width.unit();
186 lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
188 heightED.setEnabled(!using_scale);
189 heightUnitCO.setEnabled(!using_scale);
191 aspectratioCB.setChecked(data.keepAspectRatio);
193 bool const disable_aspectRatio = using_scale ||
194 data.width.zero() || data.height.zero();
195 aspectratioCB.setEnabled(!disable_aspectRatio);
199 void getSize(external::ResizeData & data,
200 QLineEdit const & widthED, QComboBox const & widthUnitCO,
201 QLineEdit const & heightED, LengthCombo const & heightUnitCO,
202 QCheckBox const & aspectratioCB)
204 string const width = fromqstr(widthED.text());
206 if (widthUnitCO.currentItem() > 0) {
207 // Subtract one, because scale is 0.
208 int const unit = widthUnitCO.currentItem() - 1;
211 if (isValidLength(width, &w))
213 else if (isStrDbl(width))
214 data.width = LyXLength(strToDbl(width),
215 static_cast<LyXLength::UNIT>(unit));
217 data.width = LyXLength();
219 data.scale = string();
222 // scaling instead of a width
224 data.width = LyXLength();
227 data.height = LyXLength(widgetsToLength(&heightED, &heightUnitCO));
229 data.keepAspectRatio = aspectratioCB.isChecked();
233 void setCrop(QCheckBox & clipCB,
234 QLineEdit & xlED, QLineEdit & ybED,
235 QLineEdit & xrED, QLineEdit & ytED,
236 external::ClipData const & data)
238 clipCB.setChecked(data.clip);
239 graphics::BoundingBox const & bbox = data.bbox;
240 xlED.setText(toqstr(tostr(bbox.xl)));
241 ybED.setText(toqstr(tostr(bbox.yb)));
242 xrED.setText(toqstr(tostr(bbox.xr)));
243 ytED.setText(toqstr(tostr(bbox.yt)));
247 void getCrop(external::ClipData & data,
248 QCheckBox const & clipCB,
249 QLineEdit const & xlED, QLineEdit const & ybED,
250 QLineEdit const & xrED, QLineEdit const & ytED,
253 data.clip = clipCB.isChecked();
258 data.bbox.xl = strToInt(fromqstr(xlED.text()));
259 data.bbox.yb = strToInt(fromqstr(ybED.text()));
260 data.bbox.xr = strToInt(fromqstr(xrED.text()));
261 data.bbox.yt = strToInt(fromqstr(ytED.text()));
265 void getExtra(external::ExtraData & data,
266 QExternal::MapType const & extra)
268 typedef QExternal::MapType MapType;
269 MapType::const_iterator it = extra.begin();
270 MapType::const_iterator const end = extra.end();
271 for (; it != end; ++it)
272 data.set(it->first, trim(fromqstr(it->second)));
278 typedef QController<ControlExternal, QView<QExternalDialog> > base_class;
280 QExternal::QExternal(Dialog & parent)
281 : base_class(parent, _("LyX: External Material"))
285 void QExternal::build_dialog()
287 dialog_.reset(new QExternalDialog(this));
289 bcview().setOK(dialog_->okPB);
290 bcview().setApply(dialog_->applyPB);
291 bcview().setCancel(dialog_->closePB);
293 bcview().addReadOnly(dialog_->fileED);
294 bcview().addReadOnly(dialog_->browsePB);
295 bcview().addReadOnly(dialog_->editPB);
296 bcview().addReadOnly(dialog_->externalCO);
297 bcview().addReadOnly(dialog_->draftCB);
298 bcview().addReadOnly(dialog_->displayscaleED);
299 bcview().addReadOnly(dialog_->showCO);
300 bcview().addReadOnly(dialog_->displayCB);
301 bcview().addReadOnly(dialog_->angleED);
302 bcview().addReadOnly(dialog_->originCO);
303 bcview().addReadOnly(dialog_->heightUnitCO);
304 bcview().addReadOnly(dialog_->heightED);
305 bcview().addReadOnly(dialog_->aspectratioCB);
306 bcview().addReadOnly(dialog_->widthUnitCO);
307 bcview().addReadOnly(dialog_->widthED);
308 bcview().addReadOnly(dialog_->clipCB);
309 bcview().addReadOnly(dialog_->getbbPB);
310 bcview().addReadOnly(dialog_->ytED);
311 bcview().addReadOnly(dialog_->xlED);
312 bcview().addReadOnly(dialog_->xrED);
313 bcview().addReadOnly(dialog_->ybED);
314 bcview().addReadOnly(dialog_->extraFormatCO);
315 bcview().addReadOnly(dialog_->extraED);
317 addCheckedLineEdit(bcview(), dialog_->angleED, dialog_->angleLA);
318 addCheckedLineEdit(bcview(), dialog_->displayscaleED, dialog_->scaleLA);
319 addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
320 addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
321 addCheckedLineEdit(bcview(), dialog_->xlED, dialog_->lbLA);
322 addCheckedLineEdit(bcview(), dialog_->ybED, dialog_->lbLA);
323 addCheckedLineEdit(bcview(), dialog_->xrED, dialog_->rtLA);
324 addCheckedLineEdit(bcview(), dialog_->ytED, dialog_->rtLA);
326 std::vector<string> templates(controller().getTemplates());
328 for (std::vector<string>::const_iterator cit = templates.begin();
329 cit != templates.end(); ++cit) {
330 dialog_->externalCO->insertItem(toqstr(*cit), -1);
333 // Fill the origins combo
334 typedef vector<external::RotationDataType> Origins;
335 Origins const & all_origins = external::all_origins();
336 for (Origins::size_type i = 0; i != all_origins.size(); ++i)
337 dialog_->originCO->insertItem(toqstr(external::origin_gui_str(i)));
339 // Fill the width combo
340 dialog_->widthUnitCO->insertItem(qt_("Scale%"));
341 for (int i = 0; i < num_units; i++)
342 dialog_->widthUnitCO->insertItem(unit_name_gui[i], -1);
346 void QExternal::update_contents()
348 dialog_->tab->setCurrentPage(0);
349 InsetExternalParams const & params = controller().params();
352 params.filename.outputFilename(kernel().bufferFilepath());
353 dialog_->fileED->setText(toqstr(name));
355 dialog_->externalCO->setCurrentItem(
356 controller().getTemplateNumber(params.templatename()));
359 dialog_->draftCB->setChecked(params.draft);
361 setDisplay(*dialog_->displayCB, *dialog_->showCO,
362 *dialog_->displayscaleED,
363 params.display, params.lyxscale, readOnly());
365 setRotation(*dialog_->angleED, *dialog_->originCO, params.rotationdata);
367 setSize(*dialog_->widthED, *dialog_->widthUnitCO,
368 *dialog_->heightED, *dialog_->heightUnitCO,
369 *dialog_->aspectratioCB,
372 setCrop(*dialog_->clipCB,
373 *dialog_->xlED, *dialog_->ybED,
374 *dialog_->xrED, *dialog_->ytED,
376 controller().bbChanged(!params.clipdata.bbox.empty());
382 void QExternal::updateTemplate()
384 external::Template templ =
385 controller().getTemplate(dialog_->externalCO->currentItem());
386 dialog_->externalTV->setText(toqstr(templ.helpText));
388 // Ascertain which (if any) transformations the template supports
389 // and disable tabs hosting unsupported transforms.
390 typedef vector<external::TransformID> TransformIDs;
391 TransformIDs const transformIds = templ.transformIds;
392 TransformIDs::const_iterator tr_begin = transformIds.begin();
393 TransformIDs::const_iterator const tr_end = transformIds.end();
395 bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
396 dialog_->tab->setTabEnabled(dialog_->rotatetab, found);
398 found = find(tr_begin, tr_end, external::Resize) != tr_end;
399 dialog_->tab->setTabEnabled(dialog_->scaletab, found);
401 found = find(tr_begin, tr_end, external::Clip) != tr_end;
402 dialog_->tab->setTabEnabled(dialog_->croptab, found);
404 found = find(tr_begin, tr_end, external::Extra) != tr_end;
405 dialog_->tab->setTabEnabled(dialog_->optionstab, found);
409 // Ascertain whether the template has any formats supporting
410 // the 'Extra' option
411 QLineEdit * const extraED = dialog_->extraED;
412 QComboBox * const extraCB = dialog_->extraFormatCO;
418 external::Template::Formats::const_iterator it = templ.formats.begin();
419 external::Template::Formats::const_iterator end = templ.formats.end();
420 for (; it != end; ++it) {
421 if (it->second.option_transformers.find(external::Extra) ==
422 it->second.option_transformers.end())
424 string const format = it->first;
425 string const opt = controller().params().extradata.get(format);
426 extraCB->insertItem(toqstr(format));
427 extra_[format] = toqstr(opt);
430 bool const enabled = extraCB->count() > 0;
432 dialog_->tab->setTabEnabled(dialog_->optionstab, enabled);
433 extraED->setEnabled(enabled && !kernel().isBufferReadonly());
434 extraCB->setEnabled(enabled);
437 extraCB->setCurrentItem(0);
438 extraED->setText(extra_[fromqstr(extraCB->currentText())]);
443 void QExternal::apply()
445 InsetExternalParams params = controller().params();
447 params.filename.set(fromqstr(dialog_->fileED->text()),
448 kernel().bufferFilepath());
450 params.settemplate(controller().getTemplate(
451 dialog_->externalCO->currentItem()).lyxName);
453 params.draft = dialog_->draftCB->isChecked();
455 getDisplay(params.display, params.lyxscale,
456 *dialog_->displayCB, *dialog_->showCO,
457 *dialog_->displayscaleED);
459 if (dialog_->tab->isTabEnabled(dialog_->rotatetab))
460 getRotation(params.rotationdata,
461 *dialog_->angleED, *dialog_->originCO);
463 if (dialog_->tab->isTabEnabled(dialog_->scaletab))
464 getSize(params.resizedata,
465 *dialog_->widthED, *dialog_->widthUnitCO,
466 *dialog_->heightED, *dialog_->heightUnitCO,
467 *dialog_->aspectratioCB);
469 if (dialog_->tab->isTabEnabled(dialog_->croptab))
470 getCrop(params.clipdata,
472 *dialog_->xlED, *dialog_->ybED,
473 *dialog_->xrED, *dialog_->ytED,
474 controller().bbChanged());
476 if (dialog_->tab->isTabEnabled(dialog_->optionstab))
477 getExtra(params.extradata, extra_);
479 controller().setParams(params);
483 void QExternal::getBB()
485 dialog_->xlED->setText("0");
486 dialog_->ybED->setText("0");
487 dialog_->xrED->setText("0");
488 dialog_->ytED->setText("0");
490 string const filename = fromqstr(dialog_->fileED->text());
491 if (filename.empty())
494 string const bb = controller().readBB(filename);
498 dialog_->xlED->setText(toqstr(token(bb, ' ', 0)));
499 dialog_->ybED->setText(toqstr(token(bb, ' ', 1)));
500 dialog_->xrED->setText(toqstr(token(bb, ' ', 2)));
501 dialog_->ytED->setText(toqstr(token(bb, ' ', 3)));
503 controller().bbChanged(false);
506 } // namespace frontend