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"
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
43 string const text = fromqstr(qtext);
44 if (text.empty() || support::isStrDbl(text))
45 return QValidator::Acceptable;
49 return (isValidGlueLength(text, &gl)) ?
50 QValidator::Acceptable : QValidator::Intermediate;
54 bool const valid_length = isValidLength(text, &l);
56 return QValidator::Intermediate;
59 return QValidator::Acceptable;
61 return b_.inPixels(100) <= l.inPixels(100) ?
62 QValidator::Acceptable : QValidator::Intermediate;
66 void LengthValidator::setBottom(Length const & b)
73 void LengthValidator::setBottom(GlueLength const & g)
81 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
83 LengthValidator * v = new LengthValidator(ed);
84 v->setBottom(Length());
89 LengthAutoValidator::LengthAutoValidator(QWidget * parent)
90 : LengthValidator(parent)
94 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
96 string const text = fromqstr(qtext);
98 return QValidator::Acceptable;
99 return LengthValidator::validate(qtext, dummy);
103 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
105 LengthAutoValidator * v = new LengthAutoValidator(ed);
106 v->setBottom(Length());
111 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent)
112 : 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 {
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 "Validator_moc.cpp"