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