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, bool const accept_empty)
37 : QValidator(parent), acceptable_if_empty_(accept_empty)
41 QValidator::State LengthValidator::validate(QString & qtext, int &) const
43 if (!acceptable_if_empty_ && qtext.isEmpty())
44 return QValidator::Intermediate;
47 double d = loc.toDouble(qtext.trimmed(), &ok);
48 // QLocale::toDouble accepts something like "1."
50 bool dp = qtext.endsWith(loc.decimalPoint());
53 QLocale c(QLocale::C);
54 d = c.toDouble(qtext.trimmed(), &ok);
55 dp = qtext.endsWith(c.decimalPoint());
58 if (ok && unsigned_ && d < 0)
59 return QValidator::Invalid;
61 if (ok && positive_ && d == 0)
62 // A plausible intermediate value, see #12508
63 return QValidator::Intermediate;
65 if (ok && positive_ && d < 0)
66 return QValidator::Invalid;
68 if (qtext.isEmpty() || (ok && !dp))
69 return QValidator::Acceptable;
71 string const text = fromqstr(qtext);
75 if (unsigned_ && gl.len().value() < 0)
76 return QValidator::Invalid;
77 if (isValidGlueLength(text, &gl))
78 return QValidator::Acceptable;
79 // Also check for localized variant
80 if (isValidGlueLength(fromqstr(unlocLengthString(qtext)), &gl))
81 return QValidator::Acceptable;
82 return QValidator::Intermediate;
86 bool const valid_length = isValidLength(text, &l);
88 return QValidator::Intermediate;
91 return QValidator::Acceptable;
93 if (unsigned_ && l.value() < 0)
94 return QValidator::Invalid;
96 if (positive_ && l.value() <= 0)
97 return QValidator::Invalid;
99 return bottom_.inPixels(100) <= l.inPixels(100) ?
100 QValidator::Acceptable : QValidator::Intermediate;
104 void LengthValidator::setBottom(Length const & b)
111 void LengthValidator::setBottom(GlueLength const & g)
119 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
121 LengthValidator * v = new LengthValidator(ed);
122 v->setBottom(Length());
123 v->setUnsigned(true);
128 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
130 LengthValidator * v = new LengthValidator(ed);
131 v->setBottom(GlueLength());
132 v->setUnsigned(true);
137 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
138 : LengthValidator(parent),
143 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
145 if (qtext == autotext_)
146 return QValidator::Acceptable;
147 return LengthValidator::validate(qtext, dummy);
151 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
153 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
154 v->setBottom(Length());
155 v->setUnsigned(true);
160 LengthAutoValidator * positiveLengthAutoValidator(QLineEdit * ed, QString const & autotext)
162 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
163 v->setBottom(Length());
164 v->setPositive(true);
169 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
170 : QDoubleValidator(parent),
175 DoubleAutoValidator::DoubleAutoValidator(double bottom,
176 double top, int decimals, QObject * parent)
177 : QDoubleValidator(bottom, top, decimals, parent)
181 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
182 if (input == autotext_)
183 return QValidator::Acceptable;
184 return QDoubleValidator::validate(input, pos);
188 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
193 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
195 #if QT_VERSION < 0x060000
196 qtext.remove(QRegExp("[\\n\\r]"));
198 qtext.remove(QRegularExpression("[\\n\\r]"));
200 return QValidator::Acceptable;
204 PathValidator::PathValidator(bool acceptable_if_empty,
206 : QValidator(parent),
207 acceptable_if_empty_(acceptable_if_empty),
209 tex_allows_spaces_(false)
213 static docstring const printable_list(docstring const & invalid_chars)
216 docstring::const_iterator const begin = invalid_chars.begin();
217 docstring::const_iterator const end = invalid_chars.end();
218 docstring::const_iterator it = begin;
220 for (; it != end; ++it) {
233 QValidator::State PathValidator::validate(QString & qtext, int &) const
236 return QValidator::Acceptable;
238 docstring const text = support::trim(qstring_to_ucs4(qtext));
240 return acceptable_if_empty_ ?
241 QValidator::Acceptable : QValidator::Intermediate;
243 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
244 if (!tex_allows_spaces_)
245 invalid_chars += ' ';
247 if (text.find_first_of(invalid_chars) != docstring::npos) {
249 static int counter = 0;
251 Alert::error(_("Invalid filename"),
252 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
253 printable_list(invalid_chars));
256 return QValidator::Intermediate;
259 return QValidator::Acceptable;
263 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & rc)
265 latex_doc_ = type == KernelDocType::LaTeX;
266 tex_allows_spaces_ = rc.tex_allows_spaces;
270 PathValidator * getPathValidator(QLineEdit * ed)
274 QValidator * validator = const_cast<QValidator *>(ed->validator());
277 return dynamic_cast<PathValidator *>(validator);
280 } // namespace frontend
283 #include "moc_Validator.cpp"