3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
15 #include "Validator.h"
16 #include "qt_helpers.h"
18 #include "support/gettext.h"
21 #include "frontends/alert.h"
23 #include "support/docstring.h"
24 #include "support/lstrings.h"
35 LengthValidator::LengthValidator(QWidget * parent)
37 no_bottom_(true), glue_length_(false), unsigned_(false)
41 QValidator::State LengthValidator::validate(QString & qtext, int &) const
45 double d = loc.toDouble(qtext.trimmed(), &ok);
46 // QLocale::toDouble accepts something like "1."
48 bool dp = qtext.endsWith(loc.decimalPoint());
51 QLocale c(QLocale::C);
52 d = c.toDouble(qtext.trimmed(), &ok);
53 dp = qtext.endsWith(c.decimalPoint());
56 if (ok && unsigned_ && d < 0)
57 return QValidator::Invalid;
59 if (qtext.isEmpty() || (ok && !dp))
60 return QValidator::Acceptable;
62 string const text = fromqstr(qtext);
66 if (unsigned_ && gl.len().value() < 0)
67 return QValidator::Invalid;
68 return (isValidGlueLength(text, &gl)) ?
69 QValidator::Acceptable : QValidator::Intermediate;
73 bool const valid_length = isValidLength(text, &l);
75 return QValidator::Intermediate;
78 return QValidator::Acceptable;
80 if (unsigned_ && l.value() < 0)
81 return QValidator::Invalid;
83 return b_.inPixels(100) <= l.inPixels(100) ?
84 QValidator::Acceptable : QValidator::Intermediate;
88 void LengthValidator::setBottom(Length const & b)
95 void LengthValidator::setBottom(GlueLength const & g)
103 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
105 LengthValidator * v = new LengthValidator(ed);
106 v->setBottom(Length());
107 v->setUnsigned(true);
112 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
114 LengthValidator * v = new LengthValidator(ed);
115 v->setBottom(GlueLength());
116 v->setUnsigned(true);
121 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
122 : LengthValidator(parent),
127 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
129 if (qtext == autotext_)
130 return QValidator::Acceptable;
131 return LengthValidator::validate(qtext, dummy);
135 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
137 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
138 v->setBottom(Length());
139 v->setUnsigned(true);
144 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
145 : QDoubleValidator(parent),
150 DoubleAutoValidator::DoubleAutoValidator(double bottom,
151 double top, int decimals, QObject * parent)
152 : QDoubleValidator(bottom, top, decimals, parent)
156 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
157 if (input == autotext_)
158 return QValidator::Acceptable;
159 return QDoubleValidator::validate(input, pos);
163 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
168 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
170 qtext.remove(QRegExp("[\\n\\r]"));
171 return QValidator::Acceptable;
175 PathValidator::PathValidator(bool acceptable_if_empty,
177 : QValidator(parent),
178 acceptable_if_empty_(acceptable_if_empty),
180 tex_allows_spaces_(false)
184 static docstring const printable_list(docstring const & invalid_chars)
187 docstring::const_iterator const begin = invalid_chars.begin();
188 docstring::const_iterator const end = invalid_chars.end();
189 docstring::const_iterator it = begin;
191 for (; it != end; ++it) {
204 QValidator::State PathValidator::validate(QString & qtext, int &) const
207 return QValidator::Acceptable;
209 docstring const text = support::trim(qstring_to_ucs4(qtext));
211 return acceptable_if_empty_ ?
212 QValidator::Acceptable : QValidator::Intermediate;
214 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
215 if (!tex_allows_spaces_)
216 invalid_chars += ' ';
218 if (text.find_first_of(invalid_chars) != docstring::npos) {
220 static int counter = 0;
222 Alert::error(_("Invalid filename"),
223 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
224 printable_list(invalid_chars));
227 return QValidator::Intermediate;
230 return QValidator::Acceptable;
234 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
236 latex_doc_ = type == LATEX;
237 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
241 PathValidator * getPathValidator(QLineEdit * ed)
245 QValidator * validator = const_cast<QValidator *>(ed->validator());
248 return dynamic_cast<PathValidator *>(validator);
251 } // namespace frontend
254 #include "moc_Validator.cpp"