]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiExternal.cpp
Typos.
[lyx.git] / src / frontends / qt4 / GuiExternal.cpp
1 /**
2  * \file GuiExternal.cpp
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  * \author Asger Alstrup
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiExternal.h"
16
17 #include "FuncRequest.h"
18 #include "support/gettext.h"
19 #include "Length.h"
20 #include "LyXRC.h"
21
22 #include "insets/ExternalSupport.h"
23 #include "insets/ExternalTemplate.h"
24 #include "insets/InsetExternal.h"
25
26 #include "graphics/GraphicsCache.h"
27 #include "graphics/GraphicsCacheItem.h"
28 #include "graphics/GraphicsImage.h"
29
30 #include "support/convert.h"
31 #include "support/filetools.h"
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
34 #include "support/os.h"
35
36 #include "LengthCombo.h"
37 #include "qt_helpers.h"
38 #include "Validator.h"
39
40 #include <QCheckBox>
41 #include <QGroupBox>
42 #include <QLineEdit>
43 #include <QPushButton>
44 #include <QTabWidget>
45 #include <QTextBrowser>
46
47 using namespace std;
48 using namespace lyx::support;
49
50 namespace lyx {
51 namespace frontend {
52
53 using namespace external;
54
55 namespace {
56
57 RotationDataType origins[] = {
58         RotationData::DEFAULT,
59         RotationData::TOPLEFT,
60         RotationData::BOTTOMLEFT,
61         RotationData::BASELINELEFT,
62         RotationData::CENTER,
63         RotationData::TOPCENTER,
64         RotationData::BOTTOMCENTER,
65         RotationData::BASELINECENTER,
66         RotationData::TOPRIGHT,
67         RotationData::BOTTOMRIGHT,
68         RotationData::BASELINERIGHT
69 };
70
71
72 // These are the strings, corresponding to the above, that the GUI should
73 // use. Note that they can/should be translated.
74 char const * const origin_gui_strs[] = {
75         N_("Default"),
76         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
77         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
78         N_("Top right"), N_("Bottom right"), N_("Baseline right")
79 };
80
81 external::Template getTemplate(int i)
82 {
83         external::TemplateManager::Templates::const_iterator i1
84                 = external::TemplateManager::get().getTemplates().begin();
85         advance(i1, i);
86         return i1->second;
87 }
88
89 } // namespace anon
90
91
92 GuiExternal::GuiExternal(GuiView & lv)
93         : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
94 {
95         setupUi(this);
96
97         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
98         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
99         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
100
101         /*
102         connect(displayGB, SIGNAL(toggled(bool)),
103                 displayscaleED, SLOT(setEnabled(bool)));
104                 */
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(externalCO, SIGNAL(activated(QString)),
112                 this, SLOT(templateChanged()));
113         connect(extraED, SIGNAL(textChanged(QString)),
114                 this, SLOT(extraChanged(QString)));
115         connect(extraFormatCO, SIGNAL(activated(QString)),
116                 this, SLOT(formatChanged(QString)));
117         connect(widthUnitCO, SIGNAL(activated(int)),
118                 this, SLOT(widthUnitChanged()));
119         connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
120                 this, SLOT(change_adaptor()));
121         connect(displayGB, SIGNAL(toggled(bool)),
122                 this, SLOT(change_adaptor()));
123         connect(displayscaleED, SIGNAL(textChanged(QString)),
124                 this, SLOT(change_adaptor()));
125         connect(angleED, SIGNAL(textChanged(QString)),
126                 this, SLOT(change_adaptor()));
127         connect(widthED, SIGNAL(textChanged(QString)),
128                 this, SLOT(sizeChanged()));
129         connect(heightED, SIGNAL(textChanged(QString)),
130                 this, SLOT(sizeChanged()));
131         connect(fileED, SIGNAL(textChanged(QString)),
132                 this, SLOT(change_adaptor()));
133         connect(clipCB, SIGNAL(stateChanged(int)),
134                 this, SLOT(change_adaptor()));
135         connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
136         connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
137         connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
138         connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
139         connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
140         connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
141
142         QIntValidator * validator = new QIntValidator(displayscaleED);
143         validator->setBottom(1);
144         displayscaleED->setValidator(validator);
145
146         angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
147
148         xlED->setValidator(new QIntValidator(xlED));
149         ybED->setValidator(new QIntValidator(ybED));
150         xrED->setValidator(new QIntValidator(xrED));
151         ytED->setValidator(new QIntValidator(ytED));
152
153         widthED->setValidator(unsignedLengthValidator(widthED));
154         heightED->setValidator(unsignedLengthValidator(heightED));
155
156         setFocusProxy(fileED);
157
158         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
159
160         bc().setOK(okPB);
161         bc().setApply(applyPB);
162         bc().setCancel(closePB);
163
164         bc().addReadOnly(fileED);
165         bc().addReadOnly(browsePB);
166         bc().addReadOnly(externalCO);
167         bc().addReadOnly(draftCB);
168         bc().addReadOnly(displayscaleED);
169         bc().addReadOnly(displayGB);
170         bc().addReadOnly(angleED);
171         bc().addReadOnly(originCO);
172         bc().addReadOnly(heightUnitCO);
173         bc().addReadOnly(heightED);
174         bc().addReadOnly(aspectratioCB);
175         bc().addReadOnly(widthUnitCO);
176         bc().addReadOnly(widthED);
177         bc().addReadOnly(clipCB);
178         bc().addReadOnly(getbbPB);
179         bc().addReadOnly(ytED);
180         bc().addReadOnly(xlED);
181         bc().addReadOnly(xrED);
182         bc().addReadOnly(ybED);
183         bc().addReadOnly(extraFormatCO);
184         bc().addReadOnly(extraED);
185
186         bc().addCheckedLineEdit(angleED, angleLA);
187         bc().addCheckedLineEdit(displayscaleED, scaleLA);
188         bc().addCheckedLineEdit(heightED, heightLA);
189         bc().addCheckedLineEdit(widthED, widthLA);
190         bc().addCheckedLineEdit(xlED, lbLA);
191         bc().addCheckedLineEdit(ybED, lbLA);
192         bc().addCheckedLineEdit(xrED, rtLA);
193         bc().addCheckedLineEdit(ytED, rtLA);
194         bc().addCheckedLineEdit(fileED, fileLA);
195
196         external::TemplateManager::Templates::const_iterator i1, i2;
197         i1 = external::TemplateManager::get().getTemplates().begin();
198         i2 = external::TemplateManager::get().getTemplates().end();
199         for (; i1 != i2; ++i1)
200                 externalCO->addItem(qt_(i1->second.lyxName));
201
202         // Fill the origins combo
203         for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
204                 originCO->addItem(qt_(origin_gui_strs[i]));
205
206         // Fill the width combo
207         widthUnitCO->addItem(qt_("Scale%"));
208         for (int i = 0; i < num_units; i++)
209                 widthUnitCO->addItem(qt_(unit_name_gui[i]));
210 }
211
212
213 bool GuiExternal::activateAspectratio() const
214 {
215         if (widthUnitCO->currentIndex() == 0)
216                 return false;
217
218         string const wstr = fromqstr(widthED->text());
219         if (wstr.empty())
220                 return false;
221         bool const wIsDbl = isStrDbl(wstr);
222         if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
223                 return false;
224         Length l;
225         if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
226                 return false;
227
228         string const hstr = fromqstr(heightED->text());
229         if (hstr.empty())
230                 return false;
231         bool const hIsDbl = isStrDbl(hstr);
232         if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
233                 return false;
234         if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
235                 return false;
236
237         return true;
238 }
239
240
241 void GuiExternal::bbChanged()
242 {
243         bbChanged_ = true;
244         changed();
245 }
246
247
248 void GuiExternal::browseClicked()
249 {
250         int const choice =  externalCO->currentIndex();
251         QString const template_name = toqstr(getTemplate(choice).lyxName);
252         QString const str = browse(fileED->text(), template_name);
253         if (!str.isEmpty()) {
254                 fileED->setText(str);
255                 changed();
256         }
257 }
258
259
260 void GuiExternal::change_adaptor()
261 {
262         changed();
263 }
264
265
266 void GuiExternal::extraChanged(const QString & text)
267 {
268         extra_[extraFormatCO->currentText()] = text;
269         changed();
270 }
271
272
273 void GuiExternal::formatChanged(const QString & format)
274 {
275         extraED->setText(extra_[format]);
276 }
277
278
279 void GuiExternal::getbbClicked()
280 {
281         xlED->setText("0");
282         ybED->setText("0");
283         xrED->setText("0");
284         ytED->setText("0");
285
286         string const filename = fromqstr(fileED->text());
287         if (filename.empty())
288                 return;
289
290         FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilepath())));
291
292         // try to get it from the file, if possible
293         string bb = readBB_from_PSFile(abs_file);
294         if (bb.empty()) {
295                 // we don't, so ask the Graphics Cache if it has loaded the file
296                 int width = 0;
297                 int height = 0;
298
299                 graphics::Cache & gc = graphics::Cache::get();
300                 if (gc.inCache(abs_file)) {
301                         graphics::Image const * image = gc.item(abs_file)->image();
302
303                         if (image) {
304                                 width  = image->width();
305                                 height = image->height();
306                         }
307                 }
308                 bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
309         }
310
311         xlED->setText(toqstr(token(bb, ' ', 0)));
312         ybED->setText(toqstr(token(bb, ' ', 1)));
313         xrED->setText(toqstr(token(bb, ' ', 2)));
314         ytED->setText(toqstr(token(bb, ' ', 3)));
315
316         bbChanged_ = false;
317 }
318
319
320 void GuiExternal::sizeChanged()
321 {
322         aspectratioCB->setEnabled(activateAspectratio());
323         changed();
324 }
325
326
327 void GuiExternal::templateChanged()
328 {
329         updateTemplate();
330         changed();
331 }
332
333
334 void GuiExternal::widthUnitChanged()
335 {
336         bool useHeight = (widthUnitCO->currentIndex() > 0);
337
338         if (useHeight)
339                 widthED->setValidator(unsignedLengthValidator(widthED));
340         else
341                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
342
343         heightED->setEnabled(useHeight);
344         heightUnitCO->setEnabled(useHeight);
345         changed();
346 }
347
348
349 static Length::UNIT defaultUnit()
350 {
351         Length::UNIT default_unit = Length::CM;
352         switch (lyxrc.default_papersize) {
353         case PAPER_USLETTER:
354         case PAPER_USLEGAL:
355         case PAPER_USEXECUTIVE:
356                 default_unit = Length::IN;
357                 break;
358         default:
359                 break;
360         }
361         return default_unit;
362 }
363
364
365 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
366         external::RotationData const & data)
367 {
368         originCO.setCurrentIndex(int(data.origin()));
369         angleED.setText(toqstr(data.angle));
370 }
371
372
373 static void getRotation(external::RotationData & data,
374         QLineEdit const & angleED, QComboBox const & originCO)
375 {
376         typedef external::RotationData::OriginType OriginType;
377
378         data.origin(static_cast<OriginType>(originCO.currentIndex()));
379         data.angle = fromqstr(angleED.text());
380 }
381
382
383 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
384         QLineEdit & heightED, LengthCombo & heightUnitCO,
385         QCheckBox & aspectratioCB,
386         external::ResizeData const & data)
387 {
388         bool using_scale = data.usingScale();
389         string scale = data.scale;
390         if (data.no_resize()) {
391                 // Everything is zero, so default to this!
392                 using_scale = true;
393                 scale = "100";
394         }
395
396         if (using_scale) {
397                 widthED.setText(toqstr(scale));
398                 widthUnitCO.setCurrentIndex(0);
399         } else {
400                 widthED.setText(QString::number(data.width.value()));
401                 // Because 'Scale' is position 0...
402                 // Note also that width cannot be zero here, so
403                 // we don't need to worry about the default unit.
404                 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
405         }
406
407         string const h = data.height.zero() ? string() : data.height.asString();
408         Length::UNIT default_unit = data.width.zero() ?
409                 defaultUnit() : data.width.unit();
410         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
411
412         heightED.setEnabled(!using_scale);
413         heightUnitCO.setEnabled(!using_scale);
414
415         aspectratioCB.setChecked(data.keepAspectRatio);
416
417         bool const disable_aspectRatio = using_scale ||
418                 data.width.zero() || data.height.zero();
419         aspectratioCB.setEnabled(!disable_aspectRatio);
420 }
421
422
423 static void getSize(external::ResizeData & data,
424         QLineEdit const & widthED, QComboBox const & widthUnitCO,
425         QLineEdit const & heightED, LengthCombo const & heightUnitCO,
426         QCheckBox const & aspectratioCB)
427 {
428         string const width = fromqstr(widthED.text());
429
430         if (widthUnitCO.currentIndex() > 0) {
431                 // Subtract one, because scale is 0.
432                 int const unit = widthUnitCO.currentIndex() - 1;
433
434                 Length w;
435                 if (isValidLength(width, &w))
436                         data.width = w;
437                 else if (isStrDbl(width))
438                         data.width = Length(convert<double>(width),
439                                            static_cast<Length::UNIT>(unit));
440                 else
441                         data.width = Length();
442
443                 data.scale = string();
444
445         } else {
446                 // scaling instead of a width
447                 data.scale = width;
448                 data.width = Length();
449         }
450
451         data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
452
453         data.keepAspectRatio = aspectratioCB.isChecked();
454 }
455
456
457 void setCrop(QCheckBox & clipCB,
458         QLineEdit & xlED, QLineEdit & ybED,
459         QLineEdit & xrED, QLineEdit & ytED,
460         external::ClipData const & data)
461 {
462         clipCB.setChecked(data.clip);
463         graphics::BoundingBox const & bbox = data.bbox;
464         xlED.setText(QString::number(bbox.xl));
465         ybED.setText(QString::number(bbox.yb));
466         xrED.setText(QString::number(bbox.xr));
467         ytED.setText(QString::number(bbox.yt));
468 }
469
470
471 static void getCrop(external::ClipData & data,
472         QCheckBox const & clipCB,
473         QLineEdit const & xlED, QLineEdit const & ybED,
474         QLineEdit const & xrED, QLineEdit const & ytED,
475         bool bb_changed)
476 {
477         data.clip = clipCB.isChecked();
478
479         if (!bb_changed)
480                 return;
481
482         data.bbox.xl = xlED.text().toInt();
483         data.bbox.yb = ybED.text().toInt();
484         data.bbox.xr = xrED.text().toInt();
485         data.bbox.yt = ytED.text().toInt();
486 }
487
488
489 void GuiExternal::updateContents()
490 {
491         tab->setCurrentIndex(0);
492
493         string const name =
494                 params_.filename.outputFilename(fromqstr(bufferFilepath()));
495         fileED->setText(toqstr(name));
496
497         int index = -1;
498         external::TemplateManager::Templates::const_iterator i1, i2;
499         i1 = external::TemplateManager::get().getTemplates().begin();
500         i2 = external::TemplateManager::get().getTemplates().end();
501         for (int i = 0; i1 != i2; ++i1, ++i) {
502                 if (i1->second.lyxName == params_.templatename()) {
503                         index = i;
504                         break;
505                 }
506         }
507
508         externalCO->setCurrentIndex(index);
509         updateTemplate();
510
511         draftCB->setChecked(params_.draft);
512
513         displayGB->setChecked(params_.display);
514         displayscaleED->setEnabled(params_.display && !isBufferReadonly());
515         displayscaleED->setText(QString::number(params_.lyxscale));
516         displayGB->setEnabled(lyxrc.display_graphics);
517
518         setRotation(*angleED, *originCO, params_.rotationdata);
519
520         setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
521                 *aspectratioCB, params_.resizedata);
522
523         setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
524         bbChanged_ = !params_.clipdata.bbox.empty();
525
526         isValid();
527 }
528
529
530 void GuiExternal::updateTemplate()
531 {
532         external::Template templ = getTemplate(externalCO->currentIndex());
533         externalTB->setPlainText(qt_(templ.helpText));
534
535         // Ascertain which (if any) transformations the template supports
536         // and disable tabs and Group Boxes hosting unsupported transforms.
537         typedef vector<external::TransformID> TransformIDs;
538         TransformIDs const transformIds = templ.transformIds;
539         TransformIDs::const_iterator tr_begin = transformIds.begin();
540         TransformIDs::const_iterator const tr_end = transformIds.end();
541
542         bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
543         rotationGB->setEnabled(found);
544
545         found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
546         scaleGB->setEnabled(found);
547
548         found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
549         cropGB->setEnabled(found);
550
551         tab->setTabEnabled(tab->indexOf(sizetab),
552                 rotationGB->isEnabled()
553                 || scaleGB->isEnabled()
554                 || cropGB->isEnabled());
555
556         found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
557         optionsGB->setEnabled(found);
558
559         if (!found)
560                 return;
561
562         // Ascertain whether the template has any formats supporting
563         // the 'Extra' option
564         extra_.clear();
565         extraED->clear();
566         extraFormatCO->clear();
567
568         external::Template::Formats::const_iterator it  = templ.formats.begin();
569         external::Template::Formats::const_iterator end = templ.formats.end();
570         for (; it != end; ++it) {
571                 if (it->second.option_transformers.find(external::Extra) ==
572                     it->second.option_transformers.end())
573                         continue;
574                 string const format = it->first;
575                 string const opt = params_.extradata.get(format);
576                 extraFormatCO->addItem(toqstr(format));
577                 extra_[toqstr(format)] = toqstr(opt);
578         }
579
580         bool const enabled = extraFormatCO->count()  > 0;
581
582         optionsGB->setEnabled(enabled);
583         extraED->setEnabled(enabled && !isBufferReadonly());
584         extraFormatCO->setEnabled(enabled);
585
586         if (enabled) {
587                 extraFormatCO->setCurrentIndex(0);
588                 extraED->setText(extra_[extraFormatCO->currentText()]);
589         }
590 }
591
592
593 void GuiExternal::applyView()
594 {
595         params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilepath()));
596         params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
597
598         params_.draft = draftCB->isChecked();
599         params_.lyxscale = displayscaleED->text().toInt();
600         params_.display = displayGB->isChecked();
601
602         if (rotationGB->isEnabled())
603                 getRotation(params_.rotationdata, *angleED, *originCO);
604
605         if (scaleGB->isEnabled())
606                 getSize(params_.resizedata, *widthED, *widthUnitCO,
607                         *heightED, *heightUnitCO, *aspectratioCB);
608
609         if (cropGB->isEnabled())
610                 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
611                         *xrED, *ytED, bbChanged_);
612
613         if (optionsGB->isEnabled()) {
614                 MapType::const_iterator it = extra_.begin();
615                 MapType::const_iterator const end = extra_.end();
616                 for (; it != end; ++it)
617                         params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
618         }
619 }
620
621
622 bool GuiExternal::initialiseParams(string const & data)
623 {
624         InsetExternal::string2params(data, buffer(), params_);
625         return true;
626 }
627
628
629 void GuiExternal::clearParams()
630 {
631         params_ = InsetExternalParams();
632 }
633
634
635 void GuiExternal::dispatchParams()
636 {
637         string const lfun = InsetExternal::params2string(params_, buffer());
638         dispatch(FuncRequest(getLfun(), lfun));
639 }
640
641
642 static QStringList templateFilters(QString const & template_name)
643 {
644         /// Determine the template file extension
645         external::TemplateManager const & etm =
646                 external::TemplateManager::get();
647         external::Template const * const et_ptr =
648                 etm.getTemplateByName(fromqstr(template_name));
649
650         return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
651 }
652
653
654 QString GuiExternal::browse(QString const & input,
655                                      QString const & template_name) const
656 {
657         QString const title = qt_("Select external file");
658         QString const bufpath = bufferFilepath();
659         QStringList const filter = templateFilters(template_name);
660
661         QString const label1 = qt_("Documents|#o#O");
662         QString const dir1 = toqstr(lyxrc.document_path);
663
664         return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
665 }
666
667
668 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
669
670
671 } // namespace frontend
672 } // namespace lyx
673
674 #include "GuiExternal_moc.cpp"