1 | /* $Id: QIWidgetValidator.cpp 100959 2023-08-23 17:08:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIWidgetValidator class implementation.
|
---|
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 | /* GUI includes: */
|
---|
29 | #include "QIWidgetValidator.h"
|
---|
30 |
|
---|
31 | /* Other VBox includes: */
|
---|
32 | #include "iprt/assert.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | /*********************************************************************************************************************************
|
---|
36 | * Class QObjectValidator implementation. *
|
---|
37 | *********************************************************************************************************************************/
|
---|
38 |
|
---|
39 | QObjectValidator::QObjectValidator(QValidator *pValidator, QObject *pParent /* = 0 */)
|
---|
40 | : QObject(pParent)
|
---|
41 | , m_pValidator(pValidator)
|
---|
42 | , m_enmState(QValidator::Invalid)
|
---|
43 | {
|
---|
44 | prepare();
|
---|
45 | }
|
---|
46 |
|
---|
47 | void QObjectValidator::sltValidate(QString strInput /* = QString() */)
|
---|
48 | {
|
---|
49 | /* Make sure validator assigned: */
|
---|
50 | AssertPtrReturnVoid(m_pValidator);
|
---|
51 |
|
---|
52 | /* Validate: */
|
---|
53 | int iPosition = 0;
|
---|
54 | const QValidator::State enmState = m_pValidator->validate(strInput, iPosition);
|
---|
55 |
|
---|
56 | /* If validity state changed: */
|
---|
57 | if (m_enmState != enmState)
|
---|
58 | {
|
---|
59 | /* Update last validity state: */
|
---|
60 | m_enmState = enmState;
|
---|
61 |
|
---|
62 | /* Notifies listener(s) about validity change: */
|
---|
63 | emit sigValidityChange(m_enmState);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | void QObjectValidator::prepare()
|
---|
68 | {
|
---|
69 | /* Make sure validator assigned: */
|
---|
70 | AssertPtrReturnVoid(m_pValidator);
|
---|
71 |
|
---|
72 | /* Register validator as child: */
|
---|
73 | m_pValidator->setParent(this);
|
---|
74 |
|
---|
75 | /* Validate: */
|
---|
76 | sltValidate();
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /*********************************************************************************************************************************
|
---|
81 | * Class QObjectValidatorGroup implementation. *
|
---|
82 | *********************************************************************************************************************************/
|
---|
83 |
|
---|
84 | QObjectValidatorGroup::QObjectValidatorGroup(QObject *pParent)
|
---|
85 | : QObject(pParent)
|
---|
86 | , m_fResult(false)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | void QObjectValidatorGroup::addObjectValidator(QObjectValidator *pObjectValidator)
|
---|
91 | {
|
---|
92 | /* Make sure object-validator passed: */
|
---|
93 | AssertPtrReturnVoid(pObjectValidator);
|
---|
94 |
|
---|
95 | /* Register object-validator as child: */
|
---|
96 | pObjectValidator->setParent(this);
|
---|
97 |
|
---|
98 | /* Insert object-validator to internal map: */
|
---|
99 | m_group.insert(pObjectValidator, toResult(pObjectValidator->state()));
|
---|
100 |
|
---|
101 | /* Attach object-validator to group: */
|
---|
102 | connect(pObjectValidator, &QObjectValidator::sigValidityChange,
|
---|
103 | this, &QObjectValidatorGroup::sltValidate);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void QObjectValidatorGroup::sltValidate(QValidator::State enmState)
|
---|
107 | {
|
---|
108 | /* Determine sender object-validator: */
|
---|
109 | QObjectValidator *pObjectValidatorSender = qobject_cast<QObjectValidator*>(sender());
|
---|
110 | /* Make sure that is one of our senders: */
|
---|
111 | AssertReturnVoid(pObjectValidatorSender && m_group.contains(pObjectValidatorSender));
|
---|
112 |
|
---|
113 | /* Update internal map: */
|
---|
114 | m_group[pObjectValidatorSender] = toResult(enmState);
|
---|
115 |
|
---|
116 | /* Enumerate all the registered object-validators: */
|
---|
117 | bool fResult = true;
|
---|
118 | foreach (QObjectValidator *pObjectValidator, m_group.keys())
|
---|
119 | if (!toResult(pObjectValidator->state()))
|
---|
120 | {
|
---|
121 | fResult = false;
|
---|
122 | break;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* If validity state changed: */
|
---|
126 | if (m_fResult != fResult)
|
---|
127 | {
|
---|
128 | /* Update last validity state: */
|
---|
129 | m_fResult = fResult;
|
---|
130 |
|
---|
131 | /* Notifies listener(s) about validity change: */
|
---|
132 | emit sigValidityChange(m_fResult);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* static */
|
---|
137 | bool QObjectValidatorGroup::toResult(QValidator::State enmState)
|
---|
138 | {
|
---|
139 | return enmState == QValidator::Acceptable;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /*********************************************************************************************************************************
|
---|
144 | * Class QIULongValidator implementation. *
|
---|
145 | *********************************************************************************************************************************/
|
---|
146 |
|
---|
147 | QValidator::State QIULongValidator::validate(QString &strInput, int &iPosition) const
|
---|
148 | {
|
---|
149 | Q_UNUSED(iPosition);
|
---|
150 |
|
---|
151 | /* Get the stripped string: */
|
---|
152 | QString strStripped = strInput.trimmed();
|
---|
153 |
|
---|
154 | /* 'Intermediate' for empty string or started from '0x': */
|
---|
155 | if (strStripped.isEmpty() ||
|
---|
156 | strStripped.toUpper() == QString("0x").toUpper())
|
---|
157 | return Intermediate;
|
---|
158 |
|
---|
159 | /* Convert to ulong: */
|
---|
160 | bool fOk;
|
---|
161 | ulong uEntered = strInput.toULong(&fOk, 0);
|
---|
162 |
|
---|
163 | /* 'Invalid' if failed to convert: */
|
---|
164 | if (!fOk)
|
---|
165 | return Invalid;
|
---|
166 |
|
---|
167 | /* 'Acceptable' if fits the bounds: */
|
---|
168 | if (uEntered >= m_uBottom && uEntered <= m_uTop)
|
---|
169 | return Acceptable;
|
---|
170 |
|
---|
171 | /* 'Invalid' if more than top, 'Intermediate' if less than bottom: */
|
---|
172 | return uEntered > m_uTop ? Invalid : Intermediate;
|
---|
173 | }
|
---|