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