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