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 "QGraphics.h"
18 #include "CheckedLineEdit.h"
19 #include "LengthCombo.h"
20 #include "QGraphicsDialog.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "lengthcommon.h"
28 #include "controllers/ControlGraphics.h"
29 #include "controllers/frontend_helpers.h"
31 #include "insets/InsetGraphicsParams.h"
33 #include "support/convert.h"
34 #include "support/lstrings.h"
35 #include "support/lyxlib.h"
36 #include "support/os.h"
39 #include <QPushButton>
45 using lyx::support::float_equal;
46 using lyx::support::token;
48 using lyx::support::os::internal_path;
50 #ifndef CXX_GLOBAL_CSTD
60 typedef QController<ControlGraphics, QView<QGraphicsDialog> > graphics_base_class;
62 QGraphics::QGraphics(Dialog & parent)
63 : graphics_base_class(parent, _("Graphics"))
68 void QGraphics::build_dialog()
70 dialog_.reset(new QGraphicsDialog(this));
72 bcview().setOK(dialog_->okPB);
73 bcview().setApply(dialog_->applyPB);
74 bcview().setRestore(dialog_->restorePB);
75 bcview().setCancel(dialog_->closePB);
77 bcview().addReadOnly(dialog_->latexoptions);
78 bcview().addReadOnly(dialog_->subfigure);
79 bcview().addReadOnly(dialog_->filenameL);
80 bcview().addReadOnly(dialog_->filename);
81 bcview().addReadOnly(dialog_->browsePB);
82 bcview().addReadOnly(dialog_->unzipCB);
83 bcview().addReadOnly(dialog_->bbFrame);
84 bcview().addReadOnly(dialog_->draftCB);
85 bcview().addReadOnly(dialog_->clip);
86 bcview().addReadOnly(dialog_->unzipCB);
87 bcview().addReadOnly(dialog_->displayGB);
88 bcview().addReadOnly(dialog_->sizeGB);
89 bcview().addReadOnly(dialog_->rotationGB);
90 bcview().addReadOnly(dialog_->latexoptions);
91 bcview().addReadOnly(dialog_->getPB);
92 bcview().addReadOnly(dialog_->rotateOrderCB);
94 // initialize the length validator
95 addCheckedLineEdit(bcview(), dialog_->Scale, dialog_->scaleCB);
96 addCheckedLineEdit(bcview(), dialog_->Width, dialog_->WidthCB);
97 addCheckedLineEdit(bcview(), dialog_->Height, dialog_->HeightCB);
98 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
99 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
100 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
101 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
102 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
103 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
104 addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
110 // returns the number of the string s in the vector v
111 int getItemNo(vector<string> v, string const & s) {
112 vector<string>::const_iterator cit =
113 find(v.begin(), v.end(), s);
114 return (cit != v.end()) ? int(cit - v.begin()) : 0;
120 void QGraphics::update_contents()
122 PathValidator * path_validator = getPathValidator(dialog_->filename);
124 path_validator->setChecker(kernel().docType(), lyxrc);
126 // clear and fill in the comboboxes
127 vector<string> const bb_units = frontend::getBBUnits();
128 dialog_->lbXunit->clear();
129 dialog_->lbYunit->clear();
130 dialog_->rtXunit->clear();
131 dialog_->rtYunit->clear();
132 for (vector<string>::const_iterator it = bb_units.begin();
133 it != bb_units.end(); ++it) {
134 dialog_->lbXunit->addItem(toqstr(*it));
135 dialog_->lbYunit->addItem(toqstr(*it));
136 dialog_->rtXunit->addItem(toqstr(*it));
137 dialog_->rtYunit->addItem(toqstr(*it));
140 InsetGraphicsParams & igp = controller().params();
142 // set the right default unit
143 Length::UNIT unitDefault = Length::CM;
144 switch (lyxrc.default_papersize) {
147 case PAPER_USEXECUTIVE:
148 unitDefault = Length::IN;
155 igp.filename.outputFilename(kernel().bufferFilepath());
156 dialog_->filename->setText(toqstr(name));
158 // set the bounding box values
159 if (igp.bb.empty()) {
160 string const bb = controller().readBB(igp.filename.absFilename());
161 // the values from the file always have the bigpoint-unit bp
162 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
163 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
164 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
165 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
166 dialog_->lbXunit->setCurrentIndex(0);
167 dialog_->lbYunit->setCurrentIndex(0);
168 dialog_->rtXunit->setCurrentIndex(0);
169 dialog_->rtYunit->setCurrentIndex(0);
170 controller().bbChanged = false;
172 // get the values from the inset
174 string const xl(token(igp.bb, ' ', 0));
175 string const yl(token(igp.bb, ' ', 1));
176 string const xr(token(igp.bb, ' ', 2));
177 string const yr(token(igp.bb, ' ', 3));
178 if (isValidLength(xl, &anyLength)) {
179 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
180 string const unit(unit_name[anyLength.unit()]);
181 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
183 dialog_->lbX->setText(toqstr(xl));
185 if (isValidLength(yl, &anyLength)) {
186 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
187 string const unit(unit_name[anyLength.unit()]);
188 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
190 dialog_->lbY->setText(toqstr(xl));
192 if (isValidLength(xr, &anyLength)) {
193 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
194 string const unit(unit_name[anyLength.unit()]);
195 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
197 dialog_->rtX->setText(toqstr(xl));
199 if (isValidLength(yr, &anyLength)) {
200 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
201 string const unit(unit_name[anyLength.unit()]);
202 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
204 dialog_->rtY->setText(toqstr(xl));
206 controller().bbChanged = true;
209 // Update the draft and clip mode
210 dialog_->draftCB->setChecked(igp.draft);
211 dialog_->clip->setChecked(igp.clip);
212 dialog_->unzipCB->setChecked(igp.noUnzip);
214 // Update the subcaption check button and input field
215 dialog_->subfigure->setChecked(igp.subcaption);
216 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
219 switch (igp.display) {
220 case graphics::DefaultDisplay: item = 0; break;
221 case graphics::MonochromeDisplay: item = 1; break;
222 case graphics::GrayscaleDisplay: item = 2; break;
223 case graphics::ColorDisplay: item = 3; break;
224 case graphics::NoDisplay: item = 0; break;
226 dialog_->showCB->setCurrentIndex(item);
227 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
228 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
230 // the output section (width/height)
232 dialog_->Scale->setText(toqstr(igp.scale));
233 //igp.scale defaults to 100, so we treat it as empty
234 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
235 dialog_->scaleCB->blockSignals(true);
236 dialog_->scaleCB->setChecked(scaleChecked);
237 dialog_->scaleCB->blockSignals(false);
238 dialog_->Scale->setEnabled(scaleChecked);
240 lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
242 bool const widthChecked = !dialog_->Width->text().isEmpty() &&
243 dialog_->Width->text() != "auto";
244 dialog_->WidthCB->blockSignals(true);
245 dialog_->WidthCB->setChecked(widthChecked);
246 dialog_->WidthCB->blockSignals(false);
247 dialog_->Width->setEnabled(widthChecked);
248 dialog_->widthUnit->setEnabled(widthChecked);
250 lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
252 bool const heightChecked = !dialog_->Height->text().isEmpty()
253 && dialog_->Height->text() != "auto";
254 dialog_->HeightCB->blockSignals(true);
255 dialog_->HeightCB->setChecked(heightChecked);
256 dialog_->HeightCB->blockSignals(false);
257 dialog_->Height->setEnabled(heightChecked);
258 dialog_->heightUnit->setEnabled(heightChecked);
260 dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
261 dialog_->WidthCB->setEnabled(!scaleChecked);
262 dialog_->HeightCB->setEnabled(!scaleChecked);
263 dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
265 dialog_->setAutoText();
267 dialog_->angle->setText(toqstr(igp.rotateAngle));
268 dialog_->rotateOrderCB->setChecked(igp.scaleBeforeRotation);
270 dialog_->rotateOrderCB->setEnabled((widthChecked ||
273 (igp.rotateAngle != "0"));
275 dialog_->origin->clear();
277 vector<RotationOriginPair> origindata = getRotationOriginData();
278 vector<docstring> const origin_lang = getFirst(origindata);
279 QGraphics::origin_ltx = getSecond(origindata);
281 for (vector<docstring>::const_iterator it = origin_lang.begin();
282 it != origin_lang.end(); ++it)
283 dialog_->origin->addItem(toqstr(*it));
285 if (!igp.rotateOrigin.empty())
286 dialog_->origin->setCurrentIndex(
287 getItemNo(origin_ltx, igp.rotateOrigin));
289 dialog_->origin->setCurrentIndex(0);
291 // disable edit button when no filename is present
292 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
295 dialog_->latexoptions->setText(toqstr(igp.special));
299 void QGraphics::apply()
301 InsetGraphicsParams & igp = controller().params();
303 igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
304 kernel().bufferFilepath());
308 if (controller().bbChanged) {
310 string lbX(fromqstr(dialog_->lbX->text()));
311 string lbY(fromqstr(dialog_->lbY->text()));
312 string rtX(fromqstr(dialog_->rtX->text()));
313 string rtY(fromqstr(dialog_->rtY->text()));
315 convert<int>(lbX) + convert<int>(lbY) +
316 convert<int>(rtX) + convert<int>(rtX);
321 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
325 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
329 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
333 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
338 igp.draft = dialog_->draftCB->isChecked();
339 igp.clip = dialog_->clip->isChecked();
340 igp.subcaption = dialog_->subfigure->isChecked();
341 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
343 switch (dialog_->showCB->currentIndex()) {
344 case 0: igp.display = graphics::DefaultDisplay; break;
345 case 1: igp.display = graphics::MonochromeDisplay; break;
346 case 2: igp.display = graphics::GrayscaleDisplay; break;
347 case 3: igp.display = graphics::ColorDisplay; break;
351 if (!dialog_->displayGB->isChecked())
352 igp.display = graphics::NoDisplay;
354 //the graphics section
355 if (dialog_->scaleCB->isChecked() && !dialog_->Scale->text().isEmpty()) {
356 igp.scale = fromqstr(dialog_->Scale->text());
357 igp.width = Length("0pt");
358 igp.height = Length("0pt");
359 igp.keepAspectRatio = false;
361 igp.scale = string();
362 igp.width = dialog_->WidthCB->isChecked() ?
363 //Note that this works even if dialog_->Width is "auto", since in
364 //that case we get "0pt".
365 Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
367 igp.height = dialog_->HeightCB->isChecked() ?
368 Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
370 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
371 dialog_->aspectratio->isChecked() &&
372 igp.width.value() > 0 && igp.height.value() > 0;
375 igp.noUnzip = dialog_->unzipCB->isChecked();
377 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
379 igp.rotateAngle = fromqstr(dialog_->angle->text());
381 double rotAngle = convert<double>(igp.rotateAngle);
382 if (std::abs(rotAngle) > 360.0) {
383 rotAngle -= 360.0 * floor(rotAngle / 360.0);
384 igp.rotateAngle = convert<string>(rotAngle);
387 // save the latex name for the origin. If it is the default
388 // then origin_ltx returns ""
390 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
392 igp.scaleBeforeRotation = dialog_->rotateOrderCB->isChecked();
394 // more latex options
395 igp.special = fromqstr(dialog_->latexoptions->text());
399 void QGraphics::getBB()
401 string const filename(fromqstr(dialog_->filename->text()));
402 if (!filename.empty()) {
403 string const bb(controller().readBB(filename));
405 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
406 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
407 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
408 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
409 // the default units for the bb values when reading
411 dialog_->lbXunit->setCurrentIndex(0);
412 dialog_->lbYunit->setCurrentIndex(0);
413 dialog_->rtXunit->setCurrentIndex(0);
414 dialog_->rtYunit->setCurrentIndex(0);
416 controller().bbChanged = false;
421 bool QGraphics::isValid()
423 return !dialog_->filename->text().isEmpty();
426 } // namespace frontend