]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Validator.h
Whitespace cleanup
[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
30 #include <QValidator>
31
32 class QWidget;
33 class QLineEdit;
34
35
36 namespace lyx {
37
38 /** A class to ascertain whether the data passed to the @c validate()
39  *  member function can be interpretted as a GlueLength.
40  */
41 class LengthValidator : public QValidator
42 {
43         Q_OBJECT
44 public:
45         /// Define a validator for widget @c parent.
46         LengthValidator(QWidget * parent);
47
48         /** @returns QValidator::Acceptable if @c data is a GlueLength.
49          *  If not, returns QValidator::Intermediate.
50          */
51         QValidator::State validate(QString & data, int &) const;
52
53         /** @name Bottom
54          *  Set and retrieve the minimum allowed Length value.
55          */
56         //@{
57         void setBottom(Length const &);
58         void setBottom(GlueLength const &);
59         Length bottom() const { return b_; }
60         //@}
61
62 private:
63 #if defined(Q_DISABLE_COPY)
64         LengthValidator( const LengthValidator & );
65         LengthValidator& operator=( const LengthValidator & );
66 #endif
67
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 //FIXME This should be generalized to take "text" as part of the
79 //constructor and so to set what text we check for, rather than
80 //hard-coding it as "auto". But see qt_helpers.h for reasons this
81 //is not so trivial and an idea about how to do it. (RGH)
82 /** A class to ascertain whether the data passed to the @c validate()
83  *  member function can be interpretted as a GlueLength or is "auto".
84  */
85 class LengthAutoValidator : public LengthValidator
86 {
87         Q_OBJECT
88         public:
89         /// Define a validator for widget @c parent.
90                 LengthAutoValidator(QWidget * parent);
91
92         /** @returns QValidator::Acceptable if @c data is a GlueLength
93                 * or is "auto". If not, returns QValidator::Intermediate.
94          */
95                 QValidator::State validate(QString & data, int &) const;
96 };
97
98 /// @returns a new @c LengthAutoValidator that does not accept negative lengths.
99 LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit *);
100
101 //FIXME As above, this should really take a text argument.
102 /**
103  * A class to determine whether the passed is a double
104  * or is "auto".
105  *
106  */
107 class DoubleAutoValidator : public QDoubleValidator {
108         Q_OBJECT
109         public:
110                 DoubleAutoValidator(QWidget * parent);
111                 DoubleAutoValidator(double bottom, double top, int decimals,
112                         QObject * parent);
113                 QValidator::State validate(QString & input, int & pos) const;
114 };
115
116 // Forward declarations
117 class LyXRC;
118
119 namespace frontend { class KernelDocType; }
120
121
122 /** A class to ascertain whether the data passed to the @c validate()
123  *  member function is a valid file path.
124  *  The test is active only when the path is to be stored in a LaTeX
125  *  file, LaTeX being quite picky about legal names.
126  */
127 class PathValidator : public QValidator
128 {
129         Q_OBJECT
130 public:
131         /** Define a validator for widget @c parent.
132          *  If @c acceptable_if_empty is @c true then an empty path
133          *  is regarded as acceptable.
134          */
135         PathValidator(bool acceptable_if_empty, QWidget * parent);
136
137         /** @returns QValidator::Acceptable if @c data is a valid path.
138          *  If not, returns QValidator::Intermediate.
139          */
140         QValidator::State validate(QString &, int &) const;
141
142         /** Define what checks that @c validate() will perform.
143          *  @param doc_type checks are activated only for @c LATEX docs.
144          *  @param lyxrc contains a @c tex_allows_spaces member that
145          *  is used to define what is legal.
146          */
147         void setChecker(frontend::KernelDocType const & doc_type,
148                         LyXRC const & lyxrc);
149
150 private:
151 #if defined(Q_DISABLE_COPY)
152         PathValidator(const PathValidator &);
153         PathValidator & operator=(const PathValidator &);
154 #endif
155
156         bool acceptable_if_empty_;
157         bool latex_doc_;
158         bool tex_allows_spaces_;
159 };
160
161
162 /// @returns the PathValidator attached to the widget, or 0.
163 PathValidator * getPathValidator(QLineEdit *);
164
165 } // namespace lyx
166
167 # endif // NOT VALIDATOR_H