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