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"
37 #include <QPushButton>
43 using lyx::support::float_equal;
44 using lyx::support::token;
46 #ifndef CXX_GLOBAL_CSTD
56 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
58 QGraphics::QGraphics(Dialog & parent)
59 : base_class(parent, _("Graphics"))
64 void QGraphics::build_dialog()
66 dialog_.reset(new QGraphicsDialog(this));
68 bcview().setOK(dialog_->okPB);
69 bcview().setApply(dialog_->applyPB);
70 bcview().setRestore(dialog_->restorePB);
71 bcview().setCancel(dialog_->closePB);
73 bcview().addReadOnly(dialog_->latexoptions);
74 bcview().addReadOnly(dialog_->subfigure);
75 bcview().addReadOnly(dialog_->filenameL);
76 bcview().addReadOnly(dialog_->filename);
77 bcview().addReadOnly(dialog_->browsePB);
78 bcview().addReadOnly(dialog_->unzipCB);
79 bcview().addReadOnly(dialog_->filename);
80 bcview().addReadOnly(dialog_->bbFrame);
81 bcview().addReadOnly(dialog_->draftCB);
82 bcview().addReadOnly(dialog_->clip);
83 bcview().addReadOnly(dialog_->unzipCB);
84 bcview().addReadOnly(dialog_->displayGB);
85 bcview().addReadOnly(dialog_->sizeGB);
86 bcview().addReadOnly(dialog_->rotationGB);
87 bcview().addReadOnly(dialog_->latexoptions);
88 bcview().addReadOnly(dialog_->getPB);
90 // initialize the length validator
91 addCheckedLineEdit(bcview(), dialog_->Width, dialog_->widthL);
92 addCheckedLineEdit(bcview(), dialog_->Height, dialog_->heightL);
93 addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
94 addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
95 addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
96 addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
97 addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
98 addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
99 addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
105 // returns the number of the string s in the vector v
106 int getItemNo(vector<string> v, string const & s) {
107 vector<string>::const_iterator cit =
108 find(v.begin(), v.end(), s);
109 return (cit != v.end()) ? int(cit - v.begin()) : 0;
115 void QGraphics::update_contents()
117 PathValidator * path_validator = getPathValidator(dialog_->filename);
119 path_validator->setChecker(kernel().docType(), lyxrc);
121 // clear and fill in the comboboxes
122 vector<string> const bb_units = frontend::getBBUnits();
123 dialog_->lbXunit->clear();
124 dialog_->lbYunit->clear();
125 dialog_->rtXunit->clear();
126 dialog_->rtYunit->clear();
127 for (vector<string>::const_iterator it = bb_units.begin();
128 it != bb_units.end(); ++it) {
129 dialog_->lbXunit->addItem(toqstr(*it));
130 dialog_->lbYunit->addItem(toqstr(*it));
131 dialog_->rtXunit->addItem(toqstr(*it));
132 dialog_->rtYunit->addItem(toqstr(*it));
135 InsetGraphicsParams & igp = controller().params();
137 // set the right default unit
138 LyXLength::UNIT unitDefault = LyXLength::CM;
139 switch (lyxrc.default_papersize) {
142 case PAPER_USEXECUTIVE:
143 unitDefault = LyXLength::IN;
150 igp.filename.outputFilename(kernel().bufferFilepath());
151 dialog_->filename->setText(toqstr(name));
153 // set the bounding box values
154 if (igp.bb.empty()) {
155 string const bb = controller().readBB(igp.filename.absFilename());
156 // the values from the file always have the bigpoint-unit bp
157 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
158 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
159 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
160 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
161 dialog_->lbXunit->setCurrentIndex(0);
162 dialog_->lbYunit->setCurrentIndex(0);
163 dialog_->rtXunit->setCurrentIndex(0);
164 dialog_->rtYunit->setCurrentIndex(0);
165 controller().bbChanged = false;
167 // get the values from the inset
169 string const xl(token(igp.bb, ' ', 0));
170 string const yl(token(igp.bb, ' ', 1));
171 string const xr(token(igp.bb, ' ', 2));
172 string const yr(token(igp.bb, ' ', 3));
173 if (isValidLength(xl, &anyLength)) {
174 dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
175 string const unit(unit_name[anyLength.unit()]);
176 dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
178 dialog_->lbX->setText(toqstr(xl));
180 if (isValidLength(yl, &anyLength)) {
181 dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
182 string const unit(unit_name[anyLength.unit()]);
183 dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
185 dialog_->lbY->setText(toqstr(xl));
187 if (isValidLength(xr, &anyLength)) {
188 dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
189 string const unit(unit_name[anyLength.unit()]);
190 dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
192 dialog_->rtX->setText(toqstr(xl));
194 if (isValidLength(yr, &anyLength)) {
195 dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
196 string const unit(unit_name[anyLength.unit()]);
197 dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
199 dialog_->rtY->setText(toqstr(xl));
201 controller().bbChanged = true;
204 // Update the draft and clip mode
205 dialog_->draftCB->setChecked(igp.draft);
206 dialog_->clip->setChecked(igp.clip);
207 dialog_->unzipCB->setChecked(igp.noUnzip);
209 // Update the subcaption check button and input field
210 dialog_->subfigure->setChecked(igp.subcaption);
211 dialog_->subcaption->setText(toqstr(igp.subcaptionText));
214 switch (igp.display) {
215 case graphics::DefaultDisplay: item = 0; break;
216 case graphics::MonochromeDisplay: item = 1; break;
217 case graphics::GrayscaleDisplay: item = 2; break;
218 case graphics::ColorDisplay: item = 3; break;
219 case graphics::NoDisplay: item = 0; break;
221 dialog_->showCB->setCurrentIndex(item);
222 dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
223 dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
225 // the output section (width/height)
226 dialog_->Scale->setText(toqstr(igp.scale));
228 lengthToWidgets(dialog_->Width, dialog_->widthUnit,
229 igp.width.asString(), unitDefault);
231 lengthToWidgets(dialog_->Height, dialog_->heightUnit,
232 igp.height.asString(), unitDefault);
234 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
236 dialog_->scaleCB->setChecked(!igp.scale.empty() || igp.width.empty());
238 dialog_->Scale->setEnabled(!igp.scale.empty() || igp.width.empty());
240 dialog_->angle->setText(toqstr(igp.rotateAngle));
242 dialog_->origin->clear();
244 using namespace frontend;
245 vector<RotationOriginPair> origindata = getRotationOriginData();
246 vector<string> const origin_lang = getFirst(origindata);
247 QGraphics::origin_ltx = getSecond(origindata);
249 for (vector<string>::const_iterator it = origin_lang.begin();
250 it != origin_lang.end(); ++it)
251 dialog_->origin->addItem(toqstr(*it));
253 if (!igp.rotateOrigin.empty())
254 dialog_->origin->setCurrentIndex(
255 getItemNo(origin_ltx, igp.rotateOrigin));
257 dialog_->origin->setCurrentIndex(0);
259 // disable edit button when no filename is present
260 dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
263 dialog_->latexoptions->setText(toqstr(igp.special));
267 void QGraphics::apply()
269 InsetGraphicsParams & igp = controller().params();
271 igp.filename.set(fromqstr(dialog_->filename->text()),
272 kernel().bufferFilepath());
276 if (controller().bbChanged) {
278 string lbX(fromqstr(dialog_->lbX->text()));
279 string lbY(fromqstr(dialog_->lbY->text()));
280 string rtX(fromqstr(dialog_->rtX->text()));
281 string rtY(fromqstr(dialog_->rtY->text()));
283 convert<int>(lbX) + convert<int>(lbY) +
284 convert<int>(rtX) + convert<int>(rtX);
289 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
293 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
297 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
301 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
306 igp.draft = dialog_->draftCB->isChecked();
307 igp.clip = dialog_->clip->isChecked();
308 igp.subcaption = dialog_->subfigure->isChecked();
309 igp.subcaptionText = fromqstr(dialog_->subcaption->text());
311 switch (dialog_->showCB->currentIndex()) {
312 case 0: igp.display = graphics::DefaultDisplay; break;
313 case 1: igp.display = graphics::MonochromeDisplay; break;
314 case 2: igp.display = graphics::GrayscaleDisplay; break;
315 case 3: igp.display = graphics::ColorDisplay; break;
319 if (!dialog_->displayGB->isChecked())
320 igp.display = graphics::NoDisplay;
322 if (dialog_->scaleCB->isChecked()
323 && !dialog_->Scale->text().isEmpty()) {
324 igp.scale = fromqstr(dialog_->Scale->text());
326 igp.scale = string();
329 igp.width = LyXLength(widgetsToLength(dialog_->Width, dialog_->widthUnit));
331 igp.height = LyXLength(widgetsToLength(dialog_->Height, dialog_->heightUnit));
333 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
335 igp.noUnzip = dialog_->unzipCB->isChecked();
337 igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
339 igp.rotateAngle = fromqstr(dialog_->angle->text());
341 double rotAngle = convert<double>(igp.rotateAngle);
342 if (std::abs(rotAngle) > 360.0) {
343 rotAngle -= 360.0 * floor(rotAngle / 360.0);
344 igp.rotateAngle = convert<string>(rotAngle);
347 // save the latex name for the origin. If it is the default
348 // then origin_ltx returns ""
350 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
352 // more latex options
353 igp.special = fromqstr(dialog_->latexoptions->text());
357 void QGraphics::getBB()
359 string const filename(fromqstr(dialog_->filename->text()));
360 if (!filename.empty()) {
361 string const bb(controller().readBB(filename));
363 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
364 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
365 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
366 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
367 // the default units for the bb values when reading
369 dialog_->lbXunit->setCurrentIndex(0);
370 dialog_->lbYunit->setCurrentIndex(0);
371 dialog_->rtXunit->setCurrentIndex(0);
372 dialog_->rtYunit->setCurrentIndex(0);
374 controller().bbChanged = false;
379 bool QGraphics::isValid()
381 return !dialog_->filename->text().isEmpty();
384 } // namespace frontend