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