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