3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
10 * Full author contact details are available in file CREDITS.
15 #include "QGraphics.h"
17 #include "checkedwidgets.h"
18 #include "lengthcombo.h"
19 #include "QGraphicsDialog.h"
21 #include "qt_helpers.h"
22 #include "validators.h"
24 #include "lengthcommon.h"
27 #include "controllers/ControlGraphics.h"
28 #include "controllers/helper_funcs.h"
30 #include "insets/insetgraphicsParams.h"
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
36 #include <qlineedit.h>
37 #include <qpushbutton.h>
38 #include <qcheckbox.h>
39 #include <qgroupbox.h>
44 using lyx::support::float_equal;
45 using lyx::support::token;
47 #ifndef CXX_GLOBAL_CSTD
57 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
59 QGraphics::QGraphics(Dialog & parent)
60 : base_class(parent, _("Graphics"))
65 void QGraphics::build_dialog()
67 dialog_.reset(new QGraphicsDialog(this));
69 bcview().setOK(dialog_->okPB);
70 bcview().setApply(dialog_->applyPB);
71 bcview().setRestore(dialog_->restorePB);
72 bcview().setCancel(dialog_->closePB);
74 bcview().addReadOnly(dialog_->rotateGB);
75 bcview().addReadOnly(dialog_->latexoptions);
76 bcview().addReadOnly(dialog_->subfigure);
77 bcview().addReadOnly(dialog_->subcaption);
78 bcview().addReadOnly(dialog_->filenameL);
79 bcview().addReadOnly(dialog_->filename);
80 bcview().addReadOnly(dialog_->browsePB);
81 bcview().addReadOnly(dialog_->unzipCB);
82 bcview().addReadOnly(dialog_->filename);
83 bcview().addReadOnly(dialog_->lbX);
84 bcview().addReadOnly(dialog_->lbY);
85 bcview().addReadOnly(dialog_->rtX);
86 bcview().addReadOnly(dialog_->rtY);
87 bcview().addReadOnly(dialog_->lbXunit);
88 bcview().addReadOnly(dialog_->lbYunit);
89 bcview().addReadOnly(dialog_->rtXunit);
90 bcview().addReadOnly(dialog_->rtYunit);
91 bcview().addReadOnly(dialog_->draftCB);
92 bcview().addReadOnly(dialog_->clip);
93 bcview().addReadOnly(dialog_->unzipCB);
94 bcview().addReadOnly(dialog_->subfigure);
95 bcview().addReadOnly(dialog_->subcaption);
96 bcview().addReadOnly(dialog_->showCB);
97 bcview().addReadOnly(dialog_->width);
98 bcview().addReadOnly(dialog_->height);
99 bcview().addReadOnly(dialog_->displayCB);
100 bcview().addReadOnly(dialog_->displayscale);
101 bcview().addReadOnly(dialog_->widthUnit);
102 bcview().addReadOnly(dialog_->heightUnit);
103 bcview().addReadOnly(dialog_->aspectratio);
104 bcview().addReadOnly(dialog_->angle);
105 bcview().addReadOnly(dialog_->origin);
106 bcview().addReadOnly(dialog_->latexoptions);
107 bcview().addReadOnly(dialog_->getPB);
109 // initialize the length validator
110 addCheckedLineEdit(bcview(), dialog_->width, dialog_->sizewidthL_2);
111 addCheckedLineEdit(bcview(), dialog_->height, dialog_->sizeheightL_2);
112 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
113 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL_2);
114 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
115 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
116 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
117 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
118 addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
124 // returns the number of the string s in the vector v
125 int getItemNo(vector<string> v, string const & s) {
126 vector<string>::const_iterator cit =
127 find(v.begin(), v.end(), s);
128 return (cit != v.end()) ? int(cit - v.begin()) : 0;
134 void QGraphics::update_contents()
136 PathValidator * path_validator = getPathValidator(dialog_->filename);
138 path_validator->setChecker(kernel().docType(), lyxrc);
140 // clear and fill in the comboboxes
141 vector<string> const bb_units = lyx::frontend::getBBUnits();
142 dialog_->lbXunit->clear();
143 dialog_->lbYunit->clear();
144 dialog_->rtXunit->clear();
145 dialog_->rtYunit->clear();
146 for (vector<string>::const_iterator it = bb_units.begin();
147 it != bb_units.end(); ++it) {
148 dialog_->lbXunit->insertItem(toqstr(*it), -1);
149 dialog_->lbYunit->insertItem(toqstr(*it), -1);
150 dialog_->rtXunit->insertItem(toqstr(*it), -1);
151 dialog_->rtYunit->insertItem(toqstr(*it), -1);
154 InsetGraphicsParams & igp = controller().params();
156 // set the right default unit
157 LyXLength::UNIT unitDefault = LyXLength::CM;
158 switch (lyxrc.default_papersize) {
161 case PAPER_USEXECUTIVE:
162 unitDefault = LyXLength::IN;
169 igp.filename.outputFilename(kernel().bufferFilepath());
170 dialog_->filename->setText(toqstr(name));
172 // set the bounding box values
173 if (igp.bb.empty()) {
174 string const bb = controller().readBB(igp.filename.absFilename());
175 // the values from the file always have the bigpoint-unit bp
176 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
177 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
178 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
179 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
180 dialog_->lbXunit->setCurrentItem(0);
181 dialog_->lbYunit->setCurrentItem(0);
182 dialog_->rtXunit->setCurrentItem(0);
183 dialog_->rtYunit->setCurrentItem(0);
184 controller().bbChanged = false;
186 // get the values from the inset
188 string const xl(token(igp.bb, ' ', 0));
189 string const yl(token(igp.bb, ' ', 1));
190 string const xr(token(igp.bb, ' ', 2));
191 string const yr(token(igp.bb, ' ', 3));
192 if (isValidLength(xl, &anyLength)) {
193 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
194 string const unit(unit_name[anyLength.unit()]);
195 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
197 dialog_->lbX->setText(toqstr(xl));
199 if (isValidLength(yl, &anyLength)) {
200 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
201 string const unit(unit_name[anyLength.unit()]);
202 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
204 dialog_->lbY->setText(toqstr(xl));
206 if (isValidLength(xr, &anyLength)) {
207 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
208 string const unit(unit_name[anyLength.unit()]);
209 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
211 dialog_->rtX->setText(toqstr(xl));
213 if (isValidLength(yr, &anyLength)) {
214 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
215 string const unit(unit_name[anyLength.unit()]);
216 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
218 dialog_->rtY->setText(toqstr(xl));
220 controller().bbChanged = true;
223 // Update the draft and clip mode
224 dialog_->draftCB->setChecked(igp.draft);
225 dialog_->clip->setChecked(igp.clip);
226 dialog_->unzipCB->setChecked(igp.noUnzip);
228 // Update the subcaption check button and input field
229 dialog_->subfigure->setChecked(igp.subcaption);
230 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
233 switch (igp.display) {
234 case lyx::graphics::DefaultDisplay: item = 0; break;
235 case lyx::graphics::MonochromeDisplay: item = 1; break;
236 case lyx::graphics::GrayscaleDisplay: item = 2; break;
237 case lyx::graphics::ColorDisplay: item = 3; break;
238 case lyx::graphics::NoDisplay: item = 0; break;
240 dialog_->showCB->setCurrentItem(item);
241 dialog_->showCB->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
242 dialog_->displayCB->setChecked(igp.display != lyx::graphics::NoDisplay);
243 dialog_->displayscale->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
244 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
246 //// the output section (width/height)
247 // set the length combo boxes
248 // only the width has the possibility for scale%. The original
249 // units are defined in lengthcommon.C
250 // 1. the width (a listttype)
251 dialog_->widthUnit->clear();
252 dialog_->widthUnit->insertItem(qt_("Scale%"));
253 for (int i = 0; i < num_units; i++)
254 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
256 if (!igp.scale.empty()
257 && !float_equal(convert<double>(igp.scale), 0.0, 0.05)
258 || igp.width.empty()) {
259 dialog_->width->setText(toqstr(igp.scale));
260 dialog_->widthUnit->setCurrentItem(0);
262 // no scale means default width/height
263 dialog_->width->setText(toqstr(convert<string>(igp.width.value())));
264 // the width cannot have a unitDefault, because
265 // it is a "Scale%" or another user defined unit!
266 // +1 instead of the "Scale%" option
267 int unit_ = igp.width.unit();
268 dialog_->widthUnit->setCurrentItem(unit_ + 1);
270 // 2. the height (a lengthgcombo type)
271 lengthToWidgets(dialog_->height, dialog_->heightUnit,
272 igp.height.asString(), unitDefault);
274 // enable height input in case of non "Scale%" as width-unit
275 bool use_height = (dialog_->widthUnit->currentItem() > 0);
276 dialog_->height->setEnabled(use_height);
277 dialog_->heightUnit->setEnabled(use_height);
279 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
281 dialog_->angle->setText(toqstr(igp.rotateAngle));
283 dialog_->origin->clear();
285 using namespace lyx::frontend;
286 vector<RotationOriginPair> origindata = getRotationOriginData();
287 vector<string> const origin_lang = getFirst(origindata);
288 QGraphics::origin_ltx = getSecond(origindata);
290 for (vector<string>::const_iterator it = origin_lang.begin();
291 it != origin_lang.end(); ++it)
292 dialog_->origin->insertItem(toqstr(*it), -1);
294 if (!igp.rotateOrigin.empty())
295 dialog_->origin->setCurrentItem(
296 getItemNo(origin_ltx, igp.rotateOrigin));
298 dialog_->origin->setCurrentItem(0);
301 dialog_->latexoptions->setText(toqstr(igp.special));
305 void QGraphics::apply()
307 InsetGraphicsParams & igp = controller().params();
309 igp.filename.set(fromqstr(dialog_->filename->text()),
310 kernel().bufferFilepath());
314 if (controller().bbChanged) {
316 string lbX(fromqstr(dialog_->lbX->text()));
317 string lbY(fromqstr(dialog_->lbY->text()));
318 string rtX(fromqstr(dialog_->rtX->text()));
319 string rtY(fromqstr(dialog_->rtY->text()));
321 convert<int>(lbX) + convert<int>(lbY) +
322 convert<int>(rtX) + convert<int>(rtX);
327 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
331 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
335 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
339 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
344 igp.draft = dialog_->draftCB->isChecked();
345 igp.clip = dialog_->clip->isChecked();
346 igp.subcaption = dialog_->subfigure->isChecked();
347 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
349 switch (dialog_->showCB->currentItem()) {
350 case 0: igp.display = lyx::graphics::DefaultDisplay; break;
351 case 1: igp.display = lyx::graphics::MonochromeDisplay; break;
352 case 2: igp.display = lyx::graphics::GrayscaleDisplay; break;
353 case 3: igp.display = lyx::graphics::ColorDisplay; break;
357 if (!dialog_->displayCB->isChecked())
358 igp.display = lyx::graphics::NoDisplay;
360 string value = fromqstr(dialog_->width->text());
361 if (dialog_->widthUnit->currentItem() > 0 || isValidLength(value)) {
362 // width/height combination
364 widgetsToLength(dialog_->width, dialog_->widthUnit);
365 igp.scale = string();
367 // scaling instead of a width
369 igp.width = LyXLength();
371 value = fromqstr(dialog_->height->text());
373 LyXLength(widgetsToLength(dialog_->height, dialog_->heightUnit));
375 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
377 igp.noUnzip = dialog_->unzipCB->isChecked();
379 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
381 igp.rotateAngle = fromqstr(dialog_->angle->text());
383 double rotAngle = convert<double>(igp.rotateAngle);
384 if (std::abs(rotAngle) > 360.0) {
385 rotAngle -= 360.0 * floor(rotAngle / 360.0);
386 igp.rotateAngle = convert<string>(rotAngle);
389 // save the latex name for the origin. If it is the default
390 // then origin_ltx returns ""
392 QGraphics::origin_ltx[dialog_->origin->currentItem()];
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->setCurrentItem(0);
412 dialog_->lbYunit->setCurrentItem(0);
413 dialog_->rtXunit->setCurrentItem(0);
414 dialog_->rtYunit->setCurrentItem(0);
416 controller().bbChanged = false;
421 bool QGraphics::isValid()
423 return !dialog_->filename->text().isEmpty();
426 } // namespace frontend