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