2 * \file GuiGraphics.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
11 * Full author contact details are available in file CREDITS.
16 #include "GuiGraphics.h"
18 #include "ControlGraphics.h"
20 #include "LengthCombo.h"
21 #include "lengthcommon.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
26 #include "insets/InsetGraphicsParams.h"
28 #include "support/convert.h"
29 #include "support/lstrings.h"
30 #include "support/lyxlib.h"
31 #include "support/os.h"
33 #include <boost/bind.hpp>
36 #include <QCloseEvent>
39 #include <QPushButton>
45 using lyx::support::float_equal;
46 using lyx::support::token;
48 using lyx::support::os::internal_path;
50 #ifndef CXX_GLOBAL_CSTD
63 std::vector<typename Pair::first_type> const
64 getFirst(std::vector<Pair> const & pr)
66 std::vector<typename Pair::first_type> tmp(pr.size());
67 std::transform(pr.begin(), pr.end(), tmp.begin(),
68 boost::bind(&Pair::first, _1));
74 std::vector<typename Pair::second_type> const
75 getSecond(std::vector<Pair> const & pr)
77 std::vector<typename Pair::second_type> tmp(pr.size());
78 std::transform(pr.begin(), pr.end(), tmp.begin(),
79 boost::bind(&Pair::second, _1));
83 GuiGraphicsDialog::GuiGraphicsDialog(LyXView & lv)
84 : GuiDialog(lv, "graphics")
87 setViewTitle(_("Graphics"));
88 setController(new ControlGraphics(*this));
91 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
92 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
93 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
94 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
97 connect(filename, SIGNAL(textChanged(const QString &)),
98 this, SLOT(change_adaptor()));
99 connect(WidthCB, SIGNAL( clicked()),
100 this, SLOT(change_adaptor()));
101 connect(HeightCB, SIGNAL( clicked()),
102 this, SLOT(change_adaptor()));
103 connect(Width, SIGNAL(textChanged(const QString &)),
104 this, SLOT(change_adaptor()));
105 connect(Height, SIGNAL(textChanged(const QString &)),
106 this, SLOT(change_adaptor()));
107 connect(heightUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
108 this, SLOT(change_adaptor()));
109 connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
110 this, SLOT(change_adaptor()));
111 connect(aspectratio, SIGNAL(stateChanged(int)),
112 this, SLOT(change_adaptor()));
113 connect(angle, SIGNAL(textChanged(const QString &)),
114 this, SLOT(change_adaptor()));
115 connect(origin, SIGNAL(activated(int)),
116 this, SLOT(change_adaptor()));
117 connect(scaleCB, SIGNAL(clicked()),
118 this, SLOT(change_adaptor()));
119 connect(Scale, SIGNAL(textChanged(const QString &)),
120 this, SLOT(change_adaptor()));
121 connect(rotateOrderCB, SIGNAL(clicked()),
122 this, SLOT(change_adaptor()));
124 filename->setValidator(new PathValidator(true, filename));
125 setFocusProxy(filename);
127 QDoubleValidator * scaleValidator = new DoubleAutoValidator(Scale);
128 scaleValidator->setBottom(0);
129 scaleValidator->setDecimals(256); //I guess that will do
130 Scale->setValidator(scaleValidator);
131 Height->setValidator(unsignedLengthAutoValidator(Height));
132 Width->setValidator(unsignedLengthAutoValidator(Width));
133 angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
136 connect(clip, SIGNAL(stateChanged(int)),
137 this, SLOT(change_adaptor()));
138 connect(lbY, SIGNAL(textChanged(const QString&)),
139 this, SLOT(change_bb()));
140 connect(lbYunit, SIGNAL(activated(int)),
141 this, SLOT(change_bb()));
142 connect(rtY, SIGNAL(textChanged(const QString&)),
143 this, SLOT(change_bb()));
144 connect(rtYunit, SIGNAL(activated(int)),
145 this, SLOT(change_bb()));
146 connect(lbX, SIGNAL(textChanged(const QString&)),
147 this, SLOT(change_bb()));
148 connect(lbXunit, SIGNAL(activated(int)),
149 this, SLOT(change_bb()));
150 connect(rtX, SIGNAL(textChanged(const QString&)),
151 this, SLOT(change_bb()));
152 connect(rtXunit, SIGNAL(activated(int)),
153 this, SLOT(change_bb()));
154 connect(getPB, SIGNAL(clicked()),
155 this, SLOT(change_adaptor()));
157 lbX->setValidator(new QDoubleValidator(lbX));
158 lbY->setValidator(new QDoubleValidator(lbY));
159 rtX->setValidator(new QDoubleValidator(rtX));
160 rtY->setValidator(new QDoubleValidator(rtY));
163 connect(latexoptions, SIGNAL(textChanged(const QString&)),
164 this, SLOT(change_adaptor()));
165 connect(draftCB, SIGNAL(stateChanged(int)),
166 this, SLOT(change_adaptor()));
167 connect(unzipCB, SIGNAL(stateChanged(int)),
168 this, SLOT(change_adaptor()));
169 // FIXME: we should connect to clicked() when we move to Qt 4.2 because
170 // the toggled(bool) signal is also trigged when we update the widgets
171 // (rgh-4/07) this isn't as much or a problem as it was, because we're now
172 // using blockSignals() to keep from triggering that signal when we call
173 // setChecked(). Note, too, that clicked() would get called whenever it
174 // is clicked, even right clicked (I think), not just whenever it is
176 connect(subfigure, SIGNAL(toggled(bool)),
177 this, SLOT(change_adaptor()));
178 connect(subcaption, SIGNAL(textChanged(const QString&)),
179 this, SLOT(change_adaptor()));
180 connect(displayGB, SIGNAL(toggled(bool)),
181 this, SLOT(change_adaptor()));
182 connect(showCB, SIGNAL(currentIndexChanged(int)),
183 this, SLOT(change_adaptor()));
184 connect(displayscale, SIGNAL(textChanged(const QString&)),
185 this, SLOT(change_adaptor()));
186 displayscale->setValidator(new QIntValidator(displayscale));
188 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
190 bc().setApply(applyPB);
191 bc().setRestore(restorePB);
192 bc().setCancel(closePB);
194 bc().addReadOnly(latexoptions);
195 bc().addReadOnly(subfigure);
196 bc().addReadOnly(filenameL);
197 bc().addReadOnly(filename);
198 bc().addReadOnly(browsePB);
199 bc().addReadOnly(unzipCB);
200 bc().addReadOnly(bbFrame);
201 bc().addReadOnly(draftCB);
202 bc().addReadOnly(clip);
203 bc().addReadOnly(unzipCB);
204 bc().addReadOnly(displayGB);
205 bc().addReadOnly(sizeGB);
206 bc().addReadOnly(rotationGB);
207 bc().addReadOnly(latexoptions);
208 bc().addReadOnly(getPB);
209 bc().addReadOnly(rotateOrderCB);
211 // initialize the length validator
212 bc().addCheckedLineEdit(Scale, scaleCB);
213 bc().addCheckedLineEdit(Width, WidthCB);
214 bc().addCheckedLineEdit(Height, HeightCB);
215 bc().addCheckedLineEdit(displayscale, scaleLA);
216 bc().addCheckedLineEdit(angle, angleL);
217 bc().addCheckedLineEdit(lbX, xL);
218 bc().addCheckedLineEdit(lbY, yL);
219 bc().addCheckedLineEdit(rtX, xL_2);
220 bc().addCheckedLineEdit(rtY, yL_2);
221 bc().addCheckedLineEdit(filename, filenameL);
225 ControlGraphics & GuiGraphicsDialog::controller()
227 return static_cast<ControlGraphics &>(GuiDialog::controller());
231 void GuiGraphicsDialog::change_adaptor()
237 void GuiGraphicsDialog::change_bb()
239 controller().bbChanged = true;
240 LYXERR(Debug::GRAPHICS)
241 << "[controller().bb_Changed set to true]\n";
246 void GuiGraphicsDialog::closeEvent(QCloseEvent * e)
253 void GuiGraphicsDialog::on_browsePB_clicked()
255 docstring const str =
256 controller().browse(qstring_to_ucs4(filename->text()));
258 filename->setText(toqstr(str));
264 void GuiGraphicsDialog::on_getPB_clicked()
270 void GuiGraphicsDialog::on_editPB_clicked()
272 controller().editGraphics();
276 void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
278 editPB->setDisabled(filename.isEmpty());
282 void GuiGraphicsDialog::setAutoText()
284 if (scaleCB->isChecked())
286 if (!Scale->isEnabled() && Scale->text() != "100")
287 Scale->setText(QString("auto"));
289 setAutoTextCB(WidthCB, Width, widthUnit);
290 setAutoTextCB(HeightCB, Height, heightUnit);
294 void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
296 Scale->setEnabled(setScale);
298 Scale->setText("100");
299 Scale->setFocus(Qt::OtherFocusReason);
302 WidthCB->setDisabled(setScale);
303 WidthCB->blockSignals(true);
304 WidthCB->setChecked(false);
305 WidthCB->blockSignals(false);
306 Width->setEnabled(false);
307 widthUnit->setEnabled(false);
309 HeightCB->setDisabled(setScale);
310 HeightCB->blockSignals(true);
311 HeightCB->setChecked(false);
312 HeightCB->blockSignals(false);
313 Height->setEnabled(false);
314 heightUnit->setEnabled(false);
316 aspectratio->setDisabled(true);
317 aspectratio->setChecked(true);
319 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
320 HeightCB->isChecked() ||
321 scaleCB->isChecked()) &&
322 (angle->text() != "0"));
328 void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
330 Width->setEnabled(setWidth);
331 widthUnit->setEnabled(setWidth);
333 Width->setFocus(Qt::OtherFocusReason);
335 bool const setHeight = HeightCB->isChecked();
336 aspectratio->setEnabled(setWidth && setHeight);
337 aspectratio->blockSignals(true);
338 aspectratio->setChecked(!(setWidth && setHeight));
339 aspectratio->blockSignals(false);
341 scaleCB->setEnabled(!setWidth && !setHeight);
342 //already will be unchecked, so don't need to do that
343 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
344 && scaleCB->isChecked()); //should be false, but let's check
345 rotateOrderCB->setEnabled((setWidth || setHeight ||
346 scaleCB->isChecked()) &&
347 (angle->text() != "0"));
353 void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
355 Height->setEnabled(setHeight);
356 heightUnit->setEnabled(setHeight);
358 Height->setFocus(Qt::OtherFocusReason);
360 bool const setWidth = WidthCB->isChecked();
361 aspectratio->setEnabled(setWidth && setHeight);
362 aspectratio->blockSignals(true);
363 aspectratio->setChecked(!(setWidth && setHeight));
364 aspectratio->blockSignals(false);
366 scaleCB->setEnabled(!setWidth && !setHeight);
368 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
369 && scaleCB->isChecked()); //should be false
370 rotateOrderCB->setEnabled((setWidth || setHeight ||
371 scaleCB->isChecked()) &&
372 (angle->text() != "0"));
378 void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
380 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
381 HeightCB->isChecked() ||
382 scaleCB->isChecked()) &&
386 // returns the number of the string s in the vector v
387 static int getItemNo(const vector<string> & v, string const & s)
389 vector<string>::const_iterator cit =
390 find(v.begin(), v.end(), s);
391 return (cit != v.end()) ? int(cit - v.begin()) : 0;
395 void GuiGraphicsDialog::updateContents()
397 // clear and fill in the comboboxes
398 vector<string> const bb_units = frontend::getBBUnits();
403 for (vector<string>::const_iterator it = bb_units.begin();
404 it != bb_units.end(); ++it) {
405 lbXunit->addItem(toqstr(*it));
406 lbYunit->addItem(toqstr(*it));
407 rtXunit->addItem(toqstr(*it));
408 rtYunit->addItem(toqstr(*it));
411 InsetGraphicsParams & igp = controller().params();
413 // set the right default unit
414 Length::UNIT unitDefault = Length::CM;
415 switch (lyxrc.default_papersize) {
418 case PAPER_USEXECUTIVE:
419 unitDefault = Length::IN;
426 igp.filename.outputFilename(controller().bufferFilepath());
427 filename->setText(toqstr(name));
429 // set the bounding box values
430 if (igp.bb.empty()) {
431 string const bb = controller().readBB(igp.filename.absFilename());
432 // the values from the file always have the bigpoint-unit bp
433 lbX->setText(toqstr(token(bb, ' ', 0)));
434 lbY->setText(toqstr(token(bb, ' ', 1)));
435 rtX->setText(toqstr(token(bb, ' ', 2)));
436 rtY->setText(toqstr(token(bb, ' ', 3)));
437 lbXunit->setCurrentIndex(0);
438 lbYunit->setCurrentIndex(0);
439 rtXunit->setCurrentIndex(0);
440 rtYunit->setCurrentIndex(0);
441 controller().bbChanged = false;
443 // get the values from the inset
445 string const xl(token(igp.bb, ' ', 0));
446 string const yl(token(igp.bb, ' ', 1));
447 string const xr(token(igp.bb, ' ', 2));
448 string const yr(token(igp.bb, ' ', 3));
449 if (isValidLength(xl, &anyLength)) {
450 lbX->setText(toqstr(convert<string>(anyLength.value())));
451 string const unit(unit_name[anyLength.unit()]);
452 lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
454 lbX->setText(toqstr(xl));
456 if (isValidLength(yl, &anyLength)) {
457 lbY->setText(toqstr(convert<string>(anyLength.value())));
458 string const unit(unit_name[anyLength.unit()]);
459 lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
461 lbY->setText(toqstr(xl));
463 if (isValidLength(xr, &anyLength)) {
464 rtX->setText(toqstr(convert<string>(anyLength.value())));
465 string const unit(unit_name[anyLength.unit()]);
466 rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
468 rtX->setText(toqstr(xl));
470 if (isValidLength(yr, &anyLength)) {
471 rtY->setText(toqstr(convert<string>(anyLength.value())));
472 string const unit(unit_name[anyLength.unit()]);
473 rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
475 rtY->setText(toqstr(xl));
477 controller().bbChanged = true;
480 // Update the draft and clip mode
481 draftCB->setChecked(igp.draft);
482 clip->setChecked(igp.clip);
483 unzipCB->setChecked(igp.noUnzip);
485 // Update the subcaption check button and input field
486 subfigure->setChecked(igp.subcaption);
487 subcaption->setText(toqstr(igp.subcaptionText));
490 switch (igp.display) {
491 case graphics::DefaultDisplay: item = 0; break;
492 case graphics::MonochromeDisplay: item = 1; break;
493 case graphics::GrayscaleDisplay: item = 2; break;
494 case graphics::ColorDisplay: item = 3; break;
495 case graphics::NoDisplay: item = 0; break;
497 showCB->setCurrentIndex(item);
498 displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
499 displayGB->setChecked(igp.display != graphics::NoDisplay);
501 // the output section (width/height)
503 Scale->setText(toqstr(igp.scale));
504 //igp.scale defaults to 100, so we treat it as empty
505 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
506 scaleCB->blockSignals(true);
507 scaleCB->setChecked(scaleChecked);
508 scaleCB->blockSignals(false);
509 Scale->setEnabled(scaleChecked);
511 lengthAutoToWidgets(Width, widthUnit, igp.width,
513 bool const widthChecked = !Width->text().isEmpty() &&
514 Width->text() != "auto";
515 WidthCB->blockSignals(true);
516 WidthCB->setChecked(widthChecked);
517 WidthCB->blockSignals(false);
518 Width->setEnabled(widthChecked);
519 widthUnit->setEnabled(widthChecked);
521 lengthAutoToWidgets(Height, heightUnit, igp.height,
523 bool const heightChecked = !Height->text().isEmpty()
524 && Height->text() != "auto";
525 HeightCB->blockSignals(true);
526 HeightCB->setChecked(heightChecked);
527 HeightCB->blockSignals(false);
528 Height->setEnabled(heightChecked);
529 heightUnit->setEnabled(heightChecked);
531 scaleCB->setEnabled(!widthChecked && !heightChecked);
532 WidthCB->setEnabled(!scaleChecked);
533 HeightCB->setEnabled(!scaleChecked);
534 aspectratio->setEnabled(widthChecked && heightChecked);
538 angle->setText(toqstr(igp.rotateAngle));
539 rotateOrderCB->setChecked(igp.scaleBeforeRotation);
541 rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
542 && igp.rotateAngle != "0");
546 vector<RotationOriginPair> origindata = getRotationOriginData();
547 vector<docstring> const origin_lang = getFirst(origindata);
548 origin_ltx = getSecond(origindata);
550 for (vector<docstring>::const_iterator it = origin_lang.begin();
551 it != origin_lang.end(); ++it)
552 origin->addItem(toqstr(*it));
554 if (!igp.rotateOrigin.empty())
555 origin->setCurrentIndex(
556 getItemNo(origin_ltx, igp.rotateOrigin));
558 origin->setCurrentIndex(0);
560 // disable edit button when no filename is present
561 editPB->setDisabled(filename->text().isEmpty());
564 latexoptions->setText(toqstr(igp.special));
568 void GuiGraphicsDialog::applyView()
570 InsetGraphicsParams & igp = controller().params();
572 igp.filename.set(internal_path(fromqstr(filename->text())),
573 controller().bufferFilepath());
577 if (controller().bbChanged) {
579 string lbXs = fromqstr(lbX->text());
580 string lbYs = fromqstr(lbY->text());
581 string rtXs = fromqstr(rtX->text());
582 string rtYs = fromqstr(rtY->text());
584 convert<int>(lbXs) + convert<int>(lbYs) +
585 convert<int>(rtXs) + convert<int>(rtXs);
590 bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
594 bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
598 bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
602 bb += (rtYs + fromqstr(rtYunit->currentText()));
607 igp.draft = draftCB->isChecked();
608 igp.clip = clip->isChecked();
609 igp.subcaption = subfigure->isChecked();
610 igp.subcaptionText = fromqstr(subcaption->text());
612 switch (showCB->currentIndex()) {
613 case 0: igp.display = graphics::DefaultDisplay; break;
614 case 1: igp.display = graphics::MonochromeDisplay; break;
615 case 2: igp.display = graphics::GrayscaleDisplay; break;
616 case 3: igp.display = graphics::ColorDisplay; break;
620 if (!displayGB->isChecked())
621 igp.display = graphics::NoDisplay;
623 //the graphics section
624 if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
625 igp.scale = fromqstr(Scale->text());
626 igp.width = Length("0pt");
627 igp.height = Length("0pt");
628 igp.keepAspectRatio = false;
630 igp.scale = string();
631 igp.width = WidthCB->isChecked() ?
632 //Note that this works even if Width is "auto", since in
633 //that case we get "0pt".
634 Length(widgetsToLength(Width, widthUnit)):
636 igp.height = HeightCB->isChecked() ?
637 Length(widgetsToLength(Height, heightUnit)) :
639 igp.keepAspectRatio = aspectratio->isEnabled() &&
640 aspectratio->isChecked() &&
641 igp.width.value() > 0 && igp.height.value() > 0;
644 igp.noUnzip = unzipCB->isChecked();
645 igp.lyxscale = displayscale->text().toInt();
646 igp.rotateAngle = fromqstr(angle->text());
648 double rotAngle = convert<double>(igp.rotateAngle);
649 if (std::abs(rotAngle) > 360.0) {
650 rotAngle -= 360.0 * floor(rotAngle / 360.0);
651 igp.rotateAngle = convert<string>(rotAngle);
654 // save the latex name for the origin. If it is the default
655 // then origin_ltx returns ""
656 igp.rotateOrigin = origin_ltx[origin->currentIndex()];
657 igp.scaleBeforeRotation = rotateOrderCB->isChecked();
659 // more latex options
660 igp.special = fromqstr(latexoptions->text());
664 void GuiGraphicsDialog::getBB()
666 string const fn = fromqstr(filename->text());
668 string const bb = controller().readBB(fn);
670 lbX->setText(toqstr(token(bb, ' ', 0)));
671 lbY->setText(toqstr(token(bb, ' ', 1)));
672 rtX->setText(toqstr(token(bb, ' ', 2)));
673 rtY->setText(toqstr(token(bb, ' ', 3)));
674 // the default units for the bb values when reading
676 lbXunit->setCurrentIndex(0);
677 lbYunit->setCurrentIndex(0);
678 rtXunit->setCurrentIndex(0);
679 rtYunit->setCurrentIndex(0);
681 controller().bbChanged = false;
686 bool GuiGraphicsDialog::isValid()
688 return !filename->text().isEmpty();
691 } // namespace frontend
695 #include "GuiGraphics_moc.cpp"