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