]> git.lyx.org Git - features.git/commitdiff
Length validators for the graphics and box dialogs
authorJürgen Spitzmüller <spitz@lyx.org>
Sat, 27 Nov 2004 18:47:41 +0000 (18:47 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Sat, 27 Nov 2004 18:47:41 +0000 (18:47 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9320 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt2/ChangeLog
src/frontends/qt2/QBox.C
src/frontends/qt2/QBoxDialog.C
src/frontends/qt2/QGraphics.C
src/frontends/qt2/QGraphicsDialog.C
src/frontends/qt2/qt_helpers.C
src/frontends/qt2/qt_helpers.h

index ad57227c2e0464cbdee6a32d13942cec95271c18..14c50dde9e8abc40a8e67e065f11ecbd213d7f07 100644 (file)
@@ -1,3 +1,13 @@
+2004-11-27  Jürgen Spitzmüller  <j.spitzmueller@gmx.de>
+
+       * qt_helpers.[Ch]: new member function widgetsToLength
+       for ordinary QComboBoxes
+       * QGraphics.C:
+       * QGraphicsDialog.C: use new member function. Add length
+       validator
+       * QBox.C:
+       * QBoxDialog.C: Add length validator 
+
 2004-11-27  Jürgen Spitzmüller  <j.spitzmueller@gmx.de>
 
        * ui/QTabularDialogBase.ui: Rename button ("Default" to "Set"),
index 627058f48f85adc24128806c9f9d4737fc5234b7..bdc1529ae4929732437502966930146dd68946f3 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "QBox.h"
 
+#include "checkedwidgets.h"
 #include "lengthcombo.h"
 #include "QBoxDialog.h"
 #include "qt_helpers.h"
@@ -82,6 +83,10 @@ void QBox::build_dialog()
        bcview().setOK(dialog_->okPB);
        bcview().setApply(dialog_->applyPB);
        bcview().setCancel(dialog_->closePB);
+       
+       // initialize the length validator
+       addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
+       addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
 }
 
 
@@ -179,6 +184,7 @@ void QBox::apply()
        int i = 0;
        bool spec = false;
        QString special = dialog_->widthUnitsLC->currentText();
+       QString value = dialog_->widthED->text();
        if (special == qt_("Height")) {
                i = 1;
                spec = true;
@@ -191,12 +197,17 @@ void QBox::apply()
        } else if (special == qt_("Width")) {
                i = 4;
                spec = true;
+       } 
+       // the user might insert a non-special value in the line edit
+       if (isValidLength(fromqstr(value))) {
+               i = 0;
+               spec = false;
        }
        controller().params().special = ids_spec_[i];
 
        string width;
        if (spec) {
-               width = fromqstr(dialog_->widthED->text());
+               width = fromqstr(value);
                // beware: bogosity! the unit is simply ignored in this case
                width += "in";
        } else
@@ -207,6 +218,7 @@ void QBox::apply()
        i = 0;
        spec = false;
        special = dialog_->heightUnitsLC->currentText();
+       value = dialog_->heightED->text();
        if (special == qt_("Height")) {
                i = 1;
                spec = true;
@@ -220,11 +232,16 @@ void QBox::apply()
                i = 4;
                spec = true;
        }
+       // the user might insert a non-special value in the line edit
+       if (isValidLength(fromqstr(value))) {
+               i = 0;
+               spec = false;
+       }
        controller().params().height_special = ids_spec_[i];
 
        string height;
-       if (spec) {
-               height = fromqstr(dialog_->heightED->text());
+       if (spec  && !isValidLength(fromqstr(dialog_->heightED->text()))) {
+               height = fromqstr(value);
                // beware: bogosity! the unit is simply ignored in this case
                height += "in";
        } else
index 690ba81770b82cd536fdf4553672e4ae93ee8c05..cfff15aeac7e67a873c8ae809e6eefa6b5003a9c 100644 (file)
@@ -13,6 +13,7 @@
 #include "QBoxDialog.h"
 
 #include "lengthcombo.h"
+#include "lengthvalidator.h"
 #include "QBox.h"
 #include "qt_helpers.h"
 
 namespace lyx {
 namespace frontend {
 
+namespace {
+
+LengthValidator * unsignedLengthValidator(QLineEdit * ed)
+{
+       LengthValidator * v = new LengthValidator(ed);
+       v->setBottom(LyXLength());
+       return v;
+}
+
+} // namespace anon
+
+
 QBoxDialog::QBoxDialog(QBox * form)
        : QBoxDialogBase(0, 0, false, 0),
        form_(form)
@@ -35,6 +48,9 @@ QBoxDialog::QBoxDialog(QBox * form)
                form, SLOT(slotApply()));
        connect(closePB, SIGNAL(clicked()),
                form, SLOT(slotClose()));
+               
+       heightED->setValidator(unsignedLengthValidator(heightED));
+       widthED->setValidator(unsignedLengthValidator(widthED));
 }
 
 
index 2f6f0fd084a8ad46e713f98c200726d46b05f897..0a16e18801ca81d6c6b682fdffff540574f14172 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "QGraphics.h"
 
+#include "checkedwidgets.h"
 #include "lengthcombo.h"
 #include "QGraphicsDialog.h"
 #include "Qt2BC.h"
@@ -105,6 +106,16 @@ void QGraphics::build_dialog()
        bcview().addReadOnly(dialog_->origin);
        bcview().addReadOnly(dialog_->latexoptions);
        bcview().addReadOnly(dialog_->getPB);
+       
+       // initialize the length validator
+       addCheckedLineEdit(bcview(), dialog_->width, dialog_->sizewidthL_2);
+       addCheckedLineEdit(bcview(), dialog_->height, dialog_->sizeheightL_2);
+       addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
+       addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL_2);
+       addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
+       addCheckedLineEdit(bcview(), dialog_->lbY, dialog_->yL);
+       addCheckedLineEdit(bcview(), dialog_->rtX, dialog_->xL_2);
+       addCheckedLineEdit(bcview(), dialog_->rtY, dialog_->yL_2);
 }
 
 
