]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiGraphics.cpp
Compil fix.
[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         connect(groupId, SIGNAL(textChanged(const QString&)),
249                 this, SLOT(change_adaptor()));
250         displayscale->setValidator(new QIntValidator(displayscale));
251
252         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
253         bc().setOK(okPB);
254         bc().setApply(applyPB);
255         bc().setRestore(restorePB);
256         bc().setCancel(closePB);
257
258         bc().addReadOnly(latexoptions);
259         bc().addReadOnly(filenameL);
260         bc().addReadOnly(filename);
261         bc().addReadOnly(browsePB);
262         bc().addReadOnly(unzipCB);
263         bc().addReadOnly(bbFrame);
264         bc().addReadOnly(draftCB);
265         bc().addReadOnly(clip);
266         bc().addReadOnly(unzipCB);
267         bc().addReadOnly(displayGB);
268         bc().addReadOnly(sizeGB);
269         bc().addReadOnly(rotationGB);
270         bc().addReadOnly(latexoptions);
271         bc().addReadOnly(getPB);
272         bc().addReadOnly(rotateOrderCB);
273
274         // initialize the length validator
275         bc().addCheckedLineEdit(Scale, scaleCB);
276         bc().addCheckedLineEdit(Width, WidthCB);
277         bc().addCheckedLineEdit(Height, HeightCB);
278         bc().addCheckedLineEdit(displayscale, scaleLA);
279         bc().addCheckedLineEdit(angle, angleL);
280         bc().addCheckedLineEdit(lbX, xL);
281         bc().addCheckedLineEdit(lbY, yL);
282         bc().addCheckedLineEdit(rtX, xL_2);
283         bc().addCheckedLineEdit(rtY, yL_2);
284         bc().addCheckedLineEdit(filename, filenameL);
285 }
286
287
288 void GuiGraphics::change_adaptor()
289 {
290         changed();
291 }
292
293
294 void GuiGraphics::change_bb()
295 {
296         bbChanged = true;
297         LYXERR(Debug::GRAPHICS, "[bb_Changed set to true]");
298         changed();
299 }
300
301
302 void GuiGraphics::on_browsePB_clicked()
303 {
304         QString const str = browse(filename->text());
305         if (!str.isEmpty()) {
306                 filename->setText(str);
307                 changed();
308         }
309 }
310
311
312 void GuiGraphics::on_getPB_clicked()
313 {
314         getBB();
315 }
316
317
318 void GuiGraphics::setAutoText()
319 {
320         if (scaleCB->isChecked())
321                 return;
322         if (!Scale->isEnabled() && Scale->text() != "100")
323                 Scale->setText(QString("auto"));
324
325         setAutoTextCB(WidthCB, Width, widthUnit);
326         setAutoTextCB(HeightCB, Height, heightUnit);
327 }
328
329
330 void GuiGraphics::on_scaleCB_toggled(bool setScale)
331 {
332         Scale->setEnabled(setScale);
333         if (setScale) {
334                 Scale->setText("100");
335                 Scale->setFocus(Qt::OtherFocusReason);
336         }
337
338         WidthCB->setDisabled(setScale);
339         WidthCB->blockSignals(true);
340         WidthCB->setChecked(false);
341         WidthCB->blockSignals(false);
342         Width->setEnabled(false);
343         widthUnit->setEnabled(false);
344
345         HeightCB->setDisabled(setScale);
346         HeightCB->blockSignals(true);
347         HeightCB->setChecked(false);
348         HeightCB->blockSignals(false);
349         Height->setEnabled(false);
350         heightUnit->setEnabled(false);
351
352         aspectratio->setDisabled(true);
353         aspectratio->setChecked(true);
354
355         rotateOrderCB->setEnabled((WidthCB->isChecked() ||
356                                  HeightCB->isChecked() ||
357                                  scaleCB->isChecked()) &&
358                                  (angle->text() != "0"));
359
360         setAutoText();
361 }
362
363
364 void GuiGraphics::on_WidthCB_toggled(bool setWidth)
365 {
366         Width->setEnabled(setWidth);
367         widthUnit->setEnabled(setWidth);
368         if (setWidth)
369                 Width->setFocus(Qt::OtherFocusReason);
370
371         bool const setHeight = HeightCB->isChecked();
372         aspectratio->setEnabled(setWidth && setHeight);
373         aspectratio->blockSignals(true);
374         aspectratio->setChecked(!(setWidth && setHeight));
375         aspectratio->blockSignals(false);
376
377         scaleCB->setEnabled(!setWidth && !setHeight);
378         //already will be unchecked, so don't need to do that
379         Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
380                         && scaleCB->isChecked()); //should be false, but let's check
381         rotateOrderCB->setEnabled((setWidth || setHeight ||
382                                  scaleCB->isChecked()) &&
383                                  (angle->text() != "0"));
384
385         setAutoText();
386 }
387
388
389 void GuiGraphics::on_HeightCB_toggled(bool setHeight)
390 {
391         Height->setEnabled(setHeight);
392         heightUnit->setEnabled(setHeight);
393         if (setHeight)
394                 Height->setFocus(Qt::OtherFocusReason);
395
396         bool const setWidth = WidthCB->isChecked();
397         aspectratio->setEnabled(setWidth && setHeight);
398         aspectratio->blockSignals(true);
399         aspectratio->setChecked(!(setWidth && setHeight));
400         aspectratio->blockSignals(false);
401
402         scaleCB->setEnabled(!setWidth && !setHeight);
403         //already unchecked
404         Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
405                 && scaleCB->isChecked()); //should be false
406         rotateOrderCB->setEnabled((setWidth || setHeight ||
407                                  scaleCB->isChecked()) &&
408                                  (angle->text() != "0"));
409
410         setAutoText();
411 }
412
413
414 void GuiGraphics::on_angle_textChanged(const QString & filename)
415 {
416         rotateOrderCB->setEnabled((WidthCB->isChecked() ||
417                                  HeightCB->isChecked() ||
418                                  scaleCB->isChecked()) &&
419                                  (filename != "0"));
420 }
421
422 // returns the number of the string s in the vector v
423 static int itemNumber(const vector<string> & v, string const & s)
424 {
425         vector<string>::const_iterator cit =
426                     find(v.begin(), v.end(), s);
427         return (cit != v.end()) ? int(cit - v.begin()) : 0;
428 }
429
430
431 void GuiGraphics::paramsToDialog(InsetGraphicsParams const & igp)
432 {
433         static char const * const bb_units[] = { "bp", "cm", "mm", "in" };
434         size_t const bb_size = sizeof(bb_units) / sizeof(bb_units[0]);
435
436         vector<string> const units = vector<string>(bb_units, bb_units + bb_size);
437         lbXunit->clear();
438         lbYunit->clear();
439         rtXunit->clear();
440         rtYunit->clear();
441         for (vector<string>::const_iterator it = units.begin();
442             it != units.end(); ++it) {
443                 lbXunit->addItem(toqstr(*it));
444                 lbYunit->addItem(toqstr(*it));
445                 rtXunit->addItem(toqstr(*it));
446                 rtYunit->addItem(toqstr(*it));
447         }
448
449         // set the right default unit
450         Length::UNIT unitDefault = Length::CM;
451         switch (lyxrc.default_papersize) {
452                 case PAPER_USLETTER:
453                 case PAPER_USLEGAL:
454                 case PAPER_USEXECUTIVE:
455                         unitDefault = Length::IN;
456                         break;
457                 default:
458                         break;
459         }
460
461         //lyxerr << bufferFilepath();
462         string const name =
463                 igp.filename.outputFilename(fromqstr(bufferFilepath()));
464         filename->setText(toqstr(name));
465
466         // set the bounding box values
467         if (igp.bb.empty()) {
468                 string const bb = readBoundingBox(igp.filename.absFilename());
469                 // the values from the file always have the bigpoint-unit bp
470                 lbX->setText(toqstr(token(bb, ' ', 0)));
471                 lbY->setText(toqstr(token(bb, ' ', 1)));
472                 rtX->setText(toqstr(token(bb, ' ', 2)));
473                 rtY->setText(toqstr(token(bb, ' ', 3)));
474                 lbXunit->setCurrentIndex(0);
475                 lbYunit->setCurrentIndex(0);
476                 rtXunit->setCurrentIndex(0);
477                 rtYunit->setCurrentIndex(0);
478                 bbChanged = false;
479         } else {
480                 // get the values from the inset
481                 Length anyLength;
482                 string const xl = token(igp.bb, ' ', 0);
483                 string const yl = token(igp.bb, ' ', 1);
484                 string const xr = token(igp.bb, ' ', 2);
485                 string const yr = token(igp.bb, ' ', 3);
486                 if (isValidLength(xl, &anyLength)) {
487                         lbX->setText(toqstr(convert<string>(anyLength.value())));
488                         string const unit = unit_name[anyLength.unit()];
489                         lbXunit->setCurrentIndex(itemNumber(units, unit));
490                 } else {
491                         lbX->setText(toqstr(xl));
492                 }
493                 if (isValidLength(yl, &anyLength)) {
494                         lbY->setText(toqstr(convert<string>(anyLength.value())));
495                         string const unit = unit_name[anyLength.unit()];
496                         lbYunit->setCurrentIndex(itemNumber(units, unit));
497                 } else {
498                         lbY->setText(toqstr(xl));
499                 }
500                 if (isValidLength(xr, &anyLength)) {
501                         rtX->setText(toqstr(convert<string>(anyLength.value())));
502                         string const unit = unit_name[anyLength.unit()];
503                         rtXunit->setCurrentIndex(itemNumber(units, unit));
504                 } else {
505                         rtX->setText(toqstr(xl));
506                 }
507                 if (isValidLength(yr, &anyLength)) {
508                         rtY->setText(toqstr(convert<string>(anyLength.value())));
509                         string const unit = unit_name[anyLength.unit()];
510                         rtYunit->setCurrentIndex(itemNumber(units, unit));
511                 } else {
512                         rtY->setText(toqstr(xl));
513                 }
514                 bbChanged = true;
515         }
516
517         // Update the draft and clip mode
518         draftCB->setChecked(igp.draft);
519         clip->setChecked(igp.clip);
520         unzipCB->setChecked(igp.noUnzip);
521
522         int item = 0;
523         switch (igp.display) {
524                 case graphics::DefaultDisplay: item = 0; break;
525                 case graphics::MonochromeDisplay: item = 1; break;
526                 case graphics::GrayscaleDisplay: item = 2; break;
527                 case graphics::ColorDisplay: item = 3; break;
528                 case graphics::NoDisplay: item = 0; break;
529         }
530         showCB->setCurrentIndex(item);
531         displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
532         displayGB->setChecked(igp.display != graphics::NoDisplay);
533
534         // the output section (width/height)
535
536         Scale->setText(toqstr(igp.scale));
537         //igp.scale defaults to 100, so we treat it as empty
538         bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
539         scaleCB->blockSignals(true);
540         scaleCB->setChecked(scaleChecked);
541         scaleCB->blockSignals(false);
542         Scale->setEnabled(scaleChecked);
543
544         groupId->setText(toqstr(igp.groupId));
545
546         lengthAutoToWidgets(Width, widthUnit, igp.width,
547                 unitDefault);
548         bool const widthChecked = !Width->text().isEmpty() &&
549                 Width->text() != "auto";
550         WidthCB->blockSignals(true);
551         WidthCB->setChecked(widthChecked);
552         WidthCB->blockSignals(false);
553         Width->setEnabled(widthChecked);
554         widthUnit->setEnabled(widthChecked);
555
556         lengthAutoToWidgets(Height, heightUnit, igp.height,
557                 unitDefault);
558         bool const heightChecked = !Height->text().isEmpty()
559                 && Height->text() != "auto";
560         HeightCB->blockSignals(true);
561         HeightCB->setChecked(heightChecked);
562         HeightCB->blockSignals(false);
563         Height->setEnabled(heightChecked);
564         heightUnit->setEnabled(heightChecked);
565
566         scaleCB->setEnabled(!widthChecked && !heightChecked);
567         WidthCB->setEnabled(!scaleChecked);
568         HeightCB->setEnabled(!scaleChecked);
569         aspectratio->setEnabled(widthChecked && heightChecked);
570
571         setAutoText();
572
573         angle->setText(toqstr(igp.rotateAngle));
574         rotateOrderCB->setChecked(igp.scaleBeforeRotation);
575
576         rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
577                 && igp.rotateAngle != "0");
578
579         origin->clear();
580
581         vector<RotationOriginPair> origindata = getRotationOriginData();
582         vector<docstring> const origin_lang = getFirst(origindata);
583         origin_ltx = getSecond(origindata);
584
585         for (vector<docstring>::const_iterator it = origin_lang.begin();
586             it != origin_lang.end(); ++it)
587                 origin->addItem(toqstr(*it));
588
589         if (!igp.rotateOrigin.empty())
590                 origin->setCurrentIndex(itemNumber(origin_ltx, igp.rotateOrigin));
591         else
592                 origin->setCurrentIndex(0);
593
594         // latex section
595         latexoptions->setText(toqstr(igp.special));
596 }
597
598
599 void GuiGraphics::applyView()
600 {
601         InsetGraphicsParams & igp = params_;
602
603         igp.filename.set(fromqstr(filename->text()), fromqstr(bufferFilepath()));
604
605         // the bb section
606         igp.bb.erase();
607         if (bbChanged) {
608                 string bb;
609                 string lbXs = fromqstr(lbX->text());
610                 string lbYs = fromqstr(lbY->text());
611                 string rtXs = fromqstr(rtX->text());
612                 string rtYs = fromqstr(rtY->text());
613                 int bb_sum =
614                         convert<int>(lbXs) + convert<int>(lbYs) +
615                         convert<int>(rtXs) + convert<int>(rtXs);
616                 if (bb_sum) {
617                         if (lbXs.empty())
618                                 bb = "0 ";
619                         else
620                                 bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
621                         if (lbYs.empty())
622                                 bb += "0 ";
623                         else
624                                 bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
625                         if (rtXs.empty())
626                                 bb += "0 ";
627                         else
628                                 bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
629                         if (rtYs.empty())
630                                 bb += '0';
631                         else
632                                 bb += (rtYs + fromqstr(rtYunit->currentText()));
633                         igp.bb = bb;
634                 }
635         }
636
637         igp.draft = draftCB->isChecked();
638         igp.clip = clip->isChecked();
639
640         switch (showCB->currentIndex()) {
641                 case 0: igp.display = graphics::DefaultDisplay; break;
642                 case 1: igp.display = graphics::MonochromeDisplay; break;
643                 case 2: igp.display = graphics::GrayscaleDisplay; break;
644                 case 3: igp.display = graphics::ColorDisplay; break;
645                 default:;
646         }
647
648         if (!displayGB->isChecked())
649                 igp.display = graphics::NoDisplay;
650
651         //the graphics section
652         if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
653                 igp.scale = fromqstr(Scale->text());
654                 igp.width = Length("0pt");
655                 igp.height = Length("0pt");
656                 igp.keepAspectRatio = false;
657         } else {
658                 igp.scale = string();
659                 igp.width = WidthCB->isChecked() ?
660                         //Note that this works even if Width is "auto", since in
661                         //that case we get "0pt".
662                         Length(widgetsToLength(Width, widthUnit)):
663                         Length("0pt");
664                 igp.height = HeightCB->isChecked() ?
665                         Length(widgetsToLength(Height, heightUnit)) :
666                         Length("0pt");
667                 igp.keepAspectRatio = aspectratio->isEnabled() &&
668                         aspectratio->isChecked() &&
669                         igp.width.value() > 0 && igp.height.value() > 0;
670         }
671
672         igp.noUnzip = unzipCB->isChecked();
673         igp.lyxscale = displayscale->text().toInt();
674         igp.rotateAngle = fromqstr(angle->text());
675
676         double rotAngle = convert<double>(igp.rotateAngle);
677         if (abs(rotAngle) > 360.0) {
678                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
679                 igp.rotateAngle = convert<string>(rotAngle);
680         }
681
682         // save the latex name for the origin. If it is the default
683         // then origin_ltx returns ""
684         igp.rotateOrigin = origin_ltx[origin->currentIndex()];
685         igp.scaleBeforeRotation = rotateOrderCB->isChecked();
686
687         // more latex options
688         igp.special = fromqstr(latexoptions->text());
689
690         igp.groupId = fromqstr(groupId->text());
691 }
692
693
694 void GuiGraphics::getBB()
695 {
696         string const fn = fromqstr(filename->text());
697         if (fn.empty())
698                 return;
699         string const bb = readBoundingBox(fn);
700         bbChanged = false;
701         if (bb.empty())
702                 return;
703         lbX->setText(toqstr(token(bb, ' ', 0)));
704         lbY->setText(toqstr(token(bb, ' ', 1)));
705         rtX->setText(toqstr(token(bb, ' ', 2)));
706         rtY->setText(toqstr(token(bb, ' ', 3)));
707         // the default units for the bb values when reading
708         // it from the file
709         lbXunit->setCurrentIndex(0);
710         lbYunit->setCurrentIndex(0);
711         rtXunit->setCurrentIndex(0);
712         rtYunit->setCurrentIndex(0);
713 }
714
715
716 bool GuiGraphics::isValid()
717 {
718         return !filename->text().isEmpty();
719 }
720
721
722 bool GuiGraphics::initialiseParams(string const & data)
723 {
724         InsetGraphics::string2params(data, buffer(), params_);
725         paramsToDialog(params_);
726         return true;
727 }
728
729
730 void GuiGraphics::clearParams()
731 {
732         params_ = InsetGraphicsParams();
733 }
734
735
736 void GuiGraphics::dispatchParams()
737 {
738         InsetGraphicsParams tmp_params(params_);
739         string const lfun = InsetGraphics::params2string(tmp_params, buffer());
740         dispatch(FuncRequest(getLfun(), lfun));
741         if (!params_.groupId.empty())
742                 dispatch(FuncRequest(LFUN_GRAPHICS_GROUPS_UNIFY,
743                                 InsetGraphics::params2string(params_, buffer())));
744 }
745
746
747 QString GuiGraphics::browse(QString const & in_name) const
748 {
749         QString const title = qt_("Select graphics file");
750
751         // Does user clipart directory exist?
752         string clipdir = addName(package().user_support().absFilename(), "clipart");
753         FileName clip(clipdir);
754
755         // bail out to system clipart directory
756         if (!clip.isDirectory())
757                 clipdir = addName(package().system_support().absFilename(), "clipart");
758
759         return browseRelFile(in_name, bufferFilepath(),
760                 title, fileFilters(QString()), false, 
761                 qt_("Clipart|#C#c"), toqstr(clipdir),
762                 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
763 }
764
765
766 string GuiGraphics::readBoundingBox(string const & file)
767 {
768         FileName const abs_file = support::makeAbsPath(file, fromqstr(bufferFilepath()));
769
770         // try to get it from the file, if possible. Zipped files are
771         // unzipped in the readBB_from_PSFile-Function
772         string const bb = readBB_from_PSFile(abs_file);
773         if (!bb.empty())
774                 return bb;
775
776         // we don't, so ask the Graphics Cache if it has loaded the file
777         int width = 0;
778         int height = 0;
779
780         graphics::Cache & gc = graphics::Cache::get();
781         if (gc.inCache(abs_file)) {
782                 graphics::Image const * image = gc.item(abs_file)->image();
783
784                 if (image) {
785                         width  = image->width();
786                         height = image->height();
787                 }
788         }
789
790         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
791 }
792
793
794 bool GuiGraphics::isFileNameValid(string const & fname) const
795 {
796         // It may be that the filename is relative.
797         return support::makeAbsPath(fname, fromqstr(bufferFilepath())).isReadableFile();
798 }
799
800
801 Dialog * createGuiGraphics(GuiView & lv) { return new GuiGraphics(lv); }
802
803
804 } // namespace frontend
805 } // namespace lyx
806
807 #include "GuiGraphics_moc.cpp"