]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QGraphics.C
Some Qt graphics dialog improvements. Yet more work needed :/
[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()
45         : base_class(_("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().setRestore(dialog_->restorePB);
57         bc().setCancel(dialog_->closePB);
58
59         bc().addReadOnly(dialog_->rotateGB);
60         bc().addReadOnly(dialog_->latexoptions);
61         bc().addReadOnly(dialog_->subfigure);
62         bc().addReadOnly(dialog_->subcaption);
63         bc().addReadOnly(dialog_->filenameL);
64         bc().addReadOnly(dialog_->filename);
65         bc().addReadOnly(dialog_->browsePB);
66         bc().addReadOnly(dialog_->unzipCB);
67         bc().addReadOnly(dialog_->filename);
68         bc().addReadOnly(dialog_->lbX);
69         bc().addReadOnly(dialog_->lbY);
70         bc().addReadOnly(dialog_->rtX);
71         bc().addReadOnly(dialog_->rtY);
72         bc().addReadOnly(dialog_->lbXunit);
73         bc().addReadOnly(dialog_->lbYunit);
74         bc().addReadOnly(dialog_->rtXunit);
75         bc().addReadOnly(dialog_->rtYunit);
76         bc().addReadOnly(dialog_->draftCB);
77         bc().addReadOnly(dialog_->clip);
78         bc().addReadOnly(dialog_->unzipCB);
79         bc().addReadOnly(dialog_->subfigure);
80         bc().addReadOnly(dialog_->subcaption);
81         bc().addReadOnly(dialog_->showCB);
82         bc().addReadOnly(dialog_->width);
83         bc().addReadOnly(dialog_->height);
84         bc().addReadOnly(dialog_->displayCB);
85         bc().addReadOnly(dialog_->displayscale);
86         bc().addReadOnly(dialog_->widthUnit);
87         bc().addReadOnly(dialog_->heightUnit);
88         bc().addReadOnly(dialog_->aspectratio);
89         bc().addReadOnly(dialog_->angle);
90         bc().addReadOnly(dialog_->origin);
91         bc().addReadOnly(dialog_->latexoptions);
92         bc().addReadOnly(dialog_->getPB);
93 }
94
95
96 void QGraphics::update_contents()
97 {
98         InsetGraphicsParams & igp = controller().params();
99
100         // set the right default unit
101         string unit = "cm";
102         switch (lyxrc.default_papersize) {
103                 case BufferParams::PAPER_DEFAULT: break;
104         
105                 case BufferParams::PAPER_USLETTER:
106                 case BufferParams::PAPER_LEGALPAPER:
107                 case BufferParams::PAPER_EXECUTIVEPAPER: unit = "in"; break;
108         
109                 case BufferParams::PAPER_A3PAPER:
110                 case BufferParams::PAPER_A4PAPER:
111                 case BufferParams::PAPER_A5PAPER:
112                 case BufferParams::PAPER_B5PAPER: unit = "cm"; break;
113         }
114
115         dialog_->filename->setText(igp.filename.c_str());
116
117         // set the bounding box values, if exists. First we need the whole
118         // path, because the controller knows nothing about the doc-dir
119         controller().bbChanged = false;
120         if (igp.bb.empty()) {
121                 string const fileWithAbsPath(MakeAbsPath(igp.filename, OnlyPath(igp.filename)));
122                 string const bb(controller().readBB(fileWithAbsPath));
123                 if (!bb.empty()) {
124                         // get the values from the file
125                         // in this case we always have the point-unit
126                         dialog_->lbX->setText(token(bb, ' ', 0).c_str());
127                         dialog_->lbY->setText(token(bb, ' ', 1).c_str());
128                         dialog_->rtX->setText(token(bb, ' ', 2).c_str());
129                         dialog_->rtY->setText(token(bb, ' ', 3).c_str());
130                 }
131         } else {
132                 // get the values from the inset
133                 controller().bbChanged = true;
134                 dialog_->lbX->setText(token(igp.bb, ' ', 0).c_str());
135                 dialog_->lbY->setText(token(igp.bb, ' ', 1).c_str());
136                 dialog_->rtX->setText(token(igp.bb, ' ', 2).c_str());
137                 dialog_->rtY->setText(token(igp.bb, ' ', 3).c_str());
138         }
139
140         // Update the draft and clip mode
141         dialog_->draftCB->setChecked(igp.draft);
142         dialog_->clip->setChecked(igp.clip);
143         dialog_->unzipCB->setChecked(igp.noUnzip);
144
145         // Update the subcaption check button and input field
146         dialog_->subfigure->setChecked(igp.subcaption);
147         dialog_->subcaption->setText(igp.subcaptionText.c_str());
148
149         int item;
150         switch (igp.display) {
151                 case grfx::DefaultDisplay: item = 0; break;
152                 case grfx::MonochromeDisplay: item = 1; break;
153                 case grfx::GrayscaleDisplay: item = 2; break;
154                 case grfx::ColorDisplay: item = 3; break;
155                 case grfx::NoDisplay: item = 0; break;
156         }
157         dialog_->showCB->setCurrentItem(item);
158         dialog_->showCB->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
159         dialog_->displayCB->setChecked(igp.display != grfx::NoDisplay);
160         dialog_->displayscale->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
161         dialog_->displayscale->setText(tostr(igp.lyxscale).c_str());
162
163         dialog_->widthUnit->setCurrentItem(igp.width.unit());
164         dialog_->heightUnit->setCurrentItem(igp.height.unit());
165         dialog_->aspectratio->setChecked(igp.keepAspectRatio);
166
167         // Update the rotate angle
168         dialog_->angle->setText(tostr(igp.rotateAngle).c_str());
169
170         if (igp.rotateOrigin.empty()) {
171                 dialog_->origin->setCurrentItem(0);
172         } else {
173                 // FIXME fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
174         }
175
176         // latex options
177         dialog_->latexoptions->setText(igp.special.c_str());
178 }
179
180
181 void QGraphics::apply()
182 {
183         InsetGraphicsParams & igp = controller().params();
184
185         igp.filename = dialog_->filename->text();
186
187         if (!controller().bbChanged) {
188                 igp.bb = string();
189         } else {
190                 string bb;
191                 string lbX(dialog_->lbX->text());
192                 string lbY(dialog_->lbY->text());
193                 string rtX(dialog_->rtX->text());
194                 string rtY(dialog_->rtY->text());
195
196                 if (lbX.empty())
197                         bb = "0 ";
198                 else
199                         bb = lbX + " ";
200                 if (lbY.empty())
201                         bb += "0 ";
202                 else
203                         bb += (lbY + " ");
204                 if (rtX.empty())
205                         bb += "0 ";
206                 else
207                         bb += (rtX + " ");
208                 if (rtY.empty())
209                         bb += "0 ";
210                 else
211                         bb += (rtY + " ");
212
213                 igp.bb = bb;
214         }
215
216         igp.draft = dialog_->draftCB->isChecked();
217         igp.clip = dialog_->clip->isChecked();
218         igp.subcaption = dialog_->subfigure->isChecked();
219         igp.subcaptionText = dialog_->subcaption->text();
220
221         switch (dialog_->showCB->currentItem()) {
222                 case 0: igp.display = grfx::DefaultDisplay; break;
223                 case 1: igp.display = grfx::MonochromeDisplay; break;
224                 case 2: igp.display = grfx::GrayscaleDisplay; break;
225                 case 3: igp.display = grfx::ColorDisplay; break;
226                 default:;
227         }
228
229         if (!dialog_->displayCB->isChecked())
230                 igp.display = grfx::NoDisplay;
231  
232         string value(dialog_->width->text());
233         igp.width = LyXLength(strToDbl(value), dialog_->widthUnit->currentLengthItem());
234         value = string(dialog_->height->text());
235         igp.height = LyXLength(strToDbl(value), dialog_->heightUnit->currentLengthItem());
236
237         igp.keepAspectRatio = dialog_->aspectratio->isChecked();
238
239         igp.noUnzip = dialog_->unzipCB->isChecked();
240  
241         igp.lyxscale = strToInt(string(dialog_->displayscale->text()));
242
243         igp.rotateAngle = strToDbl(string(dialog_->angle->text()));
244         while (igp.rotateAngle < -360.0)
245                 igp.rotateAngle += 360.0;
246         while (igp.rotateAngle >  360.0)
247                 igp.rotateAngle -= 360.0;
248
249         if ((dialog_->origin->currentItem()) > 0)
250                 igp.rotateOrigin = dialog_->origin->currentText();
251         else
252             igp.rotateOrigin = string();
253
254         igp.special = dialog_->latexoptions->text();
255 }
256
257
258 void QGraphics::browse()
259 {
260         string const & name = controller().Browse(dialog_->filename->text().latin1());
261         if (!name.empty())
262                 dialog_->filename->setText(name.c_str());
263 }
264
265
266 void QGraphics::get()
267 {
268         string const filename(dialog_->filename->text());
269         if (!filename.empty()) {
270                 string const fileWithAbsPath(MakeAbsPath(filename, OnlyPath(filename)));
271                 string const bb(controller().readBB(fileWithAbsPath));
272                 if (!bb.empty()) {
273                         dialog_->lbX->setText(token(bb, ' ', 0).c_str());
274                         dialog_->lbY->setText(token(bb, ' ', 1).c_str());
275                         dialog_->rtX->setText(token(bb, ' ', 2).c_str());
276                         dialog_->rtY->setText(token(bb, ' ', 3).c_str());
277                 }
278                 controller().bbChanged = false;
279         }
280 }
281
282
283 bool QGraphics::isValid()
284 {
285         // FIXME: we need more here. 
286         return !string(dialog_->filename->text().latin1()).empty();
287 }