]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/validators.C
Fix unreported bug related to 3246 by Richard Heck:
[lyx.git] / src / frontends / qt4 / validators.C
1 /**
2  * \file validators.C
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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11
12 #include <config.h>
13
14 #include "validators.h"
15 #include "qt_helpers.h"
16
17 #include "gettext.h"
18 #include "lyxrc.h"
19
20 #include "frontends/Alert.h"
21
22 #include "frontends/controllers/Dialog.h"
23
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
26 #include "support/std_ostream.h"
27
28 #include <QLineEdit>
29 #include <QWidget>
30
31 #include <sstream>
32
33 using lyx::support::isStrDbl;
34 using lyx::docstring;
35
36 using std::string;
37
38
39 namespace lyx {
40
41 LengthValidator::LengthValidator(QWidget * parent)
42         : QValidator(parent),
43           no_bottom_(true), glue_length_(false)
44 {}
45
46
47 QValidator::State LengthValidator::validate(QString & qtext, int &) const
48 {
49         string const text = fromqstr(qtext);
50         if (text.empty() || isStrDbl(text))
51                 return QValidator::Acceptable;
52
53         if (glue_length_) {
54                 LyXGlueLength gl;
55                 return (isValidGlueLength(text, &gl)) ?
56                         QValidator::Acceptable : QValidator::Intermediate;
57                 }
58
59         LyXLength l;
60         bool const valid_length = isValidLength(text, &l);
61         if (!valid_length)
62                 return QValidator::Intermediate;
63
64         if (no_bottom_)
65                 return QValidator::Acceptable;
66
67         return b_.inPixels(100) <= l.inPixels(100) ?
68                 QValidator::Acceptable : QValidator::Intermediate;
69 }
70
71
72 void LengthValidator::setBottom(LyXLength const & b)
73 {
74         b_ = b;
75         no_bottom_ = false;
76 }
77
78
79 void LengthValidator::setBottom(LyXGlueLength const & g)
80 {
81         g_ = g;
82         no_bottom_ = false;
83         glue_length_ = true;
84 }
85
86
87 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
88 {
89         LengthValidator * v = new LengthValidator(ed);
90         v->setBottom(LyXLength());
91         return v;
92 }
93
94
95 PathValidator::PathValidator(bool acceptable_if_empty,
96                              QWidget * parent)
97         : QValidator(parent),
98           acceptable_if_empty_(acceptable_if_empty),
99           latex_doc_(false),
100           tex_allows_spaces_(false)
101 {}
102
103
104 namespace {
105
106 docstring const printable_list(docstring const & invalid_chars)
107 {
108         docstring s;
109         docstring::const_iterator const begin = invalid_chars.begin();
110         docstring::const_iterator const end = invalid_chars.end();
111         docstring::const_iterator it = begin;
112
113         for (; it != end; ++it) {
114                 if (it != begin)
115                         s += ", ";
116                 if (*it == ' ')
117                         s += _("space");
118                 else
119                         s += *it;
120         }
121
122         return s;
123 }
124
125 } // namespace anon
126
127
128 QValidator::State PathValidator::validate(QString & qtext, int &) const
129 {
130         if (!latex_doc_)
131                 return QValidator::Acceptable;
132
133         docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
134         if (text.empty())
135                 return  acceptable_if_empty_ ?
136                         QValidator::Acceptable : QValidator::Intermediate;
137
138         docstring invalid_chars = from_ascii("#$%{}()[]\"^");
139         if (!tex_allows_spaces_)
140                 invalid_chars += ' ';
141
142         if (text.find_first_of(invalid_chars) != docstring::npos) {
143
144                 static int counter = 0;
145                 if (counter == 0) {
146                         lyx::frontend::Alert::error(_("Invalid filename"),
147                                      _("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
148                                          printable_list(invalid_chars));
149                 }
150                 ++counter;
151                 return QValidator::Intermediate;
152         }
153
154         return QValidator::Acceptable;
155 }
156
157
158 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
159                                LyXRC const & lyxrc)
160 {
161         latex_doc_ = type == lyx::frontend::Kernel::LATEX;
162         tex_allows_spaces_ = lyxrc.tex_allows_spaces;
163 }
164
165
166 PathValidator * getPathValidator(QLineEdit * ed)
167 {
168         if (!ed)
169                 return 0;
170         QValidator * validator = const_cast<QValidator *>(ed->validator());
171         if (!validator)
172                 return 0;
173         return dynamic_cast<PathValidator *>(validator);
174 }
175
176 } // namespace lyx
177
178 #include "validators_moc.cpp"
179