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