]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/Validator.cpp
Add missing initialization
[lyx.git] / src / frontends / qt4 / Validator.cpp
index f0c253112c947093617c648e6a0302cc189c1af7..6d0ffc0175242391a297dbda0fdb84944774448c 100644 (file)
@@ -15,7 +15,7 @@
 #include "Validator.h"
 #include "qt_helpers.h"
 
-#include "gettext.h"
+#include "support/gettext.h"
 #include "LyXRC.h"
 
 #include "frontends/alert.h"
 #include "support/lstrings.h"
 
 #include <QLineEdit>
+#include <QLocale>
 #include <QWidget>
 
-using lyx::support::isStrDbl;
-using std::string;
-
+using namespace std;
 
 namespace lyx {
+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() || 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;
        }
@@ -58,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;
 }
@@ -82,49 +104,74 @@ 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
 {
-       string const text = fromqstr(qtext);
-       if (text == "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)
+{}
 
 
 DoubleAutoValidator::DoubleAutoValidator(double bottom,
-       double top, int decimals, QObject * parent) :
-       QDoubleValidator(bottom, top, decimals, parent) {}
+               double top, int decimals, QObject * parent)
+       : QDoubleValidator(bottom, top, decimals, parent)
+{}
 
 
 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
-       string const text = fromqstr(input);
-       if (text == "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),
@@ -159,9 +206,9 @@ QValidator::State PathValidator::validate(QString & qtext, int &) const
        if (!latex_doc_)
                return QValidator::Acceptable;
 
-       docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
+       docstring const text = support::trim(qstring_to_ucs4(qtext));
        if (text.empty())
-               return  acceptable_if_empty_ ?
+               return acceptable_if_empty_ ?
                        QValidator::Acceptable : QValidator::Intermediate;
 
        docstring invalid_chars = from_ascii("#$%{}()[]\"^");
@@ -172,7 +219,7 @@ QValidator::State PathValidator::validate(QString & qtext, int &) const
 
                static int counter = 0;
                if (counter == 0) {
-                       lyx::frontend::Alert::error(_("Invalid filename"),
+                       Alert::error(_("Invalid filename"),
                                     _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
                                         printable_list(invalid_chars));
                }
@@ -184,10 +231,9 @@ QValidator::State PathValidator::validate(QString & qtext, int &) const
 }
 
 
-void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
-                              LyXRC const & lyxrc)
+void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
 {
-       latex_doc_ = type == frontend::LATEX;
+       latex_doc_ = type == LATEX;
        tex_allows_spaces_ = lyxrc.tex_allows_spaces;
 }
 
@@ -202,6 +248,7 @@ PathValidator * getPathValidator(QLineEdit * ed)
        return dynamic_cast<PathValidator *>(validator);
 }
 
+} // namespace frontend
 } // namespace lyx
 
-#include "Validator_moc.cpp"
+#include "moc_Validator.cpp"