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, QString const autotext)
89 : LengthValidator(parent),
94 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
96 if (qtext == autotext_)
97 return QValidator::Acceptable;
98 return LengthValidator::validate(qtext, dummy);
102 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const autotext)
104 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
105 v->setBottom(Length());
110 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const autotext)
111 : QDoubleValidator(parent),
116 DoubleAutoValidator::DoubleAutoValidator(double bottom,
117 double top, int decimals, QObject * parent)
118 : QDoubleValidator(bottom, top, decimals, parent)
122 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
123 if (input == autotext_)
124 return QValidator::Acceptable;
125 return QDoubleValidator::validate(input, pos);
129 PathValidator::PathValidator(bool acceptable_if_empty,
131 : QValidator(parent),
132 acceptable_if_empty_(acceptable_if_empty),
134 tex_allows_spaces_(false)
138 static docstring const printable_list(docstring const & invalid_chars)
141 docstring::const_iterator const begin = invalid_chars.begin();
142 docstring::const_iterator const end = invalid_chars.end();
143 docstring::const_iterator it = begin;
145 for (; it != end; ++it) {
158 QValidator::State PathValidator::validate(QString & qtext, int &) const
161 return QValidator::Acceptable;
163 docstring const text = support::trim(qstring_to_ucs4(qtext));
165 return acceptable_if_empty_ ?
166 QValidator::Acceptable : QValidator::Intermediate;
168 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
169 if (!tex_allows_spaces_)
170 invalid_chars += ' ';
172 if (text.find_first_of(invalid_chars) != docstring::npos) {
174 static int counter = 0;
176 Alert::error(_("Invalid filename"),
177 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
178 printable_list(invalid_chars));
181 return QValidator::Intermediate;
184 return QValidator::Acceptable;
188 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
190 latex_doc_ = type == LATEX;
191 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
195 PathValidator * getPathValidator(QLineEdit * ed)
199 QValidator * validator = const_cast<QValidator *>(ed->validator());
202 return dynamic_cast<PathValidator *>(validator);
205 } // namespace frontend
208 #include "moc_Validator.cpp"