]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QGraphics.C
Fix unreported bug related to 3246 by Richard Heck:
[lyx.git] / src / frontends / qt4 / QGraphics.C
1 /**
2  * \file QGraphics.C
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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QGraphics.h"
16
17 #include "checkedwidgets.h"
18 #include "lengthcombo.h"
19 #include "QGraphicsDialog.h"
20 #include "Qt2BC.h"
21 #include "qt_helpers.h"
22 #include "validators.h"
23
24 #include "lengthcommon.h"
25 #include "lyxrc.h"
26
27 #include "controllers/ControlGraphics.h"
28 #include "controllers/helper_funcs.h"
29
30 #include "insets/insetgraphicsParams.h"
31
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
36
37 #include <QLineEdit>
38 #include <QPushButton>
39 #include <QCheckBox>
40 #include <QLabel>
41
42 #include <cmath>
43
44 using lyx::support::float_equal;
45 using lyx::support::token;
46
47 using lyx::support::os::internal_path;
48
49 #ifndef CXX_GLOBAL_CSTD
50 using std::floor;
51 #endif
52
53 using std::vector;
54 using std::string;
55
56 namespace lyx {
57 namespace frontend {
58
59 typedef QController<ControlGraphics, QView<QGraphicsDialog> > graphics_base_class;
60
61 QGraphics::QGraphics(Dialog & parent)
62         : graphics_base_class(parent, _("Graphics"))
63 {
64 }
65
66
67 void QGraphics::build_dialog()
68 {
69         dialog_.reset(new QGraphicsDialog(this));
70
71         bcview().setOK(dialog_->okPB);
72         bcview().setApply(dialog_->applyPB);
73         bcview().setRestore(dialog_->restorePB);
74         bcview().setCancel(dialog_->closePB);
75
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);
92
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);
103 }
104
105
106 namespace {
107
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;
113 }
114
115 }
116
117
118 void QGraphics::update_contents()
119 {
120         PathValidator * path_validator = getPathValidator(dialog_->filename);
121         if (path_validator)
122                 path_validator->setChecker(kernel().docType(), lyxrc);
123
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));
136         }
137
138         InsetGraphicsParams & igp = controller().params();
139
140         // set the right default unit
141         LyXLength::UNIT unitDefault = LyXLength::CM;
142         switch (lyxrc.default_papersize) {
143                 case PAPER_USLETTER:
144                 case PAPER_USLEGAL:
145                 case PAPER_USEXECUTIVE:
146                         unitDefault = LyXLength::IN;
147                         break;
148                 default:
149                         break;
150         }
151
152         string const name =
153                 igp.filename.outputFilename(kernel().bufferFilepath());
154         dialog_->filename->setText(toqstr(name));
155
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;
169         } else {
170                 // get the values from the inset
171                 LyXLength anyLength;
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));
180                 } else {
181                         dialog_->lbX->setText(toqstr(xl));
182                 }
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));
187                 } else {
188                         dialog_->lbY->setText(toqstr(xl));
189                 }
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));
194                 } else {
195                         dialog_->rtX->setText(toqstr(xl));
196                 }
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));
201                 } else {
202                         dialog_->rtY->setText(toqstr(xl));
203                 }
204                 controller().bbChanged = true;
205         }
206
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);
211
212         // Update the subcaption check button and input field
213         dialog_->subfigure->setChecked(igp.subcaption);
214         dialog_->subcaption->setText(toqstr(igp.subcaptionText));
215
216         int item = 0;
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;
223         }
224         dialog_->showCB->setCurrentIndex(item);
225         dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
226         dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
227
228         // the output section (width/height)
229         dialog_->Scale->setText(toqstr(igp.scale));
230
231         lengthToWidgets(dialog_->Width, dialog_->widthUnit,
232                 igp.width.asString(), unitDefault);
233
234         lengthToWidgets(dialog_->Height, dialog_->heightUnit,
235                 igp.height.asString(), unitDefault);
236
237         dialog_->aspectratio->setChecked(igp.keepAspectRatio);
238
239         dialog_->scaleCB->setChecked(!igp.scale.empty() || igp.width.empty());
240
241         dialog_->Scale->setEnabled(!igp.scale.empty() || igp.width.empty());
242
243         dialog_->angle->setText(toqstr(igp.rotateAngle));
244
245         dialog_->origin->clear();
246
247         vector<RotationOriginPair> origindata = getRotationOriginData();
248         vector<docstring> const origin_lang = getFirst(origindata);
249         QGraphics::origin_ltx = getSecond(origindata);
250
251         for (vector<docstring>::const_iterator it = origin_lang.begin();
252             it != origin_lang.end(); ++it)
253                 dialog_->origin->addItem(toqstr(*it));
254
255         if (!igp.rotateOrigin.empty())
256                 dialog_->origin->setCurrentIndex(
257                         getItemNo(origin_ltx, igp.rotateOrigin));
258         else
259                 dialog_->origin->setCurrentIndex(0);
260
261         // disable edit button when no filename is present
262         dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
263
264         //// latex section
265         dialog_->latexoptions->setText(toqstr(igp.special));
266 }
267
268
269 void QGraphics::apply()
270 {
271         InsetGraphicsParams & igp = controller().params();
272
273         igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
274                          kernel().bufferFilepath());
275
276         // the bb section
277         igp.bb.erase();
278         if (controller().bbChanged) {
279                 string bb;
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()));
284                 int bb_sum =
285                         convert<int>(lbX) + convert<int>(lbY) +
286                         convert<int>(rtX) + convert<int>(rtX);
287                 if (bb_sum) {
288                         if (lbX.empty())
289                                 bb = "0 ";
290                         else
291                                 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
292                         if (lbY.empty())
293                                 bb += "0 ";
294                         else
295                                 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
296                         if (rtX.empty())
297                                 bb += "0 ";
298                         else
299                                 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
300                         if (rtY.empty())
301                                 bb += '0';
302                         else
303                                 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
304                         igp.bb = bb;
305                 }
306         }
307
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());
312
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;
318                 default:;
319         }
320
321         if (!dialog_->displayGB->isChecked())
322                 igp.display = graphics::NoDisplay;
323
324         if (dialog_->scaleCB->isChecked()
325                 && !dialog_->Scale->text().isEmpty()) {
326                 igp.scale = fromqstr(dialog_->Scale->text());
327         } else {
328                 igp.scale = string();
329         }
330
331         igp.width = LyXLength(widgetsToLength(dialog_->Width, dialog_->widthUnit));
332
333         igp.height = LyXLength(widgetsToLength(dialog_->Height, dialog_->heightUnit));
334
335         igp.keepAspectRatio = dialog_->aspectratio->isChecked();
336
337         igp.noUnzip = dialog_->unzipCB->isChecked();
338
339         igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
340
341         igp.rotateAngle = fromqstr(dialog_->angle->text());
342
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);
347         }
348
349         // save the latex name for the origin. If it is the default
350         // then origin_ltx returns ""
351         igp.rotateOrigin =
352                 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
353
354         // more latex options
355         igp.special = fromqstr(dialog_->latexoptions->text());
356 }
357
358
359 void QGraphics::getBB()
360 {
361         string const filename(fromqstr(dialog_->filename->text()));
362         if (!filename.empty()) {
363                 string const bb(controller().readBB(filename));
364                 if (!bb.empty()) {
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
370                         // it from the file
371                         dialog_->lbXunit->setCurrentIndex(0);
372                         dialog_->lbYunit->setCurrentIndex(0);
373                         dialog_->rtXunit->setCurrentIndex(0);
374                         dialog_->rtYunit->setCurrentIndex(0);
375                 }
376                 controller().bbChanged = false;
377         }
378 }
379
380
381 bool QGraphics::isValid()
382 {
383         return !dialog_->filename->text().isEmpty();
384 }
385
386 } // namespace frontend
387 } // namespace lyx