]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/Validator.h
Add Sam Crawley to credits.
[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 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 "Dialog.h" // KernelDocType
29
30 #include "support/Length.h"
31
32 #include <QValidator>
33
34 class QWidget;
35 class QLineEdit;
36
37
38 namespace lyx {
39
40 class LyXRC;
41
42 namespace frontend {
43
44 /** A class to ascertain whether the data passed to the @c validate()
45  *  member function can be interpreted as a GlueLength.
46  */
47 class LengthValidator : public QValidator
48 {
49         Q_OBJECT
50 public:
51         /// Define a validator for widget @c parent.
52         LengthValidator(QWidget * parent);
53
54         /** @returns QValidator::Acceptable if @c data is a GlueLength.
55          *  If not, returns QValidator::Intermediate.
56          */
57         QValidator::State validate(QString & data, int &) const override;
58
59         /** @name Bottom
60          *  Set and retrieve the minimum allowed Length value.
61          */
62         //@{
63         void setBottom(Length const &);
64         void setBottom(GlueLength const &);
65         Length bottom() const { return b_; }
66         void setUnsigned(bool const u) { unsigned_ = u; }
67         //@}
68
69 private:
70         Length b_;
71         GlueLength g_;
72         bool no_bottom_;
73         bool glue_length_;
74         bool unsigned_;
75 };
76
77
78 /// @returns a new @c LengthValidator that does not accept negative lengths.
79 LengthValidator * unsignedLengthValidator(QLineEdit *);
80
81
82 /** @returns a new @c LengthValidator that does not accept negative lengths.
83  *  but glue lengths.
84  */
85 LengthValidator * unsignedGlueLengthValidator(QLineEdit *);
86
87
88 /** A class to ascertain whether the data passed to the @c validate()
89  *  member function can be interpreted as a GlueLength or is @param autotext.
90  */
91 class LengthAutoValidator : public LengthValidator
92 {
93         Q_OBJECT
94 public:
95         /// Define a validator for widget @c parent.
96         LengthAutoValidator(QWidget * parent, QString const & autotext);
97
98         /** @returns QValidator::Acceptable if @c data is a GlueLength
99                 * or is "auto". If not, returns QValidator::Intermediate.
100          */
101         QValidator::State validate(QString & data, int &) const override;
102
103 private:
104         QString autotext_;
105 };
106
107 /// @returns a new @c LengthAutoValidator that does not accept negative lengths.
108 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit *, QString const & autotext);
109
110
111 /**
112  * A class to determine whether the passed is a double
113  * or is @param autotext.
114  *
115  */
116 class DoubleAutoValidator : public QDoubleValidator
117 {
118         Q_OBJECT
119 public:
120         DoubleAutoValidator(QWidget * parent, QString const & autotext);
121         DoubleAutoValidator(double bottom, double top, int decimals,
122                 QObject * parent);
123         QValidator::State validate(QString & input, int & pos) const override;
124
125 private:
126         QString autotext_;
127 };
128
129
130 // A class to ascertain that no newline characters are passed.
131 class NoNewLineValidator : public QValidator
132 {
133         Q_OBJECT
134 public:
135         // Define a validator.
136         NoNewLineValidator(QWidget *);
137         // Remove newline characters from input.
138         QValidator::State validate(QString &, int &) const override;
139 };
140
141
142 /** A class to ascertain whether the data passed to the @c validate()
143  *  member function is a valid file path.
144  *  The test is active only when the path is to be stored in a LaTeX
145  *  file, LaTeX being quite picky about legal names.
146  */
147 class PathValidator : public QValidator
148 {
149         Q_OBJECT
150 public:
151         /** Define a validator for widget @c parent.
152          *  If @c acceptable_if_empty is @c true then an empty path
153          *  is regarded as acceptable.
154          */
155         PathValidator(bool acceptable_if_empty, QWidget * parent);
156
157         /** @returns QValidator::Acceptable if @c data is a valid path.
158          *  If not, returns QValidator::Intermediate.
159          */
160         QValidator::State validate(QString &, int &) const override;
161
162         /** Define what checks that @c validate() will perform.
163          *  @param doc_type checks are activated only for @c LATEX docs.
164          *  @param lyxrc contains a @c tex_allows_spaces member that
165          *  is used to define what is legal.
166          */
167         void setChecker(KernelDocType const & doc_type, LyXRC const & lyxrc);
168
169 private:
170         bool acceptable_if_empty_;
171         bool latex_doc_;
172         bool tex_allows_spaces_;
173 };
174
175
176 /// @returns the PathValidator attached to the widget, or 0.
177 PathValidator * getPathValidator(QLineEdit *);
178
179 } // namespace frontend
180 } // namespace lyx
181
182 # endif // NOT VALIDATOR_H