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)
41 QValidator::State LengthValidator::validate(QString & qtext, int &) const
44 qtext.trimmed().toDouble(&ok);
45 if (qtext.isEmpty() || ok)
46 return QValidator::Acceptable;
48 string const text = fromqstr(qtext);
52 return (isValidGlueLength(text, &gl)) ?
53 QValidator::Acceptable : QValidator::Intermediate;
57 bool const valid_length = isValidLength(text, &l);
59 return QValidator::Intermediate;
62 return QValidator::Acceptable;
64 return b_.inPixels(100) <= l.inPixels(100) ?
65 QValidator::Acceptable : QValidator::Intermediate;
69 void LengthValidator::setBottom(Length const & b)
76 void LengthValidator::setBottom(GlueLength const & g)
84 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
86 LengthValidator * v = new LengthValidator(ed);
87 v->setBottom(Length());
92 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
94 LengthValidator * v = new LengthValidator(ed);
95 v->setBottom(GlueLength());
100 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
101 : LengthValidator(parent),
106 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
108 if (qtext == autotext_)
109 return QValidator::Acceptable;
110 return LengthValidator::validate(qtext, dummy);
114 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
116 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
117 v->setBottom(Length());
122 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
123 : QDoubleValidator(parent),
128 DoubleAutoValidator::DoubleAutoValidator(double bottom,
129 double top, int decimals, QObject * parent)
130 : QDoubleValidator(bottom, top, decimals, parent)
134 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
135 if (input == autotext_)
136 return QValidator::Acceptable;
137 return QDoubleValidator::validate(input, pos);
141 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
146 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
148 qtext.remove(QRegExp("[\\n\\r]"));
149 return QValidator::Acceptable;
153 PathValidator::PathValidator(bool acceptable_if_empty,
155 : QValidator(parent),
156 acceptable_if_empty_(acceptable_if_empty),
158 tex_allows_spaces_(false)
162 static docstring const printable_list(docstring const & invalid_chars)
165 docstring::const_iterator const begin = invalid_chars.begin();
166 docstring::const_iterator const end = invalid_chars.end();
167 docstring::const_iterator it = begin;
169 for (; it != end; ++it) {
182 QValidator::State PathValidator::validate(QString & qtext, int &) const
185 return QValidator::Acceptable;
187 docstring const text = support::trim(qstring_to_ucs4(qtext));
189 return acceptable_if_empty_ ?
190 QValidator::Acceptable : QValidator::Intermediate;
192 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
193 if (!tex_allows_spaces_)
194 invalid_chars += ' ';
196 if (text.find_first_of(invalid_chars) != docstring::npos) {
198 static int counter = 0;
200 Alert::error(_("Invalid filename"),
201 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
202 printable_list(invalid_chars));
205 return QValidator::Intermediate;
208 return QValidator::Acceptable;
212 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
214 latex_doc_ = type == LATEX;
215 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
219 PathValidator * getPathValidator(QLineEdit * ed)
223 QValidator * validator = const_cast<QValidator *>(ed->validator());
226 return dynamic_cast<PathValidator *>(validator);
229 } // namespace frontend
232 #include "moc_Validator.cpp"