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