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