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