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