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"
25 #include "support/std_ostream.h"
32 using lyx::support::isStrDbl;
38 LengthValidator::LengthValidator(QWidget * parent)
40 no_bottom_(true), glue_length_(false)
44 QValidator::State LengthValidator::validate(QString & qtext, int &) const
46 string const text = fromqstr(qtext);
47 if (text.empty() || isStrDbl(text))
48 return QValidator::Acceptable;
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 LengthAutoValidator::LengthAutoValidator(QWidget * parent)
93 : LengthValidator(parent)
97 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
99 string const text = fromqstr(qtext);
101 return QValidator::Acceptable;
102 return LengthValidator::validate(qtext, dummy);
106 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
108 LengthAutoValidator * v = new LengthAutoValidator(ed);
109 v->setBottom(Length());
114 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent) :
115 QDoubleValidator(parent) {}
118 DoubleAutoValidator::DoubleAutoValidator(double bottom,
119 double top, int decimals, QObject * parent) :
120 QDoubleValidator(bottom, top, decimals, parent) {}
123 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
124 string const text = fromqstr(input);
126 return QValidator::Acceptable;
127 return QDoubleValidator::validate(input, pos);
131 PathValidator::PathValidator(bool acceptable_if_empty,
133 : QValidator(parent),
134 acceptable_if_empty_(acceptable_if_empty),
136 tex_allows_spaces_(false)
140 static docstring const printable_list(docstring const & invalid_chars)
143 docstring::const_iterator const begin = invalid_chars.begin();
144 docstring::const_iterator const end = invalid_chars.end();
145 docstring::const_iterator it = begin;
147 for (; it != end; ++it) {
160 QValidator::State PathValidator::validate(QString & qtext, int &) const
163 return QValidator::Acceptable;
165 docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
167 return acceptable_if_empty_ ?
168 QValidator::Acceptable : QValidator::Intermediate;
170 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
171 if (!tex_allows_spaces_)
172 invalid_chars += ' ';
174 if (text.find_first_of(invalid_chars) != docstring::npos) {
176 static int counter = 0;
178 lyx::frontend::Alert::error(_("Invalid filename"),
179 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
180 printable_list(invalid_chars));
183 return QValidator::Intermediate;
186 return QValidator::Acceptable;
190 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
193 latex_doc_ = type == frontend::LATEX;
194 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
198 PathValidator * getPathValidator(QLineEdit * ed)
202 QValidator * validator = const_cast<QValidator *>(ed->validator());
205 return dynamic_cast<PathValidator *>(validator);
210 #include "Validator_moc.cpp"