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