* Validator.{cpp, h}:
[lyx.git] / src / frontends / qt4 / Validator.cpp
blobe156dcb099dedbe83a86326438d197cae0c494b3
1 /**
2 * \file Validator.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Richard Heck
9 * Full author contact details are available in file CREDITS.
13 #include <config.h>
15 #include "Validator.h"
16 #include "qt_helpers.h"
18 #include "support/gettext.h"
19 #include "LyXRC.h"
21 #include "frontends/alert.h"
23 #include "support/docstring.h"
24 #include "support/lstrings.h"
26 #include <QLineEdit>
27 #include <QLocale>
28 #include <QWidget>
30 using namespace std;
32 namespace lyx {
33 namespace frontend {
35 LengthValidator::LengthValidator(QWidget * parent)
36 : QValidator(parent),
37 no_bottom_(true), glue_length_(false)
41 QValidator::State LengthValidator::validate(QString & qtext, int &) const
43 bool ok;
44 qtext.trimmed().toDouble(&ok);
45 if (qtext.isEmpty() || ok)
46 return QValidator::Acceptable;
48 string const text = fromqstr(qtext);
50 if (glue_length_) {
51 GlueLength gl;
52 return (isValidGlueLength(text, &gl)) ?
53 QValidator::Acceptable : QValidator::Intermediate;
56 Length l;
57 bool const valid_length = isValidLength(text, &l);
58 if (!valid_length)
59 return QValidator::Intermediate;
61 if (no_bottom_)
62 return QValidator::Acceptable;
64 return b_.inPixels(100) <= l.inPixels(100) ?
65 QValidator::Acceptable : QValidator::Intermediate;
69 void LengthValidator::setBottom(Length const & b)
71 b_ = b;
72 no_bottom_ = false;
76 void LengthValidator::setBottom(GlueLength const & g)
78 g_ = g;
79 no_bottom_ = false;
80 glue_length_ = true;
84 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
86 LengthValidator * v = new LengthValidator(ed);
87 v->setBottom(Length());
88 return v;
92 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
94 LengthValidator * v = new LengthValidator(ed);
95 v->setBottom(GlueLength());
96 return v;
100 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const autotext)
101 : LengthValidator(parent),
102 autotext_(autotext)
106 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
108 if (qtext == autotext_)
109 return QValidator::Acceptable;
110 return LengthValidator::validate(qtext, dummy);
114 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const autotext)
116 LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
117 v->setBottom(Length());
118 return v;
122 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const autotext)
123 : QDoubleValidator(parent),
124 autotext_(autotext)
128 DoubleAutoValidator::DoubleAutoValidator(double bottom,
129 double top, int decimals, QObject * parent)
130 : QDoubleValidator(bottom, top, decimals, parent)
134 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
135 if (input == autotext_)
136 return QValidator::Acceptable;
137 return QDoubleValidator::validate(input, pos);
141 PathValidator::PathValidator(bool acceptable_if_empty,
142 QWidget * parent)
143 : QValidator(parent),
144 acceptable_if_empty_(acceptable_if_empty),
145 latex_doc_(false),
146 tex_allows_spaces_(false)
150 static docstring const printable_list(docstring const & invalid_chars)
152 docstring s;
153 docstring::const_iterator const begin = invalid_chars.begin();
154 docstring::const_iterator const end = invalid_chars.end();
155 docstring::const_iterator it = begin;
157 for (; it != end; ++it) {
158 if (it != begin)
159 s += ", ";
160 if (*it == ' ')
161 s += _("space");
162 else
163 s += *it;
166 return s;
170 QValidator::State PathValidator::validate(QString & qtext, int &) const
172 if (!latex_doc_)
173 return QValidator::Acceptable;
175 docstring const text = support::trim(qstring_to_ucs4(qtext));
176 if (text.empty())
177 return acceptable_if_empty_ ?
178 QValidator::Acceptable : QValidator::Intermediate;
180 docstring invalid_chars = from_ascii("#$%{}()[]\"^");
181 if (!tex_allows_spaces_)
182 invalid_chars += ' ';
184 if (text.find_first_of(invalid_chars) != docstring::npos) {
186 static int counter = 0;
187 if (counter == 0) {
188 Alert::error(_("Invalid filename"),
189 _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
190 printable_list(invalid_chars));
192 ++counter;
193 return QValidator::Intermediate;
196 return QValidator::Acceptable;
200 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
202 latex_doc_ = type == LATEX;
203 tex_allows_spaces_ = lyxrc.tex_allows_spaces;
207 PathValidator * getPathValidator(QLineEdit * ed)
209 if (!ed)
210 return 0;
211 QValidator * validator = const_cast<QValidator *>(ed->validator());
212 if (!validator)
213 return 0;
214 return dynamic_cast<PathValidator *>(validator);
217 } // namespace frontend
218 } // namespace lyx
220 #include "moc_Validator.cpp"