3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
7 * \author Edwin Leuven <leuven@fee.uva.nl>
13 #pragma implementation
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>
25 #include "lengthcombo.h"
27 #include "QGraphicsDialog.h"
28 #include "QGraphics.h"
33 #include "QtLyXView.h"
34 #include "ControlGraphics.h"
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
43 typedef Qt2CB<ControlGraphics, Qt2DB<QGraphicsDialog> > base_class;
45 QGraphics::QGraphics(ControlGraphics & c)
46 : base_class(c, _("Graphics"))
51 void QGraphics::build_dialog()
53 dialog_.reset(new QGraphicsDialog(this));
55 bc().setOK(dialog_->okPB);
56 bc().setApply(dialog_->applyPB);
57 bc().setCancel(dialog_->closePB);
58 bc().setRestore(dialog_->restorePB);
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);
72 void QGraphics::update_contents()
74 InsetGraphicsParams & igp = controller().params();
77 if (lyxrc.default_papersize < 3)
79 string const defaultUnit = string(unit);
81 // Update dialog with details from inset
82 dialog_->filename->setText(igp.filename.c_str());
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;
88 string const fileWithAbsPath = MakeAbsPath(igp.filename, OnlyPath(igp.filename));
89 string bb = controller().readBB(fileWithAbsPath);
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());
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());
107 // Update the draft and clip mode
108 dialog_->draft->setChecked(igp.draft);
109 dialog_->clip->setChecked(igp.clip);
111 // Update the subcaption check button and input field
112 dialog_->subfigure->setChecked(igp.subcaption);
113 dialog_->subcaption->setText(igp.subcaptionText.c_str());
115 switch (igp.display) {
116 case InsetGraphicsParams::MONOCHROME: {
117 dialog_->show->setCurrentItem(0);
120 case InsetGraphicsParams::GRAYSCALE: {
121 dialog_->show->setCurrentItem(1);
124 case InsetGraphicsParams::COLOR: {
125 dialog_->show->setCurrentItem(2);
128 case InsetGraphicsParams::NONE: {
129 dialog_->show->setCurrentItem(3);
134 dialog_->widthUnit->setCurrentItem(igp.width.unit());
135 dialog_->heightUnit->setCurrentItem(igp.height.unit());
137 switch (igp.size_type) {
138 case InsetGraphicsParams::DEFAULT_SIZE: {
139 dialog_->defaultRB->setChecked(TRUE);
142 case InsetGraphicsParams::WH: {
143 dialog_->customRB->setChecked(TRUE);
146 case InsetGraphicsParams::SCALE: {
147 dialog_->scaleRB->setChecked(TRUE);
148 dialog_->scale->setText(tostr(igp.scale).c_str());
154 dialog_->aspectratio->setChecked(igp.keepAspectRatio);
156 // now the lyx-internally viewsize
157 dialog_->displaywidthUnit->setCurrentItem(igp.lyxwidth.unit());
158 dialog_->displayheightUnit->setCurrentItem(igp.lyxheight.unit());
160 // Update the rotate angle
161 dialog_->angle->setText(tostr(igp.rotateAngle).c_str());
163 if (igp.rotateOrigin.empty()) {
164 dialog_->origin->setCurrentItem(0);
166 //fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
171 dialog_->latexoptions->setText(igp.special.c_str());
175 void QGraphics::apply()
177 // Create the parameters structure and fill the data from the dialog.
178 InsetGraphicsParams & igp = controller().params();
180 igp.filename = dialog_->filename->text();
182 if (!controller().bbChanged) // different to the original one?
183 igp.bb = string(); // don't write anything
186 string lbX(dialog_->lbX->text());
187 string lbY(dialog_->lbY->text());
188 string rtX(dialog_->rtX->text());
189 string rtY(dialog_->rtY->text());
211 igp.draft = dialog_->draft->isChecked();
213 igp.clip = dialog_->clip->isChecked();
215 igp.subcaption = dialog_->subfigure->isChecked();
217 igp.subcaptionText = dialog_->subcaption->text();
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;
227 if (dialog_->defaultRB->isChecked())
228 igp.size_type = InsetGraphicsParams::DEFAULT_SIZE;
229 else if (dialog_->customRB->isChecked())
230 igp.size_type = InsetGraphicsParams::WH;
232 igp.size_type = InsetGraphicsParams::SCALE;
234 string value(dialog_->width->text());
235 igp.width = LyXLength(strToDbl(value), dialog_->widthUnit->currentLengthItem());
237 value = string(dialog_->height->text());
238 igp.height = LyXLength(strToDbl(value), dialog_->heightUnit->currentLengthItem());
240 value = string(dialog_->displaywidth->text());
241 igp.lyxwidth = LyXLength(strToDbl(value), dialog_->displaywidthUnit->currentLengthItem());
243 value = string(dialog_->displayheight->text());
244 igp.lyxheight = LyXLength(strToDbl(value), dialog_->displayheightUnit->currentLengthItem());
246 igp.scale = strToInt(string(dialog_->scale->text()));
248 igp.keepAspectRatio = dialog_->aspectratio->isChecked();
250 igp.rotateAngle = strToDbl(string(dialog_->angle->text()));
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;
260 if ((dialog_->origin->currentItem()) > 0)
261 igp.rotateOrigin = dialog_->origin->currentText();
263 igp.rotateOrigin = string();
265 igp.special = dialog_->latexoptions->text();
270 void QGraphics::browse()
272 string const & name = controller().Browse(dialog_->filename->text().latin1());
274 dialog_->filename->setText(name.c_str());
277 void QGraphics::get()
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);
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());
289 controller().bbChanged = false;
294 bool QGraphics::isValid()
296 return !string(dialog_->filename->text().latin1()).empty();