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