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