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 #pragma implementation
19 #include "QtLyXView.h"
20 #include "ControlGraphics.h"
21 #include "controllers/helper_funcs.h"
22 #include "support/lstrings.h"
23 #include "support/FileInfo.h"
24 #include "support/filetools.h"
25 #include "insets/insetgraphicsParams.h"
27 #include "lengthcombo.h"
29 #include "lengthcommon.h"
30 #include "lyxlength.h"
33 #include <qlineedit.h>
34 #include <qpushbutton.h>
35 #include <qcheckbox.h>
36 #include <qradiobutton.h>
37 #include <qcombobox.h>
38 #include <qgroupbox.h>
39 #include <qbuttongroup.h>
42 #include "QGraphicsDialog.h"
43 #include "QGraphics.h"
49 typedef Qt2CB<ControlGraphics, Qt2DB<QGraphicsDialog> > base_class;
52 QGraphics::QGraphics()
53 : base_class(_("Graphics"))
58 void QGraphics::build_dialog()
60 dialog_.reset(new QGraphicsDialog(this));
62 bc().setOK(dialog_->okPB);
63 bc().setApply(dialog_->applyPB);
64 bc().setRestore(dialog_->restorePB);
65 bc().setCancel(dialog_->closePB);
67 bc().addReadOnly(dialog_->rotateGB);
68 bc().addReadOnly(dialog_->latexoptions);
69 bc().addReadOnly(dialog_->subfigure);
70 bc().addReadOnly(dialog_->subcaption);
71 bc().addReadOnly(dialog_->filenameL);
72 bc().addReadOnly(dialog_->filename);
73 bc().addReadOnly(dialog_->browsePB);
74 bc().addReadOnly(dialog_->unzipCB);
75 bc().addReadOnly(dialog_->filename);
76 bc().addReadOnly(dialog_->lbX);
77 bc().addReadOnly(dialog_->lbY);
78 bc().addReadOnly(dialog_->rtX);
79 bc().addReadOnly(dialog_->rtY);
80 bc().addReadOnly(dialog_->lbXunit);
81 bc().addReadOnly(dialog_->lbYunit);
82 bc().addReadOnly(dialog_->rtXunit);
83 bc().addReadOnly(dialog_->rtYunit);
84 bc().addReadOnly(dialog_->draftCB);
85 bc().addReadOnly(dialog_->clip);
86 bc().addReadOnly(dialog_->unzipCB);
87 bc().addReadOnly(dialog_->subfigure);
88 bc().addReadOnly(dialog_->subcaption);
89 bc().addReadOnly(dialog_->showCB);
90 bc().addReadOnly(dialog_->width);
91 bc().addReadOnly(dialog_->height);
92 bc().addReadOnly(dialog_->displayCB);
93 bc().addReadOnly(dialog_->displayscale);
94 bc().addReadOnly(dialog_->widthUnit);
95 bc().addReadOnly(dialog_->heightUnit);
96 bc().addReadOnly(dialog_->aspectratio);
97 bc().addReadOnly(dialog_->angle);
98 bc().addReadOnly(dialog_->origin);
99 bc().addReadOnly(dialog_->latexoptions);
100 bc().addReadOnly(dialog_->getPB);
106 // returns the number of the string s in the vector v
107 int getItemNo(vector<string> v, string const & s) {
108 vector<string>::const_iterator cit =
109 find(v.begin(), v.end(), s);
110 return (cit != v.end()) ? int(cit - v.begin()) : 0;
113 // returns the number of the unit in the array unit_name,
114 // which is defined in lengthcommon.C
115 int getUnitNo(char const * c[], string const & s) {
117 while (i < num_units && s != c[i])
119 return (i < num_units) ? i : 0;
125 void QGraphics::update_contents()
127 // clear and fill in the comboboxes
128 vector<string> const bb_units = frnt::getBBUnits();
129 dialog_->lbXunit->clear();
130 dialog_->lbYunit->clear();
131 dialog_->rtXunit->clear();
132 dialog_->rtYunit->clear();
133 for (vector<string>::const_iterator it = bb_units.begin();
134 it != bb_units.end(); ++it) {
135 dialog_->lbXunit->insertItem((*it).c_str(), -1);
136 dialog_->lbYunit->insertItem((*it).c_str(), -1);
137 dialog_->rtXunit->insertItem((*it).c_str(), -1);
138 dialog_->rtYunit->insertItem((*it).c_str(), -1);
141 InsetGraphicsParams & igp = controller().params();
143 // set the right default unit
144 LyXLength::UNIT unitDefault = LyXLength::CM;
145 switch (lyxrc.default_papersize) {
146 case BufferParams::PAPER_DEFAULT: break;
148 case BufferParams::PAPER_USLETTER:
149 case BufferParams::PAPER_LEGALPAPER:
150 case BufferParams::PAPER_EXECUTIVEPAPER:
151 unitDefault = LyXLength::IN;
154 case BufferParams::PAPER_A3PAPER:
155 case BufferParams::PAPER_A4PAPER:
156 case BufferParams::PAPER_A5PAPER:
157 case BufferParams::PAPER_B5PAPER:
158 unitDefault = LyXLength::CM;
162 dialog_->filename->setText(igp.filename.c_str());
164 // set the bounding box values
165 if (igp.bb.empty()) {
166 string const bb = controller().readBB(igp.filename);
167 // the values from the file always have the bigpoint-unit bp
168 dialog_->lbX->setText(token(bb, ' ', 0).c_str());
169 dialog_->lbY->setText(token(bb, ' ', 1).c_str());
170 dialog_->rtX->setText(token(bb, ' ', 2).c_str());
171 dialog_->rtY->setText(token(bb, ' ', 3).c_str());
172 dialog_->lbXunit->setCurrentItem(0);
173 dialog_->lbYunit->setCurrentItem(0);
174 dialog_->rtXunit->setCurrentItem(0);
175 dialog_->rtYunit->setCurrentItem(0);
176 controller().bbChanged = false;
178 // get the values from the inset
180 string const xl(token(igp.bb, ' ', 0));
181 string const yl(token(igp.bb, ' ', 1));
182 string const xr(token(igp.bb, ' ', 2));
183 string const yr(token(igp.bb, ' ', 3));
184 if (isValidLength(xl, &anyLength)) {
185 dialog_->lbX->setText(tostr(anyLength.value()).c_str());
186 string const unit(unit_name[anyLength.unit()]);
187 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
189 dialog_->lbX->setText(xl.c_str());
191 if (isValidLength(yl, &anyLength)) {
192 dialog_->lbY->setText(tostr(anyLength.value()).c_str());
193 string const unit(unit_name[anyLength.unit()]);
194 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
196 dialog_->lbY->setText(xl.c_str());
198 if (isValidLength(xr, &anyLength)) {
199 dialog_->rtX->setText(tostr(anyLength.value()).c_str());
200 string const unit(unit_name[anyLength.unit()]);
201 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
203 dialog_->rtX->setText(xl.c_str());
205 if (isValidLength(yr, &anyLength)) {
206 dialog_->rtY->setText(tostr(anyLength.value()).c_str());
207 string const unit(unit_name[anyLength.unit()]);
208 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
210 dialog_->rtY->setText(xl.c_str());
212 controller().bbChanged = true;
215 // Update the draft and clip mode
216 dialog_->draftCB->setChecked(igp.draft);
217 dialog_->clip->setChecked(igp.clip);
218 dialog_->unzipCB->setChecked(igp.noUnzip);
220 // Update the subcaption check button and input field
221 dialog_->subfigure->setChecked(igp.subcaption);
222 dialog_->subcaption->setText(igp.subcaptionText.c_str());
225 switch (igp.display) {
226 case grfx::DefaultDisplay: item = 0; break;
227 case grfx::MonochromeDisplay: item = 1; break;
228 case grfx::GrayscaleDisplay: item = 2; break;
229 case grfx::ColorDisplay: item = 3; break;
230 case grfx::NoDisplay: item = 0; break;
232 dialog_->showCB->setCurrentItem(item);
233 dialog_->showCB->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
234 dialog_->displayCB->setChecked(igp.display != grfx::NoDisplay);
235 dialog_->displayscale->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
236 dialog_->displayscale->setText(tostr(igp.lyxscale).c_str());
238 //// the output section (width/height)
239 // set the length combo boxes
240 // only the width has the possibility for scale%. The original
241 // units are defined in lengthcommon.C
242 // 1. the width (a listttype)
243 dialog_->widthUnit->clear();
244 dialog_->widthUnit->insertItem(_("Scale%"));
245 for (int i = 0; i < num_units; i++)
246 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
248 if (!lyx::float_equal(igp.scale, 0.0, 0.05)) {
249 // there is a scale value > 0.05
250 dialog_->width->setText(tostr(igp.scale).c_str());
251 dialog_->widthUnit->setCurrentItem(0);
253 // no scale means default width/height
254 dialog_->width->setText(tostr(igp.width.value()).c_str());
255 // the width cannot have a unitDefault, because
256 // it is a "Scale%" or another user defined unit!
257 // +1 instead of the "Scale%" option
258 int unit_ = igp.width.unit();
259 dialog_->widthUnit->setCurrentItem(unit_ + 1);
261 // 2. the height (a lengthgcombo type)
262 dialog_->height->setText(tostr(igp.height.value()).c_str());
263 LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
264 igp.height.unit() : unitDefault;
265 dialog_->heightUnit->setCurrentItem(unit_);
267 // enable height input in case of non "Scale%" as width-unit
268 bool use_height = (dialog_->widthUnit->currentItem() > 0);
269 dialog_->height->setEnabled(use_height);
270 dialog_->heightUnit->setEnabled(use_height);
272 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
274 dialog_->angle->setText(tostr(igp.rotateAngle).c_str());
276 dialog_->origin->clear();
278 using namespace frnt;
279 vector<RotationOriginPair> origindata = getRotationOriginData();
280 vector<string> const origin_lang = getFirst(origindata);
281 QGraphics::origin_ltx = getSecond(origindata);
283 for (vector<string>::const_iterator it = origin_lang.begin();
284 it != origin_lang.end(); ++it)
285 dialog_->origin->insertItem((*it).c_str(), -1);
287 if (!igp.rotateOrigin.empty())
288 dialog_->origin->setCurrentItem(
289 ::getItemNo(origin_ltx, igp.rotateOrigin));
291 dialog_->origin->setCurrentItem(0);
294 dialog_->latexoptions->setText(igp.special.c_str());
298 void QGraphics::apply()
300 InsetGraphicsParams & igp = controller().params();
302 igp.filename = dialog_->filename->text().latin1();
306 if (controller().bbChanged) {
308 string lbX(dialog_->lbX->text().latin1());
309 string lbY(dialog_->lbY->text().latin1());
310 string rtX(dialog_->rtX->text().latin1());
311 string rtY(dialog_->rtY->text().latin1());
313 strToInt(lbX) + strToInt(lbY) +
314 strToInt(rtX) + strToInt(rtX);
319 bb = lbX + dialog_->lbXunit->currentText().latin1() + ' ';
323 bb += (lbY + dialog_->lbYunit->currentText().latin1() + ' ');
327 bb += (rtX + dialog_->rtXunit->currentText().latin1() + ' ');
331 bb += (rtY + dialog_->rtYunit->currentText().latin1());
336 igp.draft = dialog_->draftCB->isChecked();
337 igp.clip = dialog_->clip->isChecked();
338 igp.subcaption = dialog_->subfigure->isChecked();
339 igp.subcaptionText = dialog_->subcaption->text().latin1();
341 switch (dialog_->showCB->currentItem()) {
342 case 0: igp.display = grfx::DefaultDisplay; break;
343 case 1: igp.display = grfx::MonochromeDisplay; break;
344 case 2: igp.display = grfx::GrayscaleDisplay; break;
345 case 3: igp.display = grfx::ColorDisplay; break;
349 if (!dialog_->displayCB->isChecked())
350 igp.display = grfx::NoDisplay;
352 string value(dialog_->width->text().latin1());
353 if (dialog_->widthUnit->currentItem() > 0) {
354 // width/height combination
355 int const unitNo = getUnitNo(unit_name_gui,
356 dialog_->widthUnit->currentText().latin1());
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 = dialog_->height->text().latin1();
365 int const unitNo = getUnitNo(unit_name_gui,
366 dialog_->heightUnit->currentText().latin1());
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(dialog_->displayscale->text().latin1());
375 igp.rotateAngle = strToDbl(dialog_->angle->text().latin1());
376 while (igp.rotateAngle < -360.0)
377 igp.rotateAngle += 360.0;
378 while (igp.rotateAngle > 360.0)
379 igp.rotateAngle -= 360.0;
381 // save the latex name for the origin. If it is the default
382 // then origin_ltx returns ""
384 QGraphics::origin_ltx[dialog_->origin->currentItem()];
386 // more latex options
387 igp.special = dialog_->latexoptions->text().latin1();
391 void QGraphics::getBB()
393 string const filename(dialog_->filename->text().latin1());
394 if (!filename.empty()) {
395 string const bb(controller().readBB(filename));
397 dialog_->lbX->setText(token(bb, ' ', 0).c_str());
398 dialog_->lbY->setText(token(bb, ' ', 1).c_str());
399 dialog_->rtX->setText(token(bb, ' ', 2).c_str());
400 dialog_->rtY->setText(token(bb, ' ', 3).c_str());
401 // the default units for the bb values when reading
403 dialog_->lbXunit->setCurrentItem(0);
404 dialog_->lbYunit->setCurrentItem(0);
405 dialog_->rtXunit->setCurrentItem(0);
406 dialog_->rtYunit->setCurrentItem(0);
408 controller().bbChanged = false;
413 bool QGraphics::isValid()
415 return !string(dialog_->filename->text().latin1()).empty();