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