3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Richard Kimberly Heck
9 * Full author contact details are available in file CREDITS.
15 #include "Validator.h"
19 #include "qt_helpers.h"
21 #include "frontends/alert.h"
23 #include "support/docstring.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
36 LengthValidator::LengthValidator(QWidget * parent)
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 (ok && positive_ && d == 0)
60 // A plausible intermediate value, see #12508
61 return QValidator::Intermediate;
63 if (ok && positive_ && d < 0)
64 return QValidator::Invalid;
66 if (qtext.isEmpty() || (ok && !dp))
67 return QValidator::Acceptable;
69 string const text = fromqstr(qtext);
73 if (unsigned_ && gl.len().value() < 0)
74 return QValidator::Invalid;
75 if (isValidGlueLength(text, &gl))
76 return QValidator::Acceptable;
77 // Also check for localized variant
78 if (isValidGlueLength(fromqstr(unlocLengthString(qtext)), &gl))
79 return QValidator::Acceptable;
80 return QValidator::Intermediate;
84 bool const valid_length = isValidLength(text, &l);
86 return QValidator::Intermediate;
89 return QValidator::Acceptable;
91 if (unsigned_ && l.value() < 0)
92 return QValidator::Invalid;
94 if (positive_ && l.value() <= 0)
95 return QValidator::Invalid;
97 return bottom_.inPixels(100) <= l.inPixels(100) ?
98 QValidator::Acceptable : QValidator::Intermediate;
102 void LengthValidator::setBottom(Length const & b)
109 void LengthValidator::setBottom(GlueLength const & g)
117 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
119 LengthValidator * v = new LengthValidator(ed);
120 v->setBottom(Length());
121 v->setUnsigned(true);
126 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
128 LengthValidator * v = new LengthValidator(ed);
129 v->setBottom(GlueLength());
130 v->setUnsigned(true);
135 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
136 : LengthValidator(parent),
141 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
143 if (qtext == autotext_)
144 return QValidator::Acceptable;
145 return LengthValidator::validate(qtext, dummy);
149 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
151 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
152 v->setBottom(Length());
153 v->setUnsigned(true);
158 LengthAutoValidator * positiveLengthAutoValidator(QLineEdit * ed, QString const & autotext)
160 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
161 v->setBottom(Length());
162 v->setPositive(true);
167 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
168 : QDoubleValidator(parent),
173 DoubleAutoValidator::DoubleAutoValidator(double bottom,
174 double top, int decimals, QObject * parent)
175 : QDoubleValidator(bottom, top, decimals, parent)
179 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
180 if (input == autotext_)
181 return QValidator::Acceptable;
182 return QDoubleValidator::validate(input, pos);
186 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
191 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
193 #if QT_VERSION < 0x060000
194 qtext.remove(QRegExp("[\\n\\r]"));
196 qtext.remove(QRegularExpression("[\\n\\r]"));
198 return QValidator::Acceptable;
202 PathValidator::PathValidator(bool acceptable_if_empty,
204 : QValidator(parent),
205 acceptable_if_empty_(acceptable_if_empty),
207 tex_allows_spaces_(false)
211 static docstring const printable_list(docstring const & invalid_chars)
214 docstring::const_iterator const begin = invalid_chars.begin();
215 docstring::const_iterator const end = invalid_chars.end();
216 docstring::const_iterator it = begin;
218 for (; it != end; ++it) {
231 QValidator::State PathValidator::validate(QString & qtext, int &) const
234 return QValidator::Acceptable;
236 docstring const text = support::trim(qstring_to_ucs4(qtext));
238 return acceptable_if_empty_ ?
239 QValidator::Acceptable : QValidator::Intermediate;
241 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
242 if (!tex_allows_spaces_)
243 invalid_chars += ' ';
245 if (text.find_first_of(invalid_chars) != docstring::npos) {
247 static int counter = 0;
249 Alert::error(_("Invalid filename"),
250 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
251 printable_list(invalid_chars));
254 return QValidator::Intermediate;
257 return QValidator::Acceptable;
261 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & rc)
263 latex_doc_ = type == KernelDocType::LaTeX;
264 tex_allows_spaces_ = rc.tex_allows_spaces;
268 PathValidator * getPathValidator(QLineEdit * ed)
272 QValidator * validator = const_cast<QValidator *>(ed->validator());
275 return dynamic_cast<PathValidator *>(validator);
278 } // namespace frontend
281 #include "moc_Validator.cpp"