]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Validator.cpp
Introducing TextClassPtr.h to minimize header dependencies.
[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 lyx::support::isStrDbl;
30 using std::string;
31
32
33 namespace lyx {
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() || 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 DoubleAutoValidator::DoubleAutoValidator(double bottom,
116         double top, int decimals, QObject * parent) :
117         QDoubleValidator(bottom, top, decimals, parent) {}
118
119
120 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
121         string const text = fromqstr(input);
122         if (text == "auto")
123                 return QValidator::Acceptable;
124         return QDoubleValidator::validate(input, pos);
125 }
126
127
128 PathValidator::PathValidator(bool acceptable_if_empty,
129                              QWidget * parent)
130         : QValidator(parent),
131           acceptable_if_empty_(acceptable_if_empty),
132           latex_doc_(false),
133           tex_allows_spaces_(false)
134 {}
135
136
137 static docstring const printable_list(docstring const & invalid_chars)
138 {
139         docstring s;
140         docstring::const_iterator const begin = invalid_chars.begin();
141         docstring::const_iterator const end = invalid_chars.end();
142         docstring::const_iterator it = begin;
143
144         for (; it != end; ++it) {
145                 if (it != begin)
146                         s += ", ";
147                 if (*it == ' ')
148                         s += _("space");
149                 else
150                         s += *it;
151         }
152
153         return s;
154 }
155
156
157 QValidator::State PathValidator::validate(QString & qtext, int &) const
158 {
159         if (!latex_doc_)
160                 return QValidator::Acceptable;
161
162         docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
163         if (text.empty())
164                 return  acceptable_if_empty_ ?
165                         QValidator::Acceptable : QValidator::Intermediate;
166
167         docstring invalid_chars = from_ascii("#$%{}()[]\"^");
168         if (!tex_allows_spaces_)
169                 invalid_chars += ' ';
170
171         if (text.find_first_of(invalid_chars) != docstring::npos) {
172
173                 static int counter = 0;
174                 if (counter == 0) {
175                         lyx::frontend::Alert::error(_("Invalid filename"),
176                                      _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
177                                          printable_list(invalid_chars));
178                 }
179                 ++counter;
180                 return QValidator::Intermediate;
181         }
182
183         return QValidator::Acceptable;
184 }
185
186
187 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
188                                LyXRC const & lyxrc)
189 {
190         latex_doc_ = type == frontend::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 lyx
206
207 #include "Validator_moc.cpp"