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>
46 using lyx::support::float_equal;
47 using lyx::support::token;
49 using lyx::support::os::internal_path;
51 #ifndef CXX_GLOBAL_CSTD
63 //FIXME setAutoTextCB should really take an argument, as indicated, that
64 //determines what text is to be written for "auto". But making
65 //that work involves more extensive revisions than we now want
66 //to make, since "auto" also appears in updateContents() (see
68 //The right way to do this, I think, would be to define a class
69 //checkedLengthSet (and a partnering labeledLengthSete) that encapsulated
70 //the checkbox, line edit, and length combo together, and then made e.g.
71 //lengthToWidgets, widgetsToLength, etc, all public methods of that class.
72 //Perhaps even the validator could be exposed through it.
74 * sets a checkbox-line edit-length combo group, using "text" if the
75 * checkbox is unchecked and clearing the line edit if it previously
78 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
79 LengthCombo * lengthCombo/*, string text = "auto"*/)
81 if (!checkBox->isChecked())
82 lengthToWidgets(lineEdit, lengthCombo,
83 "auto", lengthCombo->currentLengthItem());
84 else if (lineEdit->text() == "auto")
85 lengthToWidgets(lineEdit, lengthCombo, string(),
86 lengthCombo->currentLengthItem());
92 std::vector<typename Pair::first_type> const
93 getFirst(std::vector<Pair> const & pr)
95 std::vector<typename Pair::first_type> tmp(pr.size());
96 std::transform(pr.begin(), pr.end(), tmp.begin(),
97 boost::bind(&Pair::first, _1));
103 std::vector<typename Pair::second_type> const
104 getSecond(std::vector<Pair> const & pr)
106 std::vector<typename Pair::second_type> tmp(pr.size());
107 std::transform(pr.begin(), pr.end(), tmp.begin(),
108 boost::bind(&Pair::second, _1));
112 GuiGraphicsDialog::GuiGraphicsDialog(LyXView & lv)
113 : GuiDialog(lv, "graphics")
116 setViewTitle(_("Graphics"));
117 setController(new ControlGraphics(*this));
120 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
121 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
122 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
123 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
126 connect(filename, SIGNAL(textChanged(const QString &)),
127 this, SLOT(change_adaptor()));
128 connect(WidthCB, SIGNAL( clicked()),
129 this, SLOT(change_adaptor()));
130 connect(HeightCB, SIGNAL( clicked()),
131 this, SLOT(change_adaptor()));
132 connect(Width, SIGNAL(textChanged(const QString &)),
133 this, SLOT(change_adaptor()));
134 connect(Height, SIGNAL(textChanged(const QString &)),
135 this, SLOT(change_adaptor()));
136 connect(heightUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
137 this, SLOT(change_adaptor()));
138 connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
139 this, SLOT(change_adaptor()));
140 connect(aspectratio, SIGNAL(stateChanged(int)),
141 this, SLOT(change_adaptor()));
142 connect(angle, SIGNAL(textChanged(const QString &)),
143 this, SLOT(change_adaptor()));
144 connect(origin, SIGNAL(activated(int)),
145 this, SLOT(change_adaptor()));
146 connect(scaleCB, SIGNAL(clicked()),
147 this, SLOT(change_adaptor()));
148 connect(Scale, SIGNAL(textChanged(const QString &)),
149 this, SLOT(change_adaptor()));
150 connect(rotateOrderCB, SIGNAL(clicked()),
151 this, SLOT(change_adaptor()));
153 filename->setValidator(new PathValidator(true, filename));
154 setFocusProxy(filename);
156 QDoubleValidator * scaleValidator = new DoubleAutoValidator(Scale);
157 scaleValidator->setBottom(0);
158 scaleValidator->setDecimals(256); //I guess that will do
159 Scale->setValidator(scaleValidator);
160 Height->setValidator(unsignedLengthAutoValidator(Height));
161 Width->setValidator(unsignedLengthAutoValidator(Width));
162 angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
165 connect(clip, SIGNAL(stateChanged(int)),
166 this, SLOT(change_adaptor()));
167 connect(lbY, SIGNAL(textChanged(const QString&)),
168 this, SLOT(change_bb()));
169 connect(lbYunit, SIGNAL(activated(int)),
170 this, SLOT(change_bb()));
171 connect(rtY, SIGNAL(textChanged(const QString&)),
172 this, SLOT(change_bb()));
173 connect(rtYunit, SIGNAL(activated(int)),
174 this, SLOT(change_bb()));
175 connect(lbX, SIGNAL(textChanged(const QString&)),
176 this, SLOT(change_bb()));
177 connect(lbXunit, SIGNAL(activated(int)),
178 this, SLOT(change_bb()));
179 connect(rtX, SIGNAL(textChanged(const QString&)),
180 this, SLOT(change_bb()));
181 connect(rtXunit, SIGNAL(activated(int)),
182 this, SLOT(change_bb()));
183 connect(getPB, SIGNAL(clicked()),
184 this, SLOT(change_adaptor()));
186 lbX->setValidator(new QDoubleValidator(lbX));
187 lbY->setValidator(new QDoubleValidator(lbY));
188 rtX->setValidator(new QDoubleValidator(rtX));
189 rtY->setValidator(new QDoubleValidator(rtY));
192 connect(latexoptions, SIGNAL(textChanged(const QString&)),
193 this, SLOT(change_adaptor()));
194 connect(draftCB, SIGNAL(stateChanged(int)),
195 this, SLOT(change_adaptor()));
196 connect(unzipCB, SIGNAL(stateChanged(int)),
197 this, SLOT(change_adaptor()));
198 // FIXME: we should connect to clicked() when we move to Qt 4.2 because
199 // the toggled(bool) signal is also trigged when we update the widgets
200 // (rgh-4/07) this isn't as much or a problem as it was, because we're now
201 // using blockSignals() to keep from triggering that signal when we call
202 // setChecked(). Note, too, that clicked() would get called whenever it
203 // is clicked, even right clicked (I think), not just whenever it is
205 connect(subfigure, SIGNAL(toggled(bool)),
206 this, SLOT(change_adaptor()));
207 connect(subcaption, SIGNAL(textChanged(const QString&)),
208 this, SLOT(change_adaptor()));
209 connect(displayGB, SIGNAL(toggled(bool)),
210 this, SLOT(change_adaptor()));
211 connect(showCB, SIGNAL(currentIndexChanged(int)),
212 this, SLOT(change_adaptor()));
213 connect(displayscale, SIGNAL(textChanged(const QString&)),
214 this, SLOT(change_adaptor()));
215 displayscale->setValidator(new QIntValidator(displayscale));
217 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
219 bc().setApply(applyPB);
220 bc().setRestore(restorePB);
221 bc().setCancel(closePB);
223 bc().addReadOnly(latexoptions);
224 bc().addReadOnly(subfigure);
225 bc().addReadOnly(filenameL);
226 bc().addReadOnly(filename);
227 bc().addReadOnly(browsePB);
228 bc().addReadOnly(unzipCB);
229 bc().addReadOnly(bbFrame);
230 bc().addReadOnly(draftCB);
231 bc().addReadOnly(clip);
232 bc().addReadOnly(unzipCB);
233 bc().addReadOnly(displayGB);
234 bc().addReadOnly(sizeGB);
235 bc().addReadOnly(rotationGB);
236 bc().addReadOnly(latexoptions);
237 bc().addReadOnly(getPB);
238 bc().addReadOnly(rotateOrderCB);
240 // initialize the length validator
241 bc().addCheckedLineEdit(Scale, scaleCB);
242 bc().addCheckedLineEdit(Width, WidthCB);
243 bc().addCheckedLineEdit(Height, HeightCB);
244 bc().addCheckedLineEdit(displayscale, scaleLA);
245 bc().addCheckedLineEdit(angle, angleL);
246 bc().addCheckedLineEdit(lbX, xL);
247 bc().addCheckedLineEdit(lbY, yL);
248 bc().addCheckedLineEdit(rtX, xL_2);
249 bc().addCheckedLineEdit(rtY, yL_2);
250 bc().addCheckedLineEdit(filename, filenameL);
254 ControlGraphics & GuiGraphicsDialog::controller()
256 return static_cast<ControlGraphics &>(GuiDialog::controller());
260 void GuiGraphicsDialog::change_adaptor()
266 void GuiGraphicsDialog::change_bb()
268 controller().bbChanged = true;
269 LYXERR(Debug::GRAPHICS)
270 << "[controller().bb_Changed set to true]\n";
275 void GuiGraphicsDialog::closeEvent(QCloseEvent * e)
282 void GuiGraphicsDialog::on_browsePB_clicked()
284 docstring const str =
285 controller().browse(qstring_to_ucs4(filename->text()));
287 filename->setText(toqstr(str));
293 void GuiGraphicsDialog::on_getPB_clicked()
299 void GuiGraphicsDialog::on_editPB_clicked()
301 controller().editGraphics();
305 void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
307 editPB->setDisabled(filename.isEmpty());
311 void GuiGraphicsDialog::setAutoText()
313 if (scaleCB->isChecked())
315 if (!Scale->isEnabled() && Scale->text() != "100")
316 Scale->setText(QString("auto"));
318 setAutoTextCB(WidthCB, Width, widthUnit);
319 setAutoTextCB(HeightCB, Height, heightUnit);
323 void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
325 Scale->setEnabled(setScale);
327 Scale->setText("100");
328 Scale->setFocus(Qt::OtherFocusReason);
331 WidthCB->setDisabled(setScale);
332 WidthCB->blockSignals(true);
333 WidthCB->setChecked(false);
334 WidthCB->blockSignals(false);
335 Width->setEnabled(false);
336 widthUnit->setEnabled(false);
338 HeightCB->setDisabled(setScale);
339 HeightCB->blockSignals(true);
340 HeightCB->setChecked(false);
341 HeightCB->blockSignals(false);
342 Height->setEnabled(false);
343 heightUnit->setEnabled(false);
345 aspectratio->setDisabled(true);
346 aspectratio->setChecked(true);
348 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
349 HeightCB->isChecked() ||
350 scaleCB->isChecked()) &&
351 (angle->text() != "0"));
357 void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
359 Width->setEnabled(setWidth);
360 widthUnit->setEnabled(setWidth);
362 Width->setFocus(Qt::OtherFocusReason);
364 bool const setHeight = HeightCB->isChecked();
365 aspectratio->setEnabled(setWidth && setHeight);
366 aspectratio->blockSignals(true);
367 aspectratio->setChecked(!(setWidth && setHeight));
368 aspectratio->blockSignals(false);
370 scaleCB->setEnabled(!setWidth && !setHeight);
371 //already will be unchecked, so don't need to do that
372 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
373 && scaleCB->isChecked()); //should be false, but let's check
374 rotateOrderCB->setEnabled((setWidth || setHeight ||
375 scaleCB->isChecked()) &&
376 (angle->text() != "0"));
382 void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
384 Height->setEnabled(setHeight);
385 heightUnit->setEnabled(setHeight);
387 Height->setFocus(Qt::OtherFocusReason);
389 bool const setWidth = WidthCB->isChecked();
390 aspectratio->setEnabled(setWidth && setHeight);
391 aspectratio->blockSignals(true);
392 aspectratio->setChecked(!(setWidth && setHeight));
393 aspectratio->blockSignals(false);
395 scaleCB->setEnabled(!setWidth && !setHeight);
397 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
398 && scaleCB->isChecked()); //should be false
399 rotateOrderCB->setEnabled((setWidth || setHeight ||
400 scaleCB->isChecked()) &&
401 (angle->text() != "0"));
407 void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
409 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
410 HeightCB->isChecked() ||
411 scaleCB->isChecked()) &&
415 // returns the number of the string s in the vector v
416 static int getItemNo(const vector<string> & v, string const & s)
418 vector<string>::const_iterator cit =
419 find(v.begin(), v.end(), s);
420 return (cit != v.end()) ? int(cit - v.begin()) : 0;
424 void GuiGraphicsDialog::updateContents()
426 // clear and fill in the comboboxes
427 vector<string> const bb_units = frontend::getBBUnits();
432 for (vector<string>::const_iterator it = bb_units.begin();
433 it != bb_units.end(); ++it) {
434 lbXunit->addItem(toqstr(*it));
435 lbYunit->addItem(toqstr(*it));
436 rtXunit->addItem(toqstr(*it));
437 rtYunit->addItem(toqstr(*it));
440 InsetGraphicsParams & igp = controller().params();
442 // set the right default unit
443 Length::UNIT unitDefault = Length::CM;
444 switch (lyxrc.default_papersize) {
447 case PAPER_USEXECUTIVE:
448 unitDefault = Length::IN;
455 igp.filename.outputFilename(controller().bufferFilepath());
456 filename->setText(toqstr(name));
458 // set the bounding box values
459 if (igp.bb.empty()) {
460 string const bb = controller().readBB(igp.filename.absFilename());
461 // the values from the file always have the bigpoint-unit bp
462 lbX->setText(toqstr(token(bb, ' ', 0)));
463 lbY->setText(toqstr(token(bb, ' ', 1)));
464 rtX->setText(toqstr(token(bb, ' ', 2)));
465 rtY->setText(toqstr(token(bb, ' ', 3)));
466 lbXunit->setCurrentIndex(0);
467 lbYunit->setCurrentIndex(0);
468 rtXunit->setCurrentIndex(0);
469 rtYunit->setCurrentIndex(0);
470 controller().bbChanged = false;
472 // get the values from the inset
474 string const xl(token(igp.bb, ' ', 0));
475 string const yl(token(igp.bb, ' ', 1));
476 string const xr(token(igp.bb, ' ', 2));
477 string const yr(token(igp.bb, ' ', 3));
478 if (isValidLength(xl, &anyLength)) {
479 lbX->setText(toqstr(convert<string>(anyLength.value())));
480 string const unit(unit_name[anyLength.unit()]);
481 lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
483 lbX->setText(toqstr(xl));
485 if (isValidLength(yl, &anyLength)) {
486 lbY->setText(toqstr(convert<string>(anyLength.value())));
487 string const unit(unit_name[anyLength.unit()]);
488 lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
490 lbY->setText(toqstr(xl));
492 if (isValidLength(xr, &anyLength)) {
493 rtX->setText(toqstr(convert<string>(anyLength.value())));
494 string const unit(unit_name[anyLength.unit()]);
495 rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
497 rtX->setText(toqstr(xl));
499 if (isValidLength(yr, &anyLength)) {
500 rtY->setText(toqstr(convert<string>(anyLength.value())));
501 string const unit(unit_name[anyLength.unit()]);
502 rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
504 rtY->setText(toqstr(xl));
506 controller().bbChanged = true;
509 // Update the draft and clip mode
510 draftCB->setChecked(igp.draft);
511 clip->setChecked(igp.clip);
512 unzipCB->setChecked(igp.noUnzip);
514 // Update the subcaption check button and input field
515 subfigure->setChecked(igp.subcaption);
516 subcaption->setText(toqstr(igp.subcaptionText));
519 switch (igp.display) {
520 case graphics::DefaultDisplay: item = 0; break;
521 case graphics::MonochromeDisplay: item = 1; break;
522 case graphics::GrayscaleDisplay: item = 2; break;
523 case graphics::ColorDisplay: item = 3; break;
524 case graphics::NoDisplay: item = 0; break;
526 showCB->setCurrentIndex(item);
527 displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
528 displayGB->setChecked(igp.display != graphics::NoDisplay);
530 // the output section (width/height)
532 Scale->setText(toqstr(igp.scale));
533 //igp.scale defaults to 100, so we treat it as empty
534 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
535 scaleCB->blockSignals(true);
536 scaleCB->setChecked(scaleChecked);
537 scaleCB->blockSignals(false);
538 Scale->setEnabled(scaleChecked);
540 lengthAutoToWidgets(Width, widthUnit, igp.width,
542 bool const widthChecked = !Width->text().isEmpty() &&
543 Width->text() != "auto";
544 WidthCB->blockSignals(true);
545 WidthCB->setChecked(widthChecked);
546 WidthCB->blockSignals(false);
547 Width->setEnabled(widthChecked);
548 widthUnit->setEnabled(widthChecked);
550 lengthAutoToWidgets(Height, heightUnit, igp.height,
552 bool const heightChecked = !Height->text().isEmpty()
553 && Height->text() != "auto";
554 HeightCB->blockSignals(true);
555 HeightCB->setChecked(heightChecked);
556 HeightCB->blockSignals(false);
557 Height->setEnabled(heightChecked);
558 heightUnit->setEnabled(heightChecked);
560 scaleCB->setEnabled(!widthChecked && !heightChecked);
561 WidthCB->setEnabled(!scaleChecked);
562 HeightCB->setEnabled(!scaleChecked);
563 aspectratio->setEnabled(widthChecked && heightChecked);
567 angle->setText(toqstr(igp.rotateAngle));
568 rotateOrderCB->setChecked(igp.scaleBeforeRotation);
570 rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
571 && igp.rotateAngle != "0");
575 vector<RotationOriginPair> origindata = getRotationOriginData();
576 vector<docstring> const origin_lang = getFirst(origindata);
577 origin_ltx = getSecond(origindata);
579 for (vector<docstring>::const_iterator it = origin_lang.begin();
580 it != origin_lang.end(); ++it)
581 origin->addItem(toqstr(*it));
583 if (!igp.rotateOrigin.empty())
584 origin->setCurrentIndex(
585 getItemNo(origin_ltx, igp.rotateOrigin));
587 origin->setCurrentIndex(0);
589 // disable edit button when no filename is present
590 editPB->setDisabled(filename->text().isEmpty());
593 latexoptions->setText(toqstr(igp.special));
597 void GuiGraphicsDialog::applyView()
599 InsetGraphicsParams & igp = controller().params();
601 igp.filename.set(internal_path(fromqstr(filename->text())),
602 controller().bufferFilepath());
606 if (controller().bbChanged) {
608 string lbXs = fromqstr(lbX->text());
609 string lbYs = fromqstr(lbY->text());
610 string rtXs = fromqstr(rtX->text());
611 string rtYs = fromqstr(rtY->text());
613 convert<int>(lbXs) + convert<int>(lbYs) +
614 convert<int>(rtXs) + convert<int>(rtXs);
619 bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
623 bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
627 bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
631 bb += (rtYs + fromqstr(rtYunit->currentText()));
636 igp.draft = draftCB->isChecked();
637 igp.clip = clip->isChecked();
638 igp.subcaption = subfigure->isChecked();
639 igp.subcaptionText = fromqstr(subcaption->text());
641 switch (showCB->currentIndex()) {
642 case 0: igp.display = graphics::DefaultDisplay; break;
643 case 1: igp.display = graphics::MonochromeDisplay; break;
644 case 2: igp.display = graphics::GrayscaleDisplay; break;
645 case 3: igp.display = graphics::ColorDisplay; break;
649 if (!displayGB->isChecked())
650 igp.display = graphics::NoDisplay;
652 //the graphics section
653 if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
654 igp.scale = fromqstr(Scale->text());
655 igp.width = Length("0pt");
656 igp.height = Length("0pt");
657 igp.keepAspectRatio = false;
659 igp.scale = string();
660 igp.width = WidthCB->isChecked() ?
661 //Note that this works even if Width is "auto", since in
662 //that case we get "0pt".
663 Length(widgetsToLength(Width, widthUnit)):
665 igp.height = HeightCB->isChecked() ?
666 Length(widgetsToLength(Height, heightUnit)) :
668 igp.keepAspectRatio = aspectratio->isEnabled() &&
669 aspectratio->isChecked() &&
670 igp.width.value() > 0 && igp.height.value() > 0;
673 igp.noUnzip = unzipCB->isChecked();
674 igp.lyxscale = displayscale->text().toInt();
675 igp.rotateAngle = fromqstr(angle->text());
677 double rotAngle = convert<double>(igp.rotateAngle);
678 if (std::abs(rotAngle) > 360.0) {
679 rotAngle -= 360.0 * floor(rotAngle / 360.0);
680 igp.rotateAngle = convert<string>(rotAngle);
683 // save the latex name for the origin. If it is the default
684 // then origin_ltx returns ""
685 igp.rotateOrigin = origin_ltx[origin->currentIndex()];
686 igp.scaleBeforeRotation = rotateOrderCB->isChecked();
688 // more latex options
689 igp.special = fromqstr(latexoptions->text());
693 void GuiGraphicsDialog::getBB()
695 string const fn = fromqstr(filename->text());
697 string const bb = controller().readBB(fn);
699 lbX->setText(toqstr(token(bb, ' ', 0)));
700 lbY->setText(toqstr(token(bb, ' ', 1)));
701 rtX->setText(toqstr(token(bb, ' ', 2)));
702 rtY->setText(toqstr(token(bb, ' ', 3)));
703 // the default units for the bb values when reading
705 lbXunit->setCurrentIndex(0);
706 lbYunit->setCurrentIndex(0);
707 rtXunit->setCurrentIndex(0);
708 rtYunit->setCurrentIndex(0);
710 controller().bbChanged = false;
715 bool GuiGraphicsDialog::isValid()
717 return !filename->text().isEmpty();
720 } // namespace frontend
724 #include "GuiGraphics_moc.cpp"