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