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;
46 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
48 QGraphics::QGraphics(Dialog & parent)
49 : base_class(parent, _("LyX: Graphics"))
54 void QGraphics::build_dialog()
56 dialog_.reset(new QGraphicsDialog(this));
58 bcview().setOK(dialog_->okPB);
59 bcview().setApply(dialog_->applyPB);
60 bcview().setRestore(dialog_->restorePB);
61 bcview().setCancel(dialog_->closePB);
63 bcview().addReadOnly(dialog_->rotateGB);
64 bcview().addReadOnly(dialog_->latexoptions);
65 bcview().addReadOnly(dialog_->subfigure);
66 bcview().addReadOnly(dialog_->subcaption);
67 bcview().addReadOnly(dialog_->filenameL);
68 bcview().addReadOnly(dialog_->filename);
69 bcview().addReadOnly(dialog_->browsePB);
70 bcview().addReadOnly(dialog_->unzipCB);
71 bcview().addReadOnly(dialog_->filename);
72 bcview().addReadOnly(dialog_->lbX);
73 bcview().addReadOnly(dialog_->lbY);
74 bcview().addReadOnly(dialog_->rtX);
75 bcview().addReadOnly(dialog_->rtY);
76 bcview().addReadOnly(dialog_->lbXunit);
77 bcview().addReadOnly(dialog_->lbYunit);
78 bcview().addReadOnly(dialog_->rtXunit);
79 bcview().addReadOnly(dialog_->rtYunit);
80 bcview().addReadOnly(dialog_->draftCB);
81 bcview().addReadOnly(dialog_->clip);
82 bcview().addReadOnly(dialog_->unzipCB);
83 bcview().addReadOnly(dialog_->subfigure);
84 bcview().addReadOnly(dialog_->subcaption);
85 bcview().addReadOnly(dialog_->showCB);
86 bcview().addReadOnly(dialog_->width);
87 bcview().addReadOnly(dialog_->height);
88 bcview().addReadOnly(dialog_->displayCB);
89 bcview().addReadOnly(dialog_->displayscale);
90 bcview().addReadOnly(dialog_->widthUnit);
91 bcview().addReadOnly(dialog_->heightUnit);
92 bcview().addReadOnly(dialog_->aspectratio);
93 bcview().addReadOnly(dialog_->angle);
94 bcview().addReadOnly(dialog_->origin);
95 bcview().addReadOnly(dialog_->latexoptions);
96 bcview().addReadOnly(dialog_->getPB);
102 // returns the number of the string s in the vector v
103 int getItemNo(vector<string> v, string const & s) {
104 vector<string>::const_iterator cit =
105 find(v.begin(), v.end(), s);
106 return (cit != v.end()) ? int(cit - v.begin()) : 0;
109 // returns the number of the unit in the array unit_name,
110 // which is defined in lengthcommon.C
111 int getUnitNo(char const * c[], string const & s) {
113 while (i < num_units && s != c[i])
115 return (i < num_units) ? i : 0;
121 void QGraphics::update_contents()
123 // clear and fill in the comboboxes
124 vector<string> const bb_units = frnt::getBBUnits();
125 dialog_->lbXunit->clear();
126 dialog_->lbYunit->clear();
127 dialog_->rtXunit->clear();
128 dialog_->rtYunit->clear();
129 for (vector<string>::const_iterator it = bb_units.begin();
130 it != bb_units.end(); ++it) {
131 dialog_->lbXunit->insertItem(toqstr(*it), -1);
132 dialog_->lbYunit->insertItem(toqstr(*it), -1);
133 dialog_->rtXunit->insertItem(toqstr(*it), -1);
134 dialog_->rtYunit->insertItem(toqstr(*it), -1);
137 InsetGraphicsParams & igp = controller().params();
139 // set the right default unit
140 LyXLength::UNIT unitDefault = LyXLength::CM;
141 switch (lyxrc.default_papersize) {
142 case PAPER_DEFAULT: break;
145 case PAPER_LEGALPAPER:
146 case PAPER_EXECUTIVEPAPER:
147 unitDefault = LyXLength::IN;
154 unitDefault = LyXLength::CM;
159 igp.filename.outputFilename(kernel().bufferFilepath());
160 dialog_->filename->setText(toqstr(name));
162 // set the bounding box values
163 if (igp.bb.empty()) {
164 string const bb = controller().readBB(igp.filename.absFilename());
165 // the values from the file always have the bigpoint-unit bp
166 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
167 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
168 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
169 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
170 dialog_->lbXunit->setCurrentItem(0);
171 dialog_->lbYunit->setCurrentItem(0);
172 dialog_->rtXunit->setCurrentItem(0);
173 dialog_->rtYunit->setCurrentItem(0);
174 controller().bbChanged = false;
176 // get the values from the inset
178 string const xl(token(igp.bb, ' ', 0));
179 string const yl(token(igp.bb, ' ', 1));
180 string const xr(token(igp.bb, ' ', 2));
181 string const yr(token(igp.bb, ' ', 3));
182 if (isValidLength(xl, &anyLength)) {
183 dialog_->lbX->setText(toqstr(tostr(anyLength.value())));
184 string const unit(unit_name[anyLength.unit()]);
185 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
187 dialog_->lbX->setText(toqstr(xl));
189 if (isValidLength(yl, &anyLength)) {
190 dialog_->lbY->setText(toqstr(tostr(anyLength.value())));
191 string const unit(unit_name[anyLength.unit()]);
192 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
194 dialog_->lbY->setText(toqstr(xl));
196 if (isValidLength(xr, &anyLength)) {
197 dialog_->rtX->setText(toqstr(tostr(anyLength.value())));
198 string const unit(unit_name[anyLength.unit()]);
199 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
201 dialog_->rtX->setText(toqstr(xl));
203 if (isValidLength(yr, &anyLength)) {
204 dialog_->rtY->setText(toqstr(tostr(anyLength.value())));
205 string const unit(unit_name[anyLength.unit()]);
206 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
208 dialog_->rtY->setText(toqstr(xl));
210 controller().bbChanged = true;
213 // Update the draft and clip mode
214 dialog_->draftCB->setChecked(igp.draft);
215 dialog_->clip->setChecked(igp.clip);
216 dialog_->unzipCB->setChecked(igp.noUnzip);
218 // Update the subcaption check button and input field
219 dialog_->subfigure->setChecked(igp.subcaption);
220 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
223 switch (igp.display) {
224 case lyx::graphics::DefaultDisplay: item = 0; break;
225 case lyx::graphics::MonochromeDisplay: item = 1; break;
226 case lyx::graphics::GrayscaleDisplay: item = 2; break;
227 case lyx::graphics::ColorDisplay: item = 3; break;
228 case lyx::graphics::NoDisplay: item = 0; break;
230 dialog_->showCB->setCurrentItem(item);
231 dialog_->showCB->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
232 dialog_->displayCB->setChecked(igp.display != lyx::graphics::NoDisplay);
233 dialog_->displayscale->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
234 dialog_->displayscale->setText(toqstr(tostr(igp.lyxscale)));
236 //// the output section (width/height)
237 // set the length combo boxes
238 // only the width has the possibility for scale%. The original
239 // units are defined in lengthcommon.C
240 // 1. the width (a listttype)
241 dialog_->widthUnit->clear();
242 dialog_->widthUnit->insertItem(qt_("Scale%"));
243 for (int i = 0; i < num_units; i++)
244 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
246 if (!float_equal(igp.scale, 0.0, 0.05)) {
247 // there is a scale value > 0.05
248 dialog_->width->setText(toqstr(tostr(igp.scale)));
249 dialog_->widthUnit->setCurrentItem(0);
251 // no scale means default width/height
252 dialog_->width->setText(toqstr(tostr(igp.width.value())));
253 // the width cannot have a unitDefault, because
254 // it is a "Scale%" or another user defined unit!
255 // +1 instead of the "Scale%" option
256 int unit_ = igp.width.unit();
257 dialog_->widthUnit->setCurrentItem(unit_ + 1);
259 // 2. the height (a lengthgcombo type)
260 dialog_->height->setText(toqstr(tostr(igp.height.value())));
261 LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
262 igp.height.unit() : unitDefault;
263 dialog_->heightUnit->setCurrentItem(unit_);
265 // enable height input in case of non "Scale%" as width-unit
266 bool use_height = (dialog_->widthUnit->currentItem() > 0);
267 dialog_->height->setEnabled(use_height);
268 dialog_->heightUnit->setEnabled(use_height);
270 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
272 dialog_->angle->setText(toqstr(tostr(igp.rotateAngle)));
274 dialog_->origin->clear();
276 using namespace frnt;
277 vector<RotationOriginPair> origindata = getRotationOriginData();
278 vector<string> const origin_lang = getFirst(origindata);
279 QGraphics::origin_ltx = getSecond(origindata);
281 for (vector<string>::const_iterator it = origin_lang.begin();
282 it != origin_lang.end(); ++it)
283 dialog_->origin->insertItem(toqstr(*it), -1);
285 if (!igp.rotateOrigin.empty())
286 dialog_->origin->setCurrentItem(
287 ::getItemNo(origin_ltx, igp.rotateOrigin));
289 dialog_->origin->setCurrentItem(0);
292 dialog_->latexoptions->setText(toqstr(igp.special));
296 void QGraphics::apply()
298 InsetGraphicsParams & igp = controller().params();
300 igp.filename.set(fromqstr(dialog_->filename->text()),
301 kernel().bufferFilepath());
305 if (controller().bbChanged) {
307 string lbX(fromqstr(dialog_->lbX->text()));
308 string lbY(fromqstr(dialog_->lbY->text()));
309 string rtX(fromqstr(dialog_->rtX->text()));
310 string rtY(fromqstr(dialog_->rtY->text()));
312 strToInt(lbX) + strToInt(lbY) +
313 strToInt(rtX) + strToInt(rtX);
318 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
322 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
326 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
330 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
335 igp.draft = dialog_->draftCB->isChecked();
336 igp.clip = dialog_->clip->isChecked();
337 igp.subcaption = dialog_->subfigure->isChecked();
338 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
340 switch (dialog_->showCB->currentItem()) {
341 case 0: igp.display = lyx::graphics::DefaultDisplay; break;
342 case 1: igp.display = lyx::graphics::MonochromeDisplay; break;
343 case 2: igp.display = lyx::graphics::GrayscaleDisplay; break;
344 case 3: igp.display = lyx::graphics::ColorDisplay; break;
348 if (!dialog_->displayCB->isChecked())
349 igp.display = lyx::graphics::NoDisplay;
351 string value(fromqstr(dialog_->width->text()));
352 if (dialog_->widthUnit->currentItem() > 0) {
353 // width/height combination
354 int const unitNo = getUnitNo(unit_name_gui,
355 fromqstr(dialog_->widthUnit->currentText()));
356 igp.width = LyXLength(value + unit_name_ltx[unitNo]);
359 // scaling instead of a width
360 igp.scale = strToDbl(value);
361 igp.width = LyXLength();
363 value = fromqstr(dialog_->height->text());
364 int const unitNo = getUnitNo(unit_name_gui,
365 fromqstr(dialog_->heightUnit->currentText()));
366 igp.height = LyXLength(value + unit_name_ltx[unitNo]);
368 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
370 igp.noUnzip = dialog_->unzipCB->isChecked();
372 igp.lyxscale = strToInt(fromqstr(dialog_->displayscale->text()));
374 igp.rotateAngle = strToDbl(fromqstr(dialog_->angle->text()));
375 while (igp.rotateAngle < -360.0)
376 igp.rotateAngle += 360.0;
377 while (igp.rotateAngle > 360.0)
378 igp.rotateAngle -= 360.0;
380 // save the latex name for the origin. If it is the default
381 // then origin_ltx returns ""
383 QGraphics::origin_ltx[dialog_->origin->currentItem()];
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->setCurrentItem(0);
403 dialog_->lbYunit->setCurrentItem(0);
404 dialog_->rtXunit->setCurrentItem(0);
405 dialog_->rtYunit->setCurrentItem(0);
407 controller().bbChanged = false;
412 bool QGraphics::isValid()
414 return !dialog_->filename->text().isEmpty();