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 "CheckedLineEdit.h"
19 #include "LengthCombo.h"
20 #include "GuiGraphicsDialog.h"
22 #include "qt_helpers.h"
24 #include "lengthcommon.h"
27 #include "controllers/ControlGraphics.h"
28 #include "controllers/frontend_helpers.h"
30 #include "insets/InsetGraphicsParams.h"
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
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
59 typedef QController<ControlGraphics, GuiView<GuiGraphicsDialog> > graphics_base_class;
61 GuiGraphics::GuiGraphics(Dialog & parent)
62 : graphics_base_class(parent, _("Graphics"))
67 void GuiGraphics::build_dialog()
69 dialog_.reset(new GuiGraphicsDialog(this));
71 bcview().setOK(dialog_->okPB);
72 bcview().setApply(dialog_->applyPB);
73 bcview().setRestore(dialog_->restorePB);
74 bcview().setCancel(dialog_->closePB);
76 bcview().addReadOnly(dialog_->latexoptions);
77 bcview().addReadOnly(dialog_->subfigure);
78 bcview().addReadOnly(dialog_->filenameL);
79 bcview().addReadOnly(dialog_->filename);
80 bcview().addReadOnly(dialog_->browsePB);
81 bcview().addReadOnly(dialog_->unzipCB);
82 bcview().addReadOnly(dialog_->bbFrame);
83 bcview().addReadOnly(dialog_->draftCB);
84 bcview().addReadOnly(dialog_->clip);
85 bcview().addReadOnly(dialog_->unzipCB);
86 bcview().addReadOnly(dialog_->displayGB);
87 bcview().addReadOnly(dialog_->sizeGB);
88 bcview().addReadOnly(dialog_->rotationGB);
89 bcview().addReadOnly(dialog_->latexoptions);
90 bcview().addReadOnly(dialog_->getPB);
91 bcview().addReadOnly(dialog_->rotateOrderCB);
93 // initialize the length validator
94 addCheckedLineEdit(bcview(), dialog_->Scale, dialog_->scaleCB);
95 addCheckedLineEdit(bcview(), dialog_->Width, dialog_->WidthCB);
96 addCheckedLineEdit(bcview(), dialog_->Height, dialog_->HeightCB);
97 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
98 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
99 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
100 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
101 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
102 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
103 addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
109 // returns the number of the string s in the vector v
110 int getItemNo(vector<string> v, string const & s) {
111 vector<string>::const_iterator cit =
112 find(v.begin(), v.end(), s);
113 return (cit != v.end()) ? int(cit - v.begin()) : 0;
119 void GuiGraphics::update_contents()
121 // clear and fill in the comboboxes
122 vector<string> const bb_units = frontend::getBBUnits();
123 dialog_->lbXunit->clear();
124 dialog_->lbYunit->clear();
125 dialog_->rtXunit->clear();
126 dialog_->rtYunit->clear();
127 for (vector<string>::const_iterator it = bb_units.begin();
128 it != bb_units.end(); ++it) {
129 dialog_->lbXunit->addItem(toqstr(*it));
130 dialog_->lbYunit->addItem(toqstr(*it));
131 dialog_->rtXunit->addItem(toqstr(*it));
132 dialog_->rtYunit->addItem(toqstr(*it));
135 InsetGraphicsParams & igp = controller().params();
137 // set the right default unit
138 Length::UNIT unitDefault = Length::CM;
139 switch (lyxrc.default_papersize) {
142 case PAPER_USEXECUTIVE:
143 unitDefault = Length::IN;
150 igp.filename.outputFilename(kernel().bufferFilepath());
151 dialog_->filename->setText(toqstr(name));
153 // set the bounding box values
154 if (igp.bb.empty()) {
155 string const bb = controller().readBB(igp.filename.absFilename());
156 // the values from the file always have the bigpoint-unit bp
157 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
158 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
159 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
160 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
161 dialog_->lbXunit->setCurrentIndex(0);
162 dialog_->lbYunit->setCurrentIndex(0);
163 dialog_->rtXunit->setCurrentIndex(0);
164 dialog_->rtYunit->setCurrentIndex(0);
165 controller().bbChanged = false;
167 // get the values from the inset
169 string const xl(token(igp.bb, ' ', 0));
170 string const yl(token(igp.bb, ' ', 1));
171 string const xr(token(igp.bb, ' ', 2));
172 string const yr(token(igp.bb, ' ', 3));
173 if (isValidLength(xl, &anyLength)) {
174 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
175 string const unit(unit_name[anyLength.unit()]);
176 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
178 dialog_->lbX->setText(toqstr(xl));
180 if (isValidLength(yl, &anyLength)) {
181 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
182 string const unit(unit_name[anyLength.unit()]);
183 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
185 dialog_->lbY->setText(toqstr(xl));
187 if (isValidLength(xr, &anyLength)) {
188 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
189 string const unit(unit_name[anyLength.unit()]);
190 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
192 dialog_->rtX->setText(toqstr(xl));
194 if (isValidLength(yr, &anyLength)) {
195 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
196 string const unit(unit_name[anyLength.unit()]);
197 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
199 dialog_->rtY->setText(toqstr(xl));
201 controller().bbChanged = true;
204 // Update the draft and clip mode
205 dialog_->draftCB->setChecked(igp.draft);
206 dialog_->clip->setChecked(igp.clip);
207 dialog_->unzipCB->setChecked(igp.noUnzip);
209 // Update the subcaption check button and input field
210 dialog_->subfigure->setChecked(igp.subcaption);
211 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
214 switch (igp.display) {
215 case graphics::DefaultDisplay: item = 0; break;
216 case graphics::MonochromeDisplay: item = 1; break;
217 case graphics::GrayscaleDisplay: item = 2; break;
218 case graphics::ColorDisplay: item = 3; break;
219 case graphics::NoDisplay: item = 0; break;
221 dialog_->showCB->setCurrentIndex(item);
222 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
223 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
225 // the output section (width/height)
227 dialog_->Scale->setText(toqstr(igp.scale));
228 //igp.scale defaults to 100, so we treat it as empty
229 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
230 dialog_->scaleCB->blockSignals(true);
231 dialog_->scaleCB->setChecked(scaleChecked);
232 dialog_->scaleCB->blockSignals(false);
233 dialog_->Scale->setEnabled(scaleChecked);
235 lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
237 bool const widthChecked = !dialog_->Width->text().isEmpty() &&
238 dialog_->Width->text() != "auto";
239 dialog_->WidthCB->blockSignals(true);
240 dialog_->WidthCB->setChecked(widthChecked);
241 dialog_->WidthCB->blockSignals(false);
242 dialog_->Width->setEnabled(widthChecked);
243 dialog_->widthUnit->setEnabled(widthChecked);
245 lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
247 bool const heightChecked = !dialog_->Height->text().isEmpty()
248 && dialog_->Height->text() != "auto";
249 dialog_->HeightCB->blockSignals(true);
250 dialog_->HeightCB->setChecked(heightChecked);
251 dialog_->HeightCB->blockSignals(false);
252 dialog_->Height->setEnabled(heightChecked);
253 dialog_->heightUnit->setEnabled(heightChecked);
255 dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
256 dialog_->WidthCB->setEnabled(!scaleChecked);
257 dialog_->HeightCB->setEnabled(!scaleChecked);
258 dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
260 dialog_->setAutoText();
262 dialog_->angle->setText(toqstr(igp.rotateAngle));
263 dialog_->rotateOrderCB->setChecked(igp.scaleBeforeRotation);
265 dialog_->rotateOrderCB->setEnabled((widthChecked ||
268 (igp.rotateAngle != "0"));
270 dialog_->origin->clear();
272 vector<RotationOriginPair> origindata = getRotationOriginData();
273 vector<docstring> const origin_lang = getFirst(origindata);
274 GuiGraphics::origin_ltx = getSecond(origindata);
276 for (vector<docstring>::const_iterator it = origin_lang.begin();
277 it != origin_lang.end(); ++it)
278 dialog_->origin->addItem(toqstr(*it));
280 if (!igp.rotateOrigin.empty())
281 dialog_->origin->setCurrentIndex(
282 getItemNo(origin_ltx, igp.rotateOrigin));
284 dialog_->origin->setCurrentIndex(0);
286 // disable edit button when no filename is present
287 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
290 dialog_->latexoptions->setText(toqstr(igp.special));
294 void GuiGraphics::apply()
296 InsetGraphicsParams & igp = controller().params();
298 igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
299 kernel().bufferFilepath());
303 if (controller().bbChanged) {
305 string lbX(fromqstr(dialog_->lbX->text()));
306 string lbY(fromqstr(dialog_->lbY->text()));
307 string rtX(fromqstr(dialog_->rtX->text()));
308 string rtY(fromqstr(dialog_->rtY->text()));
310 convert<int>(lbX) + convert<int>(lbY) +
311 convert<int>(rtX) + convert<int>(rtX);
316 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
320 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
324 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
328 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
333 igp.draft = dialog_->draftCB->isChecked();
334 igp.clip = dialog_->clip->isChecked();
335 igp.subcaption = dialog_->subfigure->isChecked();
336 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
338 switch (dialog_->showCB->currentIndex()) {
339 case 0: igp.display = graphics::DefaultDisplay; break;
340 case 1: igp.display = graphics::MonochromeDisplay; break;
341 case 2: igp.display = graphics::GrayscaleDisplay; break;
342 case 3: igp.display = graphics::ColorDisplay; break;
346 if (!dialog_->displayGB->isChecked())
347 igp.display = graphics::NoDisplay;
349 //the graphics section
350 if (dialog_->scaleCB->isChecked() && !dialog_->Scale->text().isEmpty()) {
351 igp.scale = fromqstr(dialog_->Scale->text());
352 igp.width = Length("0pt");
353 igp.height = Length("0pt");
354 igp.keepAspectRatio = false;
356 igp.scale = string();
357 igp.width = dialog_->WidthCB->isChecked() ?
358 //Note that this works even if dialog_->Width is "auto", since in
359 //that case we get "0pt".
360 Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
362 igp.height = dialog_->HeightCB->isChecked() ?
363 Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
365 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
366 dialog_->aspectratio->isChecked() &&
367 igp.width.value() > 0 && igp.height.value() > 0;
370 igp.noUnzip = dialog_->unzipCB->isChecked();
372 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
374 igp.rotateAngle = fromqstr(dialog_->angle->text());
376 double rotAngle = convert<double>(igp.rotateAngle);
377 if (std::abs(rotAngle) > 360.0) {
378 rotAngle -= 360.0 * floor(rotAngle / 360.0);
379 igp.rotateAngle = convert<string>(rotAngle);
382 // save the latex name for the origin. If it is the default
383 // then origin_ltx returns ""
385 GuiGraphics::origin_ltx[dialog_->origin->currentIndex()];
387 igp.scaleBeforeRotation = dialog_->rotateOrderCB->isChecked();
389 // more latex options
390 igp.special = fromqstr(dialog_->latexoptions->text());
394 void GuiGraphics::getBB()
396 string const filename(fromqstr(dialog_->filename->text()));
397 if (!filename.empty()) {
398 string const bb(controller().readBB(filename));
400 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
401 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
402 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
403 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
404 // the default units for the bb values when reading
406 dialog_->lbXunit->setCurrentIndex(0);
407 dialog_->lbYunit->setCurrentIndex(0);
408 dialog_->rtXunit->setCurrentIndex(0);
409 dialog_->rtYunit->setCurrentIndex(0);
411 controller().bbChanged = false;
416 bool GuiGraphics::isValid()
418 return !dialog_->filename->text().isEmpty();
421 } // namespace frontend