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