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