]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiGraphics.cpp
PrefShortcuts: ShortcutEdit, adapted from Edwin's patch
[lyx.git] / src / frontends / qt4 / GuiGraphics.cpp
1 /**
2  * \file GuiGraphics.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author John Levon
8  * \author Edwin Leuven
9  * \author Herbert Voß
10  * \author Richard Heck
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "GuiGraphics.h"
18
19 #include "GuiGraphics.h"
20 #include "debug.h"
21 #include "LengthCombo.h"
22 #include "lengthcommon.h"
23 #include "LyXRC.h"
24 #include "qt_helpers.h"
25 #include "Validator.h"
26 #include "frontend_helpers.h"
27
28 #include "FuncRequest.h"
29 #include "gettext.h"
30
31 #include "graphics/GraphicsCache.h"
32 #include "graphics/GraphicsCacheItem.h"
33 #include "graphics/GraphicsImage.h"
34
35 #include "insets/InsetGraphicsParams.h"
36
37 #include "support/convert.h"
38 #include "support/FileFilterList.h"
39 #include "support/filetools.h"
40 #include "support/lstrings.h"
41 #include "support/lyxlib.h"
42 #include "support/os.h"
43 #include "support/Package.h"
44 #include "support/types.h"
45
46 #include <boost/bind.hpp>
47
48 #include <QCheckBox>
49 #include <QCloseEvent>
50 #include <QLabel>
51 #include <QLineEdit>
52 #include <QPushButton>
53 #include <QValidator>
54
55 #include <algorithm>
56 #include <cmath>
57 #include <utility>
58
59 #ifndef CXX_GLOBAL_CSTD
60 using std::floor;
61 #endif
62 using std::find;
63 using std::vector;
64 using std::string;
65 using std::transform;
66 using std::make_pair;
67 using std::pair;
68 using std::vector;
69
70 namespace lyx {
71 namespace frontend {
72
73 using support::addName;
74 using support::FileFilterList;
75 using support::FileName;
76 using support::float_equal;
77 using support::makeAbsPath;
78 using support::os::internal_path;
79 using support::package;
80 using support::readBB_from_PSFile;
81 using support::token;
82
83
84 //FIXME setAutoTextCB should really take an argument, as indicated, that
85 //determines what text is to be written for "auto". But making
86 //that work involves more extensive revisions than we now want
87 //to make, since "auto" also appears in updateContents() (see
88 //GuiGraphics.cpp).
89 //The right way to do this, I think, would be to define a class
90 //checkedLengthSet (and a partnering labeledLengthSete) that encapsulated
91 //the checkbox, line edit, and length combo together, and then made e.g.
92 //lengthToWidgets, widgetsToLength, etc, all public methods of that class.
93 //Perhaps even the validator could be exposed through it.
94 /**
95  * sets a checkbox-line edit-length combo group, using "text" if the
96  * checkbox is unchecked and clearing the line edit if it previously
97  * said "text".
98 */
99 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
100         LengthCombo * lengthCombo/*, string text = "auto"*/)
101 {
102         if (!checkBox->isChecked())
103                 lengthToWidgets(lineEdit, lengthCombo,
104                                 "auto", lengthCombo->currentLengthItem());
105         else if (lineEdit->text() == "auto")
106                 lengthToWidgets(lineEdit, lengthCombo, string(),
107                                 lengthCombo->currentLengthItem());
108 }
109
110
111
112 template<class Pair>
113 vector<typename Pair::first_type> const
114 getFirst(vector<Pair> const & pr)
115 {
116         vector<typename Pair::first_type> tmp(pr.size());
117         transform(pr.begin(), pr.end(), tmp.begin(),
118                   boost::bind(&Pair::first, _1));
119         return tmp;
120 }
121
122 ///
123 template<class Pair>
124 vector<typename Pair::second_type> const
125 getSecond(vector<Pair> const & pr)
126 {
127          vector<typename Pair::second_type> tmp(pr.size());
128          transform(pr.begin(), pr.end(), tmp.begin(),
129                    boost::bind(&Pair::second, _1));
130          return tmp;
131 }
132
133 GuiGraphics::GuiGraphics(LyXView & lv)
134         : GuiDialog(lv, "graphics")
135 {
136         setupUi(this);
137         setViewTitle(_("Graphics"));
138
139         //main buttons
140         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
141         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
142         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
143         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
144
145         //graphics pane
146         connect(filename, SIGNAL(textChanged(const QString &)),
147                 this, SLOT(change_adaptor()));
148         connect(WidthCB, SIGNAL( clicked()),
149                 this, SLOT(change_adaptor()));
150         connect(HeightCB, SIGNAL( clicked()),
151                 this, SLOT(change_adaptor()));
152         connect(Width, SIGNAL(textChanged(const QString &)),
153                 this, SLOT(change_adaptor()));
154         connect(Height, SIGNAL(textChanged(const QString &)),
155                 this, SLOT(change_adaptor()));
156         connect(heightUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
157                 this, SLOT(change_adaptor()));
158         connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
159                 this, SLOT(change_adaptor()));
160         connect(aspectratio, SIGNAL(stateChanged(int)),
161                 this, SLOT(change_adaptor()));
162         connect(angle, SIGNAL(textChanged(const QString &)),
163                 this, SLOT(change_adaptor()));
164         connect(origin, SIGNAL(activated(int)),
165                 this, SLOT(change_adaptor()));
166         connect(scaleCB, SIGNAL(clicked()),
167                 this, SLOT(change_adaptor()));
168         connect(Scale, SIGNAL(textChanged(const QString &)),
169                 this, SLOT(change_adaptor()));
170         connect(rotateOrderCB, SIGNAL(clicked()),
171                 this, SLOT(change_adaptor()));
172
173         filename->setValidator(new PathValidator(true, filename));
174         setFocusProxy(filename);
175
176         QDoubleValidator * scaleValidator = new DoubleAutoValidator(Scale);
177         scaleValidator->setBottom(0);
178         scaleValidator->setDecimals(256); //I guess that will do
179         Scale->setValidator(scaleValidator);
180         Height->setValidator(unsignedLengthAutoValidator(Height));
181         Width->setValidator(unsignedLengthAutoValidator(Width));
182         angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
183
184         //clipping pane
185         connect(clip, SIGNAL(stateChanged(int)),
186                 this, SLOT(change_adaptor()));
187         connect(lbY, SIGNAL(textChanged(const QString&)),
188                 this, SLOT(change_bb()));
189         connect(lbYunit, SIGNAL(activated(int)),
190                 this, SLOT(change_bb()));
191         connect(rtY, SIGNAL(textChanged(const QString&)),
192                 this, SLOT(change_bb()));
193         connect(rtYunit, SIGNAL(activated(int)),
194                 this, SLOT(change_bb()));
195         connect(lbX, SIGNAL(textChanged(const QString&)),
196                 this, SLOT(change_bb()));
197         connect(lbXunit, SIGNAL(activated(int)),
198                 this, SLOT(change_bb()));
199         connect(rtX, SIGNAL(textChanged(const QString&)),
200                 this, SLOT(change_bb()));
201         connect(rtXunit, SIGNAL(activated(int)),
202                 this, SLOT(change_bb()));
203         connect(getPB, SIGNAL(clicked()),
204                 this, SLOT(change_adaptor()));
205
206         lbX->setValidator(new QDoubleValidator(lbX));
207         lbY->setValidator(new QDoubleValidator(lbY));
208         rtX->setValidator(new QDoubleValidator(rtX));
209         rtY->setValidator(new QDoubleValidator(rtY));
210
211         //extra options pane
212         connect(latexoptions, SIGNAL(textChanged(const QString&)),
213                 this, SLOT(change_adaptor()));
214         connect(draftCB, SIGNAL(stateChanged(int)),
215                 this, SLOT(change_adaptor()));
216         connect(unzipCB, SIGNAL(stateChanged(int)),
217                 this, SLOT(change_adaptor()));
218         // FIXME: we should connect to clicked() when we move to Qt 4.2 because
219         // the toggled(bool) signal is also trigged when we update the widgets
220         // (rgh-4/07) this isn't as much or a problem as it was, because we're now
221         // using blockSignals() to keep from triggering that signal when we call
222         // setChecked(). Note, too, that clicked() would get called whenever it
223         // is clicked, even right clicked (I think), not just whenever it is
224         // toggled.
225         connect(subfigure, SIGNAL(toggled(bool)),
226                 this, SLOT(change_adaptor()));
227         connect(subcaption, SIGNAL(textChanged(const QString&)),
228                 this, SLOT(change_adaptor()));
229         connect(displayGB, SIGNAL(toggled(bool)),
230                 this, SLOT(change_adaptor()));
231         connect(showCB, SIGNAL(currentIndexChanged(int)),
232                 this, SLOT(change_adaptor()));
233         connect(displayscale, SIGNAL(textChanged(const QString&)),
234                 this, SLOT(change_adaptor()));
235         displayscale->setValidator(new QIntValidator(displayscale));
236
237         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
238         bc().setOK(okPB);
239         bc().setApply(applyPB);
240         bc().setRestore(restorePB);
241         bc().setCancel(closePB);
242
243         bc().addReadOnly(latexoptions);
244         bc().addReadOnly(subfigure);
245         bc().addReadOnly(filenameL);
246         bc().addReadOnly(filename);
247         bc().addReadOnly(browsePB);
248         bc().addReadOnly(unzipCB);
249         bc().addReadOnly(bbFrame);
250         bc().addReadOnly(draftCB);
251         bc().addReadOnly(clip);
252         bc().addReadOnly(unzipCB);
253         bc().addReadOnly(displayGB);
254         bc().addReadOnly(sizeGB);
255         bc().addReadOnly(rotationGB);
256         bc().addReadOnly(latexoptions);
257         bc().addReadOnly(getPB);
258         bc().addReadOnly(rotateOrderCB);
259
260         // initialize the length validator
261         bc().addCheckedLineEdit(Scale, scaleCB);
262         bc().addCheckedLineEdit(Width, WidthCB);
263         bc().addCheckedLineEdit(Height, HeightCB);
264         bc().addCheckedLineEdit(displayscale, scaleLA);
265         bc().addCheckedLineEdit(angle, angleL);
266         bc().addCheckedLineEdit(lbX, xL);
267         bc().addCheckedLineEdit(lbY, yL);
268         bc().addCheckedLineEdit(rtX, xL_2);
269         bc().addCheckedLineEdit(rtY, yL_2);
270         bc().addCheckedLineEdit(filename, filenameL);
271 }
272
273
274 void GuiGraphics::change_adaptor()
275 {
276         changed();
277 }
278
279
280 void GuiGraphics::change_bb()
281 {
282         bbChanged = true;
283         LYXERR(Debug::GRAPHICS) << "[bb_Changed set to true]\n";
284         changed();
285 }
286
287
288 void GuiGraphics::closeEvent(QCloseEvent * e)
289 {
290         slotClose();
291         GuiDialog::closeEvent(e);
292 }
293
294
295 void GuiGraphics::on_browsePB_clicked()
296 {
297         docstring const str = browse(qstring_to_ucs4(filename->text()));
298         if (!str.empty()) {
299                 filename->setText(toqstr(str));
300                 embedCB->setCheckState(Qt::Unchecked);
301                 changed();
302         }
303 }
304
305
306 void GuiGraphics::on_getPB_clicked()
307 {
308         getBB();
309 }
310
311
312 void GuiGraphics::on_editPB_clicked()
313 {
314         editGraphics();
315 }
316
317
318 void GuiGraphics::on_filename_textChanged(const QString & filename)
319 {
320         editPB->setDisabled(filename.isEmpty());
321 }
322
323
324 void GuiGraphics::setAutoText()
325 {
326         if (scaleCB->isChecked())
327                 return;
328         if (!Scale->isEnabled() && Scale->text() != "100")
329                 Scale->setText(QString("auto"));
330
331         setAutoTextCB(WidthCB, Width, widthUnit);
332         setAutoTextCB(HeightCB, Height, heightUnit);
333 }
334
335
336 void GuiGraphics::on_scaleCB_toggled(bool setScale)
337 {
338         Scale->setEnabled(setScale);
339         if (setScale) {
340                 Scale->setText("100");
341                 Scale->setFocus(Qt::OtherFocusReason);
342         }
343
344         WidthCB->setDisabled(setScale);
345         WidthCB->blockSignals(true);
346         WidthCB->setChecked(false);
347         WidthCB->blockSignals(false);
348         Width->setEnabled(false);
349         widthUnit->setEnabled(false);
350
351         HeightCB->setDisabled(setScale);
352         HeightCB->blockSignals(true);
353         HeightCB->setChecked(false);
354         HeightCB->blockSignals(false);
355         Height->setEnabled(false);
356         heightUnit->setEnabled(false);
357
358         aspectratio->setDisabled(true);
359         aspectratio->setChecked(true);
360
361         rotateOrderCB->setEnabled((WidthCB->isChecked() ||
362                                  HeightCB->isChecked() ||
363                                  scaleCB->isChecked()) &&
364                                  (angle->text() != "0"));
365
366         setAutoText();
367 }
368
369
370 void GuiGraphics::on_WidthCB_toggled(bool setWidth)
371 {
372         Width->setEnabled(setWidth);
373         widthUnit->setEnabled(setWidth);
374         if (setWidth)
375                 Width->setFocus(Qt::OtherFocusReason);
376
377         bool const setHeight = HeightCB->isChecked();
378         aspectratio->setEnabled(setWidth && setHeight);
379         aspectratio->blockSignals(true);
380         aspectratio->setChecked(!(setWidth && setHeight));
381         aspectratio->blockSignals(false);
382
383         scaleCB->setEnabled(!setWidth && !setHeight);
384         //already will be unchecked, so don't need to do that
385         Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
386                         && scaleCB->isChecked()); //should be false, but let's check
387         rotateOrderCB->setEnabled((setWidth || setHeight ||
388                                  scaleCB->isChecked()) &&
389                                  (angle->text() != "0"));
390
391         setAutoText();
392 }
393
394
395 void GuiGraphics::on_HeightCB_toggled(bool setHeight)
396 {
397         Height->setEnabled(setHeight);
398         heightUnit->setEnabled(setHeight);
399         if (setHeight)
400                 Height->setFocus(Qt::OtherFocusReason);
401
402         bool const setWidth = WidthCB->isChecked();
403         aspectratio->setEnabled(setWidth && setHeight);
404         aspectratio->blockSignals(true);
405         aspectratio->setChecked(!(setWidth && setHeight));
406         aspectratio->blockSignals(false);
407
408         scaleCB->setEnabled(!setWidth && !setHeight);
409         //already unchecked
410         Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
411                 && scaleCB->isChecked()); //should be false
412         rotateOrderCB->setEnabled((setWidth || setHeight ||
413                                  scaleCB->isChecked()) &&
414                                  (angle->text() != "0"));
415
416         setAutoText();
417 }
418
419
420 void GuiGraphics::on_angle_textChanged(const QString & filename)
421 {
422         rotateOrderCB->setEnabled((WidthCB->isChecked() ||
423                                  HeightCB->isChecked() ||
424                                  scaleCB->isChecked()) &&
425                                  (filename != "0"));
426 }
427
428 // returns the number of the string s in the vector v
429 static int getItemNo(const vector<string> & v, string const & s)
430 {
431         vector<string>::const_iterator cit =
432                     find(v.begin(), v.end(), s);
433         return (cit != v.end()) ? int(cit - v.begin()) : 0;
434 }
435
436
437 void GuiGraphics::updateContents()
438 {
439         // clear and fill in the comboboxes
440         vector<string> const bb_units = frontend::getBBUnits();
441         lbXunit->clear();
442         lbYunit->clear();
443         rtXunit->clear();
444         rtYunit->clear();
445         for (vector<string>::const_iterator it = bb_units.begin();
446             it != bb_units.end(); ++it) {
447                 lbXunit->addItem(toqstr(*it));
448                 lbYunit->addItem(toqstr(*it));
449                 rtXunit->addItem(toqstr(*it));
450                 rtYunit->addItem(toqstr(*it));
451         }
452
453         InsetGraphicsParams & igp = params_;
454
455         // set the right default unit
456         Length::UNIT unitDefault = Length::CM;
457         switch (lyxrc.default_papersize) {
458                 case PAPER_USLETTER:
459                 case PAPER_USLEGAL:
460                 case PAPER_USEXECUTIVE:
461                         unitDefault = Length::IN;
462                         break;
463                 default:
464                         break;
465         }
466
467         string const name =
468                 igp.filename.outputFilename(bufferFilepath());
469         filename->setText(toqstr(name));
470     embedCB->setCheckState(igp.filename.embedded() ? Qt::Checked : Qt::Unchecked);
471
472         // set the bounding box values
473         if (igp.bb.empty()) {
474                 string const bb = readBB(igp.filename.absFilename());
475                 // the values from the file always have the bigpoint-unit bp
476                 lbX->setText(toqstr(token(bb, ' ', 0)));
477                 lbY->setText(toqstr(token(bb, ' ', 1)));
478                 rtX->setText(toqstr(token(bb, ' ', 2)));
479                 rtY->setText(toqstr(token(bb, ' ', 3)));
480                 lbXunit->setCurrentIndex(0);
481                 lbYunit->setCurrentIndex(0);
482                 rtXunit->setCurrentIndex(0);
483                 rtYunit->setCurrentIndex(0);
484                 bbChanged = false;
485         } else {
486                 // get the values from the inset
487                 Length anyLength;
488                 string const xl = token(igp.bb, ' ', 0);
489                 string const yl = token(igp.bb, ' ', 1);
490                 string const xr = token(igp.bb, ' ', 2);
491                 string const yr = token(igp.bb, ' ', 3);
492                 if (isValidLength(xl, &anyLength)) {
493                         lbX->setText(toqstr(convert<string>(anyLength.value())));
494                         string const unit(unit_name[anyLength.unit()]);
495                         lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
496                 } else {
497                         lbX->setText(toqstr(xl));
498                 }
499                 if (isValidLength(yl, &anyLength)) {
500                         lbY->setText(toqstr(convert<string>(anyLength.value())));
501                         string const unit(unit_name[anyLength.unit()]);
502                         lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
503                 } else {
504                         lbY->setText(toqstr(xl));
505                 }
506                 if (isValidLength(xr, &anyLength)) {
507                         rtX->setText(toqstr(convert<string>(anyLength.value())));
508                         string const unit(unit_name[anyLength.unit()]);
509                         rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
510                 } else {
511                         rtX->setText(toqstr(xl));
512                 }
513                 if (isValidLength(yr, &anyLength)) {
514                         rtY->setText(toqstr(convert<string>(anyLength.value())));
515                         string const unit(unit_name[anyLength.unit()]);
516                         rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
517                 } else {
518                         rtY->setText(toqstr(xl));
519                 }
520                 bbChanged = true;
521         }
522
523         // Update the draft and clip mode
524         draftCB->setChecked(igp.draft);
525         clip->setChecked(igp.clip);
526         unzipCB->setChecked(igp.noUnzip);
527
528         // Update the subcaption check button and input field
529         subfigure->setChecked(igp.subcaption);
530         subcaption->setText(toqstr(igp.subcaptionText));
531
532         int item = 0;
533         switch (igp.display) {
534                 case graphics::DefaultDisplay: item = 0; break;
535                 case graphics::MonochromeDisplay: item = 1; break;
536                 case graphics::GrayscaleDisplay: item = 2; break;
537                 case graphics::ColorDisplay: item = 3; break;
538                 case graphics::NoDisplay: item = 0; break;
539         }
540         showCB->setCurrentIndex(item);
541         displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
542         displayGB->setChecked(igp.display != graphics::NoDisplay);
543
544         // the output section (width/height)
545
546         Scale->setText(toqstr(igp.scale));
547         //igp.scale defaults to 100, so we treat it as empty
548         bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
549         scaleCB->blockSignals(true);
550         scaleCB->setChecked(scaleChecked);
551         scaleCB->blockSignals(false);
552         Scale->setEnabled(scaleChecked);
553
554         lengthAutoToWidgets(Width, widthUnit, igp.width,
555                 unitDefault);
556         bool const widthChecked = !Width->text().isEmpty() &&
557                 Width->text() != "auto";
558         WidthCB->blockSignals(true);
559         WidthCB->setChecked(widthChecked);
560         WidthCB->blockSignals(false);
561         Width->setEnabled(widthChecked);
562         widthUnit->setEnabled(widthChecked);
563
564         lengthAutoToWidgets(Height, heightUnit, igp.height,
565                 unitDefault);
566         bool const heightChecked = !Height->text().isEmpty()
567                 && Height->text() != "auto";
568         HeightCB->blockSignals(true);
569         HeightCB->setChecked(heightChecked);
570         HeightCB->blockSignals(false);
571         Height->setEnabled(heightChecked);
572         heightUnit->setEnabled(heightChecked);
573
574         scaleCB->setEnabled(!widthChecked && !heightChecked);
575         WidthCB->setEnabled(!scaleChecked);
576         HeightCB->setEnabled(!scaleChecked);
577         aspectratio->setEnabled(widthChecked && heightChecked);
578
579         setAutoText();
580
581         angle->setText(toqstr(igp.rotateAngle));
582         rotateOrderCB->setChecked(igp.scaleBeforeRotation);
583
584         rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
585                 && igp.rotateAngle != "0");
586
587         origin->clear();
588
589         vector<RotationOriginPair> origindata = getRotationOriginData();
590         vector<docstring> const origin_lang = getFirst(origindata);
591         origin_ltx = getSecond(origindata);
592
593         for (vector<docstring>::const_iterator it = origin_lang.begin();
594             it != origin_lang.end(); ++it)
595                 origin->addItem(toqstr(*it));
596
597         if (!igp.rotateOrigin.empty())
598                 origin->setCurrentIndex(
599                         getItemNo(origin_ltx, igp.rotateOrigin));
600         else
601                 origin->setCurrentIndex(0);
602
603         // disable edit button when no filename is present
604         editPB->setDisabled(filename->text().isEmpty());
605
606         //// latex section
607         latexoptions->setText(toqstr(igp.special));
608 }
609
610
611 void GuiGraphics::applyView()
612 {
613         InsetGraphicsParams & igp = params_;
614
615         igp.filename.set(internal_path(fromqstr(filename->text())),
616                          bufferFilepath());
617         igp.filename.setEmbed(embedCB->checkState() == Qt::Checked);
618
619         // the bb section
620         igp.bb.erase();
621         if (bbChanged) {
622                 string bb;
623                 string lbXs = fromqstr(lbX->text());
624                 string lbYs = fromqstr(lbY->text());
625                 string rtXs = fromqstr(rtX->text());
626                 string rtYs = fromqstr(rtY->text());
627                 int bb_sum =
628                         convert<int>(lbXs) + convert<int>(lbYs) +
629                         convert<int>(rtXs) + convert<int>(rtXs);
630                 if (bb_sum) {
631                         if (lbXs.empty())
632                                 bb = "0 ";
633                         else
634                                 bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
635                         if (lbYs.empty())
636                                 bb += "0 ";
637                         else
638                                 bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
639                         if (rtXs.empty())
640                                 bb += "0 ";
641                         else
642                                 bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
643                         if (rtYs.empty())
644                                 bb += '0';
645                         else
646                                 bb += (rtYs + fromqstr(rtYunit->currentText()));
647                         igp.bb = bb;
648                 }
649         }
650
651         igp.draft = draftCB->isChecked();
652         igp.clip = clip->isChecked();
653         igp.subcaption = subfigure->isChecked();
654         igp.subcaptionText = fromqstr(subcaption->text());
655
656         switch (showCB->currentIndex()) {
657                 case 0: igp.display = graphics::DefaultDisplay; break;
658                 case 1: igp.display = graphics::MonochromeDisplay; break;
659                 case 2: igp.display = graphics::GrayscaleDisplay; break;
660                 case 3: igp.display = graphics::ColorDisplay; break;
661                 default:;
662         }
663
664         if (!displayGB->isChecked())
665                 igp.display = graphics::NoDisplay;
666
667         //the graphics section
668         if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
669                 igp.scale = fromqstr(Scale->text());
670                 igp.width = Length("0pt");
671                 igp.height = Length("0pt");
672                 igp.keepAspectRatio = false;
673         } else {
674                 igp.scale = string();
675                 igp.width = WidthCB->isChecked() ?
676                         //Note that this works even if Width is "auto", since in
677                         //that case we get "0pt".
678                         Length(widgetsToLength(Width, widthUnit)):
679                         Length("0pt");
680                 igp.height = HeightCB->isChecked() ?
681                         Length(widgetsToLength(Height, heightUnit)) :
682                         Length("0pt");
683                 igp.keepAspectRatio = aspectratio->isEnabled() &&
684                         aspectratio->isChecked() &&
685                         igp.width.value() > 0 && igp.height.value() > 0;
686         }
687
688         igp.noUnzip = unzipCB->isChecked();
689         igp.lyxscale = displayscale->text().toInt();
690         igp.rotateAngle = fromqstr(angle->text());
691
692         double rotAngle = convert<double>(igp.rotateAngle);
693         if (std::abs(rotAngle) > 360.0) {
694                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
695                 igp.rotateAngle = convert<string>(rotAngle);
696         }
697
698         // save the latex name for the origin. If it is the default
699         // then origin_ltx returns ""
700         igp.rotateOrigin = origin_ltx[origin->currentIndex()];
701         igp.scaleBeforeRotation = rotateOrderCB->isChecked();
702
703         // more latex options
704         igp.special = fromqstr(latexoptions->text());
705 }
706
707
708 void GuiGraphics::getBB()
709 {
710         string const fn = fromqstr(filename->text());
711         if (!fn.empty()) {
712                 string const bb = readBB(fn);
713                 if (!bb.empty()) {
714                         lbX->setText(toqstr(token(bb, ' ', 0)));
715                         lbY->setText(toqstr(token(bb, ' ', 1)));
716                         rtX->setText(toqstr(token(bb, ' ', 2)));
717                         rtY->setText(toqstr(token(bb, ' ', 3)));
718                         // the default units for the bb values when reading
719                         // it from the file
720                         lbXunit->setCurrentIndex(0);
721                         lbYunit->setCurrentIndex(0);
722                         rtXunit->setCurrentIndex(0);
723                         rtYunit->setCurrentIndex(0);
724                 }
725                 bbChanged = false;
726         }
727 }
728
729
730 bool GuiGraphics::isValid()
731 {
732         return !filename->text().isEmpty();
733 }
734
735
736 bool GuiGraphics::initialiseParams(string const & data)
737 {
738         InsetGraphicsMailer::string2params(data, buffer(), params_);
739         return true;
740 }
741
742
743 void GuiGraphics::clearParams()
744 {
745         params_ = InsetGraphicsParams();
746 }
747
748
749 void GuiGraphics::dispatchParams()
750 {
751         InsetGraphicsParams tmp_params(params_);
752         string const lfun =
753                 InsetGraphicsMailer::params2string(tmp_params, buffer());
754         dispatch(FuncRequest(getLfun(), lfun));
755 }
756
757
758 docstring const GuiGraphics::browse(docstring const & in_name) const
759 {
760         docstring const title = _("Select graphics file");
761
762         // Does user clipart directory exist?
763         string clipdir = addName(package().user_support().absFilename(), "clipart");
764         FileName clip(clipdir);
765         if (!clip.exists() && clip.isDirectory())
766                 // No - bail out to system clipart directory
767                 clipdir = addName(package().system_support().absFilename(), "clipart");
768         pair<docstring, docstring> dir1(_("Clipart|#C#c"), from_utf8(clipdir));
769         pair<docstring, docstring> dir2(_("Documents|#o#O"), from_utf8(lyxrc.document_path));
770         // Show the file browser dialog
771         return browseRelFile(in_name, from_utf8(bufferFilepath()),
772                 title, FileFilterList(), false, dir1, dir2);
773 }
774
775
776 string const GuiGraphics::readBB(string const & file)
777 {
778         FileName const abs_file = makeAbsPath(file, bufferFilepath());
779
780         // try to get it from the file, if possible. Zipped files are
781         // unzipped in the readBB_from_PSFile-Function
782         string const bb = readBB_from_PSFile(abs_file);
783         if (!bb.empty())
784                 return bb;
785
786         // we don't, so ask the Graphics Cache if it has loaded the file
787         int width = 0;
788         int height = 0;
789
790         graphics::Cache & gc = graphics::Cache::get();
791         if (gc.inCache(abs_file)) {
792                 graphics::Image const * image = gc.item(abs_file)->image();
793
794                 if (image) {
795                         width  = image->getWidth();
796                         height = image->getHeight();
797                 }
798         }
799
800         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
801 }
802
803
804 bool GuiGraphics::isFilenameValid(string const & fname) const
805 {
806         // It may be that the filename is relative.
807         return makeAbsPath(fname, bufferFilepath()).isReadable();
808 }
809
810
811 void GuiGraphics::editGraphics()
812 {
813         applyView();
814         string const lfun =
815                 InsetGraphicsMailer::params2string(params_, buffer());
816         dispatch(FuncRequest(LFUN_GRAPHICS_EDIT, lfun));
817 }
818
819
820 namespace {
821
822 char const * const bb_units[] = { "bp", "cm", "mm", "in" };
823 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
824
825 // These are the strings that are stored in the LyX file and which
826 // correspond to the LaTeX identifiers shown in the comments at the
827 // end of each line.
828 char const * const rorigin_lyx_strs[] = {
829         // the LaTeX default is leftBaseline
830         "",
831         "leftTop",  "leftBottom", "leftBaseline", // lt lb lB
832         "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
833         "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
834
835 // These are the strings, corresponding to the above, that the GUI should
836 // use. Note that they can/should be translated.
837 char const * const rorigin_gui_strs[] = {
838         N_("Default"),
839         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
840         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
841         N_("Top right"), N_("Bottom right"), N_("Baseline right") };
842
843 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
844
845 } // namespace anon
846
847
848 vector<string> const getBBUnits()
849 {
850         return vector<string>(bb_units, bb_units + bb_size);
851 }
852
853
854 vector<RotationOriginPair> getRotationOriginData()
855 {
856         static vector<RotationOriginPair> data;
857         if (!data.empty())
858                 return data;
859
860         data.resize(rorigin_size);
861         for (size_type i = 0; i < rorigin_size; ++i) {
862                 data[i] = make_pair(_(rorigin_gui_strs[i]),
863                                     rorigin_lyx_strs[i]);
864         }
865
866         return data;
867 }
868
869
870 Dialog * createGuiGraphics(LyXView & lv) { return new GuiGraphics(lv); }
871
872
873 } // namespace frontend
874 } // namespace lyx
875
876 #include "GuiGraphics_moc.cpp"