]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/Validator.h
Make string-widget combination more l7n friendly
[lyx.git] / src / frontends / qt / Validator.h
1 // -*- C++ -*-
2 /**
3  * \file Validator.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  * \author Richard Kimberly Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Validators are used to decide upon the legality of some input action.
13  * For example, a "line edit" widget might be used to input a "glue length".
14  * The correct syntax for such a length is "2em + 0.5em". The LengthValidator
15  * below will report whether the input text conforms to this syntax.
16  *
17  * This information is used in LyX primarily to give the user some
18  * feedback on the validity of the input data using the "checked_widget"
19  * concept. For example, if the data is invalid then the label of
20  * a "line edit" widget is changed in colour and the dialog's "Ok"
21  * and "Apply" buttons are disabled. See checked_widgets.[Ch] for
22  * further details.
23  */
24
25 #ifndef VALIDATOR_H
26 #define VALIDATOR_H
27
28 #include "support/Length.h"
29
30 #include <QValidator>
31
32 class QWidget;
33 class QLineEdit;
34
35 namespace lyx {
36
37 class LyXRC;
38
39 namespace frontend {
40
41 enum class KernelDocType : int;
42
43 /** A class to ascertain whether the data passed to the @c validate()
44  *  member function can be interpreted as a GlueLength.
45  */
46 class LengthValidator : public QValidator
47 {
48         Q_OBJECT
49 public:
50         /// Define a validator for widget @c parent.
51         LengthValidator(QWidget * parent, bool const accept_empty = true);
52
53         /** @returns QValidator::Acceptable if @c data is a GlueLength.
54          *  If not, returns QValidator::Intermediate.
55          */
56         QValidator::State validate(QString & data, int &) const override;
57
58         /** @name Bottom
59          *  Set and retrieve the minimum allowed Length value.
60          */
61         //@{
62         void setBottom(Length const &);
63         void setBottom(GlueLength const &);
64         Length bottom() const { return bottom_; }
65         void setUnsigned(bool const u) { unsigned_ = u; }
66         void setPositive(bool const u) { positive_ = u; }
67         //@}
68
69 private:
70         Length bottom_;
71         GlueLength glue_bottom_;
72         bool no_bottom_ = true;
73         bool glue_length_ = false;
74         bool unsigned_ = false;
75         bool positive_ = false;
76         bool acceptable_if_empty_ = false;
77 };
78
79
80 /// @returns a new @c LengthValidator that does not accept negative lengths.
81 LengthValidator * unsignedLengthValidator(QLineEdit *);
82
83
84 /** @returns a new @c LengthValidator that does not accept negative lengths.
85  *  but glue lengths.
86  */
87 LengthValidator * unsignedGlueLengthValidator(QLineEdit *);
88
89
90 /** A class to ascertain whether the data passed to the @c validate()
91  *  member function can be interpreted as a GlueLength or is @param autotext.
92  */
93 class LengthAutoValidator : public LengthValidator
94 {
95         Q_OBJECT
96 public:
97         /// Define a validator for widget @c parent.
98         LengthAutoValidator(QWidget * parent, QString const & autotext);
99
100         /** @returns QValidator::Acceptable if @c data is a GlueLength
101                 * or is "auto". If not, returns QValidator::Intermediate.
102          */
103         QValidator::State validate(QString & data, int &) const override;
104
105 private:
106         QString autotext_;
107 };
108
109 /// @returns a new @c LengthAutoValidator that does not accept negative lengths.
110 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit *, QString const & autotext);
111
112
113 /// @returns a new @c LengthAutoValidator that does not accept negative lengths.
114 LengthAutoValidator * positiveLengthAutoValidator(QLineEdit *, QString const & autotext);
115
116
117 /**
118  * A class to determine whether the passed is a double
119  * or is @param autotext.
120  *
121  */
122 class DoubleAutoValidator : public QDoubleValidator
123 {
124         Q_OBJECT
125 public:
126         DoubleAutoValidator(QWidget * parent, QString const & autotext);
127         DoubleAutoValidator(double bottom, double top, int decimals,
128                 QObject * parent);
129         QValidator::State validate(QString & input, int & pos) const override;
130
131 private:
132         QString autotext_;
133 };
134
135
136 // A class to ascertain that no newline characters are passed.
137 class NoNewLineValidator : public QValidator
138 {
139         Q_OBJECT
140 public:
141         // Define a validator.
142         NoNewLineValidator(QWidget *);
143         // Remove newline characters from input.
144         QValidator::State validate(QString &, int &) const override;
145 };
146
147
148 /** A class to ascertain whether the data passed to the @c validate()
149  *  member function is a valid file path.
150  *  The test is active only when the path is to be stored in a LaTeX
151  *  file, LaTeX being quite picky about legal names.
152  */
153 class PathValidator : public QValidator
154 {
155         Q_OBJECT
156 public:
157         /** Define a validator for widget @c parent.
158          *  If @c acceptable_if_empty is @c true then an empty path
159          *  is regarded as acceptable.
160          */
161         PathValidator(bool acceptable_if_empty, QWidget * parent);
162
163         /** @returns QValidator::Acceptable if @c data is a valid path.
164          *  If not, returns QValidator::Intermediate.
165          */
166         QValidator::State validate(QString &, int &) const override;
167
168         /** Define what checks that @c validate() will perform.
169          *  @param doc_type checks are activated only for @c LATEX docs.
170          *  @param lyxrc contains a @c tex_allows_spaces member that
171          *  is used to define what is legal.
172          */
173         void setChecker(KernelDocType const & doc_type, LyXRC const & lyxrc);
174
175 private:
176         bool acceptable_if_empty_;
177         bool latex_doc_;
178         bool tex_allows_spaces_;
179 };
180
181
182 /// @returns the PathValidator attached to the widget, or 0.
183 PathValidator * getPathValidator(QLineEdit *);
184
185 } // namespace frontend
186 } // namespace lyx
187
188 # endif // NOT VALIDATOR_H