]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QGraphics.C
ws change
[lyx.git] / src / frontends / qt2 / QGraphics.C
1 /**
2  * \file QGraphics.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  * \author Edwin Leuven <leuven@fee.uva.nl>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include <qlineedit.h>
17 #include <qpushbutton.h>
18 #include <qcheckbox.h>
19 #include <qradiobutton.h>
20 #include <qcombobox.h>
21 #include <qgroupbox.h>
22 #include <qbuttongroup.h>
23 #include <qlabel.h>
24
25 #include "lengthcombo.h"
26
27 #include "QGraphicsDialog.h"
28 #include "QGraphics.h"
29 #include "Qt2BC.h"
30 #include "gettext.h"
31 #include "debug.h"
32
33 #include "QtLyXView.h"
34 #include "ControlGraphics.h"
35
36 #include "support/lstrings.h"
37 #include "support/FileInfo.h"
38 #include "support/filetools.h"  // for MakeAbsPath etc
39 #include "insets/insetgraphicsParams.h"
40 #include "lyxrc.h" // for lyxrc.display_graphics
41
42
43 typedef Qt2CB<ControlGraphics, Qt2DB<QGraphicsDialog> > base_class;
44
45 QGraphics::QGraphics(ControlGraphics & c)
46         : base_class(c, _("Graphics"))
47 {
48 }
49
50
51 void QGraphics::build_dialog()
52 {
53         dialog_.reset(new QGraphicsDialog(this));
54
55         bc().setOK(dialog_->okPB);
56         bc().setApply(dialog_->applyPB);
57         bc().setCancel(dialog_->closePB);
58         bc().setRestore(dialog_->restorePB);
59
60         bc().addReadOnly(dialog_->rotateGB);
61         bc().addReadOnly(dialog_->latexoptionsGB);
62         bc().addReadOnly(dialog_->bbGB);
63         bc().addReadOnly(dialog_->sizeBG);
64         bc().addReadOnly(dialog_->displayGB);
65         bc().addReadOnly(dialog_->subfigure);
66         bc().addReadOnly(dialog_->subcaption);
67         bc().addReadOnly(dialog_->filenameL);
68         bc().addReadOnly(dialog_->filename);
69         bc().addReadOnly(dialog_->browsePB);
70 }
71
72 void QGraphics::update_contents()
73 {
74         InsetGraphicsParams & igp = controller().params();
75
76         string unit = "cm";
77         if (lyxrc.default_papersize < 3)
78             unit = "in";
79         string const defaultUnit = string(unit);
80
81         // Update dialog with details from inset
82         dialog_->filename->setText(igp.filename.c_str());
83
84         // set the bounding box values, if exists. First we need the whole
85         // path, because the controller knows nothing about the doc-dir
86         controller().bbChanged = false;
87         if (igp.bb.empty()) {
88                 string const fileWithAbsPath = MakeAbsPath(igp.filename, OnlyPath(igp.filename));
89                 string bb = controller().readBB(fileWithAbsPath);
90                 if (!bb.empty()) {
91                         // get the values from the file
92                         // in this case we always have the point-unit
93                         dialog_->lbX->setText(token(bb,' ',0).c_str());
94                         dialog_->lbY->setText(token(bb,' ',1).c_str());
95                         dialog_->rtX->setText(token(bb,' ',2).c_str());
96                         dialog_->rtY->setText(token(bb,' ',3).c_str());
97                 }
98         } else {
99                 // get the values from the inset
100                 controller().bbChanged = true;
101                 dialog_->lbX->setText(token(igp.bb,' ',0).c_str());
102                 dialog_->lbY->setText(token(igp.bb,' ',1).c_str());
103                 dialog_->rtX->setText(token(igp.bb,' ',2).c_str());
104                 dialog_->rtY->setText(token(igp.bb,' ',3).c_str());
105         }
106
107         // Update the draft and clip mode
108         dialog_->draft->setChecked(igp.draft);
109         dialog_->clip->setChecked(igp.clip);
110
111         // Update the subcaption check button and input field
112         dialog_->subfigure->setChecked(igp.subcaption);
113         dialog_->subcaption->setText(igp.subcaptionText.c_str());
114
115         switch (igp.display) {
116             case InsetGraphicsParams::MONOCHROME: {
117                     dialog_->show->setCurrentItem(0);
118                     break;
119             }
120             case InsetGraphicsParams::GRAYSCALE: {
121                     dialog_->show->setCurrentItem(1);
122                     break;
123             }
124             case InsetGraphicsParams::COLOR: {
125                     dialog_->show->setCurrentItem(2);
126                     break;
127             }
128             case InsetGraphicsParams::NONE: {
129                     dialog_->show->setCurrentItem(3);
130                     break;
131             }
132         }
133
134         dialog_->widthUnit->setCurrentItem(igp.width.unit());
135         dialog_->heightUnit->setCurrentItem(igp.height.unit());
136
137         switch (igp.size_type) {
138             case InsetGraphicsParams::DEFAULT_SIZE: {
139                     dialog_->defaultRB->setChecked(TRUE);
140                     break;
141             }
142             case InsetGraphicsParams::WH: {
143                     dialog_->customRB->setChecked(TRUE);
144                     break;
145             }
146             case InsetGraphicsParams::SCALE: {
147                     dialog_->scaleRB->setChecked(TRUE);
148                     dialog_->scale->setText(tostr(igp.scale).c_str());
149                     break;
150             }
151         }
152
153         // aspect ratio
154         dialog_->aspectratio->setChecked(igp.keepAspectRatio);
155
156         // now the lyx-internally viewsize
157         dialog_->displaywidthUnit->setCurrentItem(igp.lyxwidth.unit());
158         dialog_->displayheightUnit->setCurrentItem(igp.lyxheight.unit());
159
160         // Update the rotate angle
161         dialog_->angle->setText(tostr(igp.rotateAngle).c_str());
162
163         if (igp.rotateOrigin.empty()) {
164                 dialog_->origin->setCurrentItem(0);
165         } else {
166                 //fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
167         }
168
169
170         // latex options
171         dialog_->latexoptions->setText(igp.special.c_str());
172 }
173
174
175 void QGraphics::apply()
176 {
177         // Create the parameters structure and fill the data from the dialog.
178         InsetGraphicsParams & igp = controller().params();
179
180         igp.filename = dialog_->filename->text();
181
182         if (!controller().bbChanged)    // different to the original one?
183                 igp.bb = string();      // don't write anything
184         else {
185                 string bb;
186                 string lbX(dialog_->lbX->text());
187                 string lbY(dialog_->lbY->text());
188                 string rtX(dialog_->rtX->text());
189                 string rtY(dialog_->rtY->text());
190
191                 if (lbX.empty())
192                         bb = "0 ";
193                 else
194                         bb = lbX + " ";
195                 if (lbY.empty())
196                         bb += "0 ";
197                 else
198                         bb += (lbY + " ");
199                 if (rtX.empty())
200                         bb += "0 ";
201                 else
202                         bb += (rtX + " ");
203                 if (rtY.empty())
204                         bb += "0 ";
205                 else
206                         bb += (rtY + " ");
207
208                 igp.bb = bb;
209         }
210
211         igp.draft = dialog_->draft->isChecked();
212
213         igp.clip = dialog_->clip->isChecked();
214
215         igp.subcaption = dialog_->subfigure->isChecked();
216
217         igp.subcaptionText = dialog_->subcaption->text();
218
219         switch(dialog_->show->currentItem()) {
220                 case 0: igp.display = InsetGraphicsParams::MONOCHROME; break;
221                 case 1: igp.display = InsetGraphicsParams::GRAYSCALE; break;
222                 case 2: igp.display = InsetGraphicsParams::COLOR; break;
223                 case 3: igp.display = InsetGraphicsParams::NONE; break;
224                 default:;
225         }
226
227         if (dialog_->defaultRB->isChecked())
228             igp.size_type = InsetGraphicsParams::DEFAULT_SIZE;
229         else if (dialog_->customRB->isChecked())
230             igp.size_type = InsetGraphicsParams::WH;
231         else
232             igp.size_type = InsetGraphicsParams::SCALE;
233
234         string value(dialog_->width->text());
235         igp.width = LyXLength(strToDbl(value), dialog_->widthUnit->currentLengthItem());
236
237         value = string(dialog_->height->text());
238         igp.height = LyXLength(strToDbl(value), dialog_->heightUnit->currentLengthItem());
239
240         value = string(dialog_->displaywidth->text());
241         igp.lyxwidth = LyXLength(strToDbl(value), dialog_->displaywidthUnit->currentLengthItem());
242
243         value = string(dialog_->displayheight->text());
244         igp.lyxheight = LyXLength(strToDbl(value), dialog_->displayheightUnit->currentLengthItem());
245
246         igp.scale = strToInt(string(dialog_->scale->text()));
247
248         igp.keepAspectRatio = dialog_->aspectratio->isChecked();
249
250         igp.rotateAngle = strToDbl(string(dialog_->angle->text()));
251
252         while (igp.rotateAngle < 0.0 || igp.rotateAngle > 360.0) {
253                 if (igp.rotateAngle < 0.0) {
254                         igp.rotateAngle += 360.0;
255                 } else if (igp.rotateAngle > 360.0) {
256                         igp.rotateAngle -= 360.0;
257                 }
258         }
259
260         if ((dialog_->origin->currentItem()) > 0)
261                 igp.rotateOrigin = dialog_->origin->currentText();
262         else
263             igp.rotateOrigin = string();
264
265         igp.special = dialog_->latexoptions->text();
266
267 }
268
269
270 void QGraphics::browse()
271 {
272         string const & name = controller().Browse(dialog_->filename->text().latin1());
273         if (!name.empty())
274                 dialog_->filename->setText(name.c_str());
275 }
276
277 void QGraphics::get()
278 {
279     string const filename(dialog_->filename->text());
280     if (!filename.empty()) {
281         string const fileWithAbsPath = MakeAbsPath(filename, OnlyPath(filename));
282         string bb = controller().readBB(fileWithAbsPath);
283         if (!bb.empty()) {
284                 dialog_->lbX->setText(token(bb,' ',0).c_str());
285                 dialog_->lbY->setText(token(bb,' ',1).c_str());
286                 dialog_->rtX->setText(token(bb,' ',2).c_str());
287                 dialog_->rtY->setText(token(bb,' ',3).c_str());
288         }
289         controller().bbChanged = false;
290     }
291 }
292
293
294 bool QGraphics::isValid()
295 {
296         return !string(dialog_->filename->text().latin1()).empty();
297 }