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