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"
22 #include "validators.h"
24 #include "lengthcommon.h"
27 #include "controllers/ControlGraphics.h"
28 #include "controllers/helper_funcs.h"
30 #include "insets/insetgraphicsParams.h"
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
38 #include <QPushButton>
44 using lyx::support::float_equal;
45 using lyx::support::token;
47 using lyx::support::os::internal_path;
49 #ifndef CXX_GLOBAL_CSTD
59 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
61 QGraphics::QGraphics(Dialog & parent)
62 : base_class(parent, _("Graphics"))
67 void QGraphics::build_dialog()
69 dialog_.reset(new QGraphicsDialog(this));
71 bcview().setOK(dialog_->okPB);
72 bcview().setApply(dialog_->applyPB);
73 bcview().setRestore(dialog_->restorePB);
74 bcview().setCancel(dialog_->closePB);
76 bcview().addReadOnly(dialog_->latexoptions);
77 bcview().addReadOnly(dialog_->subfigure);
78 bcview().addReadOnly(dialog_->filenameL);
79 bcview().addReadOnly(dialog_->filename);
80 bcview().addReadOnly(dialog_->browsePB);
81 bcview().addReadOnly(dialog_->unzipCB);
82 bcview().addReadOnly(dialog_->filename);
83 bcview().addReadOnly(dialog_->bbFrame);
84 bcview().addReadOnly(dialog_->draftCB);
85 bcview().addReadOnly(dialog_->clip);
86 bcview().addReadOnly(dialog_->unzipCB);
87 bcview().addReadOnly(dialog_->displayGB);
88 bcview().addReadOnly(dialog_->sizeGB);
89 bcview().addReadOnly(dialog_->rotationGB);
90 bcview().addReadOnly(dialog_->latexoptions);
91 bcview().addReadOnly(dialog_->getPB);
93 // initialize the length validator
94 addCheckedLineEdit(bcview(), dialog_->Width, dialog_->widthL);
95 addCheckedLineEdit(bcview(), dialog_->Height, dialog_->heightL);
96 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
97 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
98 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
99 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
100 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
101 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
102 addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
108 // returns the number of the string s in the vector v
109 int getItemNo(vector<string> v, string const & s) {
110 vector<string>::const_iterator cit =
111 find(v.begin(), v.end(), s);
112 return (cit != v.end()) ? int(cit - v.begin()) : 0;
118 void QGraphics::update_contents()
120 PathValidator * path_validator = getPathValidator(dialog_->filename);
122 path_validator->setChecker(kernel().docType(), lyxrc);
124 // clear and fill in the comboboxes
125 vector<string> const bb_units = frontend::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->addItem(toqstr(*it));
133 dialog_->lbYunit->addItem(toqstr(*it));
134 dialog_->rtXunit->addItem(toqstr(*it));
135 dialog_->rtYunit->addItem(toqstr(*it));
138 InsetGraphicsParams & igp = controller().params();
140 // set the right default unit
141 LyXLength::UNIT unitDefault = LyXLength::CM;
142 switch (lyxrc.default_papersize) {
145 case PAPER_USEXECUTIVE:
146 unitDefault = LyXLength::IN;
153 igp.filename.outputFilename(kernel().bufferFilepath());
154 dialog_->filename->setText(toqstr(name));
156 // set the bounding box values
157 if (igp.bb.empty()) {
158 string const bb = controller().readBB(igp.filename.absFilename());
159 // the values from the file always have the bigpoint-unit bp
160 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
161 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
162 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
163 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
164 dialog_->lbXunit->setCurrentIndex(0);
165 dialog_->lbYunit->setCurrentIndex(0);
166 dialog_->rtXunit->setCurrentIndex(0);
167 dialog_->rtYunit->setCurrentIndex(0);
168 controller().bbChanged = false;
170 // get the values from the inset
172 string const xl(token(igp.bb, ' ', 0));
173 string const yl(token(igp.bb, ' ', 1));
174 string const xr(token(igp.bb, ' ', 2));
175 string const yr(token(igp.bb, ' ', 3));
176 if (isValidLength(xl, &anyLength)) {
177 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
178 string const unit(unit_name[anyLength.unit()]);
179 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
181 dialog_->lbX->setText(toqstr(xl));
183 if (isValidLength(yl, &anyLength)) {
184 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
185 string const unit(unit_name[anyLength.unit()]);
186 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
188 dialog_->lbY->setText(toqstr(xl));
190 if (isValidLength(xr, &anyLength)) {
191 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
192 string const unit(unit_name[anyLength.unit()]);
193 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
195 dialog_->rtX->setText(toqstr(xl));
197 if (isValidLength(yr, &anyLength)) {
198 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
199 string const unit(unit_name[anyLength.unit()]);
200 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
202 dialog_->rtY->setText(toqstr(xl));
204 controller().bbChanged = true;
207 // Update the draft and clip mode
208 dialog_->draftCB->setChecked(igp.draft);
209 dialog_->clip->setChecked(igp.clip);
210 dialog_->unzipCB->setChecked(igp.noUnzip);
212 // Update the subcaption check button and input field
213 dialog_->subfigure->setChecked(igp.subcaption);
214 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
217 switch (igp.display) {
218 case graphics::DefaultDisplay: item = 0; break;
219 case graphics::MonochromeDisplay: item = 1; break;
220 case graphics::GrayscaleDisplay: item = 2; break;
221 case graphics::ColorDisplay: item = 3; break;
222 case graphics::NoDisplay: item = 0; break;
224 dialog_->showCB->setCurrentIndex(item);
225 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
226 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
228 // the output section (width/height)
229 dialog_->Scale->setText(toqstr(igp.scale));
231 lengthToWidgets(dialog_->Width, dialog_->widthUnit,
232 igp.width.asString(), unitDefault);
234 lengthToWidgets(dialog_->Height, dialog_->heightUnit,
235 igp.height.asString(), unitDefault);
237 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
239 dialog_->scaleCB->setChecked(!igp.scale.empty() || igp.width.empty());
241 dialog_->Scale->setEnabled(!igp.scale.empty() || igp.width.empty());
243 dialog_->angle->setText(toqstr(igp.rotateAngle));
245 dialog_->origin->clear();
247 vector<RotationOriginPair> origindata = getRotationOriginData();
248 vector<docstring> const origin_lang = getFirst(origindata);
249 QGraphics::origin_ltx = getSecond(origindata);
251 for (vector<docstring>::const_iterator it = origin_lang.begin();
252 it != origin_lang.end(); ++it)
253 dialog_->origin->addItem(toqstr(*it));
255 if (!igp.rotateOrigin.empty())
256 dialog_->origin->setCurrentIndex(
257 getItemNo(origin_ltx, igp.rotateOrigin));
259 dialog_->origin->setCurrentIndex(0);
261 // disable edit button when no filename is present
262 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
265 dialog_->latexoptions->setText(toqstr(igp.special));
269 void QGraphics::apply()
271 InsetGraphicsParams & igp = controller().params();
273 igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
274 kernel().bufferFilepath());
278 if (controller().bbChanged) {
280 string lbX(fromqstr(dialog_->lbX->text()));
281 string lbY(fromqstr(dialog_->lbY->text()));
282 string rtX(fromqstr(dialog_->rtX->text()));
283 string rtY(fromqstr(dialog_->rtY->text()));
285 convert<int>(lbX) + convert<int>(lbY) +
286 convert<int>(rtX) + convert<int>(rtX);
291 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
295 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
299 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
303 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
308 igp.draft = dialog_->draftCB->isChecked();
309 igp.clip = dialog_->clip->isChecked();
310 igp.subcaption = dialog_->subfigure->isChecked();
311 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
313 switch (dialog_->showCB->currentIndex()) {
314 case 0: igp.display = graphics::DefaultDisplay; break;
315 case 1: igp.display = graphics::MonochromeDisplay; break;
316 case 2: igp.display = graphics::GrayscaleDisplay; break;
317 case 3: igp.display = graphics::ColorDisplay; break;
321 if (!dialog_->displayGB->isChecked())
322 igp.display = graphics::NoDisplay;
324 if (dialog_->scaleCB->isChecked()
325 && !dialog_->Scale->text().isEmpty()) {
326 igp.scale = fromqstr(dialog_->Scale->text());
328 igp.scale = string();
331 igp.width = LyXLength(widgetsToLength(dialog_->Width, dialog_->widthUnit));
333 igp.height = LyXLength(widgetsToLength(dialog_->Height, dialog_->heightUnit));
335 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
337 igp.noUnzip = dialog_->unzipCB->isChecked();
339 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
341 igp.rotateAngle = fromqstr(dialog_->angle->text());
343 double rotAngle = convert<double>(igp.rotateAngle);
344 if (std::abs(rotAngle) > 360.0) {
345 rotAngle -= 360.0 * floor(rotAngle / 360.0);
346 igp.rotateAngle = convert<string>(rotAngle);
349 // save the latex name for the origin. If it is the default
350 // then origin_ltx returns ""
352 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
354 // more latex options
355 igp.special = fromqstr(dialog_->latexoptions->text());
359 void QGraphics::getBB()
361 string const filename(fromqstr(dialog_->filename->text()));
362 if (!filename.empty()) {
363 string const bb(controller().readBB(filename));
365 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
366 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
367 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
368 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
369 // the default units for the bb values when reading
371 dialog_->lbXunit->setCurrentIndex(0);
372 dialog_->lbYunit->setCurrentIndex(0);
373 dialog_->rtXunit->setCurrentIndex(0);
374 dialog_->rtYunit->setCurrentIndex(0);
376 controller().bbChanged = false;
381 bool QGraphics::isValid()
383 return !dialog_->filename->text().isEmpty();
386 } // namespace frontend