]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Validator.cpp
Move the NoNewLineValidator to Validator.cpp where it fits better.
[lyx.git] / src / frontends / qt4 / Validator.cpp
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.
5  *
6  * \author Angus Leeming
7  * \author Richard Heck
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12
13 #include <config.h>
14
15 #include "Validator.h"
16 #include "qt_helpers.h"
17
18 #include "support/gettext.h"
19 #include "LyXRC.h"
20
21 #include "frontends/alert.h"
22
23 #include "support/docstring.h"
24 #include "support/lstrings.h"
25
26 #include <QLineEdit>
27 #include <QLocale>
28 #include <QWidget>
29
30 using namespace std;
31
32 namespace lyx {
33 namespace frontend {
34
35 LengthValidator::LengthValidator(QWidget * parent)
36         : QValidator(parent),
37           no_bottom_(true), glue_length_(false)
38 {}
39
40
41 QValidator::State LengthValidator::validate(QString & qtext, int &) const
42 {
43         bool ok;
44         qtext.trimmed().toDouble(&ok);
45         if (qtext.isEmpty() || ok)
46                 return QValidator::Acceptable;
47
48         string const text = fromqstr(qtext);
49
50         if (glue_length_) {
51                 GlueLength gl;
52                 return (isValidGlueLength(text, &gl)) ?
53                         QValidator::Acceptable : QValidator::Intermediate;
54         }
55
56         Length l;
57         bool const valid_length = isValidLength(text, &l);
58         if (!valid_length)
59                 return QValidator::Intermediate;
60
61         if (no_bottom_)
62                 return QValidator::Acceptable;
63
64         return b_.inPixels(100) <= l.inPixels(100) ?
65                 QValidator::Acceptable : QValidator::Intermediate;
66 }
67
68
69 void LengthValidator::setBottom(Length const & b)
70 {
71         b_ = b;
72         no_bottom_ = false;
73 }
74
75
76 void LengthValidator::setBottom(GlueLength const & g)
77 {
78         g_ = g;
79         no_bottom_ = false;
80         glue_length_ = true;
81 }
82
83
84 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
85 {
86         LengthValidator * v = new LengthValidator(ed);
87         v->setBottom(Length());
88         return v;
89 }
90
91
92 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
93 {
94         LengthValidator * v = new LengthValidator(ed);
95         v->setBottom(GlueLength());
96         return v;
97 }
98
99
100 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const autotext)
101         : LengthValidator(parent),
102           autotext_(autotext)
103 {}
104
105
106 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
107 {
108         if (qtext == autotext_)
109                 return QValidator::Acceptable;
110         return LengthValidator::validate(qtext, dummy);
111 }
112
113
114 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const autotext)
115 {
116         LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
117         v->setBottom(Length());
118         return v;
119 }
120
121
122 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const autotext)
123         : QDoubleValidator(parent),
124           autotext_(autotext)
125 {}
126
127
128 DoubleAutoValidator::DoubleAutoValidator(double bottom,
129                 double top, int decimals, QObject * parent)
130         : QDoubleValidator(bottom, top, decimals, parent)
131 {}
132
133
134 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
135         if (input == autotext_)
136                 return QValidator::Acceptable;
137         return QDoubleValidator::validate(input, pos);
138 }
139
140
141 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
142         : QValidator(parent)
143 {}
144
145
146 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
147 {
148         qtext.remove(QRegExp("[\\n\\r]"));
149         return QValidator::Acceptable;
150 }
151
152
153 PathValidator::PathValidator(bool acceptable_if_empty,
154                              QWidget * parent)
155         : QValidator(parent),
156           acceptable_if_empty_(acceptable_if_empty),
157           latex_doc_(false),
158           tex_allows_spaces_(false)
159 {}
160
161
162 static docstring const printable_list(docstring const & invalid_chars)
163 {
164         docstring s;
165         docstring::const_iterator const begin = invalid_chars.begin();
166         docstring::const_iterator const end = invalid_chars.end();
167         docstring::const_iterator it = begin;
168
169         for (; it != end; ++it) {
170                 if (it != begin)
171                         s += ", ";
172                 if (*it == ' ')
173                         s += _("space");
174                 else
175                         s += *it;
176         }
177
178         return s;
179 }
180
181
182 QValidator::State PathValidator::validate(QString & qtext, int &) const
183 {
184         if (!latex_doc_)
185                 return QValidator::Acceptable;
186
187         docstring const text = support::trim(qstring_to_ucs4(qtext));
188         if (text.empty())
189                 return acceptable_if_empty_ ?
190                         QValidator::Acceptable : QValidator::Intermediate;
191
192         docstring invalid_chars = from_ascii("#$%{}()[]\"^");
193         if (!tex_allows_spaces_)
194                 invalid_chars += ' ';
195
196         if (text.find_first_of(invalid_chars) != docstring::npos) {
197
198                 static int counter = 0;
199                 if (counter == 0) {
200                         Alert::error(_("Invalid filename"),
201                                      _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
202                                          printable_list(invalid_chars));
203                 }
204                 ++counter;
205                 return QValidator::Intermediate;
206         }
207
208         return QValidator::Acceptable;
209 }
210
211
212 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
213 {
214         latex_doc_ = type == LATEX;
215         tex_allows_spaces_ = lyxrc.tex_allows_spaces;
216 }
217
218
219 PathValidator * getPathValidator(QLineEdit * ed)
220 {
221         if (!ed)
222                 return 0;
223         QValidator * validator = const_cast<QValidator *>(ed->validator());
224         if (!validator)
225                 return 0;
226         return dynamic_cast<PathValidator *>(validator);
227 }
228
229 } // namespace frontend
230 } // namespace lyx
231
232 #include "moc_Validator.cpp"