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