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