]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QGraphics.cpp
Whitespace cleanup
[features.git] / src / frontends / qt4 / QGraphics.cpp
1 /**
2  * \file QGraphics.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Edwin Leuven
8  * \author Herbert Voß
9  * \author Richard Heck
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "QGraphics.h"
17
18 #include "CheckedLineEdit.h"
19 #include "LengthCombo.h"
20 #include "QGraphicsDialog.h"
21 #include "Qt2BC.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
24
25 #include "lengthcommon.h"
26 #include "LyXRC.h"
27
28 #include "controllers/ControlGraphics.h"
29 #include "controllers/frontend_helpers.h"
30
31 #include "insets/InsetGraphicsParams.h"
32
33 #include "support/convert.h"
34 #include "support/lstrings.h"
35 #include "support/lyxlib.h"
36 #include "support/os.h"
37
38 #include <QLineEdit>
39 #include <QPushButton>
40 #include <QCheckBox>
41 #include <QLabel>
42
43 #include <cmath>
44
45 using lyx::support::float_equal;
46 using lyx::support::token;
47
48 using lyx::support::os::internal_path;
49
50 #ifndef CXX_GLOBAL_CSTD
51 using std::floor;
52 #endif
53
54 using std::vector;
55 using std::string;
56
57 namespace lyx {
58 namespace frontend {
59
60 typedef QController<ControlGraphics, QView<QGraphicsDialog> > graphics_base_class;
61
62 QGraphics::QGraphics(Dialog & parent)
63         : graphics_base_class(parent, _("Graphics"))
64 {
65 }
66
67
68 void QGraphics::build_dialog()
69 {
70         dialog_.reset(new QGraphicsDialog(this));
71
72         bcview().setOK(dialog_->okPB);
73         bcview().setApply(dialog_->applyPB);
74         bcview().setRestore(dialog_->restorePB);
75         bcview().setCancel(dialog_->closePB);
76
77         bcview().addReadOnly(dialog_->latexoptions);
78         bcview().addReadOnly(dialog_->subfigure);
79         bcview().addReadOnly(dialog_->filenameL);
80         bcview().addReadOnly(dialog_->filename);
81         bcview().addReadOnly(dialog_->browsePB);
82         bcview().addReadOnly(dialog_->unzipCB);
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);
92
93         // initialize the length validator
94         addCheckedLineEdit(bcview(), dialog_->Scale, dialog_->scaleCB);
95         addCheckedLineEdit(bcview(), dialog_->Width, dialog_->WidthCB);
96         addCheckedLineEdit(bcview(), dialog_->Height, dialog_->HeightCB);
97         addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
98         addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
99         addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
100         addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
101         addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
102         addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
103         addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
104 }
105
106
107 namespace {
108
109 // returns the number of the string s in the vector v
110 int getItemNo(vector<string> v, string const & s) {
111         vector<string>::const_iterator cit =
112                     find(v.begin(), v.end(), s);
113         return (cit != v.end()) ? int(cit - v.begin()) : 0;
114 }
115
116 }
117
118
119 void QGraphics::update_contents()
120 {
121         PathValidator * path_validator = getPathValidator(dialog_->filename);
122         if (path_validator)
123                 path_validator->setChecker(kernel().docType(), lyxrc);
124
125         // clear and fill in the comboboxes
126         vector<string> const bb_units = frontend::getBBUnits();
127         dialog_->lbXunit->clear();
128         dialog_->lbYunit->clear();
129         dialog_->rtXunit->clear();
130         dialog_->rtYunit->clear();
131         for (vector<string>::const_iterator it = bb_units.begin();
132             it != bb_units.end(); ++it) {
133                 dialog_->lbXunit->addItem(toqstr(*it));
134                 dialog_->lbYunit->addItem(toqstr(*it));
135                 dialog_->rtXunit->addItem(toqstr(*it));
136                 dialog_->rtYunit->addItem(toqstr(*it));
137         }
138
139         InsetGraphicsParams & igp = controller().params();
140
141         // set the right default unit
142         Length::UNIT unitDefault = Length::CM;
143         switch (lyxrc.default_papersize) {
144                 case PAPER_USLETTER:
145                 case PAPER_USLEGAL:
146                 case PAPER_USEXECUTIVE:
147                         unitDefault = Length::IN;
148                         break;
149                 default:
150                         break;
151         }
152
153         string const name =
154                 igp.filename.outputFilename(kernel().bufferFilepath());
155         dialog_->filename->setText(toqstr(name));
156
157         // set the bounding box values
158         if (igp.bb.empty()) {
159                 string const bb = controller().readBB(igp.filename.absFilename());
160                 // the values from the file always have the bigpoint-unit bp
161                 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
162                 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
163                 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
164                 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
165                 dialog_->lbXunit->setCurrentIndex(0);
166                 dialog_->lbYunit->setCurrentIndex(0);
167                 dialog_->rtXunit->setCurrentIndex(0);
168                 dialog_->rtYunit->setCurrentIndex(0);
169                 controller().bbChanged = false;
170         } else {
171                 // get the values from the inset
172                 Length anyLength;
173                 string const xl(token(igp.bb, ' ', 0));
174                 string const yl(token(igp.bb, ' ', 1));
175                 string const xr(token(igp.bb, ' ', 2));
176                 string const yr(token(igp.bb, ' ', 3));
177                 if (isValidLength(xl, &anyLength)) {
178                         dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
179                         string const unit(unit_name[anyLength.unit()]);
180                         dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
181                 } else {
182                         dialog_->lbX->setText(toqstr(xl));
183                 }
184                 if (isValidLength(yl, &anyLength)) {
185                         dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
186                         string const unit(unit_name[anyLength.unit()]);
187                         dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
188                 } else {
189                         dialog_->lbY->setText(toqstr(xl));
190                 }
191                 if (isValidLength(xr, &anyLength)) {
192                         dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
193                         string const unit(unit_name[anyLength.unit()]);
194                         dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
195                 } else {
196                         dialog_->rtX->setText(toqstr(xl));
197                 }
198                 if (isValidLength(yr, &anyLength)) {
199                         dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
200                         string const unit(unit_name[anyLength.unit()]);
201                         dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
202                 } else {
203                         dialog_->rtY->setText(toqstr(xl));
204                 }
205                 controller().bbChanged = true;
206         }
207
208         // Update the draft and clip mode
209         dialog_->draftCB->setChecked(igp.draft);
210         dialog_->clip->setChecked(igp.clip);
211         dialog_->unzipCB->setChecked(igp.noUnzip);
212
213         // Update the subcaption check button and input field
214         dialog_->subfigure->setChecked(igp.subcaption);
215         dialog_->subcaption->setText(toqstr(igp.subcaptionText));
216
217         int item = 0;
218         switch (igp.display) {
219                 case graphics::DefaultDisplay: item = 0; break;
220                 case graphics::MonochromeDisplay: item = 1; break;
221                 case graphics::GrayscaleDisplay: item = 2; break;
222                 case graphics::ColorDisplay: item = 3; break;
223                 case graphics::NoDisplay: item = 0; break;
224         }
225         dialog_->showCB->setCurrentIndex(item);
226         dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
227         dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
228
229         // the output section (width/height)
230
231         dialog_->Scale->setText(toqstr(igp.scale));
232         //igp.scale defaults to 100, so we treat it as empty
233         bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
234         dialog_->scaleCB->blockSignals(true);
235         dialog_->scaleCB->setChecked(scaleChecked);
236         dialog_->scaleCB->blockSignals(false);
237         dialog_->Scale->setEnabled(scaleChecked);
238
239         lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
240                 unitDefault);
241         bool const widthChecked = !dialog_->Width->text().isEmpty() &&
242                 dialog_->Width->text() != "auto";
243         dialog_->WidthCB->blockSignals(true);
244         dialog_->WidthCB->setChecked(widthChecked);
245         dialog_->WidthCB->blockSignals(false);
246         dialog_->Width->setEnabled(widthChecked);
247         dialog_->widthUnit->setEnabled(widthChecked);
248
249         lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
250                 unitDefault);
251         bool const heightChecked = !dialog_->Height->text().isEmpty()
252                 && dialog_->Height->text() != "auto";
253         dialog_->HeightCB->blockSignals(true);
254         dialog_->HeightCB->setChecked(heightChecked);
255         dialog_->HeightCB->blockSignals(false);
256         dialog_->Height->setEnabled(heightChecked);
257         dialog_->heightUnit->setEnabled(heightChecked);
258
259         dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
260         dialog_->WidthCB->setEnabled(!scaleChecked);
261         dialog_->HeightCB->setEnabled(!scaleChecked);
262         dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
263
264         dialog_->setAutoText();
265
266         dialog_->angle->setText(toqstr(igp.rotateAngle));
267
268         dialog_->origin->clear();
269
270         vector<RotationOriginPair> origindata = getRotationOriginData();
271         vector<docstring> const origin_lang = getFirst(origindata);
272         QGraphics::origin_ltx = getSecond(origindata);
273
274         for (vector<docstring>::const_iterator it = origin_lang.begin();
275             it != origin_lang.end(); ++it)
276                 dialog_->origin->addItem(toqstr(*it));
277
278         if (!igp.rotateOrigin.empty())
279                 dialog_->origin->setCurrentIndex(
280                         getItemNo(origin_ltx, igp.rotateOrigin));
281         else
282                 dialog_->origin->setCurrentIndex(0);
283
284         // disable edit button when no filename is present
285         dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
286
287         //// latex section
288         dialog_->latexoptions->setText(toqstr(igp.special));
289 }
290
291
292 void QGraphics::apply()
293 {
294         InsetGraphicsParams & igp = controller().params();
295
296         igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
297                          kernel().bufferFilepath());
298
299         // the bb section
300         igp.bb.erase();
301         if (controller().bbChanged) {
302                 string bb;
303                 string lbX(fromqstr(dialog_->lbX->text()));
304                 string lbY(fromqstr(dialog_->lbY->text()));
305                 string rtX(fromqstr(dialog_->rtX->text()));
306                 string rtY(fromqstr(dialog_->rtY->text()));
307                 int bb_sum =
308                         convert<int>(lbX) + convert<int>(lbY) +
309                         convert<int>(rtX) + convert<int>(rtX);
310                 if (bb_sum) {
311                         if (lbX.empty())
312                                 bb = "0 ";
313                         else
314                                 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
315                         if (lbY.empty())
316                                 bb += "0 ";
317                         else
318                                 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
319                         if (rtX.empty())
320                                 bb += "0 ";
321                         else
322                                 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
323                         if (rtY.empty())
324                                 bb += '0';
325                         else
326                                 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
327                         igp.bb = bb;
328                 }
329         }
330
331         igp.draft = dialog_->draftCB->isChecked();
332         igp.clip = dialog_->clip->isChecked();
333         igp.subcaption = dialog_->subfigure->isChecked();
334         igp.subcaptionText = fromqstr(dialog_->subcaption->text());
335
336         switch (dialog_->showCB->currentIndex()) {
337                 case 0: igp.display = graphics::DefaultDisplay; break;
338                 case 1: igp.display = graphics::MonochromeDisplay; break;
339                 case 2: igp.display = graphics::GrayscaleDisplay; break;
340                 case 3: igp.display = graphics::ColorDisplay; break;
341                 default:;
342         }
343
344         if (!dialog_->displayGB->isChecked())
345                 igp.display = graphics::NoDisplay;
346
347         //the graphics section
348         if (dialog_->scaleCB->isChecked()       && !dialog_->Scale->text().isEmpty()) {
349                 igp.scale = fromqstr(dialog_->Scale->text());
350                 igp.width = Length("0pt");
351                 igp.height = Length("0pt");
352                 igp.keepAspectRatio = false;
353         } else {
354                 igp.scale = string();
355                 igp.width = dialog_->WidthCB->isChecked() ?
356                         //Note that this works even if dialog_->Width is "auto", since in
357                         //that case we get "0pt".
358                         Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
359                         Length("0pt");
360                 igp.height = dialog_->HeightCB->isChecked() ?
361                         Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
362                         Length("0pt");
363                 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
364                         dialog_->aspectratio->isChecked() &&
365                         igp.width.value() > 0 && igp.height.value() > 0;
366         }
367
368         igp.noUnzip = dialog_->unzipCB->isChecked();
369
370         igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
371
372         igp.rotateAngle = fromqstr(dialog_->angle->text());
373
374         double rotAngle = convert<double>(igp.rotateAngle);
375         if (std::abs(rotAngle) > 360.0) {
376                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
377                 igp.rotateAngle = convert<string>(rotAngle);
378         }
379
380         // save the latex name for the origin. If it is the default
381         // then origin_ltx returns ""
382         igp.rotateOrigin =
383                 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
384
385         // more latex options
386         igp.special = fromqstr(dialog_->latexoptions->text());
387 }
388
389
390 void QGraphics::getBB()
391 {
392         string const filename(fromqstr(dialog_->filename->text()));
393         if (!filename.empty()) {
394                 string const bb(controller().readBB(filename));
395                 if (!bb.empty()) {
396                         dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
397                         dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
398                         dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
399                         dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
400                         // the default units for the bb values when reading
401                         // it from the file
402                         dialog_->lbXunit->setCurrentIndex(0);
403                         dialog_->lbYunit->setCurrentIndex(0);
404                         dialog_->rtXunit->setCurrentIndex(0);
405                         dialog_->rtYunit->setCurrentIndex(0);
406                 }
407                 controller().bbChanged = false;
408         }
409 }
410
411
412 bool QGraphics::isValid()
413 {
414         return !dialog_->filename->text().isEmpty();
415 }
416
417 } // namespace frontend
418 } // namespace lyx