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