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