]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QGraphics.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / qt2 / 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
23 #include "lengthcommon.h"
24 #include "lyxrc.h"
25
26 #include "controllers/ControlGraphics.h"
27 #include "controllers/helper_funcs.h"
28
29 #include "insets/insetgraphicsParams.h"
30
31 #include "support/convert.h"
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
34
35 #include <qlineedit.h>
36 #include <qpushbutton.h>
37 #include <qcheckbox.h>
38 #include <qgroupbox.h>
39 #include <qlabel.h>
40
41 #include <cmath>
42
43 using lyx::support::float_equal;
44 using lyx::support::token;
45
46 #ifndef CXX_GLOBAL_CSTD
47 using std::floor;
48 #endif
49
50 using std::vector;
51 using std::string;
52
53 namespace lyx {
54 namespace frontend {
55
56 typedef QController<ControlGraphics, QView<QGraphicsDialog> > base_class;
57
58 QGraphics::QGraphics(Dialog & parent)
59         : base_class(parent, _("LyX: Graphics"))
60 {
61 }
62
63
64 void QGraphics::build_dialog()
65 {
66         dialog_.reset(new QGraphicsDialog(this));
67
68         bcview().setOK(dialog_->okPB);
69         bcview().setApply(dialog_->applyPB);
70         bcview().setRestore(dialog_->restorePB);
71         bcview().setCancel(dialog_->closePB);
72
73         bcview().addReadOnly(dialog_->rotateGB);
74         bcview().addReadOnly(dialog_->latexoptions);
75         bcview().addReadOnly(dialog_->subfigure);
76         bcview().addReadOnly(dialog_->subcaption);
77         bcview().addReadOnly(dialog_->filenameL);
78         bcview().addReadOnly(dialog_->filename);
79         bcview().addReadOnly(dialog_->browsePB);
80         bcview().addReadOnly(dialog_->unzipCB);
81         bcview().addReadOnly(dialog_->filename);
82         bcview().addReadOnly(dialog_->lbX);
83         bcview().addReadOnly(dialog_->lbY);
84         bcview().addReadOnly(dialog_->rtX);
85         bcview().addReadOnly(dialog_->rtY);
86         bcview().addReadOnly(dialog_->lbXunit);
87         bcview().addReadOnly(dialog_->lbYunit);
88         bcview().addReadOnly(dialog_->rtXunit);
89         bcview().addReadOnly(dialog_->rtYunit);
90         bcview().addReadOnly(dialog_->draftCB);
91         bcview().addReadOnly(dialog_->clip);
92         bcview().addReadOnly(dialog_->unzipCB);
93         bcview().addReadOnly(dialog_->subfigure);
94         bcview().addReadOnly(dialog_->subcaption);
95         bcview().addReadOnly(dialog_->showCB);
96         bcview().addReadOnly(dialog_->width);
97         bcview().addReadOnly(dialog_->height);
98         bcview().addReadOnly(dialog_->displayCB);
99         bcview().addReadOnly(dialog_->displayscale);
100         bcview().addReadOnly(dialog_->widthUnit);
101         bcview().addReadOnly(dialog_->heightUnit);
102         bcview().addReadOnly(dialog_->aspectratio);
103         bcview().addReadOnly(dialog_->angle);
104         bcview().addReadOnly(dialog_->origin);
105         bcview().addReadOnly(dialog_->latexoptions);
106         bcview().addReadOnly(dialog_->getPB);
107
108         // initialize the length validator
109         addCheckedLineEdit(bcview(), dialog_->width, dialog_->sizewidthL_2);
110         addCheckedLineEdit(bcview(), dialog_->height, dialog_->sizeheightL_2);
111         addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
112         addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL_2);
113         addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
114         addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
115         addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
116         addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
117 }
118
119
120 namespace {
121
122 // returns the number of the string s in the vector v
123 int getItemNo(vector<string> v, string const & s) {
124         vector<string>::const_iterator cit =
125                     find(v.begin(), v.end(), s);
126         return (cit != v.end()) ? int(cit - v.begin()) : 0;
127 }
128
129 }
130
131
132 void QGraphics::update_contents()
133 {
134         // clear and fill in the comboboxes
135         vector<string> const bb_units = lyx::frontend::getBBUnits();
136         dialog_->lbXunit->clear();
137         dialog_->lbYunit->clear();
138         dialog_->rtXunit->clear();
139         dialog_->rtYunit->clear();
140         for (vector<string>::const_iterator it = bb_units.begin();
141             it != bb_units.end(); ++it) {
142                 dialog_->lbXunit->insertItem(toqstr(*it), -1);
143                 dialog_->lbYunit->insertItem(toqstr(*it), -1);
144                 dialog_->rtXunit->insertItem(toqstr(*it), -1);
145                 dialog_->rtYunit->insertItem(toqstr(*it), -1);
146         }
147
148         InsetGraphicsParams & igp = controller().params();
149
150         // set the right default unit
151         LyXLength::UNIT unitDefault = LyXLength::CM;
152         switch (lyxrc.default_papersize) {
153                 case PAPER_DEFAULT: break;
154
155                 case PAPER_USLETTER:
156                 case PAPER_LEGALPAPER:
157                 case PAPER_EXECUTIVEPAPER:
158                         unitDefault = LyXLength::IN;
159                         break;
160
161                 case PAPER_A3PAPER:
162                 case PAPER_A4PAPER:
163                 case PAPER_A5PAPER:
164                 case PAPER_B5PAPER:
165                         unitDefault = LyXLength::CM;
166                         break;
167         }
168
169         string const name =
170                 igp.filename.outputFilename(kernel().bufferFilepath());
171         dialog_->filename->setText(toqstr(name));
172
173         // set the bounding box values
174         if (igp.bb.empty()) {
175                 string const bb = controller().readBB(igp.filename.absFilename());
176                 // the values from the file always have the bigpoint-unit bp
177                 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
178                 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
179                 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
180                 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
181                 dialog_->lbXunit->setCurrentItem(0);
182                 dialog_->lbYunit->setCurrentItem(0);
183                 dialog_->rtXunit->setCurrentItem(0);
184                 dialog_->rtYunit->setCurrentItem(0);
185                 controller().bbChanged = false;
186         } else {
187                 // get the values from the inset
188                 LyXLength anyLength;
189                 string const xl(token(igp.bb, ' ', 0));
190                 string const yl(token(igp.bb, ' ', 1));
191                 string const xr(token(igp.bb, ' ', 2));
192                 string const yr(token(igp.bb, ' ', 3));
193                 if (isValidLength(xl, &anyLength)) {
194                         dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
195                         string const unit(unit_name[anyLength.unit()]);
196                         dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
197                 } else {
198                         dialog_->lbX->setText(toqstr(xl));
199                 }
200                 if (isValidLength(yl, &anyLength)) {
201                         dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
202                         string const unit(unit_name[anyLength.unit()]);
203                         dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
204                 } else {
205                         dialog_->lbY->setText(toqstr(xl));
206                 }
207                 if (isValidLength(xr, &anyLength)) {
208                         dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
209                         string const unit(unit_name[anyLength.unit()]);
210                         dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
211                 } else {
212                         dialog_->rtX->setText(toqstr(xl));
213                 }
214                 if (isValidLength(yr, &anyLength)) {
215                         dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
216                         string const unit(unit_name[anyLength.unit()]);
217                         dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
218                 } else {
219                         dialog_->rtY->setText(toqstr(xl));
220                 }
221                 controller().bbChanged = true;
222         }
223
224         // Update the draft and clip mode
225         dialog_->draftCB->setChecked(igp.draft);
226         dialog_->clip->setChecked(igp.clip);
227         dialog_->unzipCB->setChecked(igp.noUnzip);
228
229         // Update the subcaption check button and input field
230         dialog_->subfigure->setChecked(igp.subcaption);
231         dialog_->subcaption->setText(toqstr(igp.subcaptionText));
232
233         int item = 0;
234         switch (igp.display) {
235                 case lyx::graphics::DefaultDisplay: item = 0; break;
236                 case lyx::graphics::MonochromeDisplay: item = 1; break;
237                 case lyx::graphics::GrayscaleDisplay: item = 2; break;
238                 case lyx::graphics::ColorDisplay: item = 3; break;
239                 case lyx::graphics::NoDisplay: item = 0; break;
240         }
241         dialog_->showCB->setCurrentItem(item);
242         dialog_->showCB->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
243         dialog_->displayCB->setChecked(igp.display != lyx::graphics::NoDisplay);
244         dialog_->displayscale->setEnabled(igp.display != lyx::graphics::NoDisplay && !readOnly());
245         dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
246
247         //// the output section (width/height)
248         // set the length combo boxes
249         // only the width has the possibility for scale%. The original
250         // units are defined in lengthcommon.C
251         // 1. the width (a listttype)
252         dialog_->widthUnit->clear();
253         dialog_->widthUnit->insertItem(qt_("Scale%"));
254         for (int i = 0; i < num_units; i++)
255                 dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
256
257         if (!igp.scale.empty()
258                 && !float_equal(convert<double>(igp.scale), 0.0, 0.05)) {
259                 dialog_->width->setText(toqstr(igp.scale));
260                 dialog_->widthUnit->setCurrentItem(0);
261         } else {
262                 // no scale means default width/height
263                 dialog_->width->setText(toqstr(convert<string>(igp.width.value())));
264                 // the width cannot have a unitDefault, because
265                 // it is a "Scale%" or another user defined unit!
266                 // +1 instead of the "Scale%" option
267                 int unit_ = igp.width.unit();
268                 dialog_->widthUnit->setCurrentItem(unit_ + 1);
269         }
270         // 2. the height (a lengthgcombo type)
271         lengthToWidgets(dialog_->height, dialog_->heightUnit,
272                 igp.height.asString(), unitDefault);
273
274         // enable height input in case of non "Scale%" as width-unit
275         bool use_height = (dialog_->widthUnit->currentItem() > 0);
276         dialog_->height->setEnabled(use_height);
277         dialog_->heightUnit->setEnabled(use_height);
278
279         dialog_->aspectratio->setChecked(igp.keepAspectRatio);
280
281         dialog_->angle->setText(toqstr(igp.rotateAngle));
282
283         dialog_->origin->clear();
284
285         using namespace lyx::frontend;
286         vector<RotationOriginPair> origindata = getRotationOriginData();
287         vector<string> const origin_lang = getFirst(origindata);
288         QGraphics::origin_ltx = getSecond(origindata);
289
290         for (vector<string>::const_iterator it = origin_lang.begin();
291             it != origin_lang.end(); ++it)
292                 dialog_->origin->insertItem(toqstr(*it), -1);
293
294         if (!igp.rotateOrigin.empty())
295                 dialog_->origin->setCurrentItem(
296                         getItemNo(origin_ltx, igp.rotateOrigin));
297         else
298                 dialog_->origin->setCurrentItem(0);
299
300         //// latex section
301         dialog_->latexoptions->setText(toqstr(igp.special));
302 }
303
304
305 void QGraphics::apply()
306 {
307         InsetGraphicsParams & igp = controller().params();
308
309         igp.filename.set(fromqstr(dialog_->filename->text()),
310                          kernel().bufferFilepath());
311
312         // the bb section
313         igp.bb.erase();
314         if (controller().bbChanged) {
315                 string bb;
316                 string lbX(fromqstr(dialog_->lbX->text()));
317                 string lbY(fromqstr(dialog_->lbY->text()));
318                 string rtX(fromqstr(dialog_->rtX->text()));
319                 string rtY(fromqstr(dialog_->rtY->text()));
320                 int bb_sum =
321                         convert<int>(lbX) + convert<int>(lbY) +
322                         convert<int>(rtX) + convert<int>(rtX);
323                 if (bb_sum) {
324                         if (lbX.empty())
325                                 bb = "0 ";
326                         else
327                                 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
328                         if (lbY.empty())
329                                 bb += "0 ";
330                         else
331                                 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
332                         if (rtX.empty())
333                                 bb += "0 ";
334                         else
335                                 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
336                         if (rtY.empty())
337                                 bb += '0';
338                         else
339                                 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
340                         igp.bb = bb;
341                 }
342         }
343
344         igp.draft = dialog_->draftCB->isChecked();
345         igp.clip = dialog_->clip->isChecked();
346         igp.subcaption = dialog_->subfigure->isChecked();
347         igp.subcaptionText = fromqstr(dialog_->subcaption->text());
348
349         switch (dialog_->showCB->currentItem()) {
350                 case 0: igp.display = lyx::graphics::DefaultDisplay; break;
351                 case 1: igp.display = lyx::graphics::MonochromeDisplay; break;
352                 case 2: igp.display = lyx::graphics::GrayscaleDisplay; break;
353                 case 3: igp.display = lyx::graphics::ColorDisplay; break;
354                 default:;
355         }
356
357         if (!dialog_->displayCB->isChecked())
358                 igp.display = lyx::graphics::NoDisplay;
359
360         string value = fromqstr(dialog_->width->text());
361         if (dialog_->widthUnit->currentItem() > 0 || isValidLength(value)) {
362                 // width/height combination
363                 igp.width =
364                         widgetsToLength(dialog_->width, dialog_->widthUnit);
365                 igp.scale = string();
366         } else {
367                 // scaling instead of a width
368                 igp.scale = value;
369                 igp.width = LyXLength();
370         }
371         value = fromqstr(dialog_->height->text());
372         igp.height =
373                 LyXLength(widgetsToLength(dialog_->height, dialog_->heightUnit));
374
375         igp.keepAspectRatio = dialog_->aspectratio->isChecked();
376
377         igp.noUnzip = dialog_->unzipCB->isChecked();
378
379         igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
380
381         igp.rotateAngle = fromqstr(dialog_->angle->text());
382
383         double rotAngle = convert<double>(igp.rotateAngle);
384         if (std::abs(rotAngle) > 360.0) {
385                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
386                 igp.rotateAngle = convert<string>(rotAngle);
387         }
388
389         // save the latex name for the origin. If it is the default
390         // then origin_ltx returns ""
391         igp.rotateOrigin =
392                 QGraphics::origin_ltx[dialog_->origin->currentItem()];
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->setCurrentItem(0);
412                         dialog_->lbYunit->setCurrentItem(0);
413                         dialog_->rtXunit->setCurrentItem(0);
414                         dialog_->rtYunit->setCurrentItem(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