@@ -117,15 +128,6 @@ int getItemNo(vector<string> v, string const & 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 * const c[], string const & s) {
-       int i = 0;
-       while (i < num_units && s != c[i])
-               ++i;
-       return (i < num_units) ? i : 0;
-}
-
 }
 
 
@@ -268,10 +270,8 @@ void QGraphics::update_contents()
                dialog_->widthUnit->setCurrentItem(unit_ + 1);
        }
        // 2. the height (a lengthgcombo type)
-       dialog_->height->setText(toqstr(tostr(igp.height.value())));
-       LyXLength::UNIT unit_ = (igp.height.value() > 0.0) ?
-               igp.height.unit() : unitDefault;
-       dialog_->heightUnit->setCurrentItem(unit_);
+       lengthToWidgets(dialog_->height, dialog_->heightUnit, 
+               igp.height.asString(), unitDefault);
 
        // enable height input in case of non "Scale%" as width-unit
        bool use_height = (dialog_->widthUnit->currentItem() > 0);
@@ -360,11 +360,10 @@ void QGraphics::apply()
                igp.display = lyx::graphics::NoDisplay;
 
        string value = fromqstr(dialog_->width->text());
-       if (dialog_->widthUnit->currentItem() > 0) {
+       if (dialog_->widthUnit->currentItem() > 0 || isValidLength(value)) {
                // width/height combination
-               QString const text = dialog_->widthUnit->currentText();
-               int const unitNo = getUnitNo(unit_name_gui, fromqstr(text));
-               igp.width = LyXLength(value + unit_name_ltx[unitNo]);
+               igp.width = 
+                       widgetsToLength(dialog_->width, dialog_->widthUnit);
                igp.scale = 0.0;
        } else {
                // scaling instead of a width
@@ -372,9 +371,8 @@ void QGraphics::apply()
                igp.width = LyXLength();
        }
        value = fromqstr(dialog_->height->text());
-       QString text = dialog_->heightUnit->currentText();
-       int const unitNo = getUnitNo(unit_name_gui, fromqstr(text));
-       igp.height = LyXLength(value + unit_name_ltx[unitNo]);
+       igp.height = 
+               LyXLength(widgetsToLength(dialog_->height, dialog_->heightUnit));
 
        igp.keepAspectRatio = dialog_->aspectratio->isChecked();
 
index cf801d2162072c5aaf33f2346b732c29fce0d770..bdf7d76d9bdbce19a27ceec57f96034acabeaf54 100644 (file)
@@ -15,6 +15,7 @@
 #include "QGraphics.h"
 
 #include "lengthcombo.h"
+#include "lengthvalidator.h"
 #include "qt_helpers.h"
 
 #include "debug.h"
@@ -23,6 +24,7 @@
 
 #include <qpushbutton.h>
 #include <qlineedit.h>
+#include <qvalidator.h>
 
 
 using std::string;
@@ -30,6 +32,19 @@ using std::string;
 namespace lyx {
 namespace frontend {
 
+
+namespace {
+
+LengthValidator * unsignedLengthValidator(QLineEdit * ed)
+{
+       LengthValidator * v = new LengthValidator(ed);
+       v->setBottom(LyXLength());
+       return v;
+}
+
+} // namespace anon
+
+
 QGraphicsDialog::QGraphicsDialog(QGraphics * form)
        : QGraphicsDialogBase(0, 0, false, 0),
        form_(form)
@@ -44,6 +59,18 @@ QGraphicsDialog::QGraphicsDialog(QGraphics * form)
                form, SLOT(slotRestore()));
        connect(editPB, SIGNAL(clicked()),
                this, SLOT(edit_clicked()));
+               
+       angle->setValidator(new QDoubleValidator(-360, 360, 2, angle));
+
+       lbX->setValidator(new QIntValidator(lbX));
+       lbY->setValidator(new QIntValidator(lbY));
+       rtX->setValidator(new QIntValidator(rtX));
+       rtY->setValidator(new QIntValidator(rtY));
+               
+       displayscale->setValidator(new QDoubleValidator(0, 1000, 2, 
+               displayscale));
+       height->setValidator(unsignedLengthValidator(height));
+       width->setValidator(unsignedLengthValidator(width));
 }
 
 
index 91a9077335d554c8749ecfb7e57951ba7b7f76b4..95937f782954635afea16eac90e5010cafe15f55 100644 (file)
 #include "lengthcombo.h"
 #include "qt_helpers.h"
 
+#include "lengthcommon.h"
 #include "gettext.h"
 
 #include "support/lstrings.h"
 #include "support/tostr.h"
 
+#include <qcombobox.h>
 #include <qlineedit.h>
 #include <qtextcodec.h>
 
@@ -78,6 +80,22 @@ string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
 }
 
 
+LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
+{
+       QString length = input->text();
+       if (length.isEmpty())
+               return LyXLength();
+
+       // don't return unit-from-choice if the input(field) contains a unit
+       if (isValidGlueLength(fromqstr(length)))
+               return LyXLength(fromqstr(length));
+
+       LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
+       
+       return LyXLength(length.toDouble(), unit);
+}
+
+
 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
        string const & len, LyXLength::UNIT defaultUnit)
 {
index 4891ee560079e197a5feba486102db4696b20129..3ce4976fb32d8c0deb78ea0c5acc031e49452c50 100644 (file)
@@ -17,6 +17,7 @@
 #include "lyxlength.h"
 
 class LengthCombo;
+class QComboBox;
 class QLineEdit;
 class QString;
 
@@ -24,8 +25,10 @@ std::string makeFontName(std::string const & family, std::string const & foundry
 
 std::pair<std::string,std::string> parseFontName(std::string const & name);
 
-/// method to get a LyXLength from widgets
+/// method to get a LyXLength from widgets (LengthCombo)
 std::string widgetsToLength(QLineEdit const * input, LengthCombo const * combo);
+/// method to get a LyXLength from widgets (QComboBox)
+LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo);
 
 /// method to set widgets from a LyXLength
 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,