]> 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 395bb3bd22751330d9c6900f4de69dc3544a3602..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()
        : base_class(_("Graphics"))
 {
@@ -93,48 +101,115 @@ void QGraphics::build_dialog()
 }
 
 
+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();
 
        // set the right default unit
-       string unit = "cm";
+       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: unit = "in"; break;
-       
+               case BufferParams::PAPER_EXECUTIVEPAPER:
+                       unitDefault = LyXLength::IN;
+                       break;
+
                case BufferParams::PAPER_A3PAPER:
                case BufferParams::PAPER_A4PAPER:
                case BufferParams::PAPER_A5PAPER:
-               case BufferParams::PAPER_B5PAPER: unit = "cm"; break;
+               case BufferParams::PAPER_B5PAPER:
+                       unitDefault = LyXLength::CM;
+                       break;
        }
 
        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 const 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
@@ -146,7 +221,7 @@ void QGraphics::update_contents()
        dialog_->subfigure->setChecked(igp.subcaption);
        dialog_->subcaption->setText(igp.subcaptionText.c_str());
 
-       int item;
+       int item = 0;
        switch (igp.display) {
                case grfx::DefaultDisplay: item = 0; break;
                case grfx::MonochromeDisplay: item = 1; break;
@@ -160,20 +235,62 @@ void QGraphics::update_contents()
        dialog_->displayscale->setEnabled(igp.display != grfx::NoDisplay && !readOnly());
        dialog_->displayscale->setText(tostr(igp.lyxscale).c_str());
 
-       dialog_->widthUnit->setCurrentItem(igp.width.unit());
-       dialog_->heightUnit->setCurrentItem(igp.height.unit());
+       //// 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_);
+
+       // 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);
+
        dialog_->aspectratio->setChecked(igp.keepAspectRatio);
 
-       // Update the rotate angle
        dialog_->angle->setText(tostr(igp.rotateAngle).c_str());
 
-       if (igp.rotateOrigin.empty()) {
+       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);
-       } else {
-               // FIXME fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
-       }
 
-       // latex options
+       //// latex section
        dialog_->latexoptions->setText(igp.special.c_str());
 }
 
@@ -182,41 +299,44 @@ void QGraphics::apply()
 {
        InsetGraphicsParams & igp = controller().params();
 
-       igp.filename = dialog_->filename->text();
+       igp.filename = dialog_->filename->text().latin1();
 
-       if (!controller().bbChanged) {
-               igp.bb = string();
-       } 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_->draftCB->isChecked();
        igp.clip = dialog_->clip->isChecked();
        igp.subcaption = dialog_->subfigure->isChecked();
-       igp.subcaptionText = dialog_->subcaption->text();
+       igp.subcaptionText = dialog_->subcaption->text().latin1();
 
        switch (dialog_->showCB->currentItem()) {
                case 0: igp.display = grfx::DefaultDisplay; break;
@@ -228,52 +348,62 @@ void QGraphics::apply()
 
        if (!dialog_->displayCB->isChecked())
                igp.display = grfx::NoDisplay;
-       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());
+
+       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.noUnzip = dialog_->unzipCB->isChecked();
-       igp.lyxscale = strToInt(string(dialog_->displayscale->text()));
 
-       igp.rotateAngle = strToDbl(string(dialog_->angle->text()));
+       igp.lyxscale = strToInt(dialog_->displayscale->text().latin1());
+
+       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;
 
-       if ((dialog_->origin->currentItem()) > 0)
-               igp.rotateOrigin = dialog_->origin->currentText();
-       else
-           igp.rotateOrigin = string();
-
-       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()];
 
-
-void QGraphics::browse()
-{
-       string const & name = controller().Browse(dialog_->filename->text().latin1());
-       if (!name.empty())
-               dialog_->filename->setText(name.c_str());
+       // more latex options
+       igp.special = dialog_->latexoptions->text().latin1();
 }
 
 
-void QGraphics::get()
+void QGraphics::getBB()
 {
-       string const filename(dialog_->filename->text());
+       string const filename(dialog_->filename->text().latin1());
        if (!filename.empty()) {
-               string const fileWithAbsPath(MakeAbsPath(filename, OnlyPath(filename)));
-               string const bb(controller().readBB(fileWithAbsPath));
+               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;
        }
@@ -282,6 +412,5 @@ void QGraphics::get()
 
 bool QGraphics::isValid()
 {
-       // FIXME: we need more here. 
        return !string(dialog_->filename->text().latin1()).empty();
 }