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 return QValidator::Invalid;
62 if (qtext.isEmpty() || (ok && !dp))
63 return QValidator::Acceptable;
65 string const text = fromqstr(qtext);
69 if (unsigned_ && gl.len().value() < 0)
70 return QValidator::Invalid;
71 return (isValidGlueLength(text, &gl)) ?
72 QValidator::Acceptable : QValidator::Intermediate;
76 bool const valid_length = isValidLength(text, &l);
78 return QValidator::Intermediate;
81 return QValidator::Acceptable;
83 if (unsigned_ && l.value() < 0)
84 return QValidator::Invalid;
86 if (positive_ && l.value() <= 0)
87 return QValidator::Invalid;
89 return bottom_.inPixels(100) <= l.inPixels(100) ?
90 QValidator::Acceptable : QValidator::Intermediate;
94 void LengthValidator::setBottom(Length const & b)
101 void LengthValidator::setBottom(GlueLength const & g)
109 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
111 LengthValidator * v = new LengthValidator(ed);
112 v->setBottom(Length());
113 v->setUnsigned(true);
118 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
120 LengthValidator * v = new LengthValidator(ed);
121 v->setBottom(GlueLength());
122 v->setUnsigned(true);
127 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
128 : LengthValidator(parent),
133 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
135 if (qtext == autotext_)
136 return QValidator::Acceptable;
137 return LengthValidator::validate(qtext, dummy);
141 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
143 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
144 v->setBottom(Length());
145 v->setUnsigned(true);
150 LengthAutoValidator * positiveLengthAutoValidator(QLineEdit * ed, QString const & autotext)
152 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
153 v->setBottom(Length());
154 v->setPositive(true);
159 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
160 : QDoubleValidator(parent),
165 DoubleAutoValidator::DoubleAutoValidator(double bottom,
166 double top, int decimals, QObject * parent)
167 : QDoubleValidator(bottom, top, decimals, parent)
171 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
172 if (input == autotext_)
173 return QValidator::Acceptable;
174 return QDoubleValidator::validate(input, pos);
178 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
183 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
185 #if QT_VERSION < 0x060000
186 qtext.remove(QRegExp("[\\n\\r]"));
188 qtext.remove(QRegularExpression("[\\n\\r]"));
190 return QValidator::Acceptable;
194 PathValidator::PathValidator(bool acceptable_if_empty,
196 : QValidator(parent),
197 acceptable_if_empty_(acceptable_if_empty),
199 tex_allows_spaces_(false)
203 static docstring const printable_list(docstring const & invalid_chars)
206 docstring::const_iterator const begin = invalid_chars.begin();
207 docstring::const_iterator const end = invalid_chars.end();
208 docstring::const_iterator it = begin;
210 for (; it != end; ++it) {
223 QValidator::State PathValidator::validate(QString & qtext, int &) const
226 return QValidator::Acceptable;
228 docstring const text = support::trim(qstring_to_ucs4(qtext));
230 return acceptable_if_empty_ ?
231 QValidator::Acceptable : QValidator::Intermediate;
233 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
234 if (!tex_allows_spaces_)
235 invalid_chars += ' ';
237 if (text.find_first_of(invalid_chars) != docstring::npos) {
239 static int counter = 0;
241 Alert::error(_("Invalid filename"),
242 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
243 printable_list(invalid_chars));
246 return QValidator::Intermediate;
249 return QValidator::Acceptable;
253 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & rc)
255 latex_doc_ = type == KernelDocType::LaTeX;
256 tex_allows_spaces_ = rc.tex_allows_spaces;
260 PathValidator * getPathValidator(QLineEdit * ed)
264 QValidator * validator = const_cast<QValidator *>(ed->validator());
267 return dynamic_cast<PathValidator *>(validator);
270 } // namespace frontend
273 #include "moc_Validator.cpp"