]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt2/QGraphics.C
Some string(widget->text()) fixes. Weirdness
[lyx.git] / src / frontends / qt2 / QGraphics.C
index 030fb96d5bdd32038d66f4f61ea6abcb52434df7..ffd1b1035d9907234a8cdd64336c6f535b43125d 100644 (file)
@@ -1,10 +1,13 @@
 /**
  * \file QGraphics.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author John Levon <moz@compsoc.man.ac.uk>
- * \author Edwin Leuven <leuven@fee.uva.nl>
+ * \author John Levon
+ * \author Edwin Leuven
+ * \author Herbert Voss
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
@@ -15,7 +18,7 @@
 
 #include "QtLyXView.h"
 #include "ControlGraphics.h"
-
+#include "controllers/helper_funcs.h"
 #include "support/lstrings.h"
 #include "support/FileInfo.h"
 #include "support/filetools.h"
@@ -23,6 +26,8 @@
 #include "lyxrc.h"
 #include "lengthcombo.h"
 #include "gettext.h"
+#include "lengthcommon.h"
+#include "lyxlength.h"
 #include "debug.h"
 
 #include <qlineedit.h>
 #include "QGraphics.h"
 #include "Qt2BC.h"
 
+using std::vector;
+using std::endl;
 
 typedef Qt2CB<ControlGraphics, Qt2DB<QGraphicsDialog> > base_class;
 
-QGraphics::QGraphics(ControlGraphics & c)
-       : base_class(c, _("Graphics"))
+
+QGraphics::QGraphics()
+       : base_class(_("Graphics"))
 {
 }
 
@@ -53,240 +61,352 @@ void QGraphics::build_dialog()
 
        bc().setOK(dialog_->okPB);
        bc().setApply(dialog_->applyPB);
-       bc().setCancel(dialog_->closePB);
        bc().setRestore(dialog_->restorePB);
+       bc().setCancel(dialog_->closePB);
 
        bc().addReadOnly(dialog_->rotateGB);
-       bc().addReadOnly(dialog_->latexoptionsGB);
-       bc().addReadOnly(dialog_->bbGB);
-       bc().addReadOnly(dialog_->sizeBG);
-       bc().addReadOnly(dialog_->displayGB);
+       bc().addReadOnly(dialog_->latexoptions);
        bc().addReadOnly(dialog_->subfigure);
        bc().addReadOnly(dialog_->subcaption);
        bc().addReadOnly(dialog_->filenameL);
        bc().addReadOnly(dialog_->filename);
        bc().addReadOnly(dialog_->browsePB);
+       bc().addReadOnly(dialog_->unzipCB);
+       bc().addReadOnly(dialog_->filename);
+       bc().addReadOnly(dialog_->lbX);
+       bc().addReadOnly(dialog_->lbY);
+       bc().addReadOnly(dialog_->rtX);
+       bc().addReadOnly(dialog_->rtY);
+       bc().addReadOnly(dialog_->lbXunit);
+       bc().addReadOnly(dialog_->lbYunit);
+       bc().addReadOnly(dialog_->rtXunit);
+       bc().addReadOnly(dialog_->rtYunit);
+       bc().addReadOnly(dialog_->draftCB);
+       bc().addReadOnly(dialog_->clip);
+       bc().addReadOnly(dialog_->unzipCB);
+       bc().addReadOnly(dialog_->subfigure);
+       bc().addReadOnly(dialog_->subcaption);
+       bc().addReadOnly(dialog_->showCB);
+       bc().addReadOnly(dialog_->width);
+       bc().addReadOnly(dialog_->height);
+       bc().addReadOnly(dialog_->displayCB);
+       bc().addReadOnly(dialog_->displayscale);
+       bc().addReadOnly(dialog_->widthUnit);
+       bc().addReadOnly(dialog_->heightUnit);
+       bc().addReadOnly(dialog_->aspectratio);
+       bc().addReadOnly(dialog_->angle);
+       bc().addReadOnly(dialog_->origin);
+       bc().addReadOnly(dialog_->latexoptions);
+       bc().addReadOnly(dialog_->getPB);
+}
+
+
+namespace {
+
+// returns the number of the string s in the vector v
+int getItemNo(vector<string> v, string const & s) {
+       vector<string>::const_iterator cit =
+                   find(v.begin(), v.end(), s);
+       return (cit != v.end()) ? int(cit - v.begin()) : 0;
+}
+
+// returns the number of the unit in the array unit_name,
+// which is defined in lengthcommon.C
+int getUnitNo(char const * c[], string const & s) {
+       int i = 0;
+       while (i < num_units && s != c[i])
+               ++i;
+       return (i < num_units) ? i : 0;
 }
 
+}
+
+
 void QGraphics::update_contents()
 {
+       // clear and fill in the comboboxes
+       vector<string> const bb_units = frnt::getBBUnits();
+       dialog_->lbXunit->clear();
+       dialog_->lbYunit->clear();
+       dialog_->rtXunit->clear();
+       dialog_->rtYunit->clear();
+       for (vector<string>::const_iterator it = bb_units.begin();
+           it != bb_units.end(); ++it) {
+               dialog_->lbXunit->insertItem((*it).c_str(), -1);
+               dialog_->lbYunit->insertItem((*it).c_str(), -1);
+               dialog_->rtXunit->insertItem((*it).c_str(), -1);
+               dialog_->rtYunit->insertItem((*it).c_str(), -1);
+       }
+
        InsetGraphicsParams & igp = controller().params();
 
-       string unit = "cm";
-       if (lyxrc.default_papersize < 3)
-           unit = "in";
-       string const defaultUnit = string(unit);
+       // set the right default unit
+       LyXLength::UNIT unitDefault = LyXLength::CM;
+       switch (lyxrc.default_papersize) {
+               case BufferParams::PAPER_DEFAULT: break;
+
+               case BufferParams::PAPER_USLETTER:
+               case BufferParams::PAPER_LEGALPAPER:
+               case BufferParams::PAPER_EXECUTIVEPAPER:
+                       unitDefault = LyXLength::IN;
+                       break;
+
+               case BufferParams::PAPER_A3PAPER:
+               case BufferParams::PAPER_A4PAPER:
+               case BufferParams::PAPER_A5PAPER:
+               case BufferParams::PAPER_B5PAPER:
+                       unitDefault = LyXLength::CM;
+                       break;
+       }
 
-       // Update dialog with details from inset
        dialog_->filename->setText(igp.filename.c_str());
 
-       // set the bounding box values, if exists. First we need the whole
-       // path, because the controller knows nothing about the doc-dir
-       controller().bbChanged = false;
+       // set the bounding box values
        if (igp.bb.empty()) {
-               string const fileWithAbsPath = MakeAbsPath(igp.filename, OnlyPath(igp.filename));
-               string bb = controller().readBB(fileWithAbsPath);
-               if (!bb.empty()) {
-                       // get the values from the file
-                       // in this case we always have the point-unit
-                       dialog_->lbX->setText(token(bb,' ',0).c_str());
-                       dialog_->lbY->setText(token(bb,' ',1).c_str());
-                       dialog_->rtX->setText(token(bb,' ',2).c_str());
-                       dialog_->rtY->setText(token(bb,' ',3).c_str());
-               }
+               string const bb = controller().readBB(igp.filename);
+               // the values from the file always have the bigpoint-unit bp
+               dialog_->lbX->setText(token(bb, ' ', 0).c_str());
+               dialog_->lbY->setText(token(bb, ' ', 1).c_str());
+               dialog_->rtX->setText(token(bb, ' ', 2).c_str());
+               dialog_->rtY->setText(token(bb, ' ', 3).c_str());
+               dialog_->lbXunit->setCurrentItem(0);
+               dialog_->lbYunit->setCurrentItem(0);
+               dialog_->rtXunit->setCurrentItem(0);
+               dialog_->rtYunit->setCurrentItem(0);
+               controller().bbChanged = false;
        } else {
                // get the values from the inset
+               LyXLength anyLength;
+               string const xl(token(igp.bb, ' ', 0));
+               string const yl(token(igp.bb, ' ', 1));
+               string const xr(token(igp.bb, ' ', 2));
+               string const yr(token(igp.bb, ' ', 3));
+               if (isValidLength(xl, &anyLength)) {
+                       dialog_->lbX->setText(tostr(anyLength.value()).c_str());
+                       string const unit(unit_name[anyLength.unit()]);
+                       dialog_->lbXunit->setCurrentItem(getItemNo(bb_units, unit));
+               } else {
+                       dialog_->lbX->setText(xl.c_str());
+               }
+               if (isValidLength(yl, &anyLength)) {
+                       dialog_->lbY->setText(tostr(anyLength.value()).c_str());
+                       string const unit(unit_name[anyLength.unit()]);
+                       dialog_->lbYunit->setCurrentItem(getItemNo(bb_units, unit));
+               } else {
+                       dialog_->lbY->setText(xl.c_str());
+               }
+               if (isValidLength(xr, &anyLength)) {
+                       dialog_->rtX->setText(tostr(anyLength.value()).c_str());
+                       string const unit(unit_name[anyLength.unit()]);
+                       dialog_->rtXunit->setCurrentItem(getItemNo(bb_units, unit));
+               } else {
+                       dialog_->rtX->setText(xl.c_str());
+               }
+               if (isValidLength(yr, &anyLength)) {
+                       dialog_->rtY->setText(tostr(anyLength.value()).c_str());
+                       string const unit(unit_name[anyLength.unit()]);
+                       dialog_->rtYunit->setCurrentItem(getItemNo(bb_units, unit));
+               } else {
+                       dialog_->rtY->setText(xl.c_str());
+               }
                controller().bbChanged = true;
-               dialog_->lbX->setText(token(igp.bb,' ',0).c_str());
-               dialog_->lbY->setText(token(igp.bb,' ',1).c_str());
-               dialog_->rtX->setText(token(igp.bb,' ',2).c_str());
-               dialog_->rtY->setText(token(igp.bb,' ',3).c_str());
        }
 
        // Update the draft and clip mode
-       dialog_->draft->setChecked(igp.draft);
+       dialog_->draftCB->setChecked(igp.draft);
        dialog_->clip->setChecked(igp.clip);
+       dialog_->unzipCB->setChecked(igp.noUnzip);
 
        // Update the subcaption check button and input field
        dialog_->subfigure->setChecked(igp.subcaption);
        dialog_->subcaption->setText(igp.subcaptionText.c_str());
 
+       int item = 0;
        switch (igp.display) {
-           case InsetGraphicsParams::MONOCHROME: {
-                   dialog_->show->setCurrentItem(0);
-                   break;
-           }
-           case InsetGraphicsParams::GRAYSCALE: {
-                   dialog_->show->setCurrentItem(1);
-                   break;
-           }
-           case InsetGraphicsParams::COLOR: {
-                   dialog_->show->setCurrentItem(2);
-                   break;
-           }
-           case InsetGraphicsParams::NONE: {
-                   dialog_->show->setCurrentItem(3);
-                   break;
-           }
+               case grfx::DefaultDisplay: item = 0; break;
+               case grfx::MonochromeDisplay: item = 1; break;
+               case grfx::GrayscaleDisplay: item = 2; break;
+               case grfx::ColorDisplay: item = 3; break;
+               case grfx::NoDisplay: item = 0; break;
        }
-
-       dialog_->widthUnit->setCurrentItem(igp.width.unit());
-       dialog_->heightUnit->setCurrentItem(igp.height.unit());
-
-       switch (igp.size_type) {
-           case InsetGraphicsParams::DEFAULT_SIZE: {
-                   dialog_->defaultRB->setChecked(TRUE);
-                   break;
-           }
-           case InsetGraphicsParams::WH: {
-                   dialog_->customRB->setChecked(TRUE);
-                   break;
-           }
-           case InsetGraphicsParams::SCALE: {
-                   dialog_->scaleRB->setChecked(TRUE);
-                   dialog_->scale->setText(tostr(igp.scale).c_str());
-                   break;
-           }
+       dialog_->showCB->setCurrentItem(item);
+       dialog_->showCB->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
+       dialog_->displayCB->setChecked(igp.display != grfx::NoDisplay);
+       dialog_->displayscale->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
+       dialog_->displayscale->setText(tostr(igp.lyxscale).c_str());
+
+       //// the output section (width/height)
+       // set the length combo boxes
+       // only the width has the possibility for scale%. The original
+       // units are defined in lengthcommon.C
+       // 1. the width (a listttype)
+       dialog_->widthUnit->clear();
+       dialog_->widthUnit->insertItem(_("Scale%"));
+       for (int i = 0; i < num_units; i++)
+               dialog_->widthUnit->insertItem(unit_name_gui[i], -1);
+
+       if (!lyx::float_equal(igp.scale, 0.0, 0.05)) {
+               // there is a scale value > 0.05
+               dialog_->width->setText(tostr(igp.scale).c_str());
+               dialog_->widthUnit->setCurrentItem(0);
+       } else {
+               // no scale means default width/height
+               dialog_->width->setText(tostr(igp.width.value()).c_str());
+               // the width cannot have a unitDefault, because
+               // it is a "Scale%" or another user defined unit!
+               // +1 instead of the "Scale%" option
+               int unit_ = igp.width.unit();
+               dialog_->widthUnit->setCurrentItem(unit_ + 1);
        }
+       // 2. the height (a lengthgcombo type)
+       dialog_->height->setText(tostr(igp.height.value()).c_str());
+       LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
+               igp.height.unit() : unitDefault;
+       dialog_->heightUnit->setCurrentItem(unit_);
 
-       // aspect ratio
-       dialog_->aspectratio->setChecked(igp.keepAspectRatio);
+       // enable height input in case of non "Scale%" as width-unit
+       bool use_height = (dialog_->widthUnit->currentItem() > 0);
+       dialog_->height->setEnabled(use_height);
+       dialog_->heightUnit->setEnabled(use_height);
 
-       // now the lyx-internally viewsize
-       dialog_->displaywidthUnit->setCurrentItem(igp.lyxwidth.unit());
-       dialog_->displayheightUnit->setCurrentItem(igp.lyxheight.unit());
+       dialog_->aspectratio->setChecked(igp.keepAspectRatio);
 
-       // Update the rotate angle
        dialog_->angle->setText(tostr(igp.rotateAngle).c_str());
 
-       if (igp.rotateOrigin.empty()) {
-               dialog_->origin->setCurrentItem(0);
-       } else {
-               //fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
-       }
+       dialog_->origin->clear();
+
+       using namespace frnt;
+       vector<RotationOriginPair> origindata = getRotationOriginData();
+       vector<string> const origin_lang = getFirst(origindata);
+       QGraphics::origin_ltx = getSecond(origindata);
+
+       for (vector<string>::const_iterator it = origin_lang.begin();
+           it != origin_lang.end(); ++it)
+               dialog_->origin->insertItem((*it).c_str(), -1);
 
+       if (!igp.rotateOrigin.empty())
+               dialog_->origin->setCurrentItem(
+                       ::getItemNo(origin_ltx, igp.rotateOrigin));
+       else
+               dialog_->origin->setCurrentItem(0);
 
-       // latex options
+       //// latex section
        dialog_->latexoptions->setText(igp.special.c_str());
 }
 
 
 void QGraphics::apply()
 {
-       // Create the parameters structure and fill the data from the dialog.
        InsetGraphicsParams & igp = controller().params();
 
-       igp.filename = dialog_->filename->text();
+       igp.filename = dialog_->filename->text().latin1();
 
-       if (!controller().bbChanged)    // different to the original one?
-               igp.bb = string();      // don't write anything
-       else {
+       // the bb section
+       igp.bb.erase();
+       if (controller().bbChanged) {
                string bb;
-               string lbX(dialog_->lbX->text());
-               string lbY(dialog_->lbY->text());
-               string rtX(dialog_->rtX->text());
-               string rtY(dialog_->rtY->text());
-
-               if (lbX.empty())
-                       bb = "0 ";
-               else
-                       bb = lbX + " ";
-               if (lbY.empty())
-                       bb += "0 ";
-               else
-                       bb += (lbY + " ");
-               if (rtX.empty())
-                       bb += "0 ";
-               else
-                       bb += (rtX + " ");
-               if (rtY.empty())
-                       bb += "0 ";
-               else
-                       bb += (rtY + " ");
-
-               igp.bb = bb;
+               string lbX(dialog_->lbX->text().latin1());
+               string lbY(dialog_->lbY->text().latin1());
+               string rtX(dialog_->rtX->text().latin1());
+               string rtY(dialog_->rtY->text().latin1());
+               int bb_sum =
+                       strToInt(lbX) + strToInt(lbY) +
+                       strToInt(rtX) + strToInt(rtX);
+               if (bb_sum) {
+                       if (lbX.empty())
+                               bb = "0 ";
+                       else
+                               bb = lbX + dialog_->lbXunit->currentText().latin1() + ' ';
+                       if (lbY.empty())
+                               bb += "0 ";
+                       else
+                               bb += (lbY + dialog_->lbYunit->currentText().latin1() + ' ');
+                       if (rtX.empty())
+                               bb += "0 ";
+                       else
+                               bb += (rtX + dialog_->rtXunit->currentText().latin1() + ' ');
+                       if (rtY.empty())
+                               bb += '0';
+                       else
+                               bb += (rtY + dialog_->rtYunit->currentText().latin1());
+                       igp.bb = bb;
+               }
        }
 
-       igp.draft = dialog_->draft->isChecked();
-
+       igp.draft = dialog_->draftCB->isChecked();
        igp.clip = dialog_->clip->isChecked();
-
        igp.subcaption = dialog_->subfigure->isChecked();
+       igp.subcaptionText = dialog_->subcaption->text().latin1();
 
-       igp.subcaptionText = dialog_->subcaption->text();
-
-       switch(dialog_->show->currentItem()) {
-               case 0: igp.display = InsetGraphicsParams::MONOCHROME; break;
-               case 1: igp.display = InsetGraphicsParams::GRAYSCALE; break;
-               case 2: igp.display = InsetGraphicsParams::COLOR; break;
-               case 3: igp.display = InsetGraphicsParams::NONE; break;
+       switch (dialog_->showCB->currentItem()) {
+               case 0: igp.display = grfx::DefaultDisplay; break;
+               case 1: igp.display = grfx::MonochromeDisplay; break;
+               case 2: igp.display = grfx::GrayscaleDisplay; break;
+               case 3: igp.display = grfx::ColorDisplay; break;
                default:;
        }
 
-       if (dialog_->defaultRB->isChecked())
-           igp.size_type = InsetGraphicsParams::DEFAULT_SIZE;
-       else if (dialog_->customRB->isChecked())
-           igp.size_type = InsetGraphicsParams::WH;
-       else
-           igp.size_type = InsetGraphicsParams::SCALE;
-
-       string value(dialog_->width->text());
-       igp.width = LyXLength(strToDbl(value), dialog_->widthUnit->currentLengthItem());
-
-       value = string(dialog_->height->text());
-       igp.height = LyXLength(strToDbl(value), dialog_->heightUnit->currentLengthItem());
-
-       value = string(dialog_->displaywidth->text());
-       igp.lyxwidth = LyXLength(strToDbl(value), dialog_->displaywidthUnit->currentLengthItem());
-
-       value = string(dialog_->displayheight->text());
-       igp.lyxheight = LyXLength(strToDbl(value), dialog_->displayheightUnit->currentLengthItem());
+       if (!dialog_->displayCB->isChecked())
+               igp.display = grfx::NoDisplay;
 
-       igp.scale = strToInt(string(dialog_->scale->text()));
+       string value(dialog_->width->text().latin1());
+       if (dialog_->widthUnit->currentItem() > 0) {
+               // width/height combination
+               int const unitNo = getUnitNo(unit_name_gui,
+                       dialog_->widthUnit->currentText().latin1());
+               igp.width = LyXLength(value + unit_name_ltx[unitNo]);
+               igp.scale = 0.0;
+       } else {
+               // scaling instead of a width
+               igp.scale = strToDbl(value);
+               igp.width = LyXLength();
+       }
+       value = dialog_->height->text().latin1();
+       int const unitNo = getUnitNo(unit_name_gui,
+               dialog_->heightUnit->currentText().latin1());
+       igp.height = LyXLength(value + unit_name_ltx[unitNo]);
 
        igp.keepAspectRatio = dialog_->aspectratio->isChecked();
 
-       igp.rotateAngle = strToDbl(string(dialog_->angle->text()));
+       igp.noUnzip = dialog_->unzipCB->isChecked();
 
-       while (igp.rotateAngle < 0.0 || igp.rotateAngle > 360.0) {
-               if (igp.rotateAngle < 0.0) {
-                       igp.rotateAngle += 360.0;
-               } else if (igp.rotateAngle > 360.0) {
-                       igp.rotateAngle -= 360.0;
-               }
-       }
+       igp.lyxscale = strToInt(dialog_->displayscale->text().latin1());
 
-       if ((dialog_->origin->currentItem()) > 0)
-               igp.rotateOrigin = dialog_->origin->currentText();
-       else
-           igp.rotateOrigin = string();
+       igp.rotateAngle = strToDbl(dialog_->angle->text().latin1());
+       while (igp.rotateAngle < -360.0)
+               igp.rotateAngle += 360.0;
+       while (igp.rotateAngle >  360.0)
+               igp.rotateAngle -= 360.0;
 
-       igp.special = dialog_->latexoptions->text();
+       // save the latex name for the origin. If it is the default
+       // then origin_ltx returns ""
+       igp.rotateOrigin =
+               QGraphics::origin_ltx[dialog_->origin->currentItem()];
 
+       // more latex options
+       igp.special = dialog_->latexoptions->text().latin1();
 }
 
 
-void QGraphics::browse()
+void QGraphics::getBB()
 {
-       string const & name = controller().Browse(dialog_->filename->text().latin1());
-       if (!name.empty())
-               dialog_->filename->setText(name.c_str());
-}
-
-void QGraphics::get()
-{
-    string const filename(dialog_->filename->text());
-    if (!filename.empty()) {
-       string const fileWithAbsPath = MakeAbsPath(filename, OnlyPath(filename));
-       string bb = controller().readBB(fileWithAbsPath);
-       if (!bb.empty()) {
-               dialog_->lbX->setText(token(bb,' ',0).c_str());
-               dialog_->lbY->setText(token(bb,' ',1).c_str());
-               dialog_->rtX->setText(token(bb,' ',2).c_str());
-               dialog_->rtY->setText(token(bb,' ',3).c_str());
+       string const filename(dialog_->filename->text().latin1());
+       if (!filename.empty()) {
+               string const bb(controller().readBB(filename));
+               if (!bb.empty()) {
+                       dialog_->lbX->setText(token(bb, ' ', 0).c_str());
+                       dialog_->lbY->setText(token(bb, ' ', 1).c_str());
+                       dialog_->rtX->setText(token(bb, ' ', 2).c_str());
+                       dialog_->rtY->setText(token(bb, ' ', 3).c_str());
+                       // the default units for the bb values when reading
+                       // it from the file
+                       dialog_->lbXunit->setCurrentItem(0);
+                       dialog_->lbYunit->setCurrentItem(0);
+                       dialog_->rtXunit->setCurrentItem(0);
+                       dialog_->rtYunit->setCurrentItem(0);
+               }
+               controller().bbChanged = false;
        }
-       controller().bbChanged = false;
-    }
 }