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 "QtLyXView.h"
17 #include "ControlGraphics.h"
18 #include "controllers/helper_funcs.h"
19 #include "support/lstrings.h"
20 #include "support/FileInfo.h"
21 #include "support/filetools.h"
22 #include "insets/insetgraphicsParams.h"
24 #include "lengthcombo.h"
25 #include "qt_helpers.h"
26 #include "lengthcommon.h"
27 #include "lyxlength.h"
30 #include <qlineedit.h>
31 #include <qpushbutton.h>
32 #include <qcheckbox.h>
33 #include <qradiobutton.h>
34 #include <qcombobox.h>
35 #include <qgroupbox.h>
36 #include <qbuttongroup.h>
39 #include "QGraphicsDialog.h"
40 #include "QGraphics.h"
46 typedef Qt2CB<ControlGraphics, Qt2DB<QGraphicsDialog> > base_class;
49 QGraphics::QGraphics()
50 : base_class(qt_("Graphics"))
55 void QGraphics::build_dialog()
57 dialog_.reset(new QGraphicsDialog(this));
59 bc().setOK(dialog_->okPB);
60 bc().setApply(dialog_->applyPB);
61 bc().setRestore(dialog_->restorePB);
62 bc().setCancel(dialog_->closePB);
64 bc().addReadOnly(dialog_->rotateGB);
65 bc().addReadOnly(dialog_->latexoptions);
66 bc().addReadOnly(dialog_->subfigure);
67 bc().addReadOnly(dialog_->subcaption);
68 bc().addReadOnly(dialog_->filenameL);
69 bc().addReadOnly(dialog_->filename);
70 bc().addReadOnly(dialog_->browsePB);
71 bc().addReadOnly(dialog_->unzipCB);
72 bc().addReadOnly(dialog_->filename);
73 bc().addReadOnly(dialog_->lbX);
74 bc().addReadOnly(dialog_->lbY);
75 bc().addReadOnly(dialog_->rtX);
76 bc().addReadOnly(dialog_->rtY);
77 bc().addReadOnly(dialog_->lbXunit);
78 bc().addReadOnly(dialog_->lbYunit);
79 bc().addReadOnly(dialog_->rtXunit);
80 bc().addReadOnly(dialog_->rtYunit);
81 bc().addReadOnly(dialog_->draftCB);
82 bc().addReadOnly(dialog_->clip);
83 bc().addReadOnly(dialog_->unzipCB);
84 bc().addReadOnly(dialog_->subfigure);
85 bc().addReadOnly(dialog_->subcaption);
86 bc().addReadOnly(dialog_->showCB);
87 bc().addReadOnly(dialog_->width);
88 bc().addReadOnly(dialog_->height);
89 bc().addReadOnly(dialog_->displayCB);
90 bc().addReadOnly(dialog_->displayscale);
91 bc().addReadOnly(dialog_->widthUnit);
92 bc().addReadOnly(dialog_->heightUnit);
93 bc().addReadOnly(dialog_->aspectratio);
94 bc().addReadOnly(dialog_->angle);
95 bc().addReadOnly(dialog_->origin);
96 bc().addReadOnly(dialog_->latexoptions);
97 bc().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 * 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 BufferParams::PAPER_DEFAULT: break;
145 case BufferParams::PAPER_USLETTER:
146 case BufferParams::PAPER_LEGALPAPER:
147 case BufferParams::PAPER_EXECUTIVEPAPER:
148 unitDefault = LyXLength::IN;
151 case BufferParams::PAPER_A3PAPER:
152 case BufferParams::PAPER_A4PAPER:
153 case BufferParams::PAPER_A5PAPER:
154 case BufferParams::PAPER_B5PAPER:
155 unitDefault = LyXLength::CM;
159 dialog_->filename->setText(toqstr(igp.filename));
161 // set the bounding box values
162 if (igp.bb.empty()) {
163 string const bb = controller().readBB(igp.filename);
164 // the values from the file always have the bigpoint-unit bp
165 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
166 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
167 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
168 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
169 dialog_->lbXunit->setCurrentItem(0);
170 dialog_->lbYunit->setCurrentItem(0);
171 dialog_->rtXunit->setCurrentItem(0);
172 dialog_->rtYunit->setCurrentItem(0);
173 controller().bbChanged = false;
175 // get the values from the inset
177 string const xl(token(igp.bb, ' ', 0));
178 string const yl(token(igp.bb, ' ', 1));
179 string const xr(token(igp.bb, ' ', 2));
180 string const yr(token(igp.bb, ' ', 3));
181 if (isValidLength(xl, &anyLength)) {
182 dialog_->lbX->setText(toqstr(tostr(anyLength.value())));
183 string const unit(unit_name[anyLength.unit()]);
184 dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
186 dialog_->lbX->setText(toqstr(xl));
188 if (isValidLength(yl, &anyLength)) {
189 dialog_->lbY->setText(toqstr(tostr(anyLength.value())));
190 string const unit(unit_name[anyLength.unit()]);
191 dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
193 dialog_->lbY->setText(toqstr(xl));
195 if (isValidLength(xr, &anyLength)) {
196 dialog_->rtX->setText(toqstr(tostr(anyLength.value())));
197 string const unit(unit_name[anyLength.unit()]);
198 dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
200 dialog_->rtX->setText(toqstr(xl));
202 if (isValidLength(yr, &anyLength)) {
203 dialog_->rtY->setText(toqstr(tostr(anyLength.value())));
204 string const unit(unit_name[anyLength.unit()]);
205 dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
207 dialog_->rtY->setText(toqstr(xl));
209 controller().bbChanged = true;
212 // Update the draft and clip mode
213 dialog_->draftCB->setChecked(igp.draft);
214 dialog_->clip->setChecked(igp.clip);
215 dialog_->unzipCB->setChecked(igp.noUnzip);
217 // Update the subcaption check button and input field
218 dialog_->subfigure->setChecked(igp.subcaption);
219 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
222 switch (igp.display) {
223 case grfx::DefaultDisplay: item = 0; break;
224 case grfx::MonochromeDisplay: item = 1; break;
225 case grfx::GrayscaleDisplay: item = 2; break;
226 case grfx::ColorDisplay: item = 3; break;
227 case grfx::NoDisplay: item = 0; break;
229 dialog_->showCB->setCurrentItem(item);
230 dialog_->showCB->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
231 dialog_->displayCB->setChecked(igp.display != grfx::NoDisplay);
232 dialog_->displayscale->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
233 dialog_->displayscale->setText(toqstr(tostr(igp.lyxscale)));
235 //// the output section (width/height)
236 // set the length combo boxes
237 // only the width has the possibility for scale%. The original
238 // units are defined in lengthcommon.C
239 // 1. the width (a listttype)
240 dialog_->widthUnit->clear();
241 dialog_->widthUnit->insertItem(qt_("Scale%"));
242 for (int i = 0; i < num_units; i++)
243 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
245 if (!lyx::float_equal(igp.scale, 0.0, 0.05)) {
246 // there is a scale value > 0.05
247 dialog_->width->setText(toqstr(tostr(igp.scale)));
248 dialog_->widthUnit->setCurrentItem(0);
250 // no scale means default width/height
251 dialog_->width->setText(toqstr(tostr(igp.width.value())));
252 // the width cannot have a unitDefault, because
253 // it is a "Scale%" or another user defined unit!
254 // +1 instead of the "Scale%" option
255 int unit_ = igp.width.unit();
256 dialog_->widthUnit->setCurrentItem(unit_ + 1);
258 // 2. the height (a lengthgcombo type)
259 dialog_->height->setText(toqstr(tostr(igp.height.value())));
260 LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
261 igp.height.unit() : unitDefault;
262 dialog_->heightUnit->setCurrentItem(unit_);
264 // enable height input in case of non "Scale%" as width-unit
265 bool use_height = (dialog_->widthUnit->currentItem() > 0);
266 dialog_->height->setEnabled(use_height);
267 dialog_->heightUnit->setEnabled(use_height);
269 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
271 dialog_->angle->setText(toqstr(tostr(igp.rotateAngle)));
273 dialog_->origin->clear();
275 using namespace frnt;
276 vector<RotationOriginPair> origindata = getRotationOriginData();
277 vector<string> const origin_lang = getFirst(origindata);
278 QGraphics::origin_ltx = getSecond(origindata);
280 for (vector<string>::const_iterator it = origin_lang.begin();
281 it != origin_lang.end(); ++it)
282 dialog_->origin->insertItem(toqstr(*it), -1);
284 if (!igp.rotateOrigin.empty())
285 dialog_->origin->setCurrentItem(
286 ::getItemNo(origin_ltx, igp.rotateOrigin));
288 dialog_->origin->setCurrentItem(0);
291 dialog_->latexoptions->setText(toqstr(igp.special));
295 void QGraphics::apply()
297 InsetGraphicsParams & igp = controller().params();
299 igp.filename = fromqstr(dialog_->filename->text());
303 if (controller().bbChanged) {
305 string lbX(fromqstr(dialog_->lbX->text()));
306 string lbY(fromqstr(dialog_->lbY->text()));
307 string rtX(fromqstr(dialog_->rtX->text()));
308 string rtY(fromqstr(dialog_->rtY->text()));
310 strToInt(lbX) + strToInt(lbY) +
311 strToInt(rtX) + strToInt(rtX);
316 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
320 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
324 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
328 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
333 igp.draft = dialog_->draftCB->isChecked();
334 igp.clip = dialog_->clip->isChecked();
335 igp.subcaption = dialog_->subfigure->isChecked();
336 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
338 switch (dialog_->showCB->currentItem()) {
339 case 0: igp.display = grfx::DefaultDisplay; break;
340 case 1: igp.display = grfx::MonochromeDisplay; break;
341 case 2: igp.display = grfx::GrayscaleDisplay; break;
342 case 3: igp.display = grfx::ColorDisplay; break;
346 if (!dialog_->displayCB->isChecked())
347 igp.display = grfx::NoDisplay;
349 string value(fromqstr(dialog_->width->text()));
350 if (dialog_->widthUnit->currentItem() > 0) {
351 // width/height combination
352 int const unitNo = getUnitNo(unit_name_gui,
353 fromqstr(dialog_->widthUnit->currentText()));
354 igp.width = LyXLength(value + unit_name_ltx[unitNo]);
357 // scaling instead of a width
358 igp.scale = strToDbl(value);
359 igp.width = LyXLength();
361 value = fromqstr(dialog_->height->text());
362 int const unitNo = getUnitNo(unit_name_gui,
363 fromqstr(dialog_->heightUnit->currentText()));
364 igp.height = LyXLength(value + unit_name_ltx[unitNo]);
366 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
368 igp.noUnzip = dialog_->unzipCB->isChecked();
370 igp.lyxscale = strToInt(fromqstr(dialog_->displayscale->text()));
372 igp.rotateAngle = strToDbl(fromqstr(dialog_->angle->text()));
373 while (igp.rotateAngle < -360.0)
374 igp.rotateAngle += 360.0;
375 while (igp.rotateAngle > 360.0)
376 igp.rotateAngle -= 360.0;
378 // save the latex name for the origin. If it is the default
379 // then origin_ltx returns ""
381 QGraphics::origin_ltx[dialog_->origin->currentItem()];
383 // more latex options
384 igp.special = fromqstr(dialog_->latexoptions->text());
388 void QGraphics::getBB()
390 string const filename(fromqstr(dialog_->filename->text()));
391 if (!filename.empty()) {
392 string const bb(controller().readBB(filename));
394 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
395 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
396 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
397 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
398 // the default units for the bb values when reading
400 dialog_->lbXunit->setCurrentItem(0);
401 dialog_->lbYunit->setCurrentItem(0);
402 dialog_->rtXunit->setCurrentItem(0);
403 dialog_->rtYunit->setCurrentItem(0);
405 controller().bbChanged = false;
410 bool QGraphics::isValid()
412 return !dialog_->filename->text().isEmpty();