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.
16 #include "ControlGraphics.h"
17 #include "controllers/helper_funcs.h"
18 #include "support/lstrings.h"
19 #include "support/tostr.h"
20 #include "support/lyxlib.h"
21 #include "insets/insetgraphicsParams.h"
24 #include "lengthcombo.h"
25 #include "qt_helpers.h"
26 #include "lengthcommon.h"
28 #include <qlineedit.h>
29 #include <qpushbutton.h>
30 #include <qcheckbox.h>
31 #include <qgroupbox.h>
34 #include "QGraphicsDialog.h"
35 #include "QGraphics.h"
38 using lyx::support::float_equal;
39 using lyx::support::strToDbl;
40 using lyx::support::strToInt;
41 using lyx::support::token;
47 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
49 QGraphics::QGraphics(Dialog & parent)
50 : base_class(parent, _("LyX: Graphics"))
55 void QGraphics::build_dialog()
57 dialog_.reset(new QGraphicsDialog(this));
59 bcview().setOK(dialog_->okPB);
60 bcview().setApply(dialog_->applyPB);
61 bcview().setRestore(dialog_->restorePB);
62 bcview().setCancel(dialog_->closePB);
64 bcview().addReadOnly(dialog_->rotateGB);
65 bcview().addReadOnly(dialog_->latexoptions);
66 bcview().addReadOnly(dialog_->subfigure);
67 bcview().addReadOnly(dialog_->subcaption);
68 bcview().addReadOnly(dialog_->filenameL);
69 bcview().addReadOnly(dialog_->filename);
70 bcview().addReadOnly(dialog_->browsePB);
71 bcview().addReadOnly(dialog_->unzipCB);
72 bcview().addReadOnly(dialog_->filename);
73 bcview().addReadOnly(dialog_->lbX);
74 bcview().addReadOnly(dialog_->lbY);
75 bcview().addReadOnly(dialog_->rtX);
76 bcview().addReadOnly(dialog_->rtY);
77 bcview().addReadOnly(dialog_->lbXunit);
78 bcview().addReadOnly(dialog_->lbYunit);
79 bcview().addReadOnly(dialog_->rtXunit);
80 bcview().addReadOnly(dialog_->rtYunit);
81 bcview().addReadOnly(dialog_->draftCB);
82 bcview().addReadOnly(dialog_->clip);
83 bcview().addReadOnly(dialog_->unzipCB);
84 bcview().addReadOnly(dialog_->subfigure);
85 bcview().addReadOnly(dialog_->subcaption);
86 bcview().addReadOnly(dialog_->showCB);
87 bcview().addReadOnly(dialog_->width);
88 bcview().addReadOnly(dialog_->height);
89 bcview().addReadOnly(dialog_->displayCB);
90 bcview().addReadOnly(dialog_->displayscale);
91 bcview().addReadOnly(dialog_->widthUnit);
92 bcview().addReadOnly(dialog_->heightUnit);
93 bcview().addReadOnly(dialog_->aspectratio);
94 bcview().addReadOnly(dialog_->angle);
95 bcview().addReadOnly(dialog_->origin);
96 bcview().addReadOnly(dialog_->latexoptions);
97 bcview().addReadOnly(dialog_->getPB);
103 // returns the number of the string s in the vector v
104 int getItemNo(vector<string> v, string const & s) {
105 vector<string>::const_iterator cit =
106 find(v.begin(), v.end(), s);
107 return (cit != v.end()) ? int(cit - v.begin()) : 0;
110 // returns the number of the unit in the array unit_name,
111 // which is defined in lengthcommon.C
112 int getUnitNo(char const * const c[], string const & s) {
114 while (i < num_units && s != c[i])
116 return (i < num_units) ? i : 0;
122 void QGraphics::update_contents()
124 // clear and fill in the comboboxes
125 vector<string> const bb_units = frnt::getBBUnits();
126 dialog_->lbXunit->clear();
127 dialog_->lbYunit->clear();
128 dialog_->rtXunit->clear();
129 dialog_->rtYunit->clear();
130 for (vector<string>::const_iterator it = bb_units.begin();
131 it != bb_units.end(); ++it) {
132 dialog_->lbXunit->insertItem(toqstr(*it), -1);
133 dialog_->lbYunit->insertItem(toqstr(*it), -1);
134 dialog_->rtXunit->insertItem(toqstr(*it), -1);
135 dialog_->rtYunit->insertItem(toqstr(*it), -1);
138 InsetGraphicsParams & igp = controller().params();
140 // set the right default unit
141 LyXLength::UNIT unitDefault = LyXLength::CM;
142 switch (lyxrc.default_papersize) {
143 case PAPER_DEFAULT: break;
146 case PAPER_LEGALPAPER:
147 case PAPER_EXECUTIVEPAPER:
148 unitDefault = LyXLength::IN;
155 unitDefault = LyXLength::CM;
160 igp.filename.outputFilename(kernel().bufferFilepath());
161 dialog_->filename->setText(toqstr(name));
163 // set the bounding box values
164 if (igp.bb.empty()) {
165 string const bb = controller().readBB(igp.filename.absFilename());
166 // the values from the file always have the bigpoint-unit bp
167 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
168 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
169 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
170 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
171 dialog_->lbXunit->setCurrentItem(0);
172 dialog_->lbYunit->setCurrentItem(0);
173 dialog_->rtXunit->setCurrentItem(0);
174 dialog_->rtYunit->setCurrentItem(0);
175 controller().bbChanged = false;
177 // get the values from the inset
179 string const xl(token(igp.bb, ' ', 0));
180 string const yl(token(igp.bb, ' ', 1));
181 string const xr(token(igp.bb, ' ', 2));
182 string const yr(token(igp.bb, ' ', 3));
183 if (isValidLength(xl, &anyLength)) {
184 dialog_->lbX->setText(toqstr(tostr(anyLength.value())));
185 string const unit(unit_name[anyLength.unit()]);
186 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
188 dialog_->lbX->setText(toqstr(xl));
190 if (isValidLength(yl, &anyLength)) {
191 dialog_->lbY->setText(toqstr(tostr(anyLength.value())));
192 string const unit(unit_name[anyLength.unit()]);
193 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
195 dialog_->lbY->setText(toqstr(xl));
197 if (isValidLength(xr, &anyLength)) {
198 dialog_->rtX->setText(toqstr(tostr(anyLength.value())));
199 string const unit(unit_name[anyLength.unit()]);
200 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
202 dialog_->rtX->setText(toqstr(xl));
204 if (isValidLength(yr, &anyLength)) {
205 dialog_->rtY->setText(toqstr(tostr(anyLength.value())));
206 string const unit(unit_name[anyLength.unit()]);
207 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
209 dialog_->rtY->setText(toqstr(xl));
211 controller().bbChanged = true;
214 // Update the draft and clip mode
215 dialog_->draftCB->setChecked(igp.draft);
216 dialog_->clip->setChecked(igp.clip);
217 dialog_->unzipCB->setChecked(igp.noUnzip);
219 // Update the subcaption check button and input field
220 dialog_->subfigure->setChecked(igp.subcaption);
221 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
224 switch (igp.display) {
225 case lyx::graphics::DefaultDisplay: item = 0; break;
226 case lyx::graphics::MonochromeDisplay: item = 1; break;
227 case lyx::graphics::GrayscaleDisplay: item = 2; break;
228 case lyx::graphics::ColorDisplay: item = 3; break;
229 case lyx::graphics::NoDisplay: item = 0; break;
231 dialog_->showCB->setCurrentItem(item);
232 dialog_->showCB->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
233 dialog_->displayCB->setChecked(igp.display != lyx::graphics::NoDisplay);
234 dialog_->displayscale->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
235 dialog_->displayscale->setText(toqstr(tostr(igp.lyxscale)));
237 //// the output section (width/height)
238 // set the length combo boxes
239 // only the width has the possibility for scale%. The original
240 // units are defined in lengthcommon.C
241 // 1. the width (a listttype)
242 dialog_->widthUnit->clear();
243 dialog_->widthUnit->insertItem(qt_("Scale%"));
244 for (int i = 0; i < num_units; i++)
245 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
247 if (!float_equal(igp.scale, 0.0, 0.05)) {
248 // there is a scale value > 0.05
249 dialog_->width->setText(toqstr(tostr(igp.scale)));
250 dialog_->widthUnit->setCurrentItem(0);
252 // no scale means default width/height
253 dialog_->width->setText(toqstr(tostr(igp.width.value())));
254 // the width cannot have a unitDefault, because
255 // it is a "Scale%" or another user defined unit!
256 // +1 instead of the "Scale%" option
257 int unit_ = igp.width.unit();
258 dialog_->widthUnit->setCurrentItem(unit_ + 1);
260 // 2. the height (a lengthgcombo type)
261 dialog_->height->setText(toqstr(tostr(igp.height.value())));
262 LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
263 igp.height.unit() : unitDefault;
264 dialog_->heightUnit->setCurrentItem(unit_);
266 // enable height input in case of non "Scale%" as width-unit
267 bool use_height = (dialog_->widthUnit->currentItem() > 0);
268 dialog_->height->setEnabled(use_height);
269 dialog_->heightUnit->setEnabled(use_height);
271 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
273 dialog_->angle->setText(toqstr(tostr(igp.rotateAngle)));
275 dialog_->origin->clear();
277 using namespace frnt;
278 vector<RotationOriginPair> origindata = getRotationOriginData();
279 vector<string> const origin_lang = getFirst(origindata);
280 QGraphics::origin_ltx = getSecond(origindata);
282 for (vector<string>::const_iterator it = origin_lang.begin();
283 it != origin_lang.end(); ++it)
284 dialog_->origin->insertItem(toqstr(*it), -1);
286 if (!igp.rotateOrigin.empty())
287 dialog_->origin->setCurrentItem(
288 ::getItemNo(origin_ltx, igp.rotateOrigin));
290 dialog_->origin->setCurrentItem(0);
293 dialog_->latexoptions->setText(toqstr(igp.special));
297 void QGraphics::apply()
299 InsetGraphicsParams & igp = controller().params();
301 igp.filename.set(fromqstr(dialog_->filename->text()),
302 kernel().bufferFilepath());
306 if (controller().bbChanged) {
308 string lbX(fromqstr(dialog_->lbX->text()));
309 string lbY(fromqstr(dialog_->lbY->text()));
310 string rtX(fromqstr(dialog_->rtX->text()));
311 string rtY(fromqstr(dialog_->rtY->text()));
313 strToInt(lbX) + strToInt(lbY) +
314 strToInt(rtX) + strToInt(rtX);
319 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
323 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
327 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
331 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
336 igp.draft = dialog_->draftCB->isChecked();
337 igp.clip = dialog_->clip->isChecked();
338 igp.subcaption = dialog_->subfigure->isChecked();
339 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
341 switch (dialog_->showCB->currentItem()) {
342 case 0: igp.display = lyx::graphics::DefaultDisplay; break;
343 case 1: igp.display = lyx::graphics::MonochromeDisplay; break;
344 case 2: igp.display = lyx::graphics::GrayscaleDisplay; break;
345 case 3: igp.display = lyx::graphics::ColorDisplay; break;
349 if (!dialog_->displayCB->isChecked())
350 igp.display = lyx::graphics::NoDisplay;
352 string value = fromqstr(dialog_->width->text());
353 if (dialog_->widthUnit->currentItem() > 0) {
354 // width/height combination
355 QString const text = dialog_->widthUnit->currentText();
356 int const unitNo = getUnitNo(unit_name_gui, fromqstr(text));
357 igp.width = LyXLength(value + unit_name_ltx[unitNo]);
360 // scaling instead of a width
361 igp.scale = strToDbl(value);
362 igp.width = LyXLength();
364 value = fromqstr(dialog_->height->text());
365 QString text = dialog_->heightUnit->currentText();
366 int const unitNo = getUnitNo(unit_name_gui, fromqstr(text));
367 igp.height = LyXLength(value + unit_name_ltx[unitNo]);
369 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
371 igp.noUnzip = dialog_->unzipCB->isChecked();
373 igp.lyxscale = strToInt(fromqstr(dialog_->displayscale->text()));
375 igp.rotateAngle = strToDbl(fromqstr(dialog_->angle->text()));
376 #warning Guess what happens if the user enters '1e30' here?
377 while (igp.rotateAngle < -360.0)
378 igp.rotateAngle += 360.0;
379 while (igp.rotateAngle > 360.0)
380 igp.rotateAngle -= 360.0;
382 // save the latex name for the origin. If it is the default
383 // then origin_ltx returns ""
385 QGraphics::origin_ltx[dialog_->origin->currentItem()];
387 // more latex options
388 igp.special = fromqstr(dialog_->latexoptions->text());
392 void QGraphics::getBB()
394 string const filename(fromqstr(dialog_->filename->text()));
395 if (!filename.empty()) {
396 string const bb(controller().readBB(filename));
398 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
399 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
400 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
401 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
402 // the default units for the bb values when reading
404 dialog_->lbXunit->setCurrentItem(0);
405 dialog_->lbYunit->setCurrentItem(0);
406 dialog_->rtXunit->setCurrentItem(0);
407 dialog_->rtYunit->setCurrentItem(0);
409 controller().bbChanged = false;
414 bool QGraphics::isValid()
416 return !dialog_->filename->text().isEmpty();
420 void QGraphics::slotEdit()
422 controller().editGraphics();