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