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