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 "QtLyXView.h"
16 #include "ControlGraphics.h"
17 #include "controllers/helper_funcs.h"
18 #include "support/lstrings.h"
19 #include "support/tostr.h"
20 #include "support/FileInfo.h"
21 #include "support/filetools.h"
22 #include "support/lyxlib.h"
23 #include "insets/insetgraphicsParams.h"
26 #include "lengthcombo.h"
27 #include "qt_helpers.h"
28 #include "lengthcommon.h"
29 #include "lyxlength.h"
32 #include <qlineedit.h>
33 #include <qpushbutton.h>
34 #include <qcheckbox.h>
35 #include <qradiobutton.h>
36 #include <qcombobox.h>
37 #include <qgroupbox.h>
38 #include <qbuttongroup.h>
41 #include "QGraphicsDialog.h"
42 #include "QGraphics.h"
45 using namespace lyx::support;
50 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
53 QGraphics::QGraphics(Dialog & parent)
54 : base_class(parent, _("LyX: Graphics"))
59 void QGraphics::build_dialog()
61 dialog_.reset(new QGraphicsDialog(this));
63 bcview().setOK(dialog_->okPB);
64 bcview().setApply(dialog_->applyPB);
65 bcview().setRestore(dialog_->restorePB);
66 bcview().setCancel(dialog_->closePB);
68 bcview().addReadOnly(dialog_->rotateGB);
69 bcview().addReadOnly(dialog_->latexoptions);
70 bcview().addReadOnly(dialog_->subfigure);
71 bcview().addReadOnly(dialog_->subcaption);
72 bcview().addReadOnly(dialog_->filenameL);
73 bcview().addReadOnly(dialog_->filename);
74 bcview().addReadOnly(dialog_->browsePB);
75 bcview().addReadOnly(dialog_->unzipCB);
76 bcview().addReadOnly(dialog_->filename);
77 bcview().addReadOnly(dialog_->lbX);
78 bcview().addReadOnly(dialog_->lbY);
79 bcview().addReadOnly(dialog_->rtX);
80 bcview().addReadOnly(dialog_->rtY);
81 bcview().addReadOnly(dialog_->lbXunit);
82 bcview().addReadOnly(dialog_->lbYunit);
83 bcview().addReadOnly(dialog_->rtXunit);
84 bcview().addReadOnly(dialog_->rtYunit);
85 bcview().addReadOnly(dialog_->draftCB);
86 bcview().addReadOnly(dialog_->clip);
87 bcview().addReadOnly(dialog_->unzipCB);
88 bcview().addReadOnly(dialog_->subfigure);
89 bcview().addReadOnly(dialog_->subcaption);
90 bcview().addReadOnly(dialog_->showCB);
91 bcview().addReadOnly(dialog_->width);
92 bcview().addReadOnly(dialog_->height);
93 bcview().addReadOnly(dialog_->displayCB);
94 bcview().addReadOnly(dialog_->displayscale);
95 bcview().addReadOnly(dialog_->widthUnit);
96 bcview().addReadOnly(dialog_->heightUnit);
97 bcview().addReadOnly(dialog_->aspectratio);
98 bcview().addReadOnly(dialog_->angle);
99 bcview().addReadOnly(dialog_->origin);
100 bcview().addReadOnly(dialog_->latexoptions);
101 bcview().addReadOnly(dialog_->getPB);
107 // returns the number of the string s in the vector v
108 int getItemNo(vector<string> v, string const & s) {
109 vector<string>::const_iterator cit =
110 find(v.begin(), v.end(), s);
111 return (cit != v.end()) ? int(cit - v.begin()) : 0;
114 // returns the number of the unit in the array unit_name,
115 // which is defined in lengthcommon.C
116 int getUnitNo(char const * c[], string const & s) {
118 while (i < num_units && s != c[i])
120 return (i < num_units) ? i : 0;
126 void QGraphics::update_contents()
128 // clear and fill in the comboboxes
129 vector<string> const bb_units = frnt::getBBUnits();
130 dialog_->lbXunit->clear();
131 dialog_->lbYunit->clear();
132 dialog_->rtXunit->clear();
133 dialog_->rtYunit->clear();
134 for (vector<string>::const_iterator it = bb_units.begin();
135 it != bb_units.end(); ++it) {
136 dialog_->lbXunit->insertItem(toqstr(*it), -1);
137 dialog_->lbYunit->insertItem(toqstr(*it), -1);
138 dialog_->rtXunit->insertItem(toqstr(*it), -1);
139 dialog_->rtYunit->insertItem(toqstr(*it), -1);
142 InsetGraphicsParams & igp = controller().params();
144 // set the right default unit
145 LyXLength::UNIT unitDefault = LyXLength::CM;
146 switch (lyxrc.default_papersize) {
147 case BufferParams::PAPER_DEFAULT: break;
149 case BufferParams::PAPER_USLETTER:
150 case BufferParams::PAPER_LEGALPAPER:
151 case BufferParams::PAPER_EXECUTIVEPAPER:
152 unitDefault = LyXLength::IN;
155 case BufferParams::PAPER_A3PAPER:
156 case BufferParams::PAPER_A4PAPER:
157 case BufferParams::PAPER_A5PAPER:
158 case BufferParams::PAPER_B5PAPER:
159 unitDefault = LyXLength::CM;
164 igp.filename.outputFilename(kernel().buffer()->filePath());
165 dialog_->filename->setText(toqstr(name));
167 // set the bounding box values
168 if (igp.bb.empty()) {
169 string const bb = controller().readBB(igp.filename.absFilename());
170 // the values from the file always have the bigpoint-unit bp
171 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
172 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
173 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
174 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
175 dialog_->lbXunit->setCurrentItem(0);
176 dialog_->lbYunit->setCurrentItem(0);
177 dialog_->rtXunit->setCurrentItem(0);
178 dialog_->rtYunit->setCurrentItem(0);
179 controller().bbChanged = false;
181 // get the values from the inset
183 string const xl(token(igp.bb, ' ', 0));
184 string const yl(token(igp.bb, ' ', 1));
185 string const xr(token(igp.bb, ' ', 2));
186 string const yr(token(igp.bb, ' ', 3));
187 if (isValidLength(xl, &anyLength)) {
188 dialog_->lbX->setText(toqstr(tostr(anyLength.value())));
189 string const unit(unit_name[anyLength.unit()]);
190 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
192 dialog_->lbX->setText(toqstr(xl));
194 if (isValidLength(yl, &anyLength)) {
195 dialog_->lbY->setText(toqstr(tostr(anyLength.value())));
196 string const unit(unit_name[anyLength.unit()]);
197 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
199 dialog_->lbY->setText(toqstr(xl));
201 if (isValidLength(xr, &anyLength)) {
202 dialog_->rtX->setText(toqstr(tostr(anyLength.value())));
203 string const unit(unit_name[anyLength.unit()]);
204 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
206 dialog_->rtX->setText(toqstr(xl));
208 if (isValidLength(yr, &anyLength)) {
209 dialog_->rtY->setText(toqstr(tostr(anyLength.value())));
210 string const unit(unit_name[anyLength.unit()]);
211 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
213 dialog_->rtY->setText(toqstr(xl));
215 controller().bbChanged = true;
218 // Update the draft and clip mode
219 dialog_->draftCB->setChecked(igp.draft);
220 dialog_->clip->setChecked(igp.clip);
221 dialog_->unzipCB->setChecked(igp.noUnzip);
223 // Update the subcaption check button and input field
224 dialog_->subfigure->setChecked(igp.subcaption);
225 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
228 switch (igp.display) {
229 case lyx::graphics::DefaultDisplay: item = 0; break;
230 case lyx::graphics::MonochromeDisplay: item = 1; break;
231 case lyx::graphics::GrayscaleDisplay: item = 2; break;
232 case lyx::graphics::ColorDisplay: item = 3; break;
233 case lyx::graphics::NoDisplay: item = 0; break;
235 dialog_->showCB->setCurrentItem(item);
236 dialog_->showCB->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
237 dialog_->displayCB->setChecked(igp.display != lyx::graphics::NoDisplay);
238 dialog_->displayscale->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
239 dialog_->displayscale->setText(toqstr(tostr(igp.lyxscale)));
241 //// the output section (width/height)
242 // set the length combo boxes
243 // only the width has the possibility for scale%. The original
244 // units are defined in lengthcommon.C
245 // 1. the width (a listttype)
246 dialog_->widthUnit->clear();
247 dialog_->widthUnit->insertItem(qt_("Scale%"));
248 for (int i = 0; i < num_units; i++)
249 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
251 if (!float_equal(igp.scale, 0.0, 0.05)) {
252 // there is a scale value > 0.05
253 dialog_->width->setText(toqstr(tostr(igp.scale)));
254 dialog_->widthUnit->setCurrentItem(0);
256 // no scale means default width/height
257 dialog_->width->setText(toqstr(tostr(igp.width.value())));
258 // the width cannot have a unitDefault, because
259 // it is a "Scale%" or another user defined unit!
260 // +1 instead of the "Scale%" option
261 int unit_ = igp.width.unit();
262 dialog_->widthUnit->setCurrentItem(unit_ + 1);
264 // 2. the height (a lengthgcombo type)
265 dialog_->height->setText(toqstr(tostr(igp.height.value())));
266 LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
267 igp.height.unit() : unitDefault;
268 dialog_->heightUnit->setCurrentItem(unit_);
270 // enable height input in case of non "Scale%" as width-unit
271 bool use_height = (dialog_->widthUnit->currentItem() > 0);
272 dialog_->height->setEnabled(use_height);
273 dialog_->heightUnit->setEnabled(use_height);
275 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
277 dialog_->angle->setText(toqstr(tostr(igp.rotateAngle)));
279 dialog_->origin->clear();
281 using namespace frnt;
282 vector<RotationOriginPair> origindata = getRotationOriginData();
283 vector<string> const origin_lang = getFirst(origindata);
284 QGraphics::origin_ltx = getSecond(origindata);
286 for (vector<string>::const_iterator it = origin_lang.begin();
287 it != origin_lang.end(); ++it)
288 dialog_->origin->insertItem(toqstr(*it), -1);
290 if (!igp.rotateOrigin.empty())
291 dialog_->origin->setCurrentItem(
292 ::getItemNo(origin_ltx, igp.rotateOrigin));
294 dialog_->origin->setCurrentItem(0);
297 dialog_->latexoptions->setText(toqstr(igp.special));
301 void QGraphics::apply()
303 InsetGraphicsParams & igp = controller().params();
305 igp.filename.set(fromqstr(dialog_->filename->text()),
306 kernel().buffer()->filePath());
310 if (controller().bbChanged) {
312 string lbX(fromqstr(dialog_->lbX->text()));
313 string lbY(fromqstr(dialog_->lbY->text()));
314 string rtX(fromqstr(dialog_->rtX->text()));
315 string rtY(fromqstr(dialog_->rtY->text()));
317 strToInt(lbX) + strToInt(lbY) +
318 strToInt(rtX) + strToInt(rtX);
323 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
327 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
331 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
335 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
340 igp.draft = dialog_->draftCB->isChecked();
341 igp.clip = dialog_->clip->isChecked();
342 igp.subcaption = dialog_->subfigure->isChecked();
343 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
345 switch (dialog_->showCB->currentItem()) {
346 case 0: igp.display = lyx::graphics::DefaultDisplay; break;
347 case 1: igp.display = lyx::graphics::MonochromeDisplay; break;
348 case 2: igp.display = lyx::graphics::GrayscaleDisplay; break;
349 case 3: igp.display = lyx::graphics::ColorDisplay; break;
353 if (!dialog_->displayCB->isChecked())
354 igp.display = lyx::graphics::NoDisplay;
356 string value(fromqstr(dialog_->width->text()));
357 if (dialog_->widthUnit->currentItem() > 0) {
358 // width/height combination
359 int const unitNo = getUnitNo(unit_name_gui,
360 fromqstr(dialog_->widthUnit->currentText()));
361 igp.width = LyXLength(value + unit_name_ltx[unitNo]);
364 // scaling instead of a width
365 igp.scale = strToDbl(value);
366 igp.width = LyXLength();
368 value = fromqstr(dialog_->height->text());
369 int const unitNo = getUnitNo(unit_name_gui,
370 fromqstr(dialog_->heightUnit->currentText()));
371 igp.height = LyXLength(value + unit_name_ltx[unitNo]);
373 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
375 igp.noUnzip = dialog_->unzipCB->isChecked();
377 igp.lyxscale = strToInt(fromqstr(dialog_->displayscale->text()));
379 igp.rotateAngle = strToDbl(fromqstr(dialog_->angle->text()));
380 while (igp.rotateAngle < -360.0)
381 igp.rotateAngle += 360.0;
382 while (igp.rotateAngle > 360.0)
383 igp.rotateAngle -= 360.0;
385 // save the latex name for the origin. If it is the default
386 // then origin_ltx returns ""
388 QGraphics::origin_ltx[dialog_->origin->currentItem()];
390 // more latex options
391 igp.special = fromqstr(dialog_->latexoptions->text());
395 void QGraphics::getBB()
397 string const filename(fromqstr(dialog_->filename->text()));
398 if (!filename.empty()) {
399 string const bb(controller().readBB(filename));
401 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
402 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
403 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
404 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
405 // the default units for the bb values when reading
407 dialog_->lbXunit->setCurrentItem(0);
408 dialog_->lbYunit->setCurrentItem(0);
409 dialog_->rtXunit->setCurrentItem(0);
410 dialog_->rtYunit->setCurrentItem(0);
412 controller().bbChanged = false;
417 bool QGraphics::isValid()
419 return !dialog_->filename->text().isEmpty();