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