]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Validator.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 <QWidget>
28
29 using namespace std;
30
31 namespace lyx {
32 namespace frontend {
33
34 LengthValidator::LengthValidator(QWidget * parent)
35         : QValidator(parent),
36           no_bottom_(true), glue_length_(false)
37 {}
38
39
40 QValidator::State LengthValidator::validate(QString & qtext, int &) const
41 {
42         string const text = fromqstr(qtext);
43         if (text.empty() || support::isStrDbl(text))
44                 return QValidator::Acceptable;
45
46         if (glue_length_) {
47                 GlueLength gl;
48                 return (isValidGlueLength(text, &gl)) ?
49                         QValidator::Acceptable : QValidator::Intermediate;
50         }
51
52         Length l;
53         bool const valid_length = isValidLength(text, &l);
54         if (!valid_length)
55                 return QValidator::Intermediate;
56
57         if (no_bottom_)
58                 return QValidator::Acceptable;
59
60         return b_.inPixels(100) <= l.inPixels(100) ?
61                 QValidator::Acceptable : QValidator::Intermediate;
62 }
63
64
65 void LengthValidator::setBottom(Length const & b)
66 {
67         b_ = b;
68         no_bottom_ = false;
69 }
70
71
72 void LengthValidator::setBottom(GlueLength const & g)
73 {
74         g_ = g;
75         no_bottom_ = false;
76         glue_length_ = true;
77 }
78
79
80 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
81 {
82         LengthValidator * v = new LengthValidator(ed);
83         v->setBottom(Length());
84         return v;
85 }
86
87
88 LengthAutoValidator::LengthAutoValidator(QWidget * parent)
89         : LengthValidator(parent)
90 {}
91
92
93 QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
94 {
95         string const text = fromqstr(qtext);
96         if (text == "auto")
97                 return QValidator::Acceptable;
98         return LengthValidator::validate(qtext, dummy);
99 }
100
101
102 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
103 {
104         LengthAutoValidator * v = new LengthAutoValidator(ed);
105         v->setBottom(Length());
106         return v;
107 }
108
109
110 DoubleAutoValidator::DoubleAutoValidator(QWidget * parent)
111         : QDoubleValidator(parent)
112 {}
113
114
115 DoubleAutoValidator::DoubleAutoValidator(double bottom,
116                 double top, int decimals, QObject * parent)
117         : QDoubleValidator(bottom, top, decimals, parent)
118 {}
119
120
121 QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
122         if (input == "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 = 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                         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(KernelDocType const & type, LyXRC const & lyxrc)
188 {
189         latex_doc_ = type == LATEX;
190         tex_allows_spaces_ = lyxrc.tex_allows_spaces;
191 }
192
193
194 PathValidator * getPathValidator(QLineEdit * ed)
195 {
196         if (!ed)
197                 return 0;
198         QValidator * validator = const_cast<QValidator *>(ed->validator());
199         if (!validator)
200                 return 0;
201         return dynamic_cast<PathValidator *>(validator);
202 }
203
204 } // namespace frontend
205 } // namespace lyx
206
207 #include "Validator_moc.cpp"