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 "frontends/controllers/Dialog.h"
25 #include "support/docstring.h"
26 #include "support/lstrings.h"
27 #include "support/std_ostream.h"
34 using lyx::support::isStrDbl;
40 LengthValidator::LengthValidator(QWidget * parent)
42 no_bottom_(true), glue_length_(false)
46 QValidator::State LengthValidator::validate(QString & qtext, int &) const
48 string const text = fromqstr(qtext);
49 if (text.empty() || isStrDbl(text))
50 return QValidator::Acceptable;
54 return (isValidGlueLength(text, &gl)) ?
55 QValidator::Acceptable : QValidator::Intermediate;
59 bool const valid_length = isValidLength(text, &l);
61 return QValidator::Intermediate;
64 return QValidator::Acceptable;
66 return b_.inPixels(100) <= l.inPixels(100) ?
67 QValidator::Acceptable : QValidator::Intermediate;
71 void LengthValidator::setBottom(Length const & b)
78 void LengthValidator::setBottom(GlueLength const & g)
86 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
88 LengthValidator * v = new LengthValidator(ed);
89 v->setBottom(Length());
94 LengthAutoValidator::LengthAutoValidator(QWidget * parent)
95 : LengthValidator(parent)
99 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
101 string const text = fromqstr(qtext);
103 return QValidator::Acceptable;
104 return LengthValidator::validate(qtext, dummy);
108 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
110 LengthAutoValidator * v = new LengthAutoValidator(ed);
111 v->setBottom(Length());
116 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent) :
117 QDoubleValidator(parent) {}
120 DoubleAutoValidator::DoubleAutoValidator(double bottom,
121 double top, int decimals, QObject * parent) :
122 QDoubleValidator(bottom, top, decimals, parent) {}
125 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
126 string const text = fromqstr(input);
128 return QValidator::Acceptable;
129 return QDoubleValidator::validate(input, pos);
133 PathValidator::PathValidator(bool acceptable_if_empty,
135 : QValidator(parent),
136 acceptable_if_empty_(acceptable_if_empty),
138 tex_allows_spaces_(false)
142 static docstring const printable_list(docstring const & invalid_chars)
145 docstring::const_iterator const begin = invalid_chars.begin();
146 docstring::const_iterator const end = invalid_chars.end();
147 docstring::const_iterator it = begin;
149 for (; it != end; ++it) {
162 QValidator::State PathValidator::validate(QString & qtext, int &) const
165 return QValidator::Acceptable;
167 docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
169 return acceptable_if_empty_ ?
170 QValidator::Acceptable : QValidator::Intermediate;
172 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
173 if (!tex_allows_spaces_)
174 invalid_chars += ' ';
176 if (text.find_first_of(invalid_chars) != docstring::npos) {
178 static int counter = 0;
180 lyx::frontend::Alert::error(_("Invalid filename"),
181 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
182 printable_list(invalid_chars));
185 return QValidator::Intermediate;
188 return QValidator::Acceptable;
192 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
195 latex_doc_ = type == lyx::frontend::Kernel::LATEX;
196 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
200 PathValidator * getPathValidator(QLineEdit * ed)
204 QValidator * validator = const_cast<QValidator *>(ed->validator());
207 return dynamic_cast<PathValidator *>(validator);
212 #include "Validator_moc.cpp"