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