1 | /* $Id: QIWidgetValidator.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIWidgetValidator class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 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 <QPixmap>
|
---|
37 | #include <QValidator>
|
---|
38 |
|
---|
39 | /* GUI includes: */
|
---|
40 | #include "UILibraryDefs.h"
|
---|
41 |
|
---|
42 | /* Forward declarations: */
|
---|
43 | class QPixmap;
|
---|
44 | class QString;
|
---|
45 | class UISettingsPage;
|
---|
46 |
|
---|
47 |
|
---|
48 | /** QObject extension,
|
---|
49 | * providing passed QObject with validation routine. */
|
---|
50 | class SHARED_LIBRARY_STUFF QObjectValidator : public QObject
|
---|
51 | {
|
---|
52 | Q_OBJECT;
|
---|
53 |
|
---|
54 | signals:
|
---|
55 |
|
---|
56 | /** Notifies listener(s) about validity changed to @a enmState. */
|
---|
57 | void sigValidityChange(QValidator::State enmState);
|
---|
58 |
|
---|
59 | public:
|
---|
60 |
|
---|
61 | /** Constructs object validator passing @a pParent to the base-class.
|
---|
62 | * @param pValidator Brings the validator passed on to the OObject
|
---|
63 | * children and used to perform validation itself. */
|
---|
64 | QObjectValidator(QValidator *pValidator, QObject *pParent = 0);
|
---|
65 |
|
---|
66 | /** Returns last validation state. */
|
---|
67 | QValidator::State state() const { return m_enmState; }
|
---|
68 |
|
---|
69 | public slots:
|
---|
70 |
|
---|
71 | /** Performs validation: */
|
---|
72 | void sltValidate(QString strInput = QString());
|
---|
73 |
|
---|
74 | private:
|
---|
75 |
|
---|
76 | /** Prepare routine. */
|
---|
77 | void prepare();
|
---|
78 |
|
---|
79 | /** Holds the validator reference. */
|
---|
80 | QValidator *m_pValidator;
|
---|
81 |
|
---|
82 | /** Holds the validation state. */
|
---|
83 | QValidator::State m_enmState;
|
---|
84 | };
|
---|
85 |
|
---|
86 |
|
---|
87 | /** QObject extension,
|
---|
88 | * which can group various QObjectValidator instances to operate on. */
|
---|
89 | class SHARED_LIBRARY_STUFF QObjectValidatorGroup : public QObject
|
---|
90 | {
|
---|
91 | Q_OBJECT;
|
---|
92 |
|
---|
93 | signals:
|
---|
94 |
|
---|
95 | /** Notifies listener(s) about validity changed to @a fValid. */
|
---|
96 | void sigValidityChange(bool fValid);
|
---|
97 |
|
---|
98 | public:
|
---|
99 |
|
---|
100 | /** Constructs validation group passing @a pParent to the base-class. */
|
---|
101 | QObjectValidatorGroup(QObject *pParent)
|
---|
102 | : QObject(pParent)
|
---|
103 | , m_fResult(false)
|
---|
104 | {}
|
---|
105 |
|
---|
106 | /** Adds @a pObjectValidator.
|
---|
107 | * @note The ownership of @a pObjectValidator is transferred to the group,
|
---|
108 | * and it's the group's responsibility to delete it. */
|
---|
109 | void addObjectValidator(QObjectValidator *pObjectValidator);
|
---|
110 |
|
---|
111 | /** Returns last validation result. */
|
---|
112 | bool result() const { return m_fResult; }
|
---|
113 |
|
---|
114 | private slots:
|
---|
115 |
|
---|
116 | /** Performs validation for a passed @a enmState. */
|
---|
117 | void sltValidate(QValidator::State enmState);
|
---|
118 |
|
---|
119 | private:
|
---|
120 |
|
---|
121 | /** Converts QValidator::State to bool result. */
|
---|
122 | static bool toResult(QValidator::State enmState);
|
---|
123 |
|
---|
124 | /** Holds object-validators and their states. */
|
---|
125 | QMap<QObjectValidator*, bool> m_group;
|
---|
126 |
|
---|
127 | /** Holds validation result. */
|
---|
128 | bool m_fResult;
|
---|
129 | };
|
---|
130 |
|
---|
131 |
|
---|
132 | /** Page validator prototype. */
|
---|
133 | class SHARED_LIBRARY_STUFF UIPageValidator : public QObject
|
---|
134 | {
|
---|
135 | Q_OBJECT;
|
---|
136 |
|
---|
137 | signals:
|
---|
138 |
|
---|
139 | /** Notifies about validity change for @a pValidator. */
|
---|
140 | void sigValidityChanged(UIPageValidator *pValidator);
|
---|
141 |
|
---|
142 | /** Asks listener to show warning icon. */
|
---|
143 | void sigShowWarningIcon();
|
---|
144 | /** Asks listener to hide warning icon. */
|
---|
145 | void sigHideWarningIcon();
|
---|
146 |
|
---|
147 | public:
|
---|
148 |
|
---|
149 | /** Constructs page validator for a certain @a pPage,
|
---|
150 | * passing @a pParent to the base-class. */
|
---|
151 | UIPageValidator(QObject *pParent, UISettingsPage *pPage)
|
---|
152 | : QObject(pParent)
|
---|
153 | , m_pPage(pPage)
|
---|
154 | , m_fIsValid(true)
|
---|
155 | {}
|
---|
156 |
|
---|
157 | /** Returns page. */
|
---|
158 | UISettingsPage *page() const { return m_pPage; }
|
---|
159 | /** Returns warning pixmap. */
|
---|
160 | QPixmap warningPixmap() const;
|
---|
161 | /** Returns internal name. */
|
---|
162 | QString internalName() const;
|
---|
163 |
|
---|
164 | /** Returns whether validator is valid. */
|
---|
165 | bool isValid() const { return m_fIsValid; }
|
---|
166 | /** Defines whether validator @a fIsValid. */
|
---|
167 | void setValid(bool fIsValid) { m_fIsValid = fIsValid; }
|
---|
168 |
|
---|
169 | /** Returns last message. */
|
---|
170 | QString lastMessage() const { return m_strLastMessage; }
|
---|
171 | /** Defines @a strLastMessage. */
|
---|
172 | void setLastMessage(const QString &strLastMessage);
|
---|
173 |
|
---|
174 | public slots:
|
---|
175 |
|
---|
176 | /** Performs revalidation. */
|
---|
177 | void revalidate();
|
---|
178 |
|
---|
179 | private:
|
---|
180 |
|
---|
181 | /** Holds the validated page. */
|
---|
182 | UISettingsPage *m_pPage;
|
---|
183 |
|
---|
184 | /** Holds whether the page is valid. */
|
---|
185 | bool m_fIsValid;
|
---|
186 |
|
---|
187 | /** Holds the last message. */
|
---|
188 | QString m_strLastMessage;
|
---|
189 | };
|
---|
190 |
|
---|
191 |
|
---|
192 | /** QValidator extension,
|
---|
193 | * for long number validations. */
|
---|
194 | class SHARED_LIBRARY_STUFF QIULongValidator : public QValidator
|
---|
195 | {
|
---|
196 | public:
|
---|
197 |
|
---|
198 | /** Constructs long validator passing @a pParent to the base-class. */
|
---|
199 | QIULongValidator(QObject *pParent)
|
---|
200 | : QValidator(pParent)
|
---|
201 | , m_uBottom(0), m_uTop(ULONG_MAX)
|
---|
202 | {}
|
---|
203 |
|
---|
204 | /** Constructs long validator passing @a pParent to the base-class.
|
---|
205 | * @param uMinimum Holds the minimum valid border.
|
---|
206 | * @param uMaximum Holds the maximum valid border. */
|
---|
207 | QIULongValidator(ulong uMinimum, ulong uMaximum,
|
---|
208 | QObject *pParent)
|
---|
209 | : QValidator(pParent)
|
---|
210 | , m_uBottom(uMinimum), m_uTop(uMaximum)
|
---|
211 | {}
|
---|
212 |
|
---|
213 | /** Destructs long validator. */
|
---|
214 | virtual ~QIULongValidator() {}
|
---|
215 |
|
---|
216 | /** Performs validation for @a strInput at @a iPosition. */
|
---|
217 | State validate(QString &strInput, int &iPosition) const;
|
---|
218 |
|
---|
219 | /** Defines @a uBottom. */
|
---|
220 | void setBottom(ulong uBottom) { setRange(uBottom, m_uTop); }
|
---|
221 | /** Defines @a uTop. */
|
---|
222 | void setTop(ulong uTop) { setRange(m_uBottom, uTop); }
|
---|
223 | /** Defines range based on passed @a uBottom and @a uTop. */
|
---|
224 | void setRange(ulong uBottom, ulong uTop) { m_uBottom = uBottom; m_uTop = uTop; }
|
---|
225 | /** Returns bottom. */
|
---|
226 | ulong bottom() const { return m_uBottom; }
|
---|
227 | /** Returns top. */
|
---|
228 | ulong top() const { return m_uTop; }
|
---|
229 |
|
---|
230 | private:
|
---|
231 |
|
---|
232 | /** Holds the bottom. */
|
---|
233 | ulong m_uBottom;
|
---|
234 | /** Holds the top. */
|
---|
235 | ulong m_uTop;
|
---|
236 | };
|
---|
237 |
|
---|
238 |
|
---|
239 | #endif /* !FEQT_INCLUDED_SRC_extensions_QIWidgetValidator_h */
|
---|