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"
34 LengthValidator::LengthValidator(QWidget * parent)
36 no_bottom_(true), glue_length_(false)
40 QValidator::State LengthValidator::validate(QString & qtext, int &) const
42 string const text = fromqstr(qtext);
43 if (text.empty() || support::isStrDbl(text))
44 return QValidator::Acceptable;
48 return (isValidGlueLength(text, &gl)) ?
49 QValidator::Acceptable : QValidator::Intermediate;
53 bool const valid_length = isValidLength(text, &l);
55 return QValidator::Intermediate;
58 return QValidator::Acceptable;
60 return b_.inPixels(100) <= l.inPixels(100) ?
61 QValidator::Acceptable : QValidator::Intermediate;
65 void LengthValidator::setBottom(Length const & b)
72 void LengthValidator::setBottom(GlueLength const & g)
80 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
82 LengthValidator * v = new LengthValidator(ed);
83 v->setBottom(Length());
88 LengthAutoValidator::LengthAutoValidator(QWidget * parent)
89 : LengthValidator(parent)
93 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
95 string const text = fromqstr(qtext);
97 return QValidator::Acceptable;
98 return LengthValidator::validate(qtext, dummy);
102 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
104 LengthAutoValidator * v = new LengthAutoValidator(ed);
105 v->setBottom(Length());
110 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent)
111 : QDoubleValidator(parent)
115 DoubleAutoValidator::DoubleAutoValidator(double bottom,
116 double top, int decimals, QObject * parent)
117 : QDoubleValidator(bottom, top, decimals, parent)
121 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
123 return QValidator::Acceptable;
124 return QDoubleValidator::validate(input, pos);
128 PathValidator::PathValidator(bool acceptable_if_empty,
130 : QValidator(parent),
131 acceptable_if_empty_(acceptable_if_empty),
133 tex_allows_spaces_(false)
137 static docstring const printable_list(docstring const & invalid_chars)
140 docstring::const_iterator const begin = invalid_chars.begin();
141 docstring::const_iterator const end = invalid_chars.end();
142 docstring::const_iterator it = begin;
144 for (; it != end; ++it) {
157 QValidator::State PathValidator::validate(QString & qtext, int &) const
160 return QValidator::Acceptable;
162 docstring const text = support::trim(qstring_to_ucs4(qtext));
164 return acceptable_if_empty_ ?
165 QValidator::Acceptable : QValidator::Intermediate;
167 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
168 if (!tex_allows_spaces_)
169 invalid_chars += ' ';
171 if (text.find_first_of(invalid_chars) != docstring::npos) {
173 static int counter = 0;
175 Alert::error(_("Invalid filename"),
176 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
177 printable_list(invalid_chars));
180 return QValidator::Intermediate;
183 return QValidator::Acceptable;
187 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
189 latex_doc_ = type == LATEX;
190 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
194 PathValidator * getPathValidator(QLineEdit * ed)
198 QValidator * validator = const_cast<QValidator *>(ed->validator());
201 return dynamic_cast<PathValidator *>(validator);
204 } // namespace frontend
207 #include "Validator_moc.cpp"