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"
23 #include "lengthcommon.h"
26 #include "controllers/ControlGraphics.h"
27 #include "controllers/helper_funcs.h"
29 #include "insets/insetgraphicsParams.h"
31 #include "support/lstrings.h"
32 #include "support/convert.h"
33 #include "support/lyxlib.h"
35 #include <qlineedit.h>
36 #include <qpushbutton.h>
37 #include <qcheckbox.h>
38 #include <qgroupbox.h>
43 using lyx::support::float_equal;
44 using lyx::support::strToDbl;
45 using lyx::support::strToInt;
46 using lyx::support::token;
48 #ifndef CXX_GLOBAL_CSTD
58 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
60 QGraphics::QGraphics(Dialog & parent)
61 : base_class(parent, _("LyX: Graphics"))
66 void QGraphics::build_dialog()
68 dialog_.reset(new QGraphicsDialog(this));
70 bcview().setOK(dialog_->okPB);
71 bcview().setApply(dialog_->applyPB);
72 bcview().setRestore(dialog_->restorePB);
73 bcview().setCancel(dialog_->closePB);
75 bcview().addReadOnly(dialog_->rotateGB);
76 bcview().addReadOnly(dialog_->latexoptions);
77 bcview().addReadOnly(dialog_->subfigure);
78 bcview().addReadOnly(dialog_->subcaption);
79 bcview().addReadOnly(dialog_->filenameL);
80 bcview().addReadOnly(dialog_->filename);
81 bcview().addReadOnly(dialog_->browsePB);
82 bcview().addReadOnly(dialog_->unzipCB);
83 bcview().addReadOnly(dialog_->filename);
84 bcview().addReadOnly(dialog_->lbX);
85 bcview().addReadOnly(dialog_->lbY);
86 bcview().addReadOnly(dialog_->rtX);
87 bcview().addReadOnly(dialog_->rtY);
88 bcview().addReadOnly(dialog_->lbXunit);
89 bcview().addReadOnly(dialog_->lbYunit);
90 bcview().addReadOnly(dialog_->rtXunit);
91 bcview().addReadOnly(dialog_->rtYunit);
92 bcview().addReadOnly(dialog_->draftCB);
93 bcview().addReadOnly(dialog_->clip);
94 bcview().addReadOnly(dialog_->unzipCB);
95 bcview().addReadOnly(dialog_->subfigure);
96 bcview().addReadOnly(dialog_->subcaption);
97 bcview().addReadOnly(dialog_->showCB);
98 bcview().addReadOnly(dialog_->width);
99 bcview().addReadOnly(dialog_->height);
100 bcview().addReadOnly(dialog_->displayCB);
101 bcview().addReadOnly(dialog_->displayscale);
102 bcview().addReadOnly(dialog_->widthUnit);
103 bcview().addReadOnly(dialog_->heightUnit);
104 bcview().addReadOnly(dialog_->aspectratio);
105 bcview().addReadOnly(dialog_->angle);
106 bcview().addReadOnly(dialog_->origin);
107 bcview().addReadOnly(dialog_->latexoptions);
108 bcview().addReadOnly(dialog_->getPB);
110 // initialize the length validator
111 addCheckedLineEdit(bcview(), dialog_->width, dialog_->sizewidthL_2);
112 addCheckedLineEdit(bcview(), dialog_->height, dialog_->sizeheightL_2);
113 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
114 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL_2);
115 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
116 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
117 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
118 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
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 // clear and fill in the comboboxes
137 vector<string> const bb_units = lyx::frontend::getBBUnits();
138 dialog_->lbXunit->clear();
139 dialog_->lbYunit->clear();
140 dialog_->rtXunit->clear();
141 dialog_->rtYunit->clear();
142 for (vector<string>::const_iterator it = bb_units.begin();
143 it != bb_units.end(); ++it) {
144 dialog_->lbXunit->insertItem(toqstr(*it), -1);
145 dialog_->lbYunit->insertItem(toqstr(*it), -1);
146 dialog_->rtXunit->insertItem(toqstr(*it), -1);
147 dialog_->rtYunit->insertItem(toqstr(*it), -1);
150 InsetGraphicsParams & igp = controller().params();
152 // set the right default unit
153 LyXLength::UNIT unitDefault = LyXLength::CM;
154 switch (lyxrc.default_papersize) {
155 case PAPER_DEFAULT: break;
158 case PAPER_LEGALPAPER:
159 case PAPER_EXECUTIVEPAPER:
160 unitDefault = LyXLength::IN;
167 unitDefault = LyXLength::CM;
172 igp.filename.outputFilename(kernel().bufferFilepath());
173 dialog_->filename->setText(toqstr(name));
175 // set the bounding box values
176 if (igp.bb.empty()) {
177 string const bb = controller().readBB(igp.filename.absFilename());
178 // the values from the file always have the bigpoint-unit bp
179 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
180 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
181 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
182 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
183 dialog_->lbXunit->setCurrentItem(0);
184 dialog_->lbYunit->setCurrentItem(0);
185 dialog_->rtXunit->setCurrentItem(0);
186 dialog_->rtYunit->setCurrentItem(0);
187 controller().bbChanged = false;
189 // get the values from the inset
191 string const xl(token(igp.bb, ' ', 0));
192 string const yl(token(igp.bb, ' ', 1));
193 string const xr(token(igp.bb, ' ', 2));
194 string const yr(token(igp.bb, ' ', 3));
195 if (isValidLength(xl, &anyLength)) {
196 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
197 string const unit(unit_name[anyLength.unit()]);
198 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
200 dialog_->lbX->setText(toqstr(xl));
202 if (isValidLength(yl, &anyLength)) {
203 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
204 string const unit(unit_name[anyLength.unit()]);
205 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
207 dialog_->lbY->setText(toqstr(xl));
209 if (isValidLength(xr, &anyLength)) {
210 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
211 string const unit(unit_name[anyLength.unit()]);
212 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
214 dialog_->rtX->setText(toqstr(xl));
216 if (isValidLength(yr, &anyLength)) {
217 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
218 string const unit(unit_name[anyLength.unit()]);
219 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
221 dialog_->rtY->setText(toqstr(xl));
223 controller().bbChanged = true;
226 // Update the draft and clip mode
227 dialog_->draftCB->setChecked(igp.draft);
228 dialog_->clip->setChecked(igp.clip);
229 dialog_->unzipCB->setChecked(igp.noUnzip);
231 // Update the subcaption check button and input field
232 dialog_->subfigure->setChecked(igp.subcaption);
233 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
236 switch (igp.display) {
237 case lyx::graphics::DefaultDisplay: item = 0; break;
238 case lyx::graphics::MonochromeDisplay: item = 1; break;
239 case lyx::graphics::GrayscaleDisplay: item = 2; break;
240 case lyx::graphics::ColorDisplay: item = 3; break;
241 case lyx::graphics::NoDisplay: item = 0; break;
243 dialog_->showCB->setCurrentItem(item);
244 dialog_->showCB->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
245 dialog_->displayCB->setChecked(igp.display != lyx::graphics::NoDisplay);
246 dialog_->displayscale->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
247 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
249 //// the output section (width/height)
250 // set the length combo boxes
251 // only the width has the possibility for scale%. The original
252 // units are defined in lengthcommon.C
253 // 1. the width (a listttype)
254 dialog_->widthUnit->clear();
255 dialog_->widthUnit->insertItem(qt_("Scale%"));
256 for (int i = 0; i < num_units; i++)
257 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
259 if (!igp.scale.empty()
260 && !float_equal(strToDbl(igp.scale), 0.0, 0.05)) {
261 dialog_->width->setText(toqstr(igp.scale));
262 dialog_->widthUnit->setCurrentItem(0);
264 // no scale means default width/height
265 dialog_->width->setText(toqstr(convert<string>(igp.width.value())));
266 // the width cannot have a unitDefault, because
267 // it is a "Scale%" or another user defined unit!
268 // +1 instead of the "Scale%" option
269 int unit_ = igp.width.unit();
270 dialog_->widthUnit->setCurrentItem(unit_ + 1);
272 // 2. the height (a lengthgcombo type)
273 lengthToWidgets(dialog_->height, dialog_->heightUnit,
274 igp.height.asString(), unitDefault);
276 // enable height input in case of non "Scale%" as width-unit
277 bool use_height = (dialog_->widthUnit->currentItem() > 0);
278 dialog_->height->setEnabled(use_height);
279 dialog_->heightUnit->setEnabled(use_height);
281 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
283 dialog_->angle->setText(toqstr(igp.rotateAngle));
285 dialog_->origin->clear();
287 using namespace lyx::frontend;
288 vector<RotationOriginPair> origindata = getRotationOriginData();
289 vector<string> const origin_lang = getFirst(origindata);
290 QGraphics::origin_ltx = getSecond(origindata);
292 for (vector<string>::const_iterator it = origin_lang.begin();
293 it != origin_lang.end(); ++it)
294 dialog_->origin->insertItem(toqstr(*it), -1);
296 if (!igp.rotateOrigin.empty())
297 dialog_->origin->setCurrentItem(
298 getItemNo(origin_ltx, igp.rotateOrigin));
300 dialog_->origin->setCurrentItem(0);
303 dialog_->latexoptions->setText(toqstr(igp.special));
307 void QGraphics::apply()
309 InsetGraphicsParams & igp = controller().params();
311 igp.filename.set(fromqstr(dialog_->filename->text()),
312 kernel().bufferFilepath());
316 if (controller().bbChanged) {
318 string lbX(fromqstr(dialog_->lbX->text()));
319 string lbY(fromqstr(dialog_->lbY->text()));
320 string rtX(fromqstr(dialog_->rtX->text()));
321 string rtY(fromqstr(dialog_->rtY->text()));
323 strToInt(lbX) + strToInt(lbY) +
324 strToInt(rtX) + strToInt(rtX);
329 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
333 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
337 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
341 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
346 igp.draft = dialog_->draftCB->isChecked();
347 igp.clip = dialog_->clip->isChecked();
348 igp.subcaption = dialog_->subfigure->isChecked();
349 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
351 switch (dialog_->showCB->currentItem()) {
352 case 0: igp.display = lyx::graphics::DefaultDisplay; break;
353 case 1: igp.display = lyx::graphics::MonochromeDisplay; break;
354 case 2: igp.display = lyx::graphics::GrayscaleDisplay; break;
355 case 3: igp.display = lyx::graphics::ColorDisplay; break;
359 if (!dialog_->displayCB->isChecked())
360 igp.display = lyx::graphics::NoDisplay;
362 string value = fromqstr(dialog_->width->text());
363 if (dialog_->widthUnit->currentItem() > 0 || isValidLength(value)) {
364 // width/height combination
366 widgetsToLength(dialog_->width, dialog_->widthUnit);
367 igp.scale = string();
369 // scaling instead of a width
371 igp.width = LyXLength();
373 value = fromqstr(dialog_->height->text());
375 LyXLength(widgetsToLength(dialog_->height, dialog_->heightUnit));
377 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
379 igp.noUnzip = dialog_->unzipCB->isChecked();
381 igp.lyxscale = strToInt(fromqstr(dialog_->displayscale->text()));
383 igp.rotateAngle = fromqstr(dialog_->angle->text());
385 float rotAngle = strToDbl(igp.rotateAngle);
386 if (std::abs(rotAngle) > 360.0) {
387 rotAngle -= 360.0 * floor(rotAngle / 360.0);
388 igp.rotateAngle = convert<string>(rotAngle);
391 // save the latex name for the origin. If it is the default
392 // then origin_ltx returns ""
394 QGraphics::origin_ltx[dialog_->origin->currentItem()];
396 // more latex options
397 igp.special = fromqstr(dialog_->latexoptions->text());
401 void QGraphics::getBB()
403 string const filename(fromqstr(dialog_->filename->text()));
404 if (!filename.empty()) {
405 string const bb(controller().readBB(filename));
407 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
408 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
409 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
410 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
411 // the default units for the bb values when reading
413 dialog_->lbXunit->setCurrentItem(0);
414 dialog_->lbYunit->setCurrentItem(0);
415 dialog_->rtXunit->setCurrentItem(0);
416 dialog_->rtYunit->setCurrentItem(0);
418 controller().bbChanged = false;
423 bool QGraphics::isValid()
425 return !dialog_->filename->text().isEmpty();
429 void QGraphics::slotEdit()
431 controller().editGraphics();
434 } // namespace frontend