]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QGraphics.cpp
Fix bug 1749. CAUTION: lyx2lyx not fully working yet (see FIXMEs).
[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         bcview().addReadOnly(dialog_->rotateOrderCB);
93
94         // initialize the length validator
95         addCheckedLineEdit(bcview(), dialog_->Scale, dialog_->scaleCB);
96         addCheckedLineEdit(bcview(), dialog_->Width, dialog_->WidthCB);
97         addCheckedLineEdit(bcview(), dialog_->Height, dialog_->HeightCB);
98         addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
99         addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
100         addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
101         addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
102         addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
103         addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
104         addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
105 }
106
107
108 namespace {
109
110 // returns the number of the string s in the vector v
111 int getItemNo(vector<string> v, string const & s) {
112         vector<string>::const_iterator cit =
113                     find(v.begin(), v.end(), s);
114         return (cit != v.end()) ? int(cit - v.begin()) : 0;
115 }
116
117 }
118
119
120 void QGraphics::update_contents()
121 {
122         PathValidator * path_validator = getPathValidator(dialog_->filename);
123         if (path_validator)
124                 path_validator->setChecker(kernel().docType(), lyxrc);
125
126         // clear and fill in the comboboxes
127         vector<string> const bb_units = frontend::getBBUnits();
128         dialog_->lbXunit->clear();
129         dialog_->lbYunit->clear();
130         dialog_->rtXunit->clear();
131         dialog_->rtYunit->clear();
132         for (vector<string>::const_iterator it = bb_units.begin();
133             it != bb_units.end(); ++it) {
134                 dialog_->lbXunit->addItem(toqstr(*it));
135                 dialog_->lbYunit->addItem(toqstr(*it));
136                 dialog_->rtXunit->addItem(toqstr(*it));
137                 dialog_->rtYunit->addItem(toqstr(*it));
138         }
139
140         InsetGraphicsParams & igp = controller().params();
141
142         // set the right default unit
143         Length::UNIT unitDefault = Length::CM;
144         switch (lyxrc.default_papersize) {
145                 case PAPER_USLETTER:
146                 case PAPER_USLEGAL:
147                 case PAPER_USEXECUTIVE:
148                         unitDefault = Length::IN;
149                         break;
150                 default:
151                         break;
152         }
153
154         string const name =
155                 igp.filename.outputFilename(kernel().bufferFilepath());
156         dialog_->filename->setText(toqstr(name));
157
158         // set the bounding box values
159         if (igp.bb.empty()) {
160                 string const bb = controller().readBB(igp.filename.absFilename());
161                 // the values from the file always have the bigpoint-unit bp
162                 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
163                 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
164                 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
165                 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
166                 dialog_->lbXunit->setCurrentIndex(0);
167                 dialog_->lbYunit->setCurrentIndex(0);
168                 dialog_->rtXunit->setCurrentIndex(0);
169                 dialog_->rtYunit->setCurrentIndex(0);
170                 controller().bbChanged = false;
171         } else {
172                 // get the values from the inset
173                 Length anyLength;
174                 string const xl(token(igp.bb, ' ', 0));
175                 string const yl(token(igp.bb, ' ', 1));
176                 string const xr(token(igp.bb, ' ', 2));
177                 string const yr(token(igp.bb, ' ', 3));
178                 if (isValidLength(xl, &anyLength)) {
179                         dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
180                         string const unit(unit_name[anyLength.unit()]);
181                         dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
182                 } else {
183                         dialog_->lbX->setText(toqstr(xl));
184                 }
185                 if (isValidLength(yl, &anyLength)) {
186                         dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
187                         string const unit(unit_name[anyLength.unit()]);
188                         dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
189                 } else {
190                         dialog_->lbY->setText(toqstr(xl));
191                 }
192                 if (isValidLength(xr, &anyLength)) {
193                         dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
194                         string const unit(unit_name[anyLength.unit()]);
195                         dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
196                 } else {
197                         dialog_->rtX->setText(toqstr(xl));
198                 }
199                 if (isValidLength(yr, &anyLength)) {
200                         dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
201                         string const unit(unit_name[anyLength.unit()]);
202                         dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
203                 } else {
204                         dialog_->rtY->setText(toqstr(xl));
205                 }
206                 controller().bbChanged = true;
207         }
208
209         // Update the draft and clip mode
210         dialog_->draftCB->setChecked(igp.draft);
211         dialog_->clip->setChecked(igp.clip);
212         dialog_->unzipCB->setChecked(igp.noUnzip);
213
214         // Update the subcaption check button and input field
215         dialog_->subfigure->setChecked(igp.subcaption);
216         dialog_->subcaption->setText(toqstr(igp.subcaptionText));
217
218         int item = 0;
219         switch (igp.display) {
220                 case graphics::DefaultDisplay: item = 0; break;
221                 case graphics::MonochromeDisplay: item = 1; break;
222                 case graphics::GrayscaleDisplay: item = 2; break;
223                 case graphics::ColorDisplay: item = 3; break;
224                 case graphics::NoDisplay: item = 0; break;
225         }
226         dialog_->showCB->setCurrentIndex(item);
227         dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
228         dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
229
230         // the output section (width/height)
231
232         dialog_->Scale->setText(toqstr(igp.scale));
233         //igp.scale defaults to 100, so we treat it as empty
234         bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
235         dialog_->scaleCB->blockSignals(true);
236         dialog_->scaleCB->setChecked(scaleChecked);
237         dialog_->scaleCB->blockSignals(false);
238         dialog_->Scale->setEnabled(scaleChecked);
239
240         lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
241                 unitDefault);
242         bool const widthChecked = !dialog_->Width->text().isEmpty() &&
243                 dialog_->Width->text() != "auto";
244         dialog_->WidthCB->blockSignals(true);
245         dialog_->WidthCB->setChecked(widthChecked);
246         dialog_->WidthCB->blockSignals(false);
247         dialog_->Width->setEnabled(widthChecked);
248         dialog_->widthUnit->setEnabled(widthChecked);
249
250         lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
251                 unitDefault);
252         bool const heightChecked = !dialog_->Height->text().isEmpty()
253                 && dialog_->Height->text() != "auto";
254         dialog_->HeightCB->blockSignals(true);
255         dialog_->HeightCB->setChecked(heightChecked);
256         dialog_->HeightCB->blockSignals(false);
257         dialog_->Height->setEnabled(heightChecked);
258         dialog_->heightUnit->setEnabled(heightChecked);
259
260         dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
261         dialog_->WidthCB->setEnabled(!scaleChecked);
262         dialog_->HeightCB->setEnabled(!scaleChecked);
263         dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
264
265         dialog_->setAutoText();
266
267         dialog_->angle->setText(toqstr(igp.rotateAngle));
268         dialog_->rotateOrderCB->setChecked(igp.scaleBeforeRotation);
269
270         dialog_->rotateOrderCB->setEnabled((widthChecked ||
271                                            heightChecked ||
272                                            scaleChecked) &&
273                                            (igp.rotateAngle != "0"));
274
275         dialog_->origin->clear();
276
277         vector<RotationOriginPair> origindata = getRotationOriginData();
278         vector<docstring> const origin_lang = getFirst(origindata);
279         QGraphics::origin_ltx = getSecond(origindata);
280
281         for (vector<docstring>::const_iterator it = origin_lang.begin();
282             it != origin_lang.end(); ++it)
283                 dialog_->origin->addItem(toqstr(*it));
284
285         if (!igp.rotateOrigin.empty())
286                 dialog_->origin->setCurrentIndex(
287                         getItemNo(origin_ltx, igp.rotateOrigin));
288         else
289                 dialog_->origin->setCurrentIndex(0);
290
291         // disable edit button when no filename is present
292         dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
293
294         //// latex section
295         dialog_->latexoptions->setText(toqstr(igp.special));
296 }
297
298
299 void QGraphics::apply()
300 {
301         InsetGraphicsParams & igp = controller().params();
302
303         igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
304                          kernel().bufferFilepath());
305
306         // the bb section
307         igp.bb.erase();
308         if (controller().bbChanged) {
309                 string bb;
310                 string lbX(fromqstr(dialog_->lbX->text()));
311                 string lbY(fromqstr(dialog_->lbY->text()));
312                 string rtX(fromqstr(dialog_->rtX->text()));
313                 string rtY(fromqstr(dialog_->rtY->text()));
314                 int bb_sum =
315                         convert<int>(lbX) + convert<int>(lbY) +
316                         convert<int>(rtX) + convert<int>(rtX);
317                 if (bb_sum) {
318                         if (lbX.empty())
319                                 bb = "0 ";
320                         else
321                                 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
322                         if (lbY.empty())
323                                 bb += "0 ";
324                         else
325                                 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
326                         if (rtX.empty())
327                                 bb += "0 ";
328                         else
329                                 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
330                         if (rtY.empty())
331                                 bb += '0';
332                         else
333                                 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
334                         igp.bb = bb;
335                 }
336         }
337
338         igp.draft = dialog_->draftCB->isChecked();
339         igp.clip = dialog_->clip->isChecked();
340         igp.subcaption = dialog_->subfigure->isChecked();
341         igp.subcaptionText = fromqstr(dialog_->subcaption->text());
342
343         switch (dialog_->showCB->currentIndex()) {
344                 case 0: igp.display = graphics::DefaultDisplay; break;
345                 case 1: igp.display = graphics::MonochromeDisplay; break;
346                 case 2: igp.display = graphics::GrayscaleDisplay; break;
347                 case 3: igp.display = graphics::ColorDisplay; break;
348                 default:;
349         }
350
351         if (!dialog_->displayGB->isChecked())
352                 igp.display = graphics::NoDisplay;
353
354         //the graphics section
355         if (dialog_->scaleCB->isChecked()       && !dialog_->Scale->text().isEmpty()) {
356                 igp.scale = fromqstr(dialog_->Scale->text());
357                 igp.width = Length("0pt");
358                 igp.height = Length("0pt");
359                 igp.keepAspectRatio = false;
360         } else {
361                 igp.scale = string();
362                 igp.width = dialog_->WidthCB->isChecked() ?
363                         //Note that this works even if dialog_->Width is "auto", since in
364                         //that case we get "0pt".
365                         Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
366                         Length("0pt");
367                 igp.height = dialog_->HeightCB->isChecked() ?
368                         Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
369                         Length("0pt");
370                 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
371                         dialog_->aspectratio->isChecked() &&
372                         igp.width.value() > 0 && igp.height.value() > 0;
373         }
374
375         igp.noUnzip = dialog_->unzipCB->isChecked();
376
377         igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
378
379         igp.rotateAngle = fromqstr(dialog_->angle->text());
380
381         double rotAngle = convert<double>(igp.rotateAngle);
382         if (std::abs(rotAngle) > 360.0) {
383                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
384                 igp.rotateAngle = convert<string>(rotAngle);
385         }
386
387         // save the latex name for the origin. If it is the default
388         // then origin_ltx returns ""
389         igp.rotateOrigin =
390                 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
391
392         igp.scaleBeforeRotation = dialog_->rotateOrderCB->isChecked();
393
394         // more latex options
395         igp.special = fromqstr(dialog_->latexoptions->text());
396 }
397
398
399 void QGraphics::getBB()
400 {
401         string const filename(fromqstr(dialog_->filename->text()));
402         if (!filename.empty()) {
403                 string const bb(controller().readBB(filename));
404                 if (!bb.empty()) {
405                         dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
406                         dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
407                         dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
408                         dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
409                         // the default units for the bb values when reading
410                         // it from the file
411                         dialog_->lbXunit->setCurrentIndex(0);
412                         dialog_->lbYunit->setCurrentIndex(0);
413                         dialog_->rtXunit->setCurrentIndex(0);
414                         dialog_->rtYunit->setCurrentIndex(0);
415                 }
416                 controller().bbChanged = false;
417         }
418 }
419
420
421 bool QGraphics::isValid()
422 {
423         return !dialog_->filename->text().isEmpty();
424 }
425
426 } // namespace frontend
427 } // namespace lyx