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;
53 #ifndef CXX_GLOBAL_CSTD
65 //FIXME setAutoTextCB should really take an argument, as indicated, that
66 //determines what text is to be written for "auto". But making
67 //that work involves more extensive revisions than we now want
68 //to make, since "auto" also appears in updateContents() (see
70 //The right way to do this, I think, would be to define a class
71 //checkedLengthSet (and a partnering labeledLengthSete) that encapsulated
72 //the checkbox, line edit, and length combo together, and then made e.g.
73 //lengthToWidgets, widgetsToLength, etc, all public methods of that class.
74 //Perhaps even the validator could be exposed through it.
76 * sets a checkbox-line edit-length combo group, using "text" if the
77 * checkbox is unchecked and clearing the line edit if it previously
80 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
81 LengthCombo * lengthCombo/*, string text = "auto"*/)
83 if (!checkBox->isChecked())
84 lengthToWidgets(lineEdit, lengthCombo,
85 "auto", lengthCombo->currentLengthItem());
86 else if (lineEdit->text() == "auto")
87 lengthToWidgets(lineEdit, lengthCombo, string(),
88 lengthCombo->currentLengthItem());
94 vector<typename Pair::first_type> const
95 getFirst(vector<Pair> const & pr)
97 vector<typename Pair::first_type> tmp(pr.size());
98 transform(pr.begin(), pr.end(), tmp.begin(),
99 boost::bind(&Pair::first, _1));
105 vector<typename Pair::second_type> const
106 getSecond(vector<Pair> const & pr)
108 vector<typename Pair::second_type> tmp(pr.size());
109 transform(pr.begin(), pr.end(), tmp.begin(),
110 boost::bind(&Pair::second, _1));
114 GuiGraphicsDialog::GuiGraphicsDialog(LyXView & lv)
115 : GuiDialog(lv, "graphics")
118 setViewTitle(_("Graphics"));
119 setController(new ControlGraphics(*this));
122 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
123 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
124 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
125 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
128 connect(filename, SIGNAL(textChanged(const QString &)),
129 this, SLOT(change_adaptor()));
130 connect(WidthCB, SIGNAL( clicked()),
131 this, SLOT(change_adaptor()));
132 connect(HeightCB, SIGNAL( clicked()),
133 this, SLOT(change_adaptor()));
134 connect(Width, SIGNAL(textChanged(const QString &)),
135 this, SLOT(change_adaptor()));
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()));
155 filename->setValidator(new PathValidator(true, filename));
156 setFocusProxy(filename);
158 QDoubleValidator * scaleValidator = new DoubleAutoValidator(Scale);
159 scaleValidator->setBottom(0);
160 scaleValidator->setDecimals(256); //I guess that will do
161 Scale->setValidator(scaleValidator);
162 Height->setValidator(unsignedLengthAutoValidator(Height));
163 Width->setValidator(unsignedLengthAutoValidator(Width));
164 angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
167 connect(clip, SIGNAL(stateChanged(int)),
168 this, SLOT(change_adaptor()));
169 connect(lbY, SIGNAL(textChanged(const QString&)),
170 this, SLOT(change_bb()));
171 connect(lbYunit, SIGNAL(activated(int)),
172 this, SLOT(change_bb()));
173 connect(rtY, SIGNAL(textChanged(const QString&)),
174 this, SLOT(change_bb()));
175 connect(rtYunit, SIGNAL(activated(int)),
176 this, SLOT(change_bb()));
177 connect(lbX, SIGNAL(textChanged(const QString&)),
178 this, SLOT(change_bb()));
179 connect(lbXunit, SIGNAL(activated(int)),
180 this, SLOT(change_bb()));
181 connect(rtX, SIGNAL(textChanged(const QString&)),
182 this, SLOT(change_bb()));
183 connect(rtXunit, SIGNAL(activated(int)),
184 this, SLOT(change_bb()));
185 connect(getPB, SIGNAL(clicked()),
186 this, SLOT(change_adaptor()));
188 lbX->setValidator(new QDoubleValidator(lbX));
189 lbY->setValidator(new QDoubleValidator(lbY));
190 rtX->setValidator(new QDoubleValidator(rtX));
191 rtY->setValidator(new QDoubleValidator(rtY));
194 connect(latexoptions, SIGNAL(textChanged(const QString&)),
195 this, SLOT(change_adaptor()));
196 connect(draftCB, SIGNAL(stateChanged(int)),
197 this, SLOT(change_adaptor()));
198 connect(unzipCB, SIGNAL(stateChanged(int)),
199 this, SLOT(change_adaptor()));
200 // FIXME: we should connect to clicked() when we move to Qt 4.2 because
201 // the toggled(bool) signal is also trigged when we update the widgets
202 // (rgh-4/07) this isn't as much or a problem as it was, because we're now
203 // using blockSignals() to keep from triggering that signal when we call
204 // setChecked(). Note, too, that clicked() would get called whenever it
205 // is clicked, even right clicked (I think), not just whenever it is
207 connect(subfigure, SIGNAL(toggled(bool)),
208 this, SLOT(change_adaptor()));
209 connect(subcaption, SIGNAL(textChanged(const QString&)),
210 this, SLOT(change_adaptor()));
211 connect(displayGB, SIGNAL(toggled(bool)),
212 this, SLOT(change_adaptor()));
213 connect(showCB, SIGNAL(currentIndexChanged(int)),
214 this, SLOT(change_adaptor()));
215 connect(displayscale, SIGNAL(textChanged(const QString&)),
216 this, SLOT(change_adaptor()));
217 displayscale->setValidator(new QIntValidator(displayscale));
219 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
221 bc().setApply(applyPB);
222 bc().setRestore(restorePB);
223 bc().setCancel(closePB);
225 bc().addReadOnly(latexoptions);
226 bc().addReadOnly(subfigure);
227 bc().addReadOnly(filenameL);
228 bc().addReadOnly(filename);
229 bc().addReadOnly(browsePB);
230 bc().addReadOnly(unzipCB);
231 bc().addReadOnly(bbFrame);
232 bc().addReadOnly(draftCB);
233 bc().addReadOnly(clip);
234 bc().addReadOnly(unzipCB);
235 bc().addReadOnly(displayGB);
236 bc().addReadOnly(sizeGB);
237 bc().addReadOnly(rotationGB);
238 bc().addReadOnly(latexoptions);
239 bc().addReadOnly(getPB);
240 bc().addReadOnly(rotateOrderCB);
242 // initialize the length validator
243 bc().addCheckedLineEdit(Scale, scaleCB);
244 bc().addCheckedLineEdit(Width, WidthCB);
245 bc().addCheckedLineEdit(Height, HeightCB);
246 bc().addCheckedLineEdit(displayscale, scaleLA);
247 bc().addCheckedLineEdit(angle, angleL);
248 bc().addCheckedLineEdit(lbX, xL);
249 bc().addCheckedLineEdit(lbY, yL);
250 bc().addCheckedLineEdit(rtX, xL_2);
251 bc().addCheckedLineEdit(rtY, yL_2);
252 bc().addCheckedLineEdit(filename, filenameL);
256 ControlGraphics & GuiGraphicsDialog::controller()
258 return static_cast<ControlGraphics &>(GuiDialog::controller());
262 void GuiGraphicsDialog::change_adaptor()
268 void GuiGraphicsDialog::change_bb()
270 controller().bbChanged = true;
271 LYXERR(Debug::GRAPHICS)
272 << "[controller().bb_Changed set to true]\n";
277 void GuiGraphicsDialog::closeEvent(QCloseEvent * e)
284 void GuiGraphicsDialog::on_browsePB_clicked()
286 docstring const str =
287 controller().browse(qstring_to_ucs4(filename->text()));
289 filename->setText(toqstr(str));
290 embedCB->setCheckState(Qt::Unchecked);
296 void GuiGraphicsDialog::on_getPB_clicked()
302 void GuiGraphicsDialog::on_editPB_clicked()
304 controller().editGraphics();
308 void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
310 editPB->setDisabled(filename.isEmpty());
314 void GuiGraphicsDialog::setAutoText()
316 if (scaleCB->isChecked())
318 if (!Scale->isEnabled() && Scale->text() != "100")
319 Scale->setText(QString("auto"));
321 setAutoTextCB(WidthCB, Width, widthUnit);
322 setAutoTextCB(HeightCB, Height, heightUnit);
326 void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
328 Scale->setEnabled(setScale);
330 Scale->setText("100");
331 Scale->setFocus(Qt::OtherFocusReason);
334 WidthCB->setDisabled(setScale);
335 WidthCB->blockSignals(true);
336 WidthCB->setChecked(false);
337 WidthCB->blockSignals(false);
338 Width->setEnabled(false);
339 widthUnit->setEnabled(false);
341 HeightCB->setDisabled(setScale);
342 HeightCB->blockSignals(true);
343 HeightCB->setChecked(false);
344 HeightCB->blockSignals(false);
345 Height->setEnabled(false);
346 heightUnit->setEnabled(false);
348 aspectratio->setDisabled(true);
349 aspectratio->setChecked(true);
351 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
352 HeightCB->isChecked() ||
353 scaleCB->isChecked()) &&
354 (angle->text() != "0"));
360 void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
362 Width->setEnabled(setWidth);
363 widthUnit->setEnabled(setWidth);
365 Width->setFocus(Qt::OtherFocusReason);
367 bool const setHeight = HeightCB->isChecked();
368 aspectratio->setEnabled(setWidth && setHeight);
369 aspectratio->blockSignals(true);
370 aspectratio->setChecked(!(setWidth && setHeight));
371 aspectratio->blockSignals(false);
373 scaleCB->setEnabled(!setWidth && !setHeight);
374 //already will be unchecked, so don't need to do that
375 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
376 && scaleCB->isChecked()); //should be false, but let's check
377 rotateOrderCB->setEnabled((setWidth || setHeight ||
378 scaleCB->isChecked()) &&
379 (angle->text() != "0"));
385 void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
387 Height->setEnabled(setHeight);
388 heightUnit->setEnabled(setHeight);
390 Height->setFocus(Qt::OtherFocusReason);
392 bool const setWidth = WidthCB->isChecked();
393 aspectratio->setEnabled(setWidth && setHeight);
394 aspectratio->blockSignals(true);
395 aspectratio->setChecked(!(setWidth && setHeight));
396 aspectratio->blockSignals(false);
398 scaleCB->setEnabled(!setWidth && !setHeight);
400 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
401 && scaleCB->isChecked()); //should be false
402 rotateOrderCB->setEnabled((setWidth || setHeight ||
403 scaleCB->isChecked()) &&
404 (angle->text() != "0"));
410 void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
412 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
413 HeightCB->isChecked() ||
414 scaleCB->isChecked()) &&
418 // returns the number of the string s in the vector v
419 static int getItemNo(const vector<string> & v, string const & s)
421 vector<string>::const_iterator cit =
422 find(v.begin(), v.end(), s);
423 return (cit != v.end()) ? int(cit - v.begin()) : 0;
427 void GuiGraphicsDialog::updateContents()
429 // clear and fill in the comboboxes
430 vector<string> const bb_units = frontend::getBBUnits();
435 for (vector<string>::const_iterator it = bb_units.begin();
436 it != bb_units.end(); ++it) {
437 lbXunit->addItem(toqstr(*it));
438 lbYunit->addItem(toqstr(*it));
439 rtXunit->addItem(toqstr(*it));
440 rtYunit->addItem(toqstr(*it));
443 InsetGraphicsParams & igp = controller().params();
445 // set the right default unit
446 Length::UNIT unitDefault = Length::CM;
447 switch (lyxrc.default_papersize) {
450 case PAPER_USEXECUTIVE:
451 unitDefault = Length::IN;
458 igp.filename.outputFilename(controller().bufferFilepath());
459 filename->setText(toqstr(name));
460 embedCB->setCheckState(igp.filename.embedded() ? Qt::Checked : Qt::Unchecked);
462 // set the bounding box values
463 if (igp.bb.empty()) {
464 string const bb = controller().readBB(igp.filename.absFilename());
465 // the values from the file always have the bigpoint-unit bp
466 lbX->setText(toqstr(token(bb, ' ', 0)));
467 lbY->setText(toqstr(token(bb, ' ', 1)));
468 rtX->setText(toqstr(token(bb, ' ', 2)));
469 rtY->setText(toqstr(token(bb, ' ', 3)));
470 lbXunit->setCurrentIndex(0);
471 lbYunit->setCurrentIndex(0);
472 rtXunit->setCurrentIndex(0);
473 rtYunit->setCurrentIndex(0);
474 controller().bbChanged = false;
476 // get the values from the inset
478 string const xl(token(igp.bb, ' ', 0));
479 string const yl(token(igp.bb, ' ', 1));
480 string const xr(token(igp.bb, ' ', 2));
481 string const yr(token(igp.bb, ' ', 3));
482 if (isValidLength(xl, &anyLength)) {
483 lbX->setText(toqstr(convert<string>(anyLength.value())));
484 string const unit(unit_name[anyLength.unit()]);
485 lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
487 lbX->setText(toqstr(xl));
489 if (isValidLength(yl, &anyLength)) {
490 lbY->setText(toqstr(convert<string>(anyLength.value())));
491 string const unit(unit_name[anyLength.unit()]);
492 lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
494 lbY->setText(toqstr(xl));
496 if (isValidLength(xr, &anyLength)) {
497 rtX->setText(toqstr(convert<string>(anyLength.value())));
498 string const unit(unit_name[anyLength.unit()]);
499 rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
501 rtX->setText(toqstr(xl));
503 if (isValidLength(yr, &anyLength)) {
504 rtY->setText(toqstr(convert<string>(anyLength.value())));
505 string const unit(unit_name[anyLength.unit()]);
506 rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
508 rtY->setText(toqstr(xl));
510 controller().bbChanged = true;
513 // Update the draft and clip mode
514 draftCB->setChecked(igp.draft);
515 clip->setChecked(igp.clip);
516 unzipCB->setChecked(igp.noUnzip);
518 // Update the subcaption check button and input field
519 subfigure->setChecked(igp.subcaption);
520 subcaption->setText(toqstr(igp.subcaptionText));
523 switch (igp.display) {
524 case graphics::DefaultDisplay: item = 0; break;
525 case graphics::MonochromeDisplay: item = 1; break;
526 case graphics::GrayscaleDisplay: item = 2; break;
527 case graphics::ColorDisplay: item = 3; break;
528 case graphics::NoDisplay: item = 0; break;
530 showCB->setCurrentIndex(item);
531 displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
532 displayGB->setChecked(igp.display != graphics::NoDisplay);
534 // the output section (width/height)
536 Scale->setText(toqstr(igp.scale));
537 //igp.scale defaults to 100, so we treat it as empty
538 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
539 scaleCB->blockSignals(true);
540 scaleCB->setChecked(scaleChecked);
541 scaleCB->blockSignals(false);
542 Scale->setEnabled(scaleChecked);
544 lengthAutoToWidgets(Width, widthUnit, igp.width,
546 bool const widthChecked = !Width->text().isEmpty() &&
547 Width->text() != "auto";
548 WidthCB->blockSignals(true);
549 WidthCB->setChecked(widthChecked);
550 WidthCB->blockSignals(false);
551 Width->setEnabled(widthChecked);
552 widthUnit->setEnabled(widthChecked);
554 lengthAutoToWidgets(Height, heightUnit, igp.height,
556 bool const heightChecked = !Height->text().isEmpty()
557 && Height->text() != "auto";
558 HeightCB->blockSignals(true);
559 HeightCB->setChecked(heightChecked);
560 HeightCB->blockSignals(false);
561 Height->setEnabled(heightChecked);
562 heightUnit->setEnabled(heightChecked);
564 scaleCB->setEnabled(!widthChecked && !heightChecked);
565 WidthCB->setEnabled(!scaleChecked);
566 HeightCB->setEnabled(!scaleChecked);
567 aspectratio->setEnabled(widthChecked && heightChecked);
571 angle->setText(toqstr(igp.rotateAngle));
572 rotateOrderCB->setChecked(igp.scaleBeforeRotation);
574 rotateOrderCB->setEnabled( (widthChecked || heightChecked || scaleChecked)
575 && igp.rotateAngle != "0");
579 vector<RotationOriginPair> origindata = getRotationOriginData();
580 vector<docstring> const origin_lang = getFirst(origindata);
581 origin_ltx = getSecond(origindata);
583 for (vector<docstring>::const_iterator it = origin_lang.begin();
584 it != origin_lang.end(); ++it)
585 origin->addItem(toqstr(*it));
587 if (!igp.rotateOrigin.empty())
588 origin->setCurrentIndex(
589 getItemNo(origin_ltx, igp.rotateOrigin));
591 origin->setCurrentIndex(0);
593 // disable edit button when no filename is present
594 editPB->setDisabled(filename->text().isEmpty());
597 latexoptions->setText(toqstr(igp.special));
601 void GuiGraphicsDialog::applyView()
603 InsetGraphicsParams & igp = controller().params();
605 igp.filename.set(internal_path(fromqstr(filename->text())),
606 controller().bufferFilepath());
607 igp.filename.setEmbed(embedCB->checkState() == Qt::Checked);
611 if (controller().bbChanged) {
613 string lbXs = fromqstr(lbX->text());
614 string lbYs = fromqstr(lbY->text());
615 string rtXs = fromqstr(rtX->text());
616 string rtYs = fromqstr(rtY->text());
618 convert<int>(lbXs) + convert<int>(lbYs) +
619 convert<int>(rtXs) + convert<int>(rtXs);
624 bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
628 bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
632 bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
636 bb += (rtYs + fromqstr(rtYunit->currentText()));
641 igp.draft = draftCB->isChecked();
642 igp.clip = clip->isChecked();
643 igp.subcaption = subfigure->isChecked();
644 igp.subcaptionText = fromqstr(subcaption->text());
646 switch (showCB->currentIndex()) {
647 case 0: igp.display = graphics::DefaultDisplay; break;
648 case 1: igp.display = graphics::MonochromeDisplay; break;
649 case 2: igp.display = graphics::GrayscaleDisplay; break;
650 case 3: igp.display = graphics::ColorDisplay; break;
654 if (!displayGB->isChecked())
655 igp.display = graphics::NoDisplay;
657 //the graphics section
658 if (scaleCB->isChecked() && !Scale->text().isEmpty()) {
659 igp.scale = fromqstr(Scale->text());
660 igp.width = Length("0pt");
661 igp.height = Length("0pt");
662 igp.keepAspectRatio = false;
664 igp.scale = string();
665 igp.width = WidthCB->isChecked() ?
666 //Note that this works even if Width is "auto", since in
667 //that case we get "0pt".
668 Length(widgetsToLength(Width, widthUnit)):
670 igp.height = HeightCB->isChecked() ?
671 Length(widgetsToLength(Height, heightUnit)) :
673 igp.keepAspectRatio = aspectratio->isEnabled() &&
674 aspectratio->isChecked() &&
675 igp.width.value() > 0 && igp.height.value() > 0;
678 igp.noUnzip = unzipCB->isChecked();
679 igp.lyxscale = displayscale->text().toInt();
680 igp.rotateAngle = fromqstr(angle->text());
682 double rotAngle = convert<double>(igp.rotateAngle);
683 if (std::abs(rotAngle) > 360.0) {
684 rotAngle -= 360.0 * floor(rotAngle / 360.0);
685 igp.rotateAngle = convert<string>(rotAngle);
688 // save the latex name for the origin. If it is the default
689 // then origin_ltx returns ""
690 igp.rotateOrigin = origin_ltx[origin->currentIndex()];
691 igp.scaleBeforeRotation = rotateOrderCB->isChecked();
693 // more latex options
694 igp.special = fromqstr(latexoptions->text());
698 void GuiGraphicsDialog::getBB()
700 string const fn = fromqstr(filename->text());
702 string const bb = controller().readBB(fn);
704 lbX->setText(toqstr(token(bb, ' ', 0)));
705 lbY->setText(toqstr(token(bb, ' ', 1)));
706 rtX->setText(toqstr(token(bb, ' ', 2)));
707 rtY->setText(toqstr(token(bb, ' ', 3)));
708 // the default units for the bb values when reading
710 lbXunit->setCurrentIndex(0);
711 lbYunit->setCurrentIndex(0);
712 rtXunit->setCurrentIndex(0);
713 rtYunit->setCurrentIndex(0);
715 controller().bbChanged = false;
720 bool GuiGraphicsDialog::isValid()
722 return !filename->text().isEmpty();
725 } // namespace frontend
729 #include "GuiGraphics_moc.cpp"