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