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"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "controllers/frontend_helpers.h"
27 #include "insets/InsetGraphicsParams.h"
29 #include "support/convert.h"
30 #include "support/lstrings.h"
31 #include "support/lyxlib.h"
32 #include "support/os.h"
35 #include <QCloseEvent>
38 #include <QPushButton>
44 using lyx::support::float_equal;
45 using lyx::support::token;
47 using lyx::support::os::internal_path;
49 #ifndef CXX_GLOBAL_CSTD
62 std::vector<typename Pair::first_type> const
63 getFirst(std::vector<Pair> const & pr)
65 std::vector<typename Pair::first_type> tmp(pr.size());
66 std::transform(pr.begin(), pr.end(), tmp.begin(),
67 boost::bind(&Pair::first, _1));
71 GuiGraphicsDialog::GuiGraphicsDialog(GuiGraphics * form)
76 connect(okPB, SIGNAL(clicked()),
77 form, SLOT(slotOK()));
78 connect(applyPB, SIGNAL(clicked()),
79 form, SLOT(slotApply()));
80 connect(closePB, SIGNAL(clicked()),
81 form, SLOT(slotClose()));
82 connect(restorePB, SIGNAL(clicked()),
83 form, SLOT(slotRestore()));
86 connect(filename, SIGNAL(textChanged(const QString &)),
87 this, SLOT(change_adaptor()));
88 connect(WidthCB, SIGNAL( clicked()),
89 this, SLOT(change_adaptor()));
90 connect(HeightCB, SIGNAL( clicked()),
91 this, SLOT(change_adaptor()));
92 connect(Width, SIGNAL(textChanged(const QString &)),
93 this, SLOT(change_adaptor()));
94 connect(Height, SIGNAL(textChanged(const QString &)),
95 this, SLOT(change_adaptor()));
96 connect(heightUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
97 this, SLOT(change_adaptor()));
98 connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
99 this, SLOT(change_adaptor()));
100 connect(aspectratio, SIGNAL(stateChanged(int)),
101 this, SLOT(change_adaptor()));
102 connect(angle, SIGNAL(textChanged(const QString &)),
103 this, SLOT(change_adaptor()));
104 connect(origin, SIGNAL(activated(int)),
105 this, SLOT(change_adaptor()));
106 connect(scaleCB, SIGNAL(clicked()),
107 this, SLOT(change_adaptor()));
108 connect(Scale, SIGNAL(textChanged(const QString &)),
109 this, SLOT(change_adaptor()));
110 connect(rotateOrderCB, SIGNAL(clicked()),
111 this, SLOT(change_adaptor()));
113 filename->setValidator(new PathValidator(true, filename));
114 setFocusProxy(filename);
116 QDoubleValidator * scaleValidator = new DoubleAutoValidator(Scale);
117 scaleValidator->setBottom(0);
118 scaleValidator->setDecimals(256); //I guess that will do
119 Scale->setValidator(scaleValidator);
120 Height->setValidator(unsignedLengthAutoValidator(Height));
121 Width->setValidator(unsignedLengthAutoValidator(Width));
122 angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
125 connect(clip, SIGNAL(stateChanged(int)),
126 this, SLOT(change_adaptor()));
127 connect(lbY, SIGNAL(textChanged(const QString&)),
128 this, SLOT(change_bb()));
129 connect(lbYunit, SIGNAL(activated(int)),
130 this, SLOT(change_bb()));
131 connect(rtY, SIGNAL(textChanged(const QString&)),
132 this, SLOT(change_bb()));
133 connect(rtYunit, SIGNAL(activated(int)),
134 this, SLOT(change_bb()));
135 connect(lbX, SIGNAL(textChanged(const QString&)),
136 this, SLOT(change_bb()));
137 connect(lbXunit, SIGNAL(activated(int)),
138 this, SLOT(change_bb()));
139 connect(rtX, SIGNAL(textChanged(const QString&)),
140 this, SLOT(change_bb()));
141 connect(rtXunit, SIGNAL(activated(int)),
142 this, SLOT(change_bb()));
143 connect(getPB, SIGNAL(clicked()),
144 this, SLOT(change_adaptor()));
146 lbX->setValidator(new QDoubleValidator(lbX));
147 lbY->setValidator(new QDoubleValidator(lbY));
148 rtX->setValidator(new QDoubleValidator(rtX));
149 rtY->setValidator(new QDoubleValidator(rtY));
152 connect(latexoptions, SIGNAL(textChanged(const QString&)),
153 this, SLOT(change_adaptor()));
154 connect(draftCB, SIGNAL(stateChanged(int)),
155 this, SLOT(change_adaptor()));
156 connect(unzipCB, SIGNAL(stateChanged(int)),
157 this, SLOT(change_adaptor()));
158 // FIXME: we should connect to clicked() when we move to Qt 4.2 because
159 // the toggled(bool) signal is also trigged when we update the widgets
160 // (rgh-4/07) this isn't as much or a problem as it was, because we're now
161 // using blockSignals() to keep from triggering that signal when we call
162 // setChecked(). Note, too, that clicked() would get called whenever it
163 // is clicked, even right clicked (I think), not just whenever it is
165 connect(subfigure, SIGNAL(toggled(bool)),
166 this, SLOT(change_adaptor()));
167 connect(subcaption, SIGNAL(textChanged(const QString&)),
168 this, SLOT(change_adaptor()));
169 connect(displayGB, SIGNAL(toggled(bool)),
170 this, SLOT(change_adaptor()));
171 connect(showCB, SIGNAL(currentIndexChanged(int)),
172 this, SLOT(change_adaptor()));
173 connect(displayscale, SIGNAL(textChanged(const QString&)),
174 this, SLOT(change_adaptor()));
175 displayscale->setValidator(new QIntValidator(displayscale));
179 void GuiGraphicsDialog::showView()
185 void GuiGraphicsDialog::change_adaptor()
191 void GuiGraphicsDialog::change_bb()
193 form_->controller().bbChanged = true;
194 LYXERR(Debug::GRAPHICS)
195 << "[controller().bb_Changed set to true]\n";
200 void GuiGraphicsDialog::closeEvent(QCloseEvent * e)
207 void GuiGraphicsDialog::on_browsePB_clicked()
209 docstring const str =
210 form_->controller().browse(qstring_to_ucs4(filename->text()));
212 filename->setText(toqstr(str));
218 void GuiGraphicsDialog::on_getPB_clicked()
224 void GuiGraphicsDialog::on_editPB_clicked()
226 form_->controller().editGraphics();
230 void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
232 editPB->setDisabled(filename.isEmpty());
236 void GuiGraphicsDialog::setAutoText()
238 if (scaleCB->isChecked()) return;
239 if (!Scale->isEnabled() && Scale->text() != "100")
240 Scale->setText(QString("auto"));
242 setAutoTextCB(WidthCB, Width, widthUnit);
243 setAutoTextCB(HeightCB, Height, heightUnit);
247 void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
249 Scale->setEnabled(setScale);
251 Scale->setText("100");
252 Scale->setFocus(Qt::OtherFocusReason);
255 WidthCB->setDisabled(setScale);
256 WidthCB->blockSignals(true);
257 WidthCB->setChecked(false);
258 WidthCB->blockSignals(false);
259 Width->setEnabled(false);
260 widthUnit->setEnabled(false);
262 HeightCB->setDisabled(setScale);
263 HeightCB->blockSignals(true);
264 HeightCB->setChecked(false);
265 HeightCB->blockSignals(false);
266 Height->setEnabled(false);
267 heightUnit->setEnabled(false);
269 aspectratio->setDisabled(true);
270 aspectratio->setChecked(true);
272 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
273 HeightCB->isChecked() ||
274 scaleCB->isChecked()) &&
275 (angle->text() != "0"));
280 void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
282 Width->setEnabled(setWidth);
283 widthUnit->setEnabled(setWidth);
285 Width->setFocus(Qt::OtherFocusReason);
287 bool const setHeight = HeightCB->isChecked();
288 aspectratio->setEnabled(setWidth && setHeight);
289 aspectratio->blockSignals(true);
290 aspectratio->setChecked(!(setWidth && setHeight));
291 aspectratio->blockSignals(false);
293 scaleCB->setEnabled(!setWidth && !setHeight);
294 //already will be unchecked, so don't need to do that
295 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
296 && scaleCB->isChecked()); //should be false, but let's check
297 rotateOrderCB->setEnabled((setWidth || setHeight ||
298 scaleCB->isChecked()) &&
299 (angle->text() != "0"));
305 void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
307 Height->setEnabled(setHeight);
308 heightUnit->setEnabled(setHeight);
310 Height->setFocus(Qt::OtherFocusReason);
312 bool const setWidth = WidthCB->isChecked();
313 aspectratio->setEnabled(setWidth && setHeight);
314 aspectratio->blockSignals(true);
315 aspectratio->setChecked(!(setWidth && setHeight));
316 aspectratio->blockSignals(false);
318 scaleCB->setEnabled(!setWidth && !setHeight);
320 Scale->setEnabled((!setWidth && !setHeight) //=scaleCB->isEnabled()
321 && scaleCB->isChecked()); //should be false
322 rotateOrderCB->setEnabled((setWidth || setHeight ||
323 scaleCB->isChecked()) &&
324 (angle->text() != "0"));
330 void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
332 rotateOrderCB->setEnabled((WidthCB->isChecked() ||
333 HeightCB->isChecked() ||
334 scaleCB->isChecked()) &&
339 GuiGraphics::GuiGraphics(GuiDialog & parent)
340 : GuiView<GuiGraphicsDialog>(parent, _("Graphics"))
345 void GuiGraphics::build_dialog()
347 dialog_.reset(new GuiGraphicsDialog(this));
349 bc().setOK(dialog_->okPB);
350 bc().setApply(dialog_->applyPB);
351 bc().setRestore(dialog_->restorePB);
352 bc().setCancel(dialog_->closePB);
354 bc().addReadOnly(dialog_->latexoptions);
355 bc().addReadOnly(dialog_->subfigure);
356 bc().addReadOnly(dialog_->filenameL);
357 bc().addReadOnly(dialog_->filename);
358 bc().addReadOnly(dialog_->browsePB);
359 bc().addReadOnly(dialog_->unzipCB);
360 bc().addReadOnly(dialog_->bbFrame);
361 bc().addReadOnly(dialog_->draftCB);
362 bc().addReadOnly(dialog_->clip);
363 bc().addReadOnly(dialog_->unzipCB);
364 bc().addReadOnly(dialog_->displayGB);
365 bc().addReadOnly(dialog_->sizeGB);
366 bc().addReadOnly(dialog_->rotationGB);
367 bc().addReadOnly(dialog_->latexoptions);
368 bc().addReadOnly(dialog_->getPB);
369 bc().addReadOnly(dialog_->rotateOrderCB);
371 // initialize the length validator
372 bc().addCheckedLineEdit(dialog_->Scale, dialog_->scaleCB);
373 bc().addCheckedLineEdit(dialog_->Width, dialog_->WidthCB);
374 bc().addCheckedLineEdit(dialog_->Height, dialog_->HeightCB);
375 bc().addCheckedLineEdit(dialog_->displayscale, dialog_->scaleLA);
376 bc().addCheckedLineEdit(dialog_->angle, dialog_->angleL);
377 bc().addCheckedLineEdit(dialog_->lbX, dialog_->xL);
378 bc().addCheckedLineEdit(dialog_->lbY, dialog_->yL);
379 bc().addCheckedLineEdit(dialog_->rtX, dialog_->xL_2);
380 bc().addCheckedLineEdit(dialog_->rtY, dialog_->yL_2);
381 bc().addCheckedLineEdit(dialog_->filename, dialog_->filenameL);
385 // returns the number of the string s in the vector v
386 static int getItemNo(const vector<string> & v, string const & s)
388 vector<string>::const_iterator cit =
389 find(v.begin(), v.end(), s);
390 return (cit != v.end()) ? int(cit - v.begin()) : 0;
394 void GuiGraphics::update_contents()
396 // clear and fill in the comboboxes
397 vector<string> const bb_units = frontend::getBBUnits();
398 dialog_->lbXunit->clear();
399 dialog_->lbYunit->clear();
400 dialog_->rtXunit->clear();
401 dialog_->rtYunit->clear();
402 for (vector<string>::const_iterator it = bb_units.begin();
403 it != bb_units.end(); ++it) {
404 dialog_->lbXunit->addItem(toqstr(*it));
405 dialog_->lbYunit->addItem(toqstr(*it));
406 dialog_->rtXunit->addItem(toqstr(*it));
407 dialog_->rtYunit->addItem(toqstr(*it));
410 InsetGraphicsParams & igp = controller().params();
412 // set the right default unit
413 Length::UNIT unitDefault = Length::CM;
414 switch (lyxrc.default_papersize) {
417 case PAPER_USEXECUTIVE:
418 unitDefault = Length::IN;
425 igp.filename.outputFilename(kernel().bufferFilepath());
426 dialog_->filename->setText(toqstr(name));
428 // set the bounding box values
429 if (igp.bb.empty()) {
430 string const bb = controller().readBB(igp.filename.absFilename());
431 // the values from the file always have the bigpoint-unit bp
432 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
433 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
434 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
435 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
436 dialog_->lbXunit->setCurrentIndex(0);
437 dialog_->lbYunit->setCurrentIndex(0);
438 dialog_->rtXunit->setCurrentIndex(0);
439 dialog_->rtYunit->setCurrentIndex(0);
440 controller().bbChanged = false;
442 // get the values from the inset
444 string const xl(token(igp.bb, ' ', 0));
445 string const yl(token(igp.bb, ' ', 1));
446 string const xr(token(igp.bb, ' ', 2));
447 string const yr(token(igp.bb, ' ', 3));
448 if (isValidLength(xl, &anyLength)) {
449 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
450 string const unit(unit_name[anyLength.unit()]);
451 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
453 dialog_->lbX->setText(toqstr(xl));
455 if (isValidLength(yl, &anyLength)) {
456 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
457 string const unit(unit_name[anyLength.unit()]);
458 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
460 dialog_->lbY->setText(toqstr(xl));
462 if (isValidLength(xr, &anyLength)) {
463 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
464 string const unit(unit_name[anyLength.unit()]);
465 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
467 dialog_->rtX->setText(toqstr(xl));
469 if (isValidLength(yr, &anyLength)) {
470 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
471 string const unit(unit_name[anyLength.unit()]);
472 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
474 dialog_->rtY->setText(toqstr(xl));
476 controller().bbChanged = true;
479 // Update the draft and clip mode
480 dialog_->draftCB->setChecked(igp.draft);
481 dialog_->clip->setChecked(igp.clip);
482 dialog_->unzipCB->setChecked(igp.noUnzip);
484 // Update the subcaption check button and input field
485 dialog_->subfigure->setChecked(igp.subcaption);
486 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
489 switch (igp.display) {
490 case graphics::DefaultDisplay: item = 0; break;
491 case graphics::MonochromeDisplay: item = 1; break;
492 case graphics::GrayscaleDisplay: item = 2; break;
493 case graphics::ColorDisplay: item = 3; break;
494 case graphics::NoDisplay: item = 0; break;
496 dialog_->showCB->setCurrentIndex(item);
497 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
498 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
500 // the output section (width/height)
502 dialog_->Scale->setText(toqstr(igp.scale));
503 //igp.scale defaults to 100, so we treat it as empty
504 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
505 dialog_->scaleCB->blockSignals(true);
506 dialog_->scaleCB->setChecked(scaleChecked);
507 dialog_->scaleCB->blockSignals(false);
508 dialog_->Scale->setEnabled(scaleChecked);
510 lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
512 bool const widthChecked = !dialog_->Width->text().isEmpty() &&
513 dialog_->Width->text() != "auto";
514 dialog_->WidthCB->blockSignals(true);
515 dialog_->WidthCB->setChecked(widthChecked);
516 dialog_->WidthCB->blockSignals(false);
517 dialog_->Width->setEnabled(widthChecked);
518 dialog_->widthUnit->setEnabled(widthChecked);
520 lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
522 bool const heightChecked = !dialog_->Height->text().isEmpty()
523 && dialog_->Height->text() != "auto";
524 dialog_->HeightCB->blockSignals(true);
525 dialog_->HeightCB->setChecked(heightChecked);
526 dialog_->HeightCB->blockSignals(false);
527 dialog_->Height->setEnabled(heightChecked);
528 dialog_->heightUnit->setEnabled(heightChecked);
530 dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
531 dialog_->WidthCB->setEnabled(!scaleChecked);
532 dialog_->HeightCB->setEnabled(!scaleChecked);
533 dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
535 dialog_->setAutoText();
537 dialog_->angle->setText(toqstr(igp.rotateAngle));
538 dialog_->rotateOrderCB->setChecked(igp.scaleBeforeRotation);
540 dialog_->rotateOrderCB->setEnabled((widthChecked ||
543 (igp.rotateAngle != "0"));
545 dialog_->origin->clear();
547 vector<RotationOriginPair> origindata = getRotationOriginData();
548 vector<docstring> const origin_lang = getFirst(origindata);
549 GuiGraphics::origin_ltx = getSecond(origindata);
551 for (vector<docstring>::const_iterator it = origin_lang.begin();
552 it != origin_lang.end(); ++it)
553 dialog_->origin->addItem(toqstr(*it));
555 if (!igp.rotateOrigin.empty())
556 dialog_->origin->setCurrentIndex(
557 getItemNo(origin_ltx, igp.rotateOrigin));
559 dialog_->origin->setCurrentIndex(0);
561 // disable edit button when no filename is present
562 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
565 dialog_->latexoptions->setText(toqstr(igp.special));
569 void GuiGraphics::applyView()
571 InsetGraphicsParams & igp = controller().params();
573 igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
574 kernel().bufferFilepath());
578 if (controller().bbChanged) {
580 string lbX(fromqstr(dialog_->lbX->text()));
581 string lbY(fromqstr(dialog_->lbY->text()));
582 string rtX(fromqstr(dialog_->rtX->text()));
583 string rtY(fromqstr(dialog_->rtY->text()));
585 convert<int>(lbX) + convert<int>(lbY) +
586 convert<int>(rtX) + convert<int>(rtX);
591 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
595 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
599 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
603 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
608 igp.draft = dialog_->draftCB->isChecked();
609 igp.clip = dialog_->clip->isChecked();
610 igp.subcaption = dialog_->subfigure->isChecked();
611 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
613 switch (dialog_->showCB->currentIndex()) {
614 case 0: igp.display = graphics::DefaultDisplay; break;
615 case 1: igp.display = graphics::MonochromeDisplay; break;
616 case 2: igp.display = graphics::GrayscaleDisplay; break;
617 case 3: igp.display = graphics::ColorDisplay; break;
621 if (!dialog_->displayGB->isChecked())
622 igp.display = graphics::NoDisplay;
624 //the graphics section
625 if (dialog_->scaleCB->isChecked() && !dialog_->Scale->text().isEmpty()) {
626 igp.scale = fromqstr(dialog_->Scale->text());
627 igp.width = Length("0pt");
628 igp.height = Length("0pt");
629 igp.keepAspectRatio = false;
631 igp.scale = string();
632 igp.width = dialog_->WidthCB->isChecked() ?
633 //Note that this works even if dialog_->Width is "auto", since in
634 //that case we get "0pt".
635 Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
637 igp.height = dialog_->HeightCB->isChecked() ?
638 Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
640 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
641 dialog_->aspectratio->isChecked() &&
642 igp.width.value() > 0 && igp.height.value() > 0;
645 igp.noUnzip = dialog_->unzipCB->isChecked();
647 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
649 igp.rotateAngle = fromqstr(dialog_->angle->text());
651 double rotAngle = convert<double>(igp.rotateAngle);
652 if (std::abs(rotAngle) > 360.0) {
653 rotAngle -= 360.0 * floor(rotAngle / 360.0);
654 igp.rotateAngle = convert<string>(rotAngle);
657 // save the latex name for the origin. If it is the default
658 // then origin_ltx returns ""
660 GuiGraphics::origin_ltx[dialog_->origin->currentIndex()];
662 igp.scaleBeforeRotation = dialog_->rotateOrderCB->isChecked();
664 // more latex options
665 igp.special = fromqstr(dialog_->latexoptions->text());
669 void GuiGraphics::getBB()
671 string const filename(fromqstr(dialog_->filename->text()));
672 if (!filename.empty()) {
673 string const bb(controller().readBB(filename));
675 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
676 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
677 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
678 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
679 // the default units for the bb values when reading
681 dialog_->lbXunit->setCurrentIndex(0);
682 dialog_->lbYunit->setCurrentIndex(0);
683 dialog_->rtXunit->setCurrentIndex(0);
684 dialog_->rtYunit->setCurrentIndex(0);
686 controller().bbChanged = false;
691 bool GuiGraphics::isValid()
693 return !dialog_->filename->text().isEmpty();
696 } // namespace frontend
700 #include "GuiGraphics_moc.cpp"