1 | /* $Id: QIWidgetValidator.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIWidgetValidator class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef FEQT_INCLUDED_SRC_extensions_QIWidgetValidator_h
|
---|
29 | #define FEQT_INCLUDED_SRC_extensions_QIWidgetValidator_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /* Qt includes: */
|
---|
35 | #include <QMap>
|
---|
36 | #include <QValidator>
|
---|
37 |
|
---|
38 | /* GUI includes: */
|
---|
39 | #include "UILibraryDefs.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /** QObject sub-class,
|
---|
43 | * providing passed QObject with validation routine. */
|
---|
44 | class SHARED_LIBRARY_STUFF QObjectValidator : public QObject
|
---|
45 | {
|
---|
46 | Q_OBJECT;
|
---|
47 |
|
---|
48 | signals:
|
---|
49 |
|
---|
50 | /** Notifies listener(s) about validity changed to @a enmState. */
|
---|
51 | void sigValidityChange(QValidator::State enmState);
|
---|
52 |
|
---|
53 | public:
|
---|
54 |
|
---|
55 | /** Constructs object validator passing @a pParent to the base-class.
|
---|
56 | * @param pValidator Brings the validator passed on to the OObject
|
---|
57 | * children and used to perform validation itself. */
|
---|
58 | QObjectValidator(QValidator *pValidator, QObject *pParent = 0);
|
---|
59 |
|
---|
60 | /** Returns last validation state. */
|
---|
61 | QValidator::State state() const { return m_enmState; }
|
---|
62 |
|
---|
63 | public slots:
|
---|
64 |
|
---|
65 | /** Performs validation: */
|
---|
66 | void sltValidate(QString strInput = QString());
|
---|
67 |
|
---|
68 | private:
|
---|
69 |
|
---|
70 | /** Prepare routine. */
|
---|
71 | void prepare();
|
---|
72 |
|
---|
73 | /** Holds the validator reference. */
|
---|
74 | QValidator *m_pValidator;
|
---|
75 |
|
---|
76 | /** Holds the validation state. */
|
---|
77 | QValidator::State m_enmState;
|
---|
78 | };
|
---|
79 |
|
---|
80 |
|
---|
81 | /** QObject sub-class,
|
---|
82 | * which can group various QObjectValidator instances to operate on. */
|
---|
83 | class SHARED_LIBRARY_STUFF QObjectValidatorGroup : public QObject
|
---|
84 | {
|
---|
85 | Q_OBJECT;
|
---|
86 |
|
---|
87 | signals:
|
---|
88 |
|
---|
89 | /** Notifies listener(s) about validity changed to @a fValid. */
|
---|
90 | void sigValidityChange(bool fValid);
|
---|
91 |
|
---|
92 | public:
|
---|
93 |
|
---|
94 | /** Constructs validation group passing @a pParent to the base-class. */
|
---|
95 | QObjectValidatorGroup(QObject *pParent);
|
---|
96 |
|
---|
97 | /** Adds @a pObjectValidator.
|
---|
98 | * @note The ownership of @a pObjectValidator is transferred to the group,
|
---|
99 | * and it's the group's responsibility to delete it. */
|
---|
100 | void addObjectValidator(QObjectValidator *pObjectValidator);
|
---|
101 |
|
---|
102 | /** Returns last validation result. */
|
---|
103 | bool result() const { return m_fResult; }
|
---|
104 |
|
---|
105 | private slots:
|
---|
106 |
|
---|
107 | /** Performs validation for a passed @a enmState. */
|
---|
108 | void sltValidate(QValidator::State enmState);
|
---|
109 |
|
---|
110 | private:
|
---|
111 |
|
---|
112 | /** Converts QValidator::State to bool result. */
|
---|
113 | static bool toResult(QValidator::State enmState);
|
---|
114 |
|
---|
115 | /** Holds object-validators and their states. */
|
---|
116 | QMap<QObjectValidator*, bool> m_group;
|
---|
117 |
|
---|
118 | /** Holds validation result. */
|
---|
119 | bool m_fResult;
|
---|
120 | };
|
---|
121 |
|
---|
122 |
|
---|
123 | /** QValidator extension,
|
---|
124 | * for long number validations. */
|
---|
125 | class SHARED_LIBRARY_STUFF QIULongValidator : public QValidator
|
---|
126 | {
|
---|
127 | public:
|
---|
128 |
|
---|
129 | /** Constructs long validator passing @a pParent to the base-class. */
|
---|
130 | QIULongValidator(QObject *pParent)
|
---|
131 | : QValidator(pParent)
|
---|
132 | , m_uBottom(0), m_uTop(ULONG_MAX)
|
---|
133 | {}
|
---|
134 |
|
---|
135 | /** Constructs long validator passing @a pParent to the base-class.
|
---|
136 | * @param uMinimum Holds the minimum valid border.
|
---|
137 | * @param uMaximum Holds the maximum valid border. */
|
---|
138 | QIULongValidator(ulong uMinimum, ulong uMaximum,
|
---|
139 | QObject *pParent)
|
---|
140 | : QValidator(pParent)
|
---|
141 | , m_uBottom(uMinimum), m_uTop(uMaximum)
|
---|
142 | {}
|
---|
143 |
|
---|
144 | /** Destructs long validator. */
|
---|
145 | virtual ~QIULongValidator() {}
|
---|
146 |
|
---|
147 | /** Performs validation for @a strInput at @a iPosition. */
|
---|
148 | State validate(QString &strInput, int &iPosition) const RT_OVERRIDE RT_FINAL;
|
---|
149 |
|
---|
150 | /** Defines @a uBottom. */
|
---|
151 | void setBottom(ulong uBottom) { setRange(uBottom, m_uTop); }
|
---|
152 | /** Defines @a uTop. */
|
---|
153 | void setTop(ulong uTop) { setRange(m_uBottom, uTop); }
|
---|
154 | /** Defines range based on passed @a uBottom and @a uTop. */
|
---|
155 | void setRange(ulong uBottom, ulong uTop) { m_uBottom = uBottom; m_uTop = uTop; }
|
---|
156 | /** Returns bottom. */
|
---|
157 | ulong bottom() const { return m_uBottom; }
|
---|
158 | /** Returns top. */
|
---|
159 | ulong top() const { return m_uTop; }
|
---|
160 |
|
---|
161 | private:
|
---|
162 |
|
---|
163 | /** Holds the bottom. */
|
---|
164 | ulong m_uBottom;
|
---|
165 | /** Holds the top. */
|
---|
166 | ulong m_uTop;
|
---|
167 | };
|
---|
168 |
|
---|
169 |
|
---|
170 | #endif /* !FEQT_INCLUDED_SRC_extensions_QIWidgetValidator_h */
|
---|