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