]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiGraphics.cpp
020e2034aea4b8f13a9a5da38aa05afce37f32d8
[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         InsetGraphicsParams par;
320         InsetGraphics::string2params(grp, buffer(), par);
321         par.filename = params_.filename;
322         params_ = par;
323         paramsToDialog(par);
324         groupCO->blockSignals(false);
325         
326         // reset filename
327         filename->setText(current_filename);
328
329         changed();
330 }
331
332
333 void GuiGraphics::on_newGroupPB_clicked()
334 {
335         docstring newgroup;
336         if (!Alert::askForText(newgroup, _("Enter unique group name:")))
337                 return;
338         if (newgroup.empty())
339                 return;
340         if (groupCO->findData(toqstr(newgroup), Qt::MatchExactly) != -1) {
341                 Alert::warning(_("Group already defined!"), 
342                         bformat(_("A graphics group with the name '%1$s' already exists."),
343                                 newgroup));
344                 return;
345         }
346         groupCO->addItem(toqstr(newgroup), toqstr(newgroup));
347         groupCO->setCurrentIndex(
348                 groupCO->findData(toqstr(newgroup), Qt::MatchExactly));
349 }
350
351
352 void GuiGraphics::change_bb()
353 {
354         bbChanged = true;
355         LYXERR(Debug::GRAPHICS, "[bb_Changed set to true]");
356         changed();
357 }
358
359
360 void GuiGraphics::on_browsePB_clicked()
361 {
362         QString const str = browse(filename->text());
363         if (!str.isEmpty()) {
364                 filename->setText(str);
365                 changed();
366         }
367 }
368
369
370 void GuiGraphics::on_getPB_clicked()
371 {
372         getBB();
373 }
374
375
376 void GuiGraphics::setAutoText()
377 {
378         if (scaleCB->isChecked())
379                 return;
380         if (!Scale->isEnabled() && Scale->text() != "100")
381                 Scale->setText(qt_(autostr));
382
383         setAutoTextCB(WidthCB, Width, widthUnit);
384         setAutoTextCB(HeightCB, Height, heightUnit);
385 }
386
387
388 void GuiGraphics::on_scaleCB_toggled(bool setScale)
389 {
390         Scale->setEnabled(setScale);
391         if (setScale) {
392                 Scale->setText("100");
393                 Scale->setFocus(Qt::OtherFocusReason);
394         }
395
396         WidthCB->setDisabled(setScale);
397         WidthCB->blockSignals(true);
398         WidthCB->setChecked(false);
399         WidthCB->blockSignals(false);
400         Width->setEnabled(false);
401         widthUnit->setEnabled(false);
402
403         HeightCB->setDisabled(setScale);
404         HeightCB->blockSignals(true);
405         HeightCB->setChecked(false);
406         HeightCB->blockSignals(false);
407         Height->setEnabled(false);
408         heightUnit->setEnabled(false);
409
410         aspectratio->setDisabled(true);
411         aspectratio->setChecked(true);
412
413         rotateOrderCB->setEnabled((WidthCB->isChecked() ||
414                                  HeightCB->isChecked() ||
415                                  scaleCB->isChecked()) &&
416                                  (angle->text() != "0"));
417
418         setAutoText();
419 }
420
421
422 void GuiGraphics::on_WidthCB_toggled(bool setWidth)
423 {
424         Width->setEnabled(setWidth);
425         widthUnit->setEnabled(setWidth);
426         if (setWidth)
427                 Width->setFocus(Qt::OtherFocusReason);
428
429         bool const setHeight = HeightCB->isChecked();
430         aspectratio->setEnabled(setWidth && setHeight);
431         aspectratio->blockSignals(true);
432         aspectratio->setChecked(!(setWidth && setHeight));
433         aspectratio->blockSignals(false);
434
435         scaleCB->setEnabled(!setWidth && !setHeight);
436         //already will be unchecked, so don't need to do that
437         Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
438                         && scaleCB->isChecked()); //should be false, but let's check
439         rotateOrderCB->setEnabled((setWidth || setHeight ||
440                                  scaleCB->isChecked()) &&
441                                  (angle->text() != "0"));
442
443         setAutoText();
444 }
445
446
447 void GuiGraphics::on_HeightCB_toggled(bool setHeight)
448 {
449         Height->setEnabled(setHeight);
450         heightUnit->setEnabled(setHeight);
451         if (setHeight)
452                 Height->setFocus(Qt::OtherFocusReason);
453
454         bool const setWidth = WidthCB->isChecked();
455         aspectratio->setEnabled(setWidth && setHeight);
456         aspectratio->blockSignals(true);
457         aspectratio->setChecked(!(setWidth && setHeight));
458         aspectratio->blockSignals(false);
459
460         scaleCB->setEnabled(!setWidth && !setHeight);
461         //already unchecked
462         Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
463                 && scaleCB->isChecked()); //should be false
464         rotateOrderCB->setEnabled((setWidth || setHeight ||
465                                  scaleCB->isChecked()) &&
466                                  (angle->text() != "0"));
467
468         setAutoText();
469 }
470
471
472 void GuiGraphics::on_angle_textChanged(const QString & filename)
473 {
474         rotateOrderCB->setEnabled((WidthCB->isChecked() ||
475                                  HeightCB->isChecked() ||
476                                  scaleCB->isChecked()) &&
477                                  (filename != "0"));
478 }
479
480 // returns the number of the string s in the vector v
481 static int itemNumber(const vector<string> & v, string const & s)
482 {
483         vector<string>::const_iterator cit =
484                     find(v.begin(), v.end(), s);
485         return (cit != v.end()) ? int(cit - v.begin()) : 0;
486 }
487
488
489 void GuiGraphics::paramsToDialog(InsetGraphicsParams const & igp)
490 {
491         static char const * const bb_units[] = { "bp", "cm", "mm", "in" };
492         size_t const bb_size = sizeof(bb_units) / sizeof(bb_units[0]);
493
494         vector<string> const units = vector<string>(bb_units, bb_units + bb_size);
495         lbXunit->clear();
496         lbYunit->clear();
497         rtXunit->clear();
498         rtYunit->clear();
499         for (vector<string>::const_iterator it = units.begin();
500             it != units.end(); ++it) {
501                 lbXunit->addItem(toqstr(*it));
502                 lbYunit->addItem(toqstr(*it));
503                 rtXunit->addItem(toqstr(*it));
504                 rtYunit->addItem(toqstr(*it));
505         }
506
507         // set the right default unit
508         Length::UNIT unitDefault = Length::CM;
509         switch (lyxrc.default_papersize) {
510                 case PAPER_USLETTER:
511                 case PAPER_USLEGAL:
512                 case PAPER_USEXECUTIVE:
513                         unitDefault = Length::IN;
514                         break;
515                 default:
516                         break;
517         }
518
519         //lyxerr << bufferFilepath();
520         string const name =
521                 igp.filename.outputFilename(fromqstr(bufferFilepath()));
522         filename->setText(toqstr(name));
523
524         // set the bounding box values
525         if (igp.bb.empty()) {
526                 string const bb = readBoundingBox(igp.filename.absFilename());
527                 // the values from the file always have the bigpoint-unit bp
528                 lbX->setText(toqstr(token(bb, ' ', 0)));
529                 lbY->setText(toqstr(token(bb, ' ', 1)));
530                 rtX->setText(toqstr(token(bb, ' ', 2)));
531                 rtY->setText(toqstr(token(bb, ' ', 3)));
532                 lbXunit->setCurrentIndex(0);
533                 lbYunit->setCurrentIndex(0);
534                 rtXunit->setCurrentIndex(0);
535                 rtYunit->setCurrentIndex(0);
536                 bbChanged = false;
537         } else {
538                 // get the values from the inset
539                 Length anyLength;
540                 string const xl = token(igp.bb, ' ', 0);
541                 string const yl = token(igp.bb, ' ', 1);
542                 string const xr = token(igp.bb, ' ', 2);
543                 string const yr = token(igp.bb, ' ', 3);
544                 if (isValidLength(xl, &anyLength)) {
545                         lbX->setText(toqstr(convert<string>(anyLength.value())));
546                         string const unit = unit_name[anyLength.unit()];
547                         lbXunit->setCurrentIndex(itemNumber(units, unit));
548                 } else {
549                         lbX->setText(toqstr(xl));
550                 }
551                 if (isValidLength(yl, &anyLength)) {
552                         lbY->setText(toqstr(convert<string>(anyLength.value())));
553                         string const unit = unit_name[anyLength.unit()];
554                         lbYunit->setCurrentIndex(itemNumber(units, unit));
555                 } else {
556                         lbY->setText(toqstr(xl));
557                 }
558                 if (isValidLength(xr, &anyLength)) {
559                         rtX->setText(toqstr(convert<string>(anyLength.value())));
560                         string const unit = unit_name[anyLength.unit()];
561                         rtXunit->setCurrentIndex(itemNumber(units, unit));
562                 } else {
563                         rtX->setText(toqstr(xl));
564                 }
565                 if (isValidLength(yr, &anyLength)) {
566                         rtY->setText(toqstr(convert<string>(anyLength.value())));
567                         string const unit = unit_name[anyLength.unit()];
568                         rtYunit->setCurrentIndex(itemNumber(units, unit));
569                 } else {
570                         rtY->setText(toqstr(xl));
571                 }
572                 bbChanged = true;
573         }
574
575         // Update the draft and clip mode
576         draftCB->setChecked(igp.draft);
577         clip->setChecked(igp.clip);
578         unzipCB->setChecked(igp.noUnzip);
579         displayGB->setChecked(igp.display);
580         displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
581
582         // the output section (width/height)
583
584         Scale->setText(toqstr(igp.scale));
585         //igp.scale defaults to 100, so we treat it as empty
586         bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
587         scaleCB->blockSignals(true);
588         scaleCB->setChecked(scaleChecked);
589         scaleCB->blockSignals(false);
590         Scale->setEnabled(scaleChecked);
591         displayGB->setEnabled(lyxrc.display_graphics);
592
593         set<string> grp;
594         graphics::getGraphicsGroups(buffer(), grp);
595         set<string>::const_iterator it = grp.begin();
596         set<string>::const_iterator end = grp.end();
597         groupCO->blockSignals(true);
598         groupCO->clear();
599         for (; it != end; it++)
600                 groupCO->addItem(toqstr(*it), toqstr(*it));
601         groupCO->insertItem(0, qt_("None"), QString());
602         if (igp.groupId.empty())
603                 groupCO->setCurrentIndex(0);
604         else
605                 groupCO->setCurrentIndex(
606                         groupCO->findData(toqstr(igp.groupId), Qt::MatchExactly));
607         groupCO->blockSignals(false);
608
609         if (igp.width.value() == 0)
610                 lengthToWidgets(Width, widthUnit, _(autostr), unitDefault);
611         else
612                 lengthToWidgets(Width, widthUnit, igp.width, unitDefault);
613
614         bool const widthChecked = !Width->text().isEmpty() &&
615                 Width->text() != qt_(autostr);
616         WidthCB->blockSignals(true);
617         WidthCB->setChecked(widthChecked);
618         WidthCB->blockSignals(false);
619         Width->setEnabled(widthChecked);
620         widthUnit->setEnabled(widthChecked);
621
622         if (igp.height.value() == 0)
623                 lengthToWidgets(Height, heightUnit, _(autostr), unitDefault);
624         else
625                 lengthToWidgets(Height, heightUnit, igp.height, unitDefault);
626
627         bool const heightChecked = !Height->text().isEmpty()
628                 && Height->text() != qt_(autostr);
629         HeightCB->blockSignals(true);
630         HeightCB->setChecked(heightChecked);
631         HeightCB->blockSignals(false);
632         Height->setEnabled(heightChecked);
633         heightUnit->setEnabled(heightChecked);
634
635         scaleCB->setEnabled(!widthChecked && !heightChecked);
636         WidthCB->setEnabled(!scaleChecked);
637         HeightCB->setEnabled(!scaleChecked);
638         aspectratio->setEnabled(widthChecked && heightChecked);
639
640         setAutoText();
641
642         angle->setText(toqstr(igp.rotateAngle));
643         rotateOrderCB->setChecked(igp.scaleBeforeRotation);
644
645         rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
646                 && igp.rotateAngle != "0");
647
648         origin->clear();
649
650         vector<RotationOriginPair> origindata = getRotationOriginData();
651         vector<docstring> const origin_lang = getFirst(origindata);
652         origin_ltx = getSecond(origindata);
653
654         for (vector<docstring>::const_iterator it = origin_lang.begin();
655             it != origin_lang.end(); ++it)
656                 origin->addItem(toqstr(*it));
657
658         if (!igp.rotateOrigin.empty())
659                 origin->setCurrentIndex(itemNumber(origin_ltx, igp.rotateOrigin));
660         else
661                 origin->setCurrentIndex(0);
662
663         // latex section
664         latexoptions->setText(toqstr(igp.special));
665 }
666
667
668 void GuiGraphics::applyView()
669 {
670         InsetGraphicsParams & igp = params_;
671
672         igp.filename.set(fromqstr(filename->text()), fromqstr(bufferFilepath()));
673
674         // the bb section
675         igp.bb.erase();
676         if (bbChanged) {
677                 string bb;
678                 string lbXs = fromqstr(lbX->text());
679                 string lbYs = fromqstr(lbY->text());
680                 string rtXs = fromqstr(rtX->text());
681                 string rtYs = fromqstr(rtY->text());
682                 int bb_sum =
683                         convert<int>(lbXs) + convert<int>(lbYs) +
684                         convert<int>(rtXs) + convert<int>(rtXs);
685                 if (bb_sum) {
686                         if (lbXs.empty())
687                                 bb = "0 ";
688                         else
689                                 bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
690                         if (lbYs.empty())
691                                 bb += "0 ";
692                         else
693                                 bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
694                         if (rtXs.empty())
695                                 bb += "0 ";
696                         else
697                                 bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
698                         if (rtYs.empty())
699                                 bb += '0';
700                         else
701                                 bb += (rtYs + fromqstr(rtYunit->currentText()));
702                         igp.bb = bb;
703                 }
704         }
705
706         igp.draft = draftCB->isChecked();
707         igp.clip = clip->isChecked();
708         igp.display = displayGB->isChecked();
709
710         //the graphics section
711         if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
712                 igp.scale = fromqstr(Scale->text());
713                 igp.width = Length("0pt");
714                 igp.height = Length("0pt");
715                 igp.keepAspectRatio = false;
716         } else {
717                 igp.scale = string();
718                 igp.width = WidthCB->isChecked() ?
719                         //Note that this works even if Width is "auto", since in
720                         //that case we get "0pt".
721                         Length(widgetsToLength(Width, widthUnit)):
722                         Length("0pt");
723                 igp.height = HeightCB->isChecked() ?
724                         Length(widgetsToLength(Height, heightUnit)) :
725                         Length("0pt");
726                 igp.keepAspectRatio = aspectratio->isEnabled() &&
727                         aspectratio->isChecked() &&
728                         igp.width.value() > 0 && igp.height.value() > 0;
729         }
730
731         igp.noUnzip = unzipCB->isChecked();
732         igp.lyxscale = displayscale->text().toInt();
733         igp.rotateAngle = fromqstr(angle->text());
734
735         double rotAngle = convert<double>(igp.rotateAngle);
736         if (abs(rotAngle) > 360.0) {
737                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
738                 igp.rotateAngle = convert<string>(rotAngle);
739         }
740
741         // save the latex name for the origin. If it is the default
742         // then origin_ltx returns ""
743         igp.rotateOrigin = origin_ltx[origin->currentIndex()];
744         igp.scaleBeforeRotation = rotateOrderCB->isChecked();
745
746         // more latex options
747         igp.special = fromqstr(latexoptions->text());
748
749         igp.groupId = fromqstr(groupCO->itemData(
750                 groupCO->currentIndex()).toString());
751 }
752
753
754 void GuiGraphics::getBB()
755 {
756         string const fn = fromqstr(filename->text());
757         if (fn.empty())
758                 return;
759         string const bb = readBoundingBox(fn);
760         bbChanged = false;
761         if (bb.empty())
762                 return;
763         lbX->setText(toqstr(token(bb, ' ', 0)));
764         lbY->setText(toqstr(token(bb, ' ', 1)));
765         rtX->setText(toqstr(token(bb, ' ', 2)));
766         rtY->setText(toqstr(token(bb, ' ', 3)));
767         // the default units for the bb values when reading
768         // it from the file
769         lbXunit->setCurrentIndex(0);
770         lbYunit->setCurrentIndex(0);
771         rtXunit->setCurrentIndex(0);
772         rtYunit->setCurrentIndex(0);
773 }
774
775
776 bool GuiGraphics::isValid()
777 {
778         return !filename->text().isEmpty();
779 }
780
781
782 bool GuiGraphics::initialiseParams(string const & data)
783 {
784         InsetGraphics::string2params(data, buffer(), params_);
785         paramsToDialog(params_);
786         return true;
787 }
788
789
790 void GuiGraphics::clearParams()
791 {
792         params_ = InsetGraphicsParams();
793 }
794
795
796 void GuiGraphics::dispatchParams()
797 {
798         InsetGraphicsParams tmp_params(params_);
799         string const lfun = InsetGraphics::params2string(tmp_params, buffer());
800         dispatch(FuncRequest(getLfun(), lfun));
801 }
802
803
804 QString GuiGraphics::browse(QString const & in_name) const
805 {
806         QString const title = qt_("Select graphics file");
807
808         // Does user clipart directory exist?
809         string clipdir = addName(package().user_support().absFilename(), "clipart");
810         FileName clip(clipdir);
811
812         // bail out to system clipart directory
813         if (!clip.isDirectory())
814                 clipdir = addName(package().system_support().absFilename(), "clipart");
815
816         return browseRelFile(in_name, bufferFilepath(),
817                 title, fileFilters(QString()), false, 
818                 qt_("Clipart|#C#c"), toqstr(clipdir),
819                 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
820 }
821
822
823 string GuiGraphics::readBoundingBox(string const & file)
824 {
825         FileName const abs_file = support::makeAbsPath(file, fromqstr(bufferFilepath()));
826
827         // try to get it from the file, if possible. Zipped files are
828         // unzipped in the readBB_from_PSFile-Function
829         string const bb = readBB_from_PSFile(abs_file);
830         if (!bb.empty())
831                 return bb;
832
833         // we don't, so ask the Graphics Cache if it has loaded the file
834         int width = 0;
835         int height = 0;
836
837         graphics::Cache & gc = graphics::Cache::get();
838         if (gc.inCache(abs_file)) {
839                 graphics::Image const * image = gc.item(abs_file)->image();
840
841                 if (image) {
842                         width  = image->width();
843                         height = image->height();
844                 }
845         }
846
847         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
848 }
849
850
851 bool GuiGraphics::isFileNameValid(string const & fname) const
852 {
853         // It may be that the filename is relative.
854         return support::makeAbsPath(fname, fromqstr(bufferFilepath())).isReadableFile();
855 }
856
857
858 Dialog * createGuiGraphics(GuiView & lv) { return new GuiGraphics(lv); }
859
860
861 } // namespace frontend
862 } // namespace lyx
863
864 #include "moc_GuiGraphics.cpp"