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