3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
14 #include "validators.h"
15 #include "qt_helpers.h"
20 #include "frontends/Alert.h"
22 #include "frontends/controllers/Dialog.h"
24 #include "support/lstrings.h"
25 #include "support/std_ostream.h"
27 #include <qlineedit.h>
32 using lyx::support::isStrDbl;
36 LengthValidator::LengthValidator(QWidget * parent, const char * name)
37 : QValidator(parent, name),
38 no_bottom_(true), glue_length_(false)
42 QValidator::State LengthValidator::validate(QString & qtext, int &) const
44 string const text = fromqstr(qtext);
45 if (text.empty() || isStrDbl(text))
46 return QValidator::Acceptable;
50 return (isValidGlueLength(text, &gl)) ?
51 QValidator::Acceptable : QValidator::Intermediate;
55 bool const valid_length = isValidLength(text, &l);
57 return QValidator::Intermediate;
60 return QValidator::Acceptable;
62 return b_.inPixels(100) <= l.inPixels(100) ?
63 QValidator::Acceptable : QValidator::Intermediate;
67 void LengthValidator::setBottom(LyXLength const & b)
74 void LengthValidator::setBottom(LyXGlueLength const & g)
82 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
84 LengthValidator * v = new LengthValidator(ed);
85 v->setBottom(LyXLength());
90 PathValidator::PathValidator(bool acceptable_if_empty,
91 QWidget * parent, const char * name)
92 : QValidator(parent, name),
93 acceptable_if_empty_(acceptable_if_empty),
95 tex_allows_spaces_(false)
101 string const printable_list(string const & invalid_chars)
103 std::ostringstream ss;
104 string::const_iterator const begin = invalid_chars.begin();
105 string::const_iterator const end = invalid_chars.end();
106 string::const_iterator it = begin;
108 for (; it != end; ++it) {
123 QValidator::State PathValidator::validate(QString & qtext, int &) const
126 return QValidator::Acceptable;
128 string const text = lyx::support::trim(fromqstr(qtext));
130 return acceptable_if_empty_ ?
131 QValidator::Acceptable : QValidator::Intermediate;
133 string invalid_chars("#$%{}()[]\"^");
134 if (!tex_allows_spaces_)
135 invalid_chars += ' ';
137 if (text.find_first_of(invalid_chars) != string::npos) {
139 static int counter = 0;
141 Alert::error(_("Invalid filename"),
142 _("LyX does not provide LateX support for file names containing any of these characters:\n") +
143 printable_list(invalid_chars));
146 return QValidator::Intermediate;
149 return QValidator::Acceptable;
153 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
156 latex_doc_ = type == lyx::frontend::Kernel::LATEX;
157 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
161 PathValidator * getPathValidator(QLineEdit * ed)
165 QValidator * validator = const_cast<QValidator *>(ed->validator());
168 return dynamic_cast<PathValidator *>(validator);