]> git.lyx.org Git - features.git/blob - src/frontends/qt/Validator.cpp
Disable zoom slider when there is no buffer open (#12185)
[features.git] / src / frontends / qt / 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 Kimberly 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
17 #include "Dialog.h"
18 #include "LyXRC.h"
19 #include "qt_helpers.h"
20
21 #include "frontends/alert.h"
22
23 #include "support/docstring.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26
27 #include <QLineEdit>
28 #include <QLocale>
29 #include <QWidget>
30
31 using namespace std;
32
33 namespace lyx {
34 namespace frontend {
35
36 LengthValidator::LengthValidator(QWidget * parent)
37         : QValidator(parent)
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 (ok && positive_ && d <=0)
60                 return QValidator::Invalid;
61
62         if (qtext.isEmpty() || (ok && !dp))
63                 return QValidator::Acceptable;
64
65         string const text = fromqstr(qtext);
66
67         if (glue_length_) {
68                 GlueLength gl;
69                 if (unsigned_ && gl.len().value() < 0)
70                         return QValidator::Invalid;
71                 return (isValidGlueLength(text, &gl)) ?
72                         QValidator::Acceptable : QValidator::Intermediate;
73         }
74
75         Length l;
76         bool const valid_length = isValidLength(text, &l);
77         if (!valid_length)
78                 return QValidator::Intermediate;
79
80         if (no_bottom_)
81                 return QValidator::Acceptable;
82
83         if (unsigned_ && l.value() < 0)
84                 return QValidator::Invalid;
85
86         if (positive_ && l.value() <= 0)
87                 return QValidator::Invalid;
88
89         return bottom_.inPixels(100) <= l.inPixels(100) ?
90                 QValidator::Acceptable : QValidator::Intermediate;
91 }
92
93
94 void LengthValidator::setBottom(Length const & b)
95 {
96         bottom_ = b;
97         no_bottom_ = false;
98 }
99
100
101 void LengthValidator::setBottom(GlueLength const & g)
102 {
103         glue_bottom_ = g;
104         no_bottom_ = false;
105         glue_length_ = true;
106 }
107
108
109 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
110 {
111         LengthValidator * v = new LengthValidator(ed);
112         v->setBottom(Length());
113         v->setUnsigned(true);
114         return v;
115 }
116
117
118 LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
119 {
120         LengthValidator * v = new LengthValidator(ed);
121         v->setBottom(GlueLength());
122         v->setUnsigned(true);
123         return v;
124 }
125
126
127 LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
128         : LengthValidator(parent),
129           autotext_(autotext)
130 {}
131
132
133 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
134 {
135         if (qtext == autotext_)
136                 return QValidator::Acceptable;
137         return LengthValidator::validate(qtext, dummy);
138 }
139
140
141 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
142 {
143         LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
144         v->setBottom(Length());
145         v->setUnsigned(true);
146         return v;
147 }
148
149
150 LengthAutoValidator * positiveLengthAutoValidator(QLineEdit * ed, QString const & autotext)
151 {
152         LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
153         v->setBottom(Length());
154         v->setPositive(true);
155         return v;
156 }
157
158
159 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
160         : QDoubleValidator(parent),
161           autotext_(autotext)
162 {}
163
164
165 DoubleAutoValidator::DoubleAutoValidator(double bottom,
166                 double top, int decimals, QObject * parent)
167         : QDoubleValidator(bottom, top, decimals, parent)
168 {}
169
170
171 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
172         if (input == autotext_)
173                 return QValidator::Acceptable;
174         return QDoubleValidator::validate(input, pos);
175 }
176
177
178 NoNewLineValidator::NoNewLineValidator(QWidget * parent)
179         : QValidator(parent)
180 {}
181
182
183 QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
184 {
185         qtext.remove(QRegExp("[\\n\\r]"));
186         return QValidator::Acceptable;
187 }
188
189
190 PathValidator::PathValidator(bool acceptable_if_empty,
191                              QWidget * parent)
192         : QValidator(parent),
193           acceptable_if_empty_(acceptable_if_empty),
194           latex_doc_(false),
195           tex_allows_spaces_(false)
196 {}
197
198
199 static docstring const printable_list(docstring const & invalid_chars)
200 {
201         docstring s;
202         docstring::const_iterator const begin = invalid_chars.begin();
203         docstring::const_iterator const end = invalid_chars.end();
204         docstring::const_iterator it = begin;
205
206         for (; it != end; ++it) {
207                 if (it != begin)
208                         s += ", ";
209                 if (*it == ' ')
210                         s += _("space");
211                 else
212                         s += *it;
213         }
214
215         return s;
216 }
217
218
219 QValidator::State PathValidator::validate(QString & qtext, int &) const
220 {
221         if (!latex_doc_)
222                 return QValidator::Acceptable;
223
224         docstring const text = support::trim(qstring_to_ucs4(qtext));
225         if (text.empty())
226                 return acceptable_if_empty_ ?
227                         QValidator::Acceptable : QValidator::Intermediate;
228
229         docstring invalid_chars = from_ascii("#$%{}()[]\"^");
230         if (!tex_allows_spaces_)
231                 invalid_chars += ' ';
232
233         if (text.find_first_of(invalid_chars) != docstring::npos) {
234
235                 static int counter = 0;
236                 if (counter == 0) {
237                         Alert::error(_("Invalid filename"),
238                                      _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
239                                          printable_list(invalid_chars));
240                 }
241                 ++counter;
242                 return QValidator::Intermediate;
243         }
244
245         return QValidator::Acceptable;
246 }
247
248
249 void PathValidator::setChecker(KernelDocType const & type, LyXRC const & rc)
250 {
251         latex_doc_ = type == KernelDocType::LaTeX;
252         tex_allows_spaces_ = rc.tex_allows_spaces;
253 }
254
255
256 PathValidator * getPathValidator(QLineEdit * ed)
257 {
258         if (!ed)
259                 return nullptr;
260         QValidator * validator = const_cast<QValidator *>(ed->validator());
261         if (!validator)
262                 return nullptr;
263         return dynamic_cast<PathValidator *>(validator);
264 }
265
266 } // namespace frontend
267 } // namespace lyx
268
269 #include "moc_Validator.cpp"