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
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 */
setAutoTextCB(QCheckBox * checkBox,QLineEdit * lineEdit,LengthCombo * lengthCombo)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 
GuiGraphics(GuiView & lv)112 GuiGraphics::GuiGraphics(GuiView & lv)
113 	: GuiDialog(lv, "graphics", qt_("Graphics")), bbChanged(false)
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 	// FIXME: we should connect to clicked() when we move to Qt 4.2	because
200 	// the toggled(bool) signal is also trigged when we update the widgets
201 	// (rgh-4/07) this isn't as much or a problem as it was, because we're now
202 	// using blockSignals() to keep from triggering that signal when we call
203 	// setChecked(). Note, too, that clicked() would get called whenever it
204 	// is clicked, even right clicked (I think), not just whenever it is
205 	// toggled.
206 	connect(displayGB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
207 	connect(displayscale, SIGNAL(textChanged(const QString &)),
208 		this, SLOT(change_adaptor()));
209 	connect(groupCO, SIGNAL(currentIndexChanged(int)),
210 		this, SLOT(changeGroup(int)));
211 
212 	displayscale->setValidator(new QIntValidator(displayscale));
213 
214 	bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
215 	bc().setOK(okPB);
216 	bc().setApply(applyPB);
217 	bc().setRestore(restorePB);
218 	bc().setCancel(closePB);
219 
220 	bc().addReadOnly(latexoptions);
221 	bc().addReadOnly(filenameL);
222 	bc().addReadOnly(filename);
223 	bc().addReadOnly(browsePB);
224 	bc().addReadOnly(bbFrame);
225 	bc().addReadOnly(draftCB);
226 	bc().addReadOnly(clip);
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 
change_adaptor()248 void GuiGraphics::change_adaptor()
249 {
250 	changed();
251 }
252 
253 
changeGroup(int)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 
on_newGroupPB_clicked()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 
changeBB()344 void GuiGraphics::changeBB()
345 {
346 	bbChanged = true;
347 	LYXERR(Debug::GRAPHICS, "[bb_Changed set to true]");
348 	changed();
349 }
350 
351 
on_browsePB_clicked()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 
on_getPB_clicked()362 void GuiGraphics::on_getPB_clicked()
363 {
364 	getBB();
365 }
366 
367 
setAutoText()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 
on_scaleCB_toggled(bool setScale)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 	rotateOrderCB->setEnabled((WidthCB->isChecked() ||
403 				 HeightCB->isChecked() ||
404 				 scaleCB->isChecked()) &&
405 				 (angle->text() != "0"));
406 
407 	setAutoText();
408 	updateAspectRatioStatus();
409 }
410 
411 
on_WidthCB_toggled(bool setWidth)412 void GuiGraphics::on_WidthCB_toggled(bool setWidth)
413 {
414 	Width->setEnabled(setWidth);
415 	widthUnit->setEnabled(setWidth);
416 	if (setWidth)
417 		Width->setFocus(Qt::OtherFocusReason);
418 
419 	bool const setHeight = HeightCB->isChecked();
420 	scaleCB->setEnabled(!setWidth && !setHeight);
421 	//already will be unchecked, so don't need to do that
422 	Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
423 			&& scaleCB->isChecked()); //should be false, but let's check
424 	rotateOrderCB->setEnabled((setWidth || setHeight ||
425 				 scaleCB->isChecked()) &&
426 				 (angle->text() != "0"));
427 
428 	setAutoText();
429 	updateAspectRatioStatus();
430 }
431 
432 
on_HeightCB_toggled(bool setHeight)433 void GuiGraphics::on_HeightCB_toggled(bool setHeight)
434 {
435 	Height->setEnabled(setHeight);
436 	heightUnit->setEnabled(setHeight);
437 	if (setHeight)
438 		Height->setFocus(Qt::OtherFocusReason);
439 
440 	bool const setWidth = WidthCB->isChecked();
441 	scaleCB->setEnabled(!setWidth && !setHeight);
442 	//already unchecked
443 	Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
444 		&& scaleCB->isChecked()); //should be false
445 	rotateOrderCB->setEnabled((setWidth || setHeight ||
446 				 scaleCB->isChecked()) &&
447 				 (angle->text() != "0"));
448 
449 	setAutoText();
450 	updateAspectRatioStatus();
451 }
452 
453 
updateAspectRatioStatus()454 void GuiGraphics::updateAspectRatioStatus()
455 {
456 	// keepaspectratio only makes sense if both a width _and_ a
457 	// height are given, since its function is (see graphics manual):
458 	// "If set to true then specifying both 'width' and 'height'
459 	// (or 'totalheight') does not distort the figure but scales
460 	// such that neither of the specified dimensions is _exceeded_."
461 	aspectratio->setEnabled(
462 		WidthCB->isChecked() && !Width->text().isEmpty()
463 		&& Width->text() != qt_(autostr)
464 		&& HeightCB->isChecked() && !Height->text().isEmpty()
465 		&& Height->text() != qt_(autostr)
466 		);
467 	if (!aspectratio->isEnabled())
468 		aspectratio->setChecked(false);
469 }
470 
471 
on_aspectratio_toggled(bool aspectratio)472 void GuiGraphics::on_aspectratio_toggled(bool aspectratio)
473 {
474 	if (aspectratio) {
475 		WidthCB->setText(qt_("Set max. &width:"));
476 		HeightCB->setText(qt_("Set max. &height:"));
477 		Width->setToolTip(qt_("Maximal width of image in output"));
478 		Height->setToolTip(qt_("Maximal height of image in output"));
479 	} else {
480 		WidthCB->setText(qt_("Set &width:"));
481 		HeightCB->setText(qt_("Set &height:"));
482 		Width->setToolTip(qt_("Width of image in output"));
483 		Height->setToolTip(qt_("Height of image in output"));
484 	}
485 }
486 
487 
on_angle_textChanged(const QString & filename)488 void GuiGraphics::on_angle_textChanged(const QString & filename)
489 {
490 	rotateOrderCB->setEnabled((WidthCB->isChecked() ||
491 				 HeightCB->isChecked() ||
492 				 scaleCB->isChecked()) &&
493 				 (filename != "0"));
494 }
495 
496 
paramsToDialog(InsetGraphicsParams const & igp)497 void GuiGraphics::paramsToDialog(InsetGraphicsParams const & igp)
498 {
499 	static char const * const bb_units[] = { "bp", "cm", "mm", "in" };
500 	static char const * const bb_units_gui[] = { N_("bp"), N_("cm"), N_("mm"), N_("in[[unit of measure]]") };
501 	size_t const bb_size = sizeof(bb_units) / sizeof(bb_units[0]);
502 
503 	lbXunit->clear();
504 	lbYunit->clear();
505 	rtXunit->clear();
506 	rtYunit->clear();
507 
508 	for (size_t i = 0; i < bb_size; i++) {
509 		lbXunit->addItem(qt_(bb_units_gui[i]),
510 			toqstr(bb_units[i]));
511 		lbYunit->addItem(qt_(bb_units_gui[i]),
512 			toqstr(bb_units[i]));
513 		rtXunit->addItem(qt_(bb_units_gui[i]),
514 			toqstr(bb_units[i]));
515 		rtYunit->addItem(qt_(bb_units_gui[i]),
516 			toqstr(bb_units[i]));
517 	}
518 
519 	// set the right default unit
520 	Length::UNIT const defaultUnit = Length::defaultUnit();
521 
522 	//lyxerr << bufferFilePath();
523 	string const name =
524 		igp.filename.outputFileName(fromqstr(bufferFilePath()));
525 	filename->setText(toqstr(name));
526 
527 	// set the bounding box values
528 	if (igp.bbox.empty()) {
529 		string const bb = readBoundingBox(igp.filename.absFileName());
530 		// the values from the file always have the bigpoint-unit bp
531 		doubleToWidget(lbX, token(bb, ' ', 0));
532 		doubleToWidget(lbY, token(bb, ' ', 1));
533 		doubleToWidget(rtX, token(bb, ' ', 2));
534 		doubleToWidget(rtY, token(bb, ' ', 3));
535 		lbXunit->setCurrentIndex(0);
536 		lbYunit->setCurrentIndex(0);
537 		rtXunit->setCurrentIndex(0);
538 		rtYunit->setCurrentIndex(0);
539 		bbChanged = false;
540 	} else {
541 		// get the values from the inset
542 		doubleToWidget(lbX, igp.bbox.xl.value());
543 		string unit = unit_name[igp.bbox.xl.unit()];
544 		lbXunit->setCurrentIndex(lbXunit->findData(toqstr(unit)));
545 		doubleToWidget(lbY, igp.bbox.yb.value());
546 		unit = unit_name[igp.bbox.yb.unit()];
547 		lbYunit->setCurrentIndex(lbYunit->findData(toqstr(unit)));
548 		doubleToWidget(rtX, igp.bbox.xr.value());
549 		unit = unit_name[igp.bbox.xr.unit()];
550 		rtXunit->setCurrentIndex(rtXunit->findData(toqstr(unit)));
551 		doubleToWidget(rtY, igp.bbox.yt.value());
552 		unit = unit_name[igp.bbox.yt.unit()];
553 		rtYunit->setCurrentIndex(rtYunit->findData(toqstr(unit)));
554 		bbChanged = true;
555 	}
556 
557 	// Update the draft and clip mode
558 	draftCB->setChecked(igp.draft);
559 	clip->setChecked(igp.clip);
560 	displayGB->setChecked(igp.display);
561 	displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
562 
563 	// the output section (width/height)
564 
565 	doubleToWidget(Scale, igp.scale);
566 	//igp.scale defaults to 100, so we treat it as empty
567 	bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
568 	scaleCB->blockSignals(true);
569 	scaleCB->setChecked(scaleChecked);
570 	scaleCB->blockSignals(false);
571 	Scale->setEnabled(scaleChecked);
572 	displayGB->setEnabled(lyxrc.display_graphics);
573 
574 	set<string> grp;
575 	graphics::getGraphicsGroups(buffer(), grp);
576 	set<string>::const_iterator it = grp.begin();
577 	set<string>::const_iterator end = grp.end();
578 	groupCO->blockSignals(true);
579 	groupCO->clear();
580 	for (; it != end; ++it)
581 		groupCO->addItem(toqstr(*it), toqstr(*it));
582 	groupCO->insertItem(0, qt_("None"), QString());
583 	if (igp.groupId.empty())
584 		groupCO->setCurrentIndex(0);
585 	else
586 		groupCO->setCurrentIndex(
587 			groupCO->findData(toqstr(igp.groupId), Qt::MatchExactly));
588 	groupCO->blockSignals(false);
589 
590 	if (igp.width.value() == 0)
591 		lengthToWidgets(Width, widthUnit, _(autostr), defaultUnit);
592 	else
593 		lengthToWidgets(Width, widthUnit, igp.width, defaultUnit);
594 
595 	bool const widthChecked = !Width->text().isEmpty() &&
596 		Width->text() != qt_(autostr);
597 	WidthCB->blockSignals(true);
598 	WidthCB->setChecked(widthChecked);
599 	WidthCB->blockSignals(false);
600 	Width->setEnabled(widthChecked);
601 	widthUnit->setEnabled(widthChecked);
602 
603 	if (igp.height.value() == 0)
604 		lengthToWidgets(Height, heightUnit, _(autostr), defaultUnit);
605 	else
606 		lengthToWidgets(Height, heightUnit, igp.height, defaultUnit);
607 
608 	bool const heightChecked = !Height->text().isEmpty()
609 		&& Height->text() != qt_(autostr);
610 	HeightCB->blockSignals(true);
611 	HeightCB->setChecked(heightChecked);
612 	HeightCB->blockSignals(false);
613 	Height->setEnabled(heightChecked);
614 	heightUnit->setEnabled(heightChecked);
615 
616 	scaleCB->setEnabled(!widthChecked && !heightChecked);
617 	WidthCB->setEnabled(!scaleChecked);
618 	HeightCB->setEnabled(!scaleChecked);
619 
620 	setAutoText();
621 	updateAspectRatioStatus();
622 
623 	doubleToWidget(angle, igp.rotateAngle);
624 	rotateOrderCB->setChecked(igp.scaleBeforeRotation);
625 
626 	rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
627 		&& igp.rotateAngle != "0");
628 
629 	origin->clear();
630 
631 	for (size_t i = 0; i < rorigin_size; i++) {
632 		origin->addItem(qt_(rorigin_gui_strs[i]),
633 			toqstr(rorigin_lyx_strs[i]));
634 	}
635 
636 	if (!igp.rotateOrigin.empty())
637 		origin->setCurrentIndex(origin->findData(toqstr(igp.rotateOrigin)));
638 	else
639 		origin->setCurrentIndex(0);
640 
641 	// latex section
642 	latexoptions->setText(toqstr(igp.special));
643 	// cf bug #3852
644 	filename->setFocus();
645 }
646 
647 
applyView()648 void GuiGraphics::applyView()
649 {
650 	InsetGraphicsParams & igp = params_;
651 
652 	igp.filename.set(fromqstr(filename->text()), fromqstr(bufferFilePath()));
653 
654 	// the bb section
655 	igp.bbox = graphics::BoundingBox();
656 	if (bbChanged) {
657 		string lbXs = widgetToDoubleStr(lbX);
658 		string lbYs = widgetToDoubleStr(lbY);
659 		string rtXs = widgetToDoubleStr(rtX);
660 		string rtYs = widgetToDoubleStr(rtY);
661 		int bb_sum =
662 			convert<int>(lbXs) + convert<int>(lbYs) +
663 			convert<int>(rtXs) + convert<int>(rtXs);
664 		if (bb_sum) {
665 			if (lbXs.empty())
666 				lbXs = "0";
667 			igp.bbox.xl = Length(lbXs + fromqstr(lbXunit->currentText()));
668 			if (lbYs.empty())
669 				lbYs = "0";
670 			igp.bbox.yb = Length(lbYs + fromqstr(lbYunit->currentText()));
671 			if (rtXs.empty())
672 				rtXs = "0";
673 			igp.bbox.xr = Length(rtXs + fromqstr(rtXunit->currentText()));
674 			if (rtYs.empty())
675 				rtYs = "0";
676 			igp.bbox.yt = Length(rtYs + fromqstr(rtYunit->currentText()));
677 		}
678 	}
679 
680 	igp.draft = draftCB->isChecked();
681 	igp.clip = clip->isChecked();
682 	igp.display = displayGB->isChecked();
683 
684 	//the graphics section
685 	if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
686 		igp.scale = widgetToDoubleStr(Scale);
687 		igp.width = Length("0pt");
688 		igp.height = Length("0pt");
689 		igp.keepAspectRatio = false;
690 	} else {
691 		igp.scale = string();
692 		igp.width = WidthCB->isChecked() ?
693 			//Note that this works even if Width is "auto", since in
694 			//that case we get "0pt".
695 			Length(widgetsToLength(Width, widthUnit)):
696 			Length("0pt");
697 		igp.height = HeightCB->isChecked() ?
698 			Length(widgetsToLength(Height, heightUnit)) :
699 			Length("0pt");
700 		igp.keepAspectRatio = aspectratio->isChecked();
701 	}
702 
703 	igp.lyxscale = displayscale->text().toInt();
704 	igp.rotateAngle = widgetToDoubleStr(angle);
705 
706 	double rotAngle = widgetToDouble(angle);
707 	if (abs(rotAngle) > 360.0) {
708 		rotAngle -= 360.0 * floor(rotAngle / 360.0);
709 		igp.rotateAngle = convert<string>(rotAngle);
710 	}
711 
712 	// save the latex name for the origin. If it is the default
713 	// then origin_ltx returns ""
714 	igp.rotateOrigin =
715 		fromqstr(origin->itemData(origin->currentIndex()).toString());
716 	igp.scaleBeforeRotation = rotateOrderCB->isChecked();
717 
718 	// more latex options
719 	igp.special = fromqstr(latexoptions->text());
720 
721 	igp.groupId = fromqstr(groupCO->itemData(
722 		groupCO->currentIndex()).toString());
723 	current_group_ = igp.groupId;
724 }
725 
726 
getBB()727 void GuiGraphics::getBB()
728 {
729 	string const fn = fromqstr(filename->text());
730 	if (fn.empty())
731 		return;
732 	string const bb = readBoundingBox(fn);
733 	bbChanged = false;
734 	if (bb.empty())
735 		return;
736 	doubleToWidget(lbX, token(bb, ' ', 0));
737 	doubleToWidget(lbY, token(bb, ' ', 1));
738 	doubleToWidget(rtX, token(bb, ' ', 2));
739 	doubleToWidget(rtY, token(bb, ' ', 3));
740 	// the default units for the bb values when reading
741 	// it from the file
742 	lbXunit->setCurrentIndex(0);
743 	lbYunit->setCurrentIndex(0);
744 	rtXunit->setCurrentIndex(0);
745 	rtYunit->setCurrentIndex(0);
746 }
747 
748 
isValid()749 bool GuiGraphics::isValid()
750 {
751 	return !filename->text().isEmpty();
752 }
753 
754 
initialiseParams(string const & data)755 bool GuiGraphics::initialiseParams(string const & data)
756 {
757 	InsetGraphics::string2params(data, buffer(), params_);
758 	paramsToDialog(params_);
759 	current_group_ = params_.groupId;
760 	return true;
761 }
762 
763 
clearParams()764 void GuiGraphics::clearParams()
765 {
766 	params_ = InsetGraphicsParams();
767 }
768 
769 
dispatchParams()770 void GuiGraphics::dispatchParams()
771 {
772 	InsetGraphicsParams tmp_params(params_);
773 	string const lfun = InsetGraphics::params2string(tmp_params, buffer());
774 	dispatch(FuncRequest(getLfun(), lfun));
775 }
776 
777 
browse(QString const & in_name) const778 QString GuiGraphics::browse(QString const & in_name) const
779 {
780 	QString const title = qt_("Select graphics file");
781 
782 	// Does user clipart directory exist?
783 	string clipdir = addName(package().user_support().absFileName(), "clipart");
784 	FileName clip(clipdir);
785 
786 	// bail out to system clipart directory
787 	if (!clip.isDirectory())
788 		clipdir = addName(package().system_support().absFileName(), "clipart");
789 
790 	return browseRelToParent(in_name, bufferFilePath(),
791 		title, fileFilters(QString()), false,
792 		qt_("Clipart|#C#c"), toqstr(clipdir),
793 		qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
794 }
795 
796 
readBoundingBox(string const & file)797 string GuiGraphics::readBoundingBox(string const & file)
798 {
799 	FileName const abs_file = support::makeAbsPath(file, fromqstr(bufferFilePath()));
800 
801 	// try to get it from the file, if possible. Zipped files are
802 	// unzipped in the readBB_from_PSFile-Function
803 	string const bb = graphics::readBB_from_PSFile(abs_file);
804 	if (!bb.empty())
805 		return bb;
806 
807 	// we don't, so ask the Graphics Cache if it has loaded the file
808 	int width = 0;
809 	int height = 0;
810 
811 	graphics::Cache & gc = graphics::Cache::get();
812 	if (gc.inCache(abs_file)) {
813 		graphics::Image const * image = gc.item(abs_file)->image();
814 
815 		if (image) {
816 			width  = image->width();
817 			height = image->height();
818 		}
819 	}
820 
821 	return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
822 }
823 
824 
isFileNameValid(string const & fname) const825 bool GuiGraphics::isFileNameValid(string const & fname) const
826 {
827 	// It may be that the filename is relative.
828 	return support::makeAbsPath(fname, fromqstr(bufferFilePath())).isReadableFile();
829 }
830 
831 
createGuiGraphics(GuiView & lv)832 Dialog * createGuiGraphics(GuiView & lv) { return new GuiGraphics(lv); }
833 
834 
835 } // namespace frontend
836 } // namespace lyx
837 
838 #include "moc_GuiGraphics.cpp"
839