]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/Validator.cpp
Add missing initialization
[lyx.git] / src / frontends / qt4 / Validator.cpp
index f376afbe2a0921e861572f2d5173c1d87fa82820..6d0ffc0175242391a297dbda0fdb84944774448c 100644 (file)
@@ -24,6 +24,7 @@
 #include "support/lstrings.h"
 
 #include <QLineEdit>
+#include <QLocale>
 #include <QWidget>
 
 using namespace std;
@@ -33,18 +34,37 @@ namespace frontend {
 
 LengthValidator::LengthValidator(QWidget * parent)
        : QValidator(parent),
-         no_bottom_(true), glue_length_(false)
+         no_bottom_(true), glue_length_(false), unsigned_(false)
 {}
 
 
 QValidator::State LengthValidator::validate(QString & qtext, int &) const
 {
-       string const text = fromqstr(qtext);
-       if (text.empty() || support::isStrDbl(text))
+       QLocale loc;
+       bool ok;
+       double d = loc.toDouble(qtext.trimmed(), &ok);
+       // QLocale::toDouble accepts something like "1."
+       // We don't.
+       bool dp = qtext.endsWith(loc.decimalPoint());
+       if (!ok) {
+               // Fall back to C
+               QLocale c(QLocale::C);
+               d = c.toDouble(qtext.trimmed(), &ok);
+               dp = qtext.endsWith(c.decimalPoint());
+       }
+
+       if (ok && unsigned_ && d < 0)
+               return QValidator::Invalid;
+
+       if (qtext.isEmpty() || (ok && !dp))
                return QValidator::Acceptable;
 
+       string const text = fromqstr(qtext);
+
        if (glue_length_) {
                GlueLength gl;
+               if (unsigned_ && gl.len().value() < 0)
+                       return QValidator::Invalid;
                return (isValidGlueLength(text, &gl)) ?
                        QValidator::Acceptable : QValidator::Intermediate;
        }
@@ -57,6 +77,9 @@ QValidator::State LengthValidator::validate(QString & qtext, int &) const
        if (no_bottom_)
                return QValidator::Acceptable;
 
+       if (unsigned_ && l.value() < 0)
+               return QValidator::Invalid;
+
        return b_.inPixels(100) <= l.inPixels(100) ?
                QValidator::Acceptable : QValidator::Intermediate;
 }
@@ -81,33 +104,46 @@ LengthValidator * unsignedLengthValidator(QLineEdit * ed)
 {
        LengthValidator * v = new LengthValidator(ed);
        v->setBottom(Length());
+       v->setUnsigned(true);
        return v;
 }
 
 
-LengthAutoValidator::LengthAutoValidator(QWidget * parent)
-       : LengthValidator(parent)
+LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
+{
+       LengthValidator * v = new LengthValidator(ed);
+       v->setBottom(GlueLength());
+       v->setUnsigned(true);
+       return v;
+}
+
+
+LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
+       : LengthValidator(parent),
+         autotext_(autotext)
 {}
 
 
 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
 {
-       if (qtext == "auto")
+       if (qtext == autotext_)
                return QValidator::Acceptable;
        return LengthValidator::validate(qtext, dummy);
 }
 
 
-LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
+LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
 {
-       LengthAutoValidator * v = new LengthAutoValidator(ed);
+       LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
        v->setBottom(Length());
+       v->setUnsigned(true);
        return v;
 }
 
 
-DoubleAutoValidator::DoubleAutoValidator(QWidget * parent)
-       : QDoubleValidator(parent)
+DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
+       : QDoubleValidator(parent),
+         autotext_(autotext)
 {}
 
 
@@ -118,12 +154,24 @@ DoubleAutoValidator::DoubleAutoValidator(double bottom,
 
 
 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
-       if (input == "auto")
+       if (input == autotext_)
                return QValidator::Acceptable;
        return QDoubleValidator::validate(input, pos);
 }
 
 
+NoNewLineValidator::NoNewLineValidator(QWidget * parent)
+       : QValidator(parent)
+{}
+
+
+QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
+{
+       qtext.remove(QRegExp("[\\n\\r]"));
+       return QValidator::Acceptable;
+}
+
+
 PathValidator::PathValidator(bool acceptable_if_empty,
                             QWidget * parent)
        : QValidator(parent),
@@ -203,4 +251,4 @@ PathValidator * getPathValidator(QLineEdit * ed)
 } // namespace frontend
 } // namespace lyx
 
-#include "Validator_moc.cpp"
+#include "moc_Validator.cpp"