]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QExternal.C
Patch 1 Log:
[lyx.git] / src / frontends / qt4 / QExternal.C
1 /**
2  * \file QExternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
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"
17 #include "lyxrc.h"
18
19 #include "controllers/ControlExternal.h"
20
21 #include "insets/ExternalTemplate.h"
22 #include "insets/insetexternal.h"
23
24 #include "support/lstrings.h"
25 #include "support/convert.h"
26
27 #include "QExternal.h"
28 #include "QExternalDialog.h"
29 #include "Qt2BC.h"
30
31 #include "checkedwidgets.h"
32 #include "lengthcombo.h"
33 #include "qt_helpers.h"
34 #include "validators.h"
35
36 #include <QLineEdit>
37 #include <QPushButton>
38 #include <QCheckBox>
39 #include <QTabWidget>
40 #include <QTextBrowser>
41
42 namespace external = lyx::external;
43
44 using lyx::support::isStrDbl;
45 using lyx::support::token;
46 using lyx::support::trim;
47
48 using std::string;
49 using std::vector;
50 using std::find;
51
52 namespace lyx {
53 namespace frontend {
54
55 namespace {
56
57 LyXLength::UNIT defaultUnit()
58 {
59         LyXLength::UNIT default_unit = LyXLength::CM;
60         switch (lyxrc.default_papersize) {
61         case PAPER_USLETTER:
62         case PAPER_USLEGAL:
63         case PAPER_USEXECUTIVE:
64                 default_unit = LyXLength::IN;
65                 break;
66         default:
67                 break;
68         }
69         return default_unit;
70 }
71
72
73 void setDisplay(QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
74                 external::DisplayType display, unsigned int scale,
75                 bool read_only)
76 {
77         int item = 0;
78         switch (display) {
79         case external::DefaultDisplay:
80                 item = 0;
81                 break;
82         case external::MonochromeDisplay:
83                 item = 1;
84                 break;
85         case external::GrayscaleDisplay:
86                 item = 2;
87                 break;
88         case external::ColorDisplay:
89                 item = 3;
90                 break;
91         case external::PreviewDisplay:
92                 item = 4;
93                 break;
94         case external::NoDisplay:
95                 item = 0;
96                 break;
97         }
98
99         showCO.setCurrentIndex(item);
100         bool const no_display = display == external::NoDisplay;
101         showCO.setEnabled(!no_display && !read_only);
102         displayCB.setChecked(!no_display);
103         scaleED.setEnabled(!no_display && !read_only);
104         scaleED.setText(toqstr(convert<string>(scale)));
105 }
106
107
108 void getDisplay(external::DisplayType & display,
109                 unsigned int & scale,
110                 QCheckBox const & displayCB,
111                 QComboBox const & showCO,
112                 QLineEdit const & scaleED)
113 {
114         switch (showCO.currentIndex()) {
115         case 0:
116                 display = external::DefaultDisplay;
117                 break;
118         case 1:
119                 display = external::MonochromeDisplay;
120                 break;
121         case 2:
122                 display = external::GrayscaleDisplay;
123                 break;
124         case 3:
125                 display = external::ColorDisplay;
126                 break;
127         case 4:
128                 display = external::PreviewDisplay;
129                 break;
130         }
131
132         if (!displayCB.isChecked())
133                 display = external::NoDisplay;
134
135         scale = convert<int>(fromqstr(scaleED.text()));
136 }
137
138
139 void setRotation(QLineEdit & angleED, QComboBox & originCO,
140                  external::RotationData const & data)
141 {
142         originCO.setCurrentIndex(int(data.origin()));
143         angleED.setText(toqstr(data.angle));
144 }
145
146
147 void getRotation(external::RotationData & data,
148                  QLineEdit const & angleED, QComboBox const & originCO)
149 {
150         typedef external::RotationData::OriginType OriginType;
151
152         data.origin(static_cast<OriginType>(originCO.currentIndex()));
153         data.angle = fromqstr(angleED.text());
154 }
155
156
157 void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
158              QLineEdit & heightED, LengthCombo & heightUnitCO,
159              QCheckBox & aspectratioCB,
160              external::ResizeData const & data)
161 {
162         bool using_scale = data.usingScale();
163         std::string scale = data.scale;
164         if (data.no_resize()) {
165                 // Everything is zero, so default to this!
166                 using_scale = true;
167                 scale = "100";
168         }
169
170         if (using_scale) {
171                 widthED.setText(toqstr(scale));
172                 widthUnitCO.setCurrentIndex(0);
173         } else {
174                 widthED.setText(toqstr(convert<string>(data.width.value())));
175                 // Because 'Scale' is position 0...
176                 // Note also that width cannot be zero here, so
177                 // we don't need to worry about the default unit.
178                 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
179         }
180
181         string const h = data.height.zero() ? string() : data.height.asString();
182         LyXLength::UNIT default_unit = data.width.zero() ?
183                 defaultUnit() : data.width.unit();
184         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
185
186         heightED.setEnabled(!using_scale);
187         heightUnitCO.setEnabled(!using_scale);
188
189         aspectratioCB.setChecked(data.keepAspectRatio);
190
191         bool const disable_aspectRatio = using_scale ||
192                 data.width.zero() || data.height.zero();
193         aspectratioCB.setEnabled(!disable_aspectRatio);
194 }
195
196
197 void getSize(external::ResizeData & data,
198              QLineEdit const & widthED, QComboBox const & widthUnitCO,
199              QLineEdit const & heightED, LengthCombo const & heightUnitCO,
200              QCheckBox const & aspectratioCB)
201 {
202         string const width = fromqstr(widthED.text());
203
204         if (widthUnitCO.currentIndex() > 0) {
205                 // Subtract one, because scale is 0.
206                 int const unit = widthUnitCO.currentIndex() - 1;
207
208                 LyXLength w;
209                 if (isValidLength(width, &w))
210                         data.width = w;
211                 else if (isStrDbl(width))
212                         data.width = LyXLength(convert<double>(width),
213                                            static_cast<LyXLength::UNIT>(unit));
214                 else
215                         data.width = LyXLength();
216
217                 data.scale = string();
218
219         } else {
220                 // scaling instead of a width
221                 data.scale = width;
222                 data.width = LyXLength();
223         }
224
225         data.height = LyXLength(widgetsToLength(&heightED, &heightUnitCO));
226
227         data.keepAspectRatio = aspectratioCB.isChecked();
228 }
229
230
231 void setCrop(QCheckBox & clipCB,
232              QLineEdit & xlED, QLineEdit & ybED,
233              QLineEdit & xrED, QLineEdit & ytED,
234              external::ClipData const & data)
235 {
236         clipCB.setChecked(data.clip);
237         graphics::BoundingBox const & bbox = data.bbox;
238         xlED.setText(toqstr(convert<string>(bbox.xl)));
239         ybED.setText(toqstr(convert<string>(bbox.yb)));
240         xrED.setText(toqstr(convert<string>(bbox.xr)));
241         ytED.setText(toqstr(convert<string>(bbox.yt)));
242 }
243
244
245 void getCrop(external::ClipData & data,
246              QCheckBox const & clipCB,
247              QLineEdit const & xlED, QLineEdit const & ybED,
248              QLineEdit const & xrED, QLineEdit const & ytED,
249              bool bb_changed)
250 {
251         data.clip = clipCB.isChecked();
252
253         if (!bb_changed)
254                 return;
255
256         data.bbox.xl = convert<int>(fromqstr(xlED.text()));
257         data.bbox.yb = convert<int>(fromqstr(ybED.text()));
258         data.bbox.xr = convert<int>(fromqstr(xrED.text()));
259         data.bbox.yt = convert<int>(fromqstr(ytED.text()));
260 }
261
262
263 void getExtra(external::ExtraData & data,
264               QExternal::MapType const & extra)
265 {
266         typedef QExternal::MapType MapType;
267         MapType::const_iterator it  = extra.begin();
268         MapType::const_iterator const end = extra.end();
269         for (; it != end; ++it)
270                 data.set(it->first, trim(fromqstr(it->second)));
271 }
272
273 } // namespace anon
274
275
276 typedef QController<ControlExternal, QView<QExternalDialog> > base_class;
277
278 QExternal::QExternal(Dialog & parent)
279         : base_class(parent, _("External Material"))
280 {}
281
282
283 void QExternal::build_dialog()
284 {
285         dialog_.reset(new QExternalDialog(this));
286
287         bcview().setOK(dialog_->okPB);
288         bcview().setApply(dialog_->applyPB);
289         bcview().setCancel(dialog_->closePB);
290
291         bcview().addReadOnly(dialog_->fileED);
292         bcview().addReadOnly(dialog_->browsePB);
293         bcview().addReadOnly(dialog_->editPB);
294         bcview().addReadOnly(dialog_->externalCO);
295         bcview().addReadOnly(dialog_->draftCB);
296         bcview().addReadOnly(dialog_->displayscaleED);
297         bcview().addReadOnly(dialog_->showCO);
298         bcview().addReadOnly(dialog_->displayCB);
299         bcview().addReadOnly(dialog_->angleED);
300         bcview().addReadOnly(dialog_->originCO);
301         bcview().addReadOnly(dialog_->heightUnitCO);
302         bcview().addReadOnly(dialog_->heightED);
303         bcview().addReadOnly(dialog_->aspectratioCB);
304         bcview().addReadOnly(dialog_->widthUnitCO);
305         bcview().addReadOnly(dialog_->widthED);
306         bcview().addReadOnly(dialog_->clipCB);
307         bcview().addReadOnly(dialog_->getbbPB);
308         bcview().addReadOnly(dialog_->ytED);
309         bcview().addReadOnly(dialog_->xlED);
310         bcview().addReadOnly(dialog_->xrED);
311         bcview().addReadOnly(dialog_->ybED);
312         bcview().addReadOnly(dialog_->extraFormatCO);
313         bcview().addReadOnly(dialog_->extraED);
314
315         addCheckedLineEdit(bcview(), dialog_->angleED, dialog_->angleLA);
316         addCheckedLineEdit(bcview(), dialog_->displayscaleED, dialog_->scaleLA);
317         addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
318         addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
319         addCheckedLineEdit(bcview(), dialog_->xlED, dialog_->lbLA);
320         addCheckedLineEdit(bcview(), dialog_->ybED, dialog_->lbLA);
321         addCheckedLineEdit(bcview(), dialog_->xrED, dialog_->rtLA);
322         addCheckedLineEdit(bcview(), dialog_->ytED, dialog_->rtLA);
323         addCheckedLineEdit(bcview(), dialog_->fileED, dialog_->fileLA);
324
325         std::vector<string> templates(controller().getTemplates());
326
327         for (std::vector<string>::const_iterator cit = templates.begin();
328                 cit != templates.end(); ++cit) {
329                 dialog_->externalCO->addItem(toqstr(*cit));
330         }
331
332         // Fill the origins combo
333         typedef vector<external::RotationDataType> Origins;
334         Origins const & all_origins = external::all_origins();
335         for (Origins::size_type i = 0; i != all_origins.size(); ++i)
336                 dialog_->originCO->addItem(toqstr(external::origin_gui_str(i)));
337
338         // Fill the width combo
339         dialog_->widthUnitCO->addItem(qt_("Scale%"));
340         for (int i = 0; i < num_units; i++)
341                 dialog_->widthUnitCO->addItem(unit_name_gui[i]);
342 }
343
344
345 void QExternal::update_contents()
346 {
347         PathValidator * path_validator = getPathValidator(dialog_->fileED);
348         if (path_validator)
349                 path_validator->setChecker(kernel().docType(), lyxrc);
350
351         dialog_->tab->setCurrentIndex(0);
352         InsetExternalParams const & params = controller().params();
353
354         string const name =
355                 params.filename.outputFilename(kernel().bufferFilepath());
356         dialog_->fileED->setText(toqstr(name));
357
358         dialog_->externalCO->setCurrentIndex(
359                 controller().getTemplateNumber(params.templatename()));
360         updateTemplate();
361
362         dialog_->draftCB->setChecked(params.draft);
363
364         setDisplay(*dialog_->displayCB, *dialog_->showCO,
365                    *dialog_->displayscaleED,
366                    params.display, params.lyxscale, readOnly());
367
368         setRotation(*dialog_->angleED, *dialog_->originCO, params.rotationdata);
369
370         setSize(*dialog_->widthED, *dialog_->widthUnitCO,
371                 *dialog_->heightED, *dialog_->heightUnitCO,
372                 *dialog_->aspectratioCB,
373                 params.resizedata);
374
375         setCrop(*dialog_->clipCB,
376                 *dialog_->xlED, *dialog_->ybED,
377                 *dialog_->xrED, *dialog_->ytED,
378                 params.clipdata);
379         controller().bbChanged(!params.clipdata.bbox.empty());
380
381         isValid();
382 }
383
384
385 void QExternal::updateTemplate()
386 {
387         external::Template templ =
388                 controller().getTemplate(dialog_->externalCO->currentIndex());
389         dialog_->externalTB->setPlainText(toqstr(templ.helpText));
390
391         // Ascertain which (if any) transformations the template supports
392         // and disable tabs hosting unsupported transforms.
393         typedef vector<external::TransformID> TransformIDs;
394         TransformIDs const transformIds = templ.transformIds;
395         TransformIDs::const_iterator tr_begin = transformIds.begin();
396         TransformIDs::const_iterator const tr_end = transformIds.end();
397
398         bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
399         dialog_->tab->setTabEnabled(
400                 dialog_->tab->indexOf(dialog_->rotatetab), found);
401         found = find(tr_begin, tr_end, external::Resize) != tr_end;
402         dialog_->tab->setTabEnabled(
403                 dialog_->tab->indexOf(dialog_->scaletab), found);
404
405         found = find(tr_begin, tr_end, external::Clip) != tr_end;
406         dialog_->tab->setTabEnabled(
407                 dialog_->tab->indexOf(dialog_->croptab), found);
408
409         found = find(tr_begin, tr_end, external::Extra) != tr_end;
410         dialog_->tab->setTabEnabled(
411                 dialog_->tab->indexOf(dialog_->optionstab), found);
412
413         if (!found)
414                 return;
415
416         // Ascertain whether the template has any formats supporting
417         // the 'Extra' option
418         QLineEdit * const extraED = dialog_->extraED;
419         QComboBox * const extraCB = dialog_->extraFormatCO;
420
421         extra_.clear();
422         extraED->clear();
423         extraCB->clear();
424
425         external::Template::Formats::const_iterator it  = templ.formats.begin();
426         external::Template::Formats::const_iterator end = templ.formats.end();
427         for (; it != end; ++it) {
428                 if (it->second.option_transformers.find(external::Extra) ==
429                     it->second.option_transformers.end())
430                         continue;
431                 string const format = it->first;
432                 string const opt = controller().params().extradata.get(format);
433                 extraCB->addItem(toqstr(format));
434                 extra_[format] = toqstr(opt);
435         }
436
437         bool const enabled = extraCB->count()  > 0;
438
439         dialog_->tab->setTabEnabled(
440                 dialog_->tab->indexOf(dialog_->optionstab), enabled);
441         extraED->setEnabled(enabled && !kernel().isBufferReadonly());
442         extraCB->setEnabled(enabled);
443
444         if (enabled) {
445                 extraCB->setCurrentIndex(0);
446                 extraED->setText(extra_[fromqstr(extraCB->currentText())]);
447         }
448 }
449
450
451 void QExternal::apply()
452 {
453         InsetExternalParams params = controller().params();
454
455         params.filename.set(fromqstr(dialog_->fileED->text()),
456                             kernel().bufferFilepath());
457
458         params.settemplate(controller().getTemplate(
459                                    dialog_->externalCO->currentIndex()).lyxName);
460
461         params.draft = dialog_->draftCB->isChecked();
462
463         getDisplay(params.display, params.lyxscale,
464                    *dialog_->displayCB, *dialog_->showCO,
465                    *dialog_->displayscaleED);
466
467         if (dialog_->tab->isTabEnabled(
468                 dialog_->tab->indexOf(dialog_->rotatetab)))
469                 getRotation(params.rotationdata,
470                             *dialog_->angleED, *dialog_->originCO);
471
472         if (dialog_->tab->isTabEnabled(
473                 dialog_->tab->indexOf(dialog_->scaletab)))
474                 getSize(params.resizedata,
475                         *dialog_->widthED, *dialog_->widthUnitCO,
476                         *dialog_->heightED, *dialog_->heightUnitCO,
477                         *dialog_->aspectratioCB);
478
479         if (dialog_->tab->isTabEnabled(
480                 dialog_->tab->indexOf(dialog_->croptab)))
481                 getCrop(params.clipdata,
482                         *dialog_->clipCB,
483                         *dialog_->xlED, *dialog_->ybED,
484                         *dialog_->xrED, *dialog_->ytED,
485                         controller().bbChanged());
486
487         if (dialog_->tab->isTabEnabled(
488                 dialog_->tab->indexOf(dialog_->optionstab)))
489                 getExtra(params.extradata, extra_);
490
491         controller().setParams(params);
492 }
493
494
495 void QExternal::getBB()
496 {
497         dialog_->xlED->setText("0");
498         dialog_->ybED->setText("0");
499         dialog_->xrED->setText("0");
500         dialog_->ytED->setText("0");
501
502         string const filename = fromqstr(dialog_->fileED->text());
503         if (filename.empty())
504                 return;
505
506         string const bb = controller().readBB(filename);
507         if (bb.empty())
508                 return;
509
510         dialog_->xlED->setText(toqstr(token(bb, ' ', 0)));
511         dialog_->ybED->setText(toqstr(token(bb, ' ', 1)));
512         dialog_->xrED->setText(toqstr(token(bb, ' ', 2)));
513         dialog_->ytED->setText(toqstr(token(bb, ' ', 3)));
514
515         controller().bbChanged(false);
516 }
517
518 } // namespace frontend
519 } // namespace lyx