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