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