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