]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QGraphics.cpp
Transfer Text::drawSelection() from InsetText::drawSelection() to InsetText::draw...
[lyx.git] / src / frontends / qt4 / QGraphics.cpp
1 /**
2  * \file QGraphics.cpp
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  * \author Richard Heck
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "QGraphics.h"
17
18 #include "CheckedLineEdit.h"
19 #include "LengthCombo.h"
20 #include "QGraphicsDialog.h"
21 #include "Qt2BC.h"
22 #include "qt_helpers.h"
23
24 #include "lengthcommon.h"
25 #include "LyXRC.h"
26
27 #include "controllers/ControlGraphics.h"
28 #include "controllers/frontend_helpers.h"
29
30 #include "insets/InsetGraphicsParams.h"
31
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
36
37 #include <QLineEdit>
38 #include <QPushButton>
39 #include <QCheckBox>
40 #include <QLabel>
41
42 #include <cmath>
43
44 using lyx::support::float_equal;
45 using lyx::support::token;
46
47 using lyx::support::os::internal_path;
48
49 #ifndef CXX_GLOBAL_CSTD
50 using std::floor;
51 #endif
52
53 using std::vector;
54 using std::string;
55
56 namespace lyx {
57 namespace frontend {
58
59 typedef QController<ControlGraphics, QView<QGraphicsDialog> > graphics_base_class;
60
61 QGraphics::QGraphics(Dialog & parent)
62         : graphics_base_class(parent, _("Graphics"))
63 {
64 }
65
66
67 void QGraphics::build_dialog()
68 {
69         dialog_.reset(new QGraphicsDialog(this));
70
71         bcview().setOK(dialog_->okPB);
72         bcview().setApply(dialog_->applyPB);
73         bcview().setRestore(dialog_->restorePB);
74         bcview().setCancel(dialog_->closePB);
75
76         bcview().addReadOnly(dialog_->latexoptions);
77         bcview().addReadOnly(dialog_->subfigure);
78         bcview().addReadOnly(dialog_->filenameL);
79         bcview().addReadOnly(dialog_->filename);
80         bcview().addReadOnly(dialog_->browsePB);
81         bcview().addReadOnly(dialog_->unzipCB);
82         bcview().addReadOnly(dialog_->bbFrame);
83         bcview().addReadOnly(dialog_->draftCB);
84         bcview().addReadOnly(dialog_->clip);
85         bcview().addReadOnly(dialog_->unzipCB);
86         bcview().addReadOnly(dialog_->displayGB);
87         bcview().addReadOnly(dialog_->sizeGB);
88         bcview().addReadOnly(dialog_->rotationGB);
89         bcview().addReadOnly(dialog_->latexoptions);
90         bcview().addReadOnly(dialog_->getPB);
91         bcview().addReadOnly(dialog_->rotateOrderCB);
92
93         // initialize the length validator
94         addCheckedLineEdit(bcview(), dialog_->Scale, dialog_->scaleCB);
95         addCheckedLineEdit(bcview(), dialog_->Width, dialog_->WidthCB);
96         addCheckedLineEdit(bcview(), dialog_->Height, dialog_->HeightCB);
97         addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
98         addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
99         addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
100         addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
101         addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
102         addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
103         addCheckedLineEdit(bcview(), dialog_->filename, dialog_->filenameL);
104 }
105
106
107 namespace {
108
109 // returns the number of the string s in the vector v
110 int getItemNo(vector<string> v, string const & s) {
111         vector<string>::const_iterator cit =
112                     find(v.begin(), v.end(), s);
113         return (cit != v.end()) ? int(cit - v.begin()) : 0;
114 }
115
116 }
117
118
119 void QGraphics::update_contents()
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         Length::UNIT unitDefault = Length::CM;
139         switch (lyxrc.default_papersize) {
140                 case PAPER_USLETTER:
141                 case PAPER_USLEGAL:
142                 case PAPER_USEXECUTIVE:
143                         unitDefault = Length::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                 Length 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
227         dialog_->Scale->setText(toqstr(igp.scale));
228         //igp.scale defaults to 100, so we treat it as empty
229         bool const scaleChecked = !igp.scale.empty() && igp.scale != "100";
230         dialog_->scaleCB->blockSignals(true);
231         dialog_->scaleCB->setChecked(scaleChecked);
232         dialog_->scaleCB->blockSignals(false);
233         dialog_->Scale->setEnabled(scaleChecked);
234
235         lengthAutoToWidgets(dialog_->Width, dialog_->widthUnit, igp.width,
236                 unitDefault);
237         bool const widthChecked = !dialog_->Width->text().isEmpty() &&
238                 dialog_->Width->text() != "auto";
239         dialog_->WidthCB->blockSignals(true);
240         dialog_->WidthCB->setChecked(widthChecked);
241         dialog_->WidthCB->blockSignals(false);
242         dialog_->Width->setEnabled(widthChecked);
243         dialog_->widthUnit->setEnabled(widthChecked);
244
245         lengthAutoToWidgets(dialog_->Height, dialog_->heightUnit, igp.height,
246                 unitDefault);
247         bool const heightChecked = !dialog_->Height->text().isEmpty()
248                 && dialog_->Height->text() != "auto";
249         dialog_->HeightCB->blockSignals(true);
250         dialog_->HeightCB->setChecked(heightChecked);
251         dialog_->HeightCB->blockSignals(false);
252         dialog_->Height->setEnabled(heightChecked);
253         dialog_->heightUnit->setEnabled(heightChecked);
254
255         dialog_->scaleCB->setEnabled(!widthChecked && !heightChecked);
256         dialog_->WidthCB->setEnabled(!scaleChecked);
257         dialog_->HeightCB->setEnabled(!scaleChecked);
258         dialog_->aspectratio->setEnabled(widthChecked && heightChecked);
259
260         dialog_->setAutoText();
261
262         dialog_->angle->setText(toqstr(igp.rotateAngle));
263         dialog_->rotateOrderCB->setChecked(igp.scaleBeforeRotation);
264
265         dialog_->rotateOrderCB->setEnabled((widthChecked ||
266                                            heightChecked ||
267                                            scaleChecked) &&
268                                            (igp.rotateAngle != "0"));
269
270         dialog_->origin->clear();
271
272         vector<RotationOriginPair> origindata = getRotationOriginData();
273         vector<docstring> const origin_lang = getFirst(origindata);
274         QGraphics::origin_ltx = getSecond(origindata);
275
276         for (vector<docstring>::const_iterator it = origin_lang.begin();
277             it != origin_lang.end(); ++it)
278                 dialog_->origin->addItem(toqstr(*it));
279
280         if (!igp.rotateOrigin.empty())
281                 dialog_->origin->setCurrentIndex(
282                         getItemNo(origin_ltx, igp.rotateOrigin));
283         else
284                 dialog_->origin->setCurrentIndex(0);
285
286         // disable edit button when no filename is present
287         dialog_->editPB->setDisabled(dialog_->filename->text().isEmpty());
288
289         //// latex section
290         dialog_->latexoptions->setText(toqstr(igp.special));
291 }
292
293
294 void QGraphics::apply()
295 {
296         InsetGraphicsParams & igp = controller().params();
297
298         igp.filename.set(internal_path(fromqstr(dialog_->filename->text())),
299                          kernel().bufferFilepath());
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                         convert<int>(lbX) + convert<int>(lbY) +
311                         convert<int>(rtX) + convert<int>(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->currentIndex()) {
339                 case 0: igp.display = graphics::DefaultDisplay; break;
340                 case 1: igp.display = graphics::MonochromeDisplay; break;
341                 case 2: igp.display = graphics::GrayscaleDisplay; break;
342                 case 3: igp.display = graphics::ColorDisplay; break;
343                 default:;
344         }
345
346         if (!dialog_->displayGB->isChecked())
347                 igp.display = graphics::NoDisplay;
348
349         //the graphics section
350         if (dialog_->scaleCB->isChecked()       && !dialog_->Scale->text().isEmpty()) {
351                 igp.scale = fromqstr(dialog_->Scale->text());
352                 igp.width = Length("0pt");
353                 igp.height = Length("0pt");
354                 igp.keepAspectRatio = false;
355         } else {
356                 igp.scale = string();
357                 igp.width = dialog_->WidthCB->isChecked() ?
358                         //Note that this works even if dialog_->Width is "auto", since in
359                         //that case we get "0pt".
360                         Length(widgetsToLength(dialog_->Width, dialog_->widthUnit)):
361                         Length("0pt");
362                 igp.height = dialog_->HeightCB->isChecked() ?
363                         Length(widgetsToLength(dialog_->Height, dialog_->heightUnit)) :
364                         Length("0pt");
365                 igp.keepAspectRatio = dialog_->aspectratio->isEnabled() &&
366                         dialog_->aspectratio->isChecked() &&
367                         igp.width.value() > 0 && igp.height.value() > 0;
368         }
369
370         igp.noUnzip = dialog_->unzipCB->isChecked();
371
372         igp.lyxscale = convert<int>(fromqstr(dialog_->displayscale->text()));
373
374         igp.rotateAngle = fromqstr(dialog_->angle->text());
375
376         double rotAngle = convert<double>(igp.rotateAngle);
377         if (std::abs(rotAngle) > 360.0) {
378                 rotAngle -= 360.0 * floor(rotAngle / 360.0);
379                 igp.rotateAngle = convert<string>(rotAngle);
380         }
381
382         // save the latex name for the origin. If it is the default
383         // then origin_ltx returns ""
384         igp.rotateOrigin =
385                 QGraphics::origin_ltx[dialog_->origin->currentIndex()];
386
387         igp.scaleBeforeRotation = dialog_->rotateOrderCB->isChecked();
388
389         // more latex options
390         igp.special = fromqstr(dialog_->latexoptions->text());
391 }
392
393
394 void QGraphics::getBB()
395 {
396         string const filename(fromqstr(dialog_->filename->text()));
397         if (!filename.empty()) {
398                 string const bb(controller().readBB(filename));
399                 if (!bb.empty()) {
400                         dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
401                         dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
402                         dialog_->rtX->setText(toqstr(token(bb, ' ', 2)));
403                         dialog_->rtY->setText(toqstr(token(bb, ' ', 3)));
404                         // the default units for the bb values when reading
405                         // it from the file
406                         dialog_->lbXunit->setCurrentIndex(0);
407                         dialog_->lbYunit->setCurrentIndex(0);
408                         dialog_->rtXunit->setCurrentIndex(0);
409                         dialog_->rtYunit->setCurrentIndex(0);
410                 }
411                 controller().bbChanged = false;
412         }
413 }
414
415
416 bool QGraphics::isValid()
417 {
418         return !dialog_->filename->text().isEmpty();
419 }
420
421 } // namespace frontend
422 } // namespace lyx