]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Validator.cpp
pimpl not needed here
[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 #include "support/std_ostream.h"
26
27 #include <QLineEdit>
28 #include <QWidget>
29
30 #include <sstream>
31
32 using lyx::support::isStrDbl;
33 using std::string;
34
35
36 namespace lyx {
37
38 LengthValidator::LengthValidator(QWidget * parent)
39         : QValidator(parent),
40           no_bottom_(true), glue_length_(false)
41 {}
42
43
44 QValidator::State LengthValidator::validate(QString & qtext, int &) const
45 {
46         string const text = fromqstr(qtext);
47         if (text.empty() || isStrDbl(text))
48                 return QValidator::Acceptable;
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 LengthAutoValidator::LengthAutoValidator(QWidget * parent)
93         : LengthValidator(parent)
94 {}
95
96
97 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
98 {
99         string const text = fromqstr(qtext);
100         if (text == "auto")
101                 return QValidator::Acceptable;
102         return LengthValidator::validate(qtext, dummy);
103 }
104
105
106 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
107 {
108         LengthAutoValidator * v = new LengthAutoValidator(ed);
109         v->setBottom(Length());
110         return v;
111 }
112
113
114 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent) :
115         QDoubleValidator(parent) {}
116
117
118 DoubleAutoValidator::DoubleAutoValidator(double bottom,
119         double top, int decimals, QObject * parent) :
120         QDoubleValidator(bottom, top, decimals, parent) {}
121
122
123 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
124         string const text = fromqstr(input);
125         if (text == "auto")
126                 return QValidator::Acceptable;
127         return QDoubleValidator::validate(input, pos);
128 }
129
130
131 PathValidator::PathValidator(bool acceptable_if_empty,
132                              QWidget * parent)
133         : QValidator(parent),
134           acceptable_if_empty_(acceptable_if_empty),
135           latex_doc_(false),
136           tex_allows_spaces_(false)
137 {}
138
139
140 static docstring const printable_list(docstring const & invalid_chars)
141 {
142         docstring s;
143         docstring::const_iterator const begin = invalid_chars.begin();
144         docstring::const_iterator const end = invalid_chars.end();
145         docstring::const_iterator it = begin;
146
147         for (; it != end; ++it) {
148                 if (it != begin)
149                         s += ", ";
150                 if (*it == ' ')
151                         s += _("space");
152                 else
153                         s += *it;
154         }
155
156         return s;
157 }
158
159
160 QValidator::State PathValidator::validate(QString & qtext, int &) const
161 {
162         if (!latex_doc_)
163                 return QValidator::Acceptable;
164
165         docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
166         if (text.empty())
167                 return  acceptable_if_empty_ ?
168                         QValidator::Acceptable : QValidator::Intermediate;
169
170         docstring invalid_chars = from_ascii("#$%{}()[]\"^");
171         if (!tex_allows_spaces_)
172                 invalid_chars += ' ';
173
174         if (text.find_first_of(invalid_chars) != docstring::npos) {
175
176                 static int counter = 0;
177                 if (counter == 0) {
178                         lyx::frontend::Alert::error(_("Invalid filename"),
179                                      _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
180                                          printable_list(invalid_chars));
181                 }
182                 ++counter;
183                 return QValidator::Intermediate;
184         }
185
186         return QValidator::Acceptable;
187 }
188
189
190 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
191                                LyXRC const & lyxrc)
192 {
193         latex_doc_ = type == frontend::LATEX;
194         tex_allows_spaces_ = lyxrc.tex_allows_spaces;
195 }
196
197
198 PathValidator * getPathValidator(QLineEdit * ed)
199 {
200         if (!ed)
201                 return 0;
202         QValidator * validator = const_cast<QValidator *>(ed->validator());
203         if (!validator)
204                 return 0;
205         return dynamic_cast<PathValidator *>(validator);
206 }
207
208 } // namespace lyx
209
210 #include "Validator_moc.cpp"