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