]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QGraphics.C
Remove unused symbol encoding
[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>
37 #include <QPushButton>
38 #include <QCheckBox>
39 #include <QLabel>
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, _("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_->latexoptions);
74         bcview().addReadOnly(dialog_->subfigure);
75         bcview().addReadOnly(dialog_->filenameL);
76         bcview().addReadOnly(dialog_->filename);
77         bcview().addReadOnly(dialog_->browsePB);
78         bcview().addReadOnly(dialog_->unzipCB);
79         bcview().addReadOnly(dialog_->filename);
80         bcview().addReadOnly(dialog_->bbFrame);
81         bcview().addReadOnly(dialog_->draftCB);
82         bcview().addReadOnly(dialog_->clip);
83         bcview().addReadOnly(dialog_->unzipCB);
84         bcview().addReadOnly(dialog_->displayGB);
85         bcview().addReadOnly(dialog_->sizeGB);
86         bcview().addReadOnly(dialog_->rotationGB);
87         bcview().addReadOnly(dialog_->latexoptions);
88         bcview().addReadOnly(dialog_->getPB);
89
90         // initialize the length validator
91         addCheckedLineEdit(bcview(), dialog_->Width, dialog_->widthL);
92         addCheckedLineEdit(bcview(), dialog_->Height, dialog_->heightL);
93         addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
94         addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
95         addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
96         addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
97         addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
98         addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
99         addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
100 }
101
102
103 namespace {
104
105 // returns the number of the string s in the vector v
106 int getItemNo(vector<string> v, string const & s) {
107         vector<string>::const_iterator cit =
108                     find(v.begin(), v.end(), s);
109         return (cit != v.end()) ? int(cit - v.begin()) : 0;
110 }
111
112 }
113
114
115 void QGraphics::update_contents()
116 {
117         PathValidator * path_validator = getPathValidator(dialog_->filename);
118         if (path_validator)
119                 path_validator->setChecker(kernel().docType(), lyxrc);
120
121         // clear and fill in the comboboxes
122         vector<string> const bb_units = frontend::getBBUnits();
123         dialog_->lbXunit->clear();
124         dialog_->lbYunit->clear();
125         dialog_->rtXunit->clear();
126         dialog_->rtYunit->clear();
127         for (vector<string>::const_iterator it = bb_units.begin();
128             it != bb_units.end(); ++it) {
129                 dialog_->lbXunit->addItem(toqstr(*it));
130                 dialog_->lbYunit->addItem(toqstr(*it));
131                 dialog_->rtXunit->addItem(toqstr(*it));
132                 dialog_->rtYunit->addItem(toqstr(*it));
133         }
134
135         InsetGraphicsParams & igp = controller().params();
136
137         // set the right default unit
138         LyXLength::UNIT unitDefault = LyXLength::CM;
139         switch (lyxrc.default_papersize) {
140                 case PAPER_USLETTER:
141                 case PAPER_USLEGAL:
142                 case PAPER_USEXECUTIVE:
143                         unitDefault = LyXLength::IN;
144                         break;
145                 default:
146                         break;
147         }
148
149         string const name =
150                 igp.filename.outputFilename(kernel().bufferFilepath());
151         dialog_->filename->setText(toqstr(name));
152
153         // set the bounding box values
154         if (igp.bb.empty()) {
155                 string const bb = controller().readBB(igp.filename.absFilename());
156                 // the values from the file always have the bigpoint-unit bp
157                 dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
158                 dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
159                 dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
160                 dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
161                 dialog_->lbXunit->setCurrentIndex(0);
162                 dialog_->lbYunit->setCurrentIndex(0);
163                 dialog_->rtXunit->setCurrentIndex(0);
164                 dialog_->rtYunit->setCurrentIndex(0);
165                 controller().bbChanged = false;
166         } else {
167                 // get the values from the inset
168                 LyXLength anyLength;
169                 string const xl(token(igp.bb, ' ', 0));
170                 string const yl(token(igp.bb, ' ', 1));
171                 string const xr(token(igp.bb, ' ', 2));
172                 string const yr(token(igp.bb, ' ', 3));
173                 if (isValidLength(xl, &anyLength)) {
174                         dialog_->lbX->setText(toqstr(convert<string>(anyLength.value())));
175                         string const unit(unit_name[anyLength.unit()]);
176                         dialog_->lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
177                 } else {
178                         dialog_->lbX->setText(toqstr(xl));
179                 }
180                 if (isValidLength(yl, &anyLength)) {
181                         dialog_->lbY->setText(toqstr(convert<string>(anyLength.value())));
182                         string const unit(unit_name[anyLength.unit()]);
183                         dialog_->lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
184                 } else {
185                         dialog_->lbY->setText(toqstr(xl));
186                 }
187                 if (isValidLength(xr, &anyLength)) {
188                         dialog_->rtX->setText(toqstr(convert<string>(anyLength.value())));
189                         string const unit(unit_name[anyLength.unit()]);
190                         dialog_->rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
191                 } else {
192                         dialog_->rtX->setText(toqstr(xl));
193                 }
194                 if (isValidLength(yr, &anyLength)) {
195                         dialog_->rtY->setText(toqstr(convert<string>(anyLength.value())));
196                         string const unit(unit_name[anyLength.unit()]);
197                         dialog_->rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
198                 } else {
199                         dialog_->rtY->setText(toqstr(xl));
200                 }
201                 controller().bbChanged = true;
202         }
203
204         // Update the draft and clip mode
205         dialog_->draftCB->setChecked(igp.draft);
206         dialog_->clip->setChecked(igp.clip);
207         dialog_->unzipCB->setChecked(igp.noUnzip);
208
209         // Update the subcaption check button and input field
210         dialog_->subfigure->setChecked(igp.subcaption);
211         dialog_->subcaption->setText(toqstr(igp.subcaptionText));
212
213         int item = 0;
214         switch (igp.display) {
215                 case graphics::DefaultDisplay: item = 0; break;
216                 case graphics::MonochromeDisplay: item = 1; break;
217                 case graphics::GrayscaleDisplay: item = 2; break;
218                 case graphics::ColorDisplay: item = 3; break;
219                 case graphics::NoDisplay: item = 0; break;
220         }
221         dialog_->showCB->setCurrentIndex(item);
222         dialog_->displayscale->setText(toqstr(convert<string>(igp.lyxscale)));
223         dialog_->displayGB->setChecked(igp.display != graphics::NoDisplay);
224
225         // the output section (width/height)
226         dialog_->Scale->setText(toqstr(igp.scale));
227
228         lengthToWidgets(dialog_->Width, dialog_->widthUnit,
229                 igp.width.asString(), unitDefault);
230
231         lengthToWidgets(dialog_->Height, dialog_->heightUnit,
232                 igp.height.asString(), unitDefault);
233
234         dialog_->aspectratio->setChecked(igp.keepAspectRatio);
235
236         dialog_->scaleCB->setChecked(!igp.scale.empty() || igp.width.empty());
237
238         dialog_->Scale->setEnabled(!igp.scale.empty() || igp.width.empty());
239
240         dialog_->angle->setText(toqstr(igp.rotateAngle));
241
242         dialog_->origin->clear();
243
244         using namespace frontend;
245         vector<RotationOriginPair> origindata = getRotationOriginData();
246         vector<string> const origin_lang = getFirst(origindata);
247         QGraphics::origin_ltx = getSecond(origindata);
248
249         for (vector<string>::const_iterator it = origin_lang.begin();
250             it != origin_lang.end(); ++it)
251                 dialog_->origin->addItem(toqstr(*it));
252
253         if (!igp.rotateOrigin.empty())
254                 dialog_->origin->setCurrentIndex(
255                         getItemNo(origin_ltx, igp.rotateOrigin));
256         else
257                 dialog_->origin->setCurrentIndex(0);
258
259         // disable edit button when no filename is present
260         dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
261
262         //// latex section
263         dialog_->latexoptions->setText(toqstr(igp.special));
264 }
265
266
267 void QGraphics::apply()
268 {
269         InsetGraphicsParams & igp = controller().params();
270
271         igp.filename.set(fromqstr(dialog_->filename->text()),
272                          kernel().bufferFilepath());
273
274         // the bb section
275         igp.bb.erase();
276         if (controller().bbChanged) {
277                 string bb;
278                 string lbX(fromqstr(dialog_->lbX->text()));
279                 string lbY(fromqstr(dialog_->lbY->text()));
280                 string rtX(fromqstr(dialog_->rtX->text()));
281                 string rtY(fromqstr(dialog_->rtY->text()));
282                 int bb_sum =
283                         convert<int>(lbX) + convert<int>(lbY) +
284                         convert<int>(rtX) + convert<int>(rtX);
285                 if (bb_sum) {
286                         if (lbX.empty())
287                                 bb = "0 ";
288                         else
289                                 bb = lbX + fromqstr(dialog_->lbXunit->currentText()) + ' ';
290                         if (lbY.empty())
291                                 bb += "0 ";
292                         else
293                                 bb += (lbY + fromqstr(dialog_->lbYunit->currentText()) + ' ');
294                         if (rtX.empty())
295                                 bb += "0 ";
296                         else
297                                 bb += (rtX + fromqstr(dialog_->rtXunit->currentText()) + ' ');
298                         if (rtY.empty())
299                                 bb += '0';
300                         else
301                                 bb += (rtY + fromqstr(dialog_->rtYunit->currentText()));
302                         igp.bb = bb;
303                 }
304         }
305
306         igp.draft = dialog_->draftCB->isChecked();
307         igp.clip = dialog_->clip->isChecked();
308         igp.subcaption = dialog_->subfigure->isChecked();
309         igp.subcaptionText = fromqstr(dialog_->subcaption->text());
310
311         switch (dialog_->showCB->currentIndex()) {
312                 case 0: igp.display = graphics::DefaultDisplay; break;
313                 case 1: igp.display = graphics::MonochromeDisplay; break;
314                 case 2: igp.display = graphics::GrayscaleDisplay; break;
315                 case 3: igp.display = graphics::ColorDisplay; break;
316                 default:;
317         }
318
319         if (!dialog_->displayGB->isChecked())
320                 igp.display = graphics::NoDisplay;
321
322         if (dialog_->scaleCB->isChecked()
323                 && !dialog_->Scale->text().isEmpty()) {
324                 igp.scale = fromqstr(dialog_->Scale->text());
325         } else {
326                 igp.scale = string();
327         }
328
329         igp.width = LyXLength(widgetsToLength(dialog_->Width, dialog_->widthUnit));
330
331         igp.height = LyXLength(widgetsToLength(dialog_->Height, dialog_->heightUnit));
332
333         igp.keepAspectRatio = dialog_->aspectratio->isChecked();
334
335         igp.noUnzip = dialog_->unzipCB->isChecked();
336
337         igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
338
339         igp.rotateAngle = fromqstr(dialog_->angle->text());
340
341         double rotAngle = convert<double>(igp.rotateAngle);
342         if (std::abs(rotAngle) > 360.0) {
343                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
344                 igp.rotateAngle = convert<string>(rotAngle);
345         }
346
347         // save the latex name for the origin. If it is the default
348         // then origin_ltx returns ""
349         igp.rotateOrigin =
350                 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
351
352         // more latex options
353         igp.special = fromqstr(dialog_->latexoptions->text());
354 }
355
356
357 void QGraphics::getBB()
358 {
359         string const filename(fromqstr(dialog_->filename->text()));
360         if (!filename.empty()) {
361                 string const bb(controller().readBB(filename));
362                 if (!bb.empty()) {
363                         dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
364                         dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
365                         dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
366                         dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
367                         // the default units for the bb values when reading
368                         // it from the file
369                         dialog_->lbXunit->setCurrentIndex(0);
370                         dialog_->lbYunit->setCurrentIndex(0);
371                         dialog_->rtXunit->setCurrentIndex(0);
372                         dialog_->rtYunit->setCurrentIndex(0);
373                 }
374                 controller().bbChanged = false;
375         }
376 }
377
378
379 bool QGraphics::isValid()
380 {
381         return !dialog_->filename->text().isEmpty();
382 }
383
384 } // namespace frontend
385 } // namespace lyx