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