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