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 "checkedwidgets.h"
19 #include "lengthcombo.h"
20 #include "QGraphicsDialog.h"
22 #include "qt_helpers.h"
23 #include "validators.h"
25 #include "lengthcommon.h"
28 #include "controllers/ControlGraphics.h"
29 #include "controllers/helper_funcs.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);
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 QGraphics::update_contents()
121 PathValidator * path_validator = getPathValidator(dialog_->filename);
123 path_validator->setChecker(kernel().docType(), lyxrc);
125 // clear and fill in the comboboxes
126 vector<string> const bb_units = frontend::getBBUnits();
127 dialog_->lbXunit->clear();
128 dialog_->lbYunit->clear();
129 dialog_->rtXunit->clear();
130 dialog_->rtYunit->clear();
131 for (vector<string>::const_iterator it = bb_units.begin();
132 it != bb_units.end(); ++it) {
133 dialog_->lbXunit->addItem(toqstr(*it));
134 dialog_->lbYunit->addItem(toqstr(*it));
135 dialog_->rtXunit->addItem(toqstr(*it));
136 dialog_->rtYunit->addItem(toqstr(*it));
139 InsetGraphicsParams & igp = controller().params();
141 // set the right default unit
142 LyXLength::UNIT unitDefault = LyXLength::CM;
143 switch (lyxrc.default_papersize) {
146 case PAPER_USEXECUTIVE:
147 unitDefault = LyXLength::IN;
154 igp.filename.outputFilename(kernel().bufferFilepath());
155 dialog_->filename->setText(toqstr(name));
157 // set the bounding box values
158 if (igp.bb.empty()) {
159 string const bb = controller().readBB(igp.filename.absFilename());
160 // the values from the file always have the bigpoint-unit bp
161 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
162 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
163 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
164 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
165 dialog_->lbXunit->setCurrentIndex(0);
166 dialog_->lbYunit->setCurrentIndex(0);
167 dialog_->rtXunit->setCurrentIndex(0);
168 dialog_->rtYunit->setCurrentIndex(0);
169 controller().bbChanged = false;
171 // get the values from the inset
173 string const xl(token(igp.bb, ' ', 0));
174 string const yl(token(igp.bb, ' ', 1));
175 string const xr(token(igp.bb, ' ', 2));
176 string const yr(token(igp.bb, ' ', 3));
177 if (isValidLength(xl, &anyLength)) {
178 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
179 string const unit(unit_name[anyLength.unit()]);
180 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
182 dialog_->lbX->setText(toqstr(xl));
184 if (isValidLength(yl, &anyLength)) {
185 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
186 string const unit(unit_name[anyLength.unit()]);
187 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
189 dialog_->lbY->setText(toqstr(xl));
191 if (isValidLength(xr, &anyLength)) {
192 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
193 string const unit(unit_name[anyLength.unit()]);
194 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
196 dialog_->rtX->setText(toqstr(xl));
198 if (isValidLength(yr, &anyLength)) {
199 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
200 string const unit(unit_name[anyLength.unit()]);
201 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
203 dialog_->rtY->setText(toqstr(xl));
205 controller().bbChanged = true;
208 // Update the draft and clip mode
209 dialog_->draftCB->setChecked(igp.draft);
210 dialog_->clip->setChecked(igp.clip);
211 dialog_->unzipCB->setChecked(igp.noUnzip);
213 // Update the subcaption check button and input field
214 dialog_->subfigure->setChecked(igp.subcaption);
215 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
218 switch (igp.display) {
219 case graphics::DefaultDisplay: item = 0; break;
220 case graphics::MonochromeDisplay: item = 1; break;
221 case graphics::GrayscaleDisplay: item = 2; break;
222 case graphics::ColorDisplay: item = 3; break;
223 case graphics::NoDisplay: item = 0; break;
225 dialog_->showCB->setCurrentIndex(item);
226 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
227 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
229 // the output section (width/height)
231 dialog_->Scale->setText(toqstr(igp.scale));
232 //igp.scale defaults to 100, so we treat it as empty
233 bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
234 dialog_->scaleCB->blockSignals(true);
235 dialog_->scaleCB->setChecked(scaleChecked);
236 dialog_->scaleCB->blockSignals(false);
237 dialog_->Scale->setEnabled(scaleChecked);
239 lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
241 bool const widthChecked = !dialog_->Width->text().isEmpty() &&
242 dialog_->Width->text() != "auto";
243 dialog_->WidthCB->blockSignals(true);
244 dialog_->WidthCB->setChecked(widthChecked);
245 dialog_->WidthCB->blockSignals(false);
246 dialog_->Width->setEnabled(widthChecked);
247 dialog_->widthUnit->setEnabled(widthChecked);
249 lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
251 bool const heightChecked = !dialog_->Height->text().isEmpty()
252 && dialog_->Height->text() != "auto";
253 dialog_->HeightCB->blockSignals(true);
254 dialog_->HeightCB->setChecked(heightChecked);
255 dialog_->HeightCB->blockSignals(false);
256 dialog_->Height->setEnabled(heightChecked);
257 dialog_->heightUnit->setEnabled(heightChecked);
259 dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
260 dialog_->WidthCB->setEnabled(!scaleChecked);
261 dialog_->HeightCB->setEnabled(!scaleChecked);
262 dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
264 dialog_->setAutoText();
266 dialog_->angle->setText(toqstr(igp.rotateAngle));
268 dialog_->origin->clear();
270 vector<RotationOriginPair> origindata = getRotationOriginData();
271 vector<docstring> const origin_lang = getFirst(origindata);
272 QGraphics::origin_ltx = getSecond(origindata);
274 for (vector<docstring>::const_iterator it = origin_lang.begin();
275 it != origin_lang.end(); ++it)
276 dialog_->origin->addItem(toqstr(*it));
278 if (!igp.rotateOrigin.empty())
279 dialog_->origin->setCurrentIndex(
280 getItemNo(origin_ltx, igp.rotateOrigin));
282 dialog_->origin->setCurrentIndex(0);
284 // disable edit button when no filename is present
285 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
288 dialog_->latexoptions->setText(toqstr(igp.special));
292 void QGraphics::apply()
294 InsetGraphicsParams & igp = controller().params();
296 igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
297 kernel().bufferFilepath());
301 if (controller().bbChanged) {
303 string lbX(fromqstr(dialog_->lbX->text()));
304 string lbY(fromqstr(dialog_->lbY->text()));
305 string rtX(fromqstr(dialog_->rtX->text()));
306 string rtY(fromqstr(dialog_->rtY->text()));
308 convert<int>(lbX) + convert<int>(lbY) +
309 convert<int>(rtX) + convert<int>(rtX);
314 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
318 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
322 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
326 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
331 igp.draft = dialog_->draftCB->isChecked();
332 igp.clip = dialog_->clip->isChecked();
333 igp.subcaption = dialog_->subfigure->isChecked();
334 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
336 switch (dialog_->showCB->currentIndex()) {
337 case 0: igp.display = graphics::DefaultDisplay; break;
338 case 1: igp.display = graphics::MonochromeDisplay; break;
339 case 2: igp.display = graphics::GrayscaleDisplay; break;
340 case 3: igp.display = graphics::ColorDisplay; break;
344 if (!dialog_->displayGB->isChecked())
345 igp.display = graphics::NoDisplay;
347 //the graphics section
348 if (dialog_->scaleCB->isChecked() && !dialog_->Scale->text().isEmpty()) {
349 igp.scale = fromqstr(dialog_->Scale->text());
350 igp.width = LyXLength("0pt");
351 igp.height = LyXLength("0pt");
352 igp.keepAspectRatio = false;
354 igp.scale = string();
355 igp.width = dialog_->WidthCB->isChecked() ?
356 //Note that this works even if dialog_->Width is "auto", since in
357 //that case we get "0pt".
358 LyXLength(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
360 igp.height = dialog_->HeightCB->isChecked() ?
361 LyXLength(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
363 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
364 dialog_->aspectratio->isChecked() &&
365 igp.width.value() > 0 && igp.height.value() > 0;
368 igp.noUnzip = dialog_->unzipCB->isChecked();
370 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
372 igp.rotateAngle = fromqstr(dialog_->angle->text());
374 double rotAngle = convert<double>(igp.rotateAngle);
375 if (std::abs(rotAngle) > 360.0) {
376 rotAngle -= 360.0 * floor(rotAngle / 360.0);
377 igp.rotateAngle = convert<string>(rotAngle);
380 // save the latex name for the origin. If it is the default
381 // then origin_ltx returns ""
383 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
385 // more latex options
386 igp.special = fromqstr(dialog_->latexoptions->text());
390 void QGraphics::getBB()
392 string const filename(fromqstr(dialog_->filename->text()));
393 if (!filename.empty()) {
394 string const bb(controller().readBB(filename));
396 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
397 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
398 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
399 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
400 // the default units for the bb values when reading
402 dialog_->lbXunit->setCurrentIndex(0);
403 dialog_->lbYunit->setCurrentIndex(0);
404 dialog_->rtXunit->setCurrentIndex(0);
405 dialog_->rtYunit->setCurrentIndex(0);
407 controller().bbChanged = false;
412 bool QGraphics::isValid()
414 return !dialog_->filename->text().isEmpty();
417 } // namespace frontend