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"
19 #include "LengthCombo.h"
20 #include "lengthcommon.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
26 #include "controllers/frontend_helpers.h"
28 #include "insets/InsetGraphicsParams.h"
30 #include "support/convert.h"
31 #include "support/lstrings.h"
32 #include "support/lyxlib.h"
33 #include "support/os.h"
37 #include "insets/InsetGraphicsParams.h"
40 #include <QCloseEvent>
43 #include <QPushButton>
47 using lyx::support::float_equal;
48 using lyx::support::token;
50 using lyx::support::os::internal_path;
52 #ifndef CXX_GLOBAL_CSTD
64 GuiGraphicsDialog::GuiGraphicsDialog(GuiGraphics * form)
69 connect(okPB, SIGNAL(clicked()),
70 form, SLOT(slotOK()));
71 connect(applyPB, SIGNAL(clicked()),
72 form, SLOT(slotApply()));
73 connect(closePB, SIGNAL(clicked()),
74 form, SLOT(slotClose()));
75 connect(restorePB, SIGNAL(clicked()),
76 form, SLOT(slotRestore()));
79 connect(filename, SIGNAL(textChanged(const QString &)),
80 this, SLOT(change_adaptor()));
81 connect(WidthCB, SIGNAL( clicked()),
82 this, SLOT(change_adaptor()));
83 connect(HeightCB, SIGNAL( clicked()),
84 this, SLOT(change_adaptor()));
85 connect(Width, SIGNAL(textChanged(const QString &)),
86 this, SLOT(change_adaptor()));
87 connect(Height, SIGNAL(textChanged(const QString &)),
88 this, SLOT(change_adaptor()));
89 connect(heightUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
90 this, SLOT(change_adaptor()));
91 connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
92 this, SLOT(change_adaptor()));
93 connect(aspectratio, SIGNAL(stateChanged(int)),
94 this, SLOT(change_adaptor()));
95 connect(angle, SIGNAL(textChanged(const QString &)),
96 this, SLOT(change_adaptor()));
97 connect(origin, SIGNAL(activated(int)),
98 this, SLOT(change_adaptor()));
99 connect(scaleCB, SIGNAL(clicked()),
100 this, SLOT(change_adaptor()));
101 connect(Scale, SIGNAL(textChanged(const QString &)),
102 this, SLOT(change_adaptor()));
103 connect(rotateOrderCB, SIGNAL(clicked()),
104 this, SLOT(change_adaptor()));
106 filename->setValidator(new PathValidator(true, filename));
107 setFocusProxy(filename);
109 QDoubleValidator * scaleValidator = new DoubleAutoValidator(Scale);
110 scaleValidator->setBottom(0);
111 scaleValidator->setDecimals(256); //I guess that will do
112 Scale->setValidator(scaleValidator);
113 Height->setValidator(unsignedLengthAutoValidator(Height));
114 Width->setValidator(unsignedLengthAutoValidator(Width));
115 angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
118 connect(clip, SIGNAL(stateChanged(int)),
119 this, SLOT(change_adaptor()));
120 connect(lbY, SIGNAL(textChanged(const QString&)),
121 this, SLOT(change_bb()));
122 connect(lbYunit, SIGNAL(activated(int)),
123 this, SLOT(change_bb()));
124 connect(rtY, SIGNAL(textChanged(const QString&)),
125 this, SLOT(change_bb()));
126 connect(rtYunit, SIGNAL(activated(int)),
127 this, SLOT(change_bb()));
128 connect(lbX, SIGNAL(textChanged(const QString&)),
129 this, SLOT(change_bb()));
130 connect(lbXunit, SIGNAL(activated(int)),
131 this, SLOT(change_bb()));
132 connect(rtX, SIGNAL(textChanged(const QString&)),
133 this, SLOT(change_bb()));
134 connect(rtXunit, SIGNAL(activated(int)),
135 this, SLOT(change_bb()));
136 connect(getPB, SIGNAL(clicked()),
137 this, SLOT(change_adaptor()));
139 lbX->setValidator(new QDoubleValidator(lbX));
140 lbY->setValidator(new QDoubleValidator(lbY));
141 rtX->setValidator(new QDoubleValidator(rtX));
142 rtY->setValidator(new QDoubleValidator(rtY));
145 connect(latexoptions, SIGNAL(textChanged(const QString&)),
146 this, SLOT(change_adaptor()));
147 connect(draftCB, SIGNAL(stateChanged(int)),
148 this, SLOT(change_adaptor()));
149 connect(unzipCB, SIGNAL(stateChanged(int)),
150 this, SLOT(change_adaptor()));
151 // FIXME: we should connect to clicked() when we move to Qt 4.2 because
152 // the toggled(bool) signal is also trigged when we update the widgets
153 // (rgh-4/07) this isn't as much or a problem as it was, because we're now
154 // using blockSignals() to keep from triggering that signal when we call
155 // setChecked(). Note, too, that clicked() would get called whenever it
156 // is clicked, even right clicked (I think), not just whenever it is
158 connect(subfigure, SIGNAL(toggled(bool)),
159 this, SLOT(change_adaptor()));
160 connect(subcaption, SIGNAL(textChanged(const QString&)),
161 this, SLOT(change_adaptor()));
162 connect(displayGB, SIGNAL(toggled(bool)),
163 this, SLOT(change_adaptor()));
164 connect(showCB, SIGNAL(currentIndexChanged(int)),
165 this, SLOT(change_adaptor()));
166 connect(displayscale, SIGNAL(textChanged(const QString&)),
167 this, SLOT(change_adaptor()));
168 displayscale->setValidator(new QIntValidator(displayscale));
172 void GuiGraphicsDialog::show()
178 void GuiGraphicsDialog::change_adaptor()
184 void GuiGraphicsDialog::change_bb()
186 form_->controller().bbChanged = true;
187 LYXERR(Debug::GRAPHICS)
188 << "[controller().bb_Changed set to true]\n";
193 void GuiGraphicsDialog::closeEvent(QCloseEvent * e)
200 void GuiGraphicsDialog::on_browsePB_clicked()
202 docstring const str =
203 form_->controller().browse(qstring_to_ucs4(filename->text()));
205 filename->setText(toqstr(str));
211 void GuiGraphicsDialog::on_getPB_clicked()
217 void GuiGraphicsDialog::on_editPB_clicked()
219 form_->controller().editGraphics();
223 void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
225 editPB->setDisabled(filename.isEmpty());
229 void GuiGraphicsDialog::setAutoText()
231 if (scaleCB->isChecked()) return;
232 if (!Scale->isEnabled() && Scale->text() != "100")
233 Scale->setText(QString("auto"));
235 setAutoTextCB(WidthCB, Width, widthUnit);
236 setAutoTextCB(HeightCB, Height, heightUnit);
240 void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
242 Scale->setEnabled(setScale);
244 Scale->setText("100");
245 Scale->setFocus(Qt::OtherFocusReason);
248 WidthCB->setDisabled(setScale);
249 WidthCB->blockSignals(true);
250 WidthCB->setChecked(false);
251 WidthCB->blockSignals(false);
252 Width->setEnabled(false);
253 widthUnit->setEnabled(false);
255 HeightCB->setDisabled(setScale);
256 HeightCB->blockSignals(true);
257 HeightCB->setChecked(false);
258 HeightCB->blockSignals(false);
259 Height->setEnabled(false);
260 heightUnit->setEnabled(false);
262 aspectratio->setDisabled(true);
263 aspectratio->setChecked(true);
265 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
266 HeightCB->isChecked() ||
267 scaleCB->isChecked()) &&
268 (angle->text() != "0"));
273 void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
275 Width->setEnabled(setWidth);
276 widthUnit->setEnabled(setWidth);
278 Width->setFocus(Qt::OtherFocusReason);
280 bool const setHeight = HeightCB->isChecked();
281 aspectratio->setEnabled(setWidth && setHeight);
282 aspectratio->blockSignals(true);
283 aspectratio->setChecked(!(setWidth && setHeight));
284 aspectratio->blockSignals(false);
286 scaleCB->setEnabled(!setWidth && !setHeight);
287 //already will be unchecked, so don't need to do that
288 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
289 && scaleCB->isChecked()); //should be false, but let's check
290 rotateOrderCB->setEnabled((setWidth || setHeight ||
291 scaleCB->isChecked()) &&
292 (angle->text() != "0"));
298 void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
300 Height->setEnabled(setHeight);
301 heightUnit->setEnabled(setHeight);
303 Height->setFocus(Qt::OtherFocusReason);
305 bool const setWidth = WidthCB->isChecked();
306 aspectratio->setEnabled(setWidth && setHeight);
307 aspectratio->blockSignals(true);
308 aspectratio->setChecked(!(setWidth && setHeight));
309 aspectratio->blockSignals(false);
311 scaleCB->setEnabled(!setWidth && !setHeight);
313 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
314 && scaleCB->isChecked()); //should be false
315 rotateOrderCB->setEnabled((setWidth || setHeight ||
316 scaleCB->isChecked()) &&
317 (angle->text() != "0"));
323 void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
325 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
326 HeightCB->isChecked() ||
327 scaleCB->isChecked()) &&
332 GuiGraphics::GuiGraphics(Dialog & parent)
333 : GuiView<GuiGraphicsDialog>(parent, _("Graphics"))
338 void GuiGraphics::build_dialog()
340 dialog_.reset(new GuiGraphicsDialog(this));
342 bcview().setOK(dialog_->okPB);
343 bcview().setApply(dialog_->applyPB);
344 bcview().setRestore(dialog_->restorePB);
345 bcview().setCancel(dialog_->closePB);
347 bcview().addReadOnly(dialog_->latexoptions);
348 bcview().addReadOnly(dialog_->subfigure);
349 bcview().addReadOnly(dialog_->filenameL);
350 bcview().addReadOnly(dialog_->filename);
351 bcview().addReadOnly(dialog_->browsePB);
352 bcview().addReadOnly(dialog_->unzipCB);
353 bcview().addReadOnly(dialog_->bbFrame);
354 bcview().addReadOnly(dialog_->draftCB);
355 bcview().addReadOnly(dialog_->clip);
356 bcview().addReadOnly(dialog_->unzipCB);
357 bcview().addReadOnly(dialog_->displayGB);
358 bcview().addReadOnly(dialog_->sizeGB);
359 bcview().addReadOnly(dialog_->rotationGB);
360 bcview().addReadOnly(dialog_->latexoptions);
361 bcview().addReadOnly(dialog_->getPB);
362 bcview().addReadOnly(dialog_->rotateOrderCB);
364 // initialize the length validator
365 addCheckedLineEdit(bcview(), dialog_->Scale, dialog_->scaleCB);
366 addCheckedLineEdit(bcview(), dialog_->Width, dialog_->WidthCB);
367 addCheckedLineEdit(bcview(), dialog_->Height, dialog_->HeightCB);
368 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
369 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
370 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
371 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
372 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
373 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
374 addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
378 // returns the number of the string s in the vector v
379 static int getItemNo(const vector<string> & v, string const & s)
381 vector<string>::const_iterator cit =
382 find(v.begin(), v.end(), s);
383 return (cit != v.end()) ? int(cit - v.begin()) : 0;
387 void GuiGraphics::update_contents()
389 // clear and fill in the comboboxes
390 vector<string> const bb_units = frontend::getBBUnits();
391 dialog_->lbXunit->clear();
392 dialog_->lbYunit->clear();
393 dialog_->rtXunit->clear();
394 dialog_->rtYunit->clear();
395 for (vector<string>::const_iterator it = bb_units.begin();
396 it != bb_units.end(); ++it) {
397 dialog_->lbXunit->addItem(toqstr(*it));
398 dialog_->lbYunit->addItem(toqstr(*it));
399 dialog_->rtXunit->addItem(toqstr(*it));
400 dialog_->rtYunit->addItem(toqstr(*it));
403 InsetGraphicsParams & igp = controller().params();
405 // set the right default unit
406 Length::UNIT unitDefault = Length::CM;
407 switch (lyxrc.default_papersize) {
410 case PAPER_USEXECUTIVE:
411 unitDefault = Length::IN;
418 igp.filename.outputFilename(kernel().bufferFilepath());
419 dialog_->filename->setText(toqstr(name));
421 // set the bounding box values
422 if (igp.bb.empty()) {
423 string const bb = controller().readBB(igp.filename.absFilename());
424 // the values from the file always have the bigpoint-unit bp
425 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
426 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
427 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
428 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
429 dialog_->lbXunit->setCurrentIndex(0);
430 dialog_->lbYunit->setCurrentIndex(0);
431 dialog_->rtXunit->setCurrentIndex(0);
432 dialog_->rtYunit->setCurrentIndex(0);
433 controller().bbChanged = false;
435 // get the values from the inset
437 string const xl(token(igp.bb, ' ', 0));
438 string const yl(token(igp.bb, ' ', 1));
439 string const xr(token(igp.bb, ' ', 2));
440 string const yr(token(igp.bb, ' ', 3));
441 if (isValidLength(xl, &anyLength)) {
442 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
443 string const unit(unit_name[anyLength.unit()]);
444 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
446 dialog_->lbX->setText(toqstr(xl));
448 if (isValidLength(yl, &anyLength)) {
449 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
450 string const unit(unit_name[anyLength.unit()]);
451 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
453 dialog_->lbY->setText(toqstr(xl));
455 if (isValidLength(xr, &anyLength)) {
456 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
457 string const unit(unit_name[anyLength.unit()]);
458 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
460 dialog_->rtX->setText(toqstr(xl));
462 if (isValidLength(yr, &anyLength)) {
463 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
464 string const unit(unit_name[anyLength.unit()]);
465 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
467 dialog_->rtY->setText(toqstr(xl));
469 controller().bbChanged = true;
472 // Update the draft and clip mode
473 dialog_->draftCB->setChecked(igp.draft);
474 dialog_->clip->setChecked(igp.clip);
475 dialog_->unzipCB->setChecked(igp.noUnzip);
477 // Update the subcaption check button and input field
478 dialog_->subfigure->setChecked(igp.subcaption);
479 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
482 switch (igp.display) {
483 case graphics::DefaultDisplay: item = 0; break;
484 case graphics::MonochromeDisplay: item = 1; break;
485 case graphics::GrayscaleDisplay: item = 2; break;
486 case graphics::ColorDisplay: item = 3; break;
487 case graphics::NoDisplay: item = 0; break;
489 dialog_->showCB->setCurrentIndex(item);
490 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
491 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
493 // the output section (width/height)
495 dialog_->Scale->setText(toqstr(igp.scale));
496 //igp.scale defaults to 100, so we treat it as empty
497 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
498 dialog_->scaleCB->blockSignals(true);
499 dialog_->scaleCB->setChecked(scaleChecked);
500 dialog_->scaleCB->blockSignals(false);
501 dialog_->Scale->setEnabled(scaleChecked);
503 lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
505 bool const widthChecked = !dialog_->Width->text().isEmpty() &&
506 dialog_->Width->text() != "auto";
507 dialog_->WidthCB->blockSignals(true);
508 dialog_->WidthCB->setChecked(widthChecked);
509 dialog_->WidthCB->blockSignals(false);
510 dialog_->Width->setEnabled(widthChecked);
511 dialog_->widthUnit->setEnabled(widthChecked);
513 lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
515 bool const heightChecked = !dialog_->Height->text().isEmpty()
516 && dialog_->Height->text() != "auto";
517 dialog_->HeightCB->blockSignals(true);
518 dialog_->HeightCB->setChecked(heightChecked);
519 dialog_->HeightCB->blockSignals(false);
520 dialog_->Height->setEnabled(heightChecked);
521 dialog_->heightUnit->setEnabled(heightChecked);
523 dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
524 dialog_->WidthCB->setEnabled(!scaleChecked);
525 dialog_->HeightCB->setEnabled(!scaleChecked);
526 dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
528 dialog_->setAutoText();
530 dialog_->angle->setText(toqstr(igp.rotateAngle));
531 dialog_->rotateOrderCB->setChecked(igp.scaleBeforeRotation);
533 dialog_->rotateOrderCB->setEnabled((widthChecked ||
536 (igp.rotateAngle != "0"));
538 dialog_->origin->clear();
540 vector<RotationOriginPair> origindata = getRotationOriginData();
541 vector<docstring> const origin_lang = getFirst(origindata);
542 GuiGraphics::origin_ltx = getSecond(origindata);
544 for (vector<docstring>::const_iterator it = origin_lang.begin();
545 it != origin_lang.end(); ++it)
546 dialog_->origin->addItem(toqstr(*it));
548 if (!igp.rotateOrigin.empty())
549 dialog_->origin->setCurrentIndex(
550 getItemNo(origin_ltx, igp.rotateOrigin));
552 dialog_->origin->setCurrentIndex(0);
554 // disable edit button when no filename is present
555 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
558 dialog_->latexoptions->setText(toqstr(igp.special));
562 void GuiGraphics::apply()
564 InsetGraphicsParams & igp = controller().params();
566 igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
567 kernel().bufferFilepath());
571 if (controller().bbChanged) {
573 string lbX(fromqstr(dialog_->lbX->text()));
574 string lbY(fromqstr(dialog_->lbY->text()));
575 string rtX(fromqstr(dialog_->rtX->text()));
576 string rtY(fromqstr(dialog_->rtY->text()));
578 convert<int>(lbX) + convert<int>(lbY) +
579 convert<int>(rtX) + convert<int>(rtX);
584 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
588 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
592 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
596 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
601 igp.draft = dialog_->draftCB->isChecked();
602 igp.clip = dialog_->clip->isChecked();
603 igp.subcaption = dialog_->subfigure->isChecked();
604 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
606 switch (dialog_->showCB->currentIndex()) {
607 case 0: igp.display = graphics::DefaultDisplay; break;
608 case 1: igp.display = graphics::MonochromeDisplay; break;
609 case 2: igp.display = graphics::GrayscaleDisplay; break;
610 case 3: igp.display = graphics::ColorDisplay; break;
614 if (!dialog_->displayGB->isChecked())
615 igp.display = graphics::NoDisplay;
617 //the graphics section
618 if (dialog_->scaleCB->isChecked() && !dialog_->Scale->text().isEmpty()) {
619 igp.scale = fromqstr(dialog_->Scale->text());
620 igp.width = Length("0pt");
621 igp.height = Length("0pt");
622 igp.keepAspectRatio = false;
624 igp.scale = string();
625 igp.width = dialog_->WidthCB->isChecked() ?
626 //Note that this works even if dialog_->Width is "auto", since in
627 //that case we get "0pt".
628 Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
630 igp.height = dialog_->HeightCB->isChecked() ?
631 Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
633 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
634 dialog_->aspectratio->isChecked() &&
635 igp.width.value() > 0 && igp.height.value() > 0;
638 igp.noUnzip = dialog_->unzipCB->isChecked();
640 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
642 igp.rotateAngle = fromqstr(dialog_->angle->text());
644 double rotAngle = convert<double>(igp.rotateAngle);
645 if (std::abs(rotAngle) > 360.0) {
646 rotAngle -= 360.0 * floor(rotAngle / 360.0);
647 igp.rotateAngle = convert<string>(rotAngle);
650 // save the latex name for the origin. If it is the default
651 // then origin_ltx returns ""
653 GuiGraphics::origin_ltx[dialog_->origin->currentIndex()];
655 igp.scaleBeforeRotation = dialog_->rotateOrderCB->isChecked();
657 // more latex options
658 igp.special = fromqstr(dialog_->latexoptions->text());
662 void GuiGraphics::getBB()
664 string const filename(fromqstr(dialog_->filename->text()));
665 if (!filename.empty()) {
666 string const bb(controller().readBB(filename));
668 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
669 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
670 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
671 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
672 // the default units for the bb values when reading
674 dialog_->lbXunit->setCurrentIndex(0);
675 dialog_->lbYunit->setCurrentIndex(0);
676 dialog_->rtXunit->setCurrentIndex(0);
677 dialog_->rtYunit->setCurrentIndex(0);
679 controller().bbChanged = false;
684 bool GuiGraphics::isValid()
686 return !dialog_->filename->text().isEmpty();
689 } // namespace frontend
693 #include "GuiGraphics_moc.cpp"