]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/validators.C
This commit saves the need to check for lyx::use_gui in a number of places.
[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 LengthValidator::LengthValidator(QWidget * parent)
40         : QValidator(parent),
41           no_bottom_(true), glue_length_(false)
42 {}
43
44
45 QValidator::State LengthValidator::validate(QString & qtext, int &) const
46 {
47         string const text = fromqstr(qtext);
48         if (text.empty() || isStrDbl(text))
49                 return QValidator::Acceptable;
50
51         if (glue_length_) {
52                 LyXGlueLength gl;
53                 return (isValidGlueLength(text, &gl)) ?
54                         QValidator::Acceptable : QValidator::Intermediate;
55                 }
56
57         LyXLength l;
58         bool const valid_length = isValidLength(text, &l);
59         if (!valid_length)
60                 return QValidator::Intermediate;
61
62         if (no_bottom_)
63                 return QValidator::Acceptable;
64
65         return b_.inPixels(100) <= l.inPixels(100) ?
66                 QValidator::Acceptable : QValidator::Intermediate;
67 }
68
69
70 void LengthValidator::setBottom(LyXLength const & b)
71 {
72         b_ = b;
73         no_bottom_ = false;
74 }
75
76
77 void LengthValidator::setBottom(LyXGlueLength const & g)
78 {
79         g_ = g;
80         no_bottom_ = false;
81         glue_length_ = true;
82 }
83
84
85 LengthValidator * unsignedLengthValidator(QLineEdit * ed)
86 {
87         LengthValidator * v = new LengthValidator(ed);
88         v->setBottom(LyXLength());
89         return v;
90 }
91
92
93 PathValidator::PathValidator(bool acceptable_if_empty,
94                              QWidget * parent)
95         : QValidator(parent),
96           acceptable_if_empty_(acceptable_if_empty),
97           latex_doc_(false),
98           tex_allows_spaces_(false)
99 {}
100
101
102 namespace {
103
104 docstring const printable_list(docstring const & invalid_chars)
105 {
106         docstring s;
107         docstring::const_iterator const begin = invalid_chars.begin();
108         docstring::const_iterator const end = invalid_chars.end();
109         docstring::const_iterator it = begin;
110
111         for (; it != end; ++it) {
112                 if (it != begin)
113                         s += lyx::from_ascii(", ");
114                 if (*it == lyx::char_type(' '))
115                         s += _("space");
116                 else
117                         s += *it;
118         }
119
120         return s;
121 }
122
123 } // namespace anon
124
125
126 QValidator::State PathValidator::validate(QString & qtext, int &) const
127 {
128         if (!latex_doc_)
129                 return QValidator::Acceptable;
130
131         docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
132         if (text.empty())
133                 return  acceptable_if_empty_ ?
134                         QValidator::Acceptable : QValidator::Intermediate;
135
136         docstring invalid_chars = lyx::from_ascii("#$%{}()[]\"^");
137         if (!tex_allows_spaces_)
138                 invalid_chars += ' ';
139
140         if (text.find_first_of(invalid_chars) != docstring::npos) {
141
142                 static int counter = 0;
143                 if (counter == 0) {
144                         lyx::frontend::Alert::error(_("Invalid filename"),
145                                      _("LyX does not provide LateX support for file names containing any of these characters:\n") +
146                                          printable_list(invalid_chars));
147                 }
148                 ++counter;
149                 return QValidator::Intermediate;
150         }
151
152         return QValidator::Acceptable;
153 }
154
155
156 void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
157                                LyXRC const & lyxrc)
158 {
159         latex_doc_ = type == lyx::frontend::Kernel::LATEX;
160         tex_allows_spaces_ = lyxrc.tex_allows_spaces;
161 }
162
163
164 PathValidator * getPathValidator(QLineEdit * ed)
165 {
166         if (!ed)
167                 return 0;
168         QValidator * validator = const_cast<QValidator *>(ed->validator());
169         if (!validator)
170                 return 0;
171         return dynamic_cast<PathValidator *>(validator);
172 }
173
174 #include "validators_moc.cpp"