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