The attached patch adds length validators to the graphics and box dialogs. The graphics dialog was the only one who didn't use widgetsToLength (so it was not possible to enter lengths directly, like in the other dialogs). I have intergrated this (with a new helper function that can handle QComboBoxes).
(The remaining dialogs will be a breeze. I just wanted to handle these special cases first) OK? Jürgen P.S.: Angus, why did you initialize the validator in QExternal->widthED to double? Shouldn't a user be able to enter "3.5cm" into the line edit widget even *if* "Scale" is still selected in the combo?
Index: QBox.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QBox.C,v retrieving revision 1.3 diff -p -u -r1.3 QBox.C --- QBox.C 20 May 2004 09:36:27 -0000 1.3 +++ QBox.C 25 Nov 2004 13:44:06 -0000 @@ -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: QBoxDialog.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QBoxDialog.C,v retrieving revision 1.5 diff -p -u -r1.5 QBoxDialog.C --- QBoxDialog.C 2 Jun 2004 20:13:18 -0000 1.5 +++ QBoxDialog.C 25 Nov 2004 13:44:06 -0000 @@ -13,6 +13,7 @@ #include "QBoxDialog.h" #include "lengthcombo.h" +#include "lengthvalidator.h" #include "QBox.h" #include "qt_helpers.h" @@ -23,6 +24,18 @@ 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: QGraphics.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QGraphics.C,v retrieving revision 1.49 diff -p -u -r1.49 QGraphics.C --- QGraphics.C 20 May 2004 09:36:27 -0000 1.49 +++ QGraphics.C 25 Nov 2004 13:44:07 -0000 @@ -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 c 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: QGraphicsDialog.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QGraphicsDialog.C,v retrieving revision 1.28 diff -p -u -r1.28 QGraphicsDialog.C --- QGraphicsDialog.C 2 Jun 2004 20:13:18 -0000 1.28 +++ QGraphicsDialog.C 25 Nov 2004 13:44:07 -0000 @@ -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(QGraphi 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: qt_helpers.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/qt_helpers.C,v retrieving revision 1.17 diff -p -u -r1.17 qt_helpers.C --- qt_helpers.C 20 May 2004 09:36:28 -0000 1.17 +++ qt_helpers.C 25 Nov 2004 13:44:08 -0000 @@ -14,11 +14,13 @@ #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> @@ -75,6 +77,22 @@ string widgetsToLength(QLineEdit const * LyXLength::UNIT unit = combo->currentLengthItem(); return LyXLength(length.toDouble(), unit).asString(); +} + + +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); } Index: qt_helpers.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/qt_helpers.h,v retrieving revision 1.9 diff -p -u -r1.9 qt_helpers.h --- qt_helpers.h 6 Oct 2003 15:42:52 -0000 1.9 +++ qt_helpers.h 25 Nov 2004 13:44:08 -0000 @@ -17,6 +17,7 @@ #include "lyxlength.h" class LengthCombo; +class QComboBox; class QLineEdit; class QString; @@ -24,8 +25,10 @@ std::string makeFontName(std::string con 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,