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