1 | /* $Id: UISettingsWarningPane.cpp 100961 2023-08-23 18:05:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UISettingsWarningPane class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 | /* Qt includes: */
|
---|
29 | #include <QHBoxLayout>
|
---|
30 | #include <QEvent>
|
---|
31 | #include <QLabel>
|
---|
32 | #include <QTimer>
|
---|
33 |
|
---|
34 | /* GUI includes: */
|
---|
35 | #include "UISettingsPageValidator.h"
|
---|
36 | #include "UISettingsWarningPane.h"
|
---|
37 |
|
---|
38 | /* Other VBox includes: */
|
---|
39 | #include <iprt/assert.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | UISettingsWarningPane::UISettingsWarningPane(QWidget *pParent)
|
---|
43 | : QWidget(pParent)
|
---|
44 | , m_pIconLayout(0)
|
---|
45 | , m_pTextLabel(0)
|
---|
46 | , m_pHoverTimer(0)
|
---|
47 | , m_iHoveredIconLabelPosition(-1)
|
---|
48 | {
|
---|
49 | prepare();
|
---|
50 | }
|
---|
51 |
|
---|
52 | void UISettingsWarningPane::setWarningLabel(const QString &strWarningLabel)
|
---|
53 | {
|
---|
54 | /* Assign passed text directly to warning-label: */
|
---|
55 | m_pTextLabel->setText(strWarningLabel);
|
---|
56 | }
|
---|
57 |
|
---|
58 | void UISettingsWarningPane::registerValidator(UISettingsPageValidator *pValidator)
|
---|
59 | {
|
---|
60 | /* Make sure validator exists: */
|
---|
61 | AssertPtrReturnVoid(pValidator);
|
---|
62 |
|
---|
63 | /* Make sure validator is not registered yet: */
|
---|
64 | if (m_validators.contains(pValidator))
|
---|
65 | {
|
---|
66 | AssertMsgFailed(("Validator is registered already!\n"));
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* Register validator: */
|
---|
71 | m_validators << pValidator;
|
---|
72 |
|
---|
73 | /* Create icon-label for newly registered validator: */
|
---|
74 | QLabel *pIconLabel = new QLabel;
|
---|
75 | {
|
---|
76 | /* Configure icon-label: */
|
---|
77 | pIconLabel->setMouseTracking(true);
|
---|
78 | pIconLabel->installEventFilter(this);
|
---|
79 | pIconLabel->setPixmap(pValidator->warningPixmap());
|
---|
80 | connect(pValidator, &UISettingsPageValidator::sigShowWarningIcon, pIconLabel, &QLabel::show);
|
---|
81 | connect(pValidator, &UISettingsPageValidator::sigHideWarningIcon, pIconLabel, &QLabel::hide);
|
---|
82 |
|
---|
83 | /* Add icon-label into list: */
|
---|
84 | m_icons << pIconLabel;
|
---|
85 | /* Add icon-label into layout: */
|
---|
86 | m_pIconLayout->addWidget(pIconLabel);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* Mark icon as 'unhovered': */
|
---|
90 | m_hovered << false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | bool UISettingsWarningPane::eventFilter(QObject *pObject, QEvent *pEvent)
|
---|
94 | {
|
---|
95 | /* Depending on event-type: */
|
---|
96 | switch (pEvent->type())
|
---|
97 | {
|
---|
98 | /* One of icons hovered: */
|
---|
99 | case QEvent::MouseMove:
|
---|
100 | {
|
---|
101 | /* Cast object to label: */
|
---|
102 | if (QLabel *pIconLabel = qobject_cast<QLabel*>(pObject))
|
---|
103 | {
|
---|
104 | /* Search for the corresponding icon: */
|
---|
105 | if (m_icons.contains(pIconLabel))
|
---|
106 | {
|
---|
107 | /* Mark icon-label hovered if not yet: */
|
---|
108 | int iIconLabelPosition = m_icons.indexOf(pIconLabel);
|
---|
109 | if (!m_hovered[iIconLabelPosition])
|
---|
110 | {
|
---|
111 | m_hovered[iIconLabelPosition] = true;
|
---|
112 | m_iHoveredIconLabelPosition = iIconLabelPosition;
|
---|
113 | m_pHoverTimer->start();
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | break;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* One of icons unhovered: */
|
---|
121 | case QEvent::Leave:
|
---|
122 | {
|
---|
123 | /* Cast object to label: */
|
---|
124 | if (QLabel *pIconLabel = qobject_cast<QLabel*>(pObject))
|
---|
125 | {
|
---|
126 | /* Search for the corresponding icon: */
|
---|
127 | if (m_icons.contains(pIconLabel))
|
---|
128 | {
|
---|
129 | /* Mark icon-label unhovered if not yet: */
|
---|
130 | int iIconLabelPosition = m_icons.indexOf(pIconLabel);
|
---|
131 | if (m_hovered[iIconLabelPosition])
|
---|
132 | {
|
---|
133 | m_hovered[iIconLabelPosition] = false;
|
---|
134 | if (m_pHoverTimer->isActive())
|
---|
135 | {
|
---|
136 | m_pHoverTimer->stop();
|
---|
137 | m_iHoveredIconLabelPosition = -1;
|
---|
138 | }
|
---|
139 | else
|
---|
140 | emit sigHoverLeave(m_validators[iIconLabelPosition]);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | break;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /* Default case: */
|
---|
148 | default:
|
---|
149 | break;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* Call to base-class: */
|
---|
153 | return QWidget::eventFilter(pObject, pEvent);
|
---|
154 | }
|
---|
155 |
|
---|
156 | void UISettingsWarningPane::sltHandleHoverTimer()
|
---|
157 | {
|
---|
158 | /* Notify listeners about hovering: */
|
---|
159 | if (m_iHoveredIconLabelPosition >= 0 && m_iHoveredIconLabelPosition < m_validators.size())
|
---|
160 | emit sigHoverEnter(m_validators[m_iHoveredIconLabelPosition]);
|
---|
161 | }
|
---|
162 |
|
---|
163 | void UISettingsWarningPane::prepare()
|
---|
164 | {
|
---|
165 | /* Create main-layout: */
|
---|
166 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
167 | {
|
---|
168 | /* Configure layout: */
|
---|
169 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
170 |
|
---|
171 | /* Add left stretch: */
|
---|
172 | pMainLayout->addStretch();
|
---|
173 |
|
---|
174 | /* Create text-label: */
|
---|
175 | m_pTextLabel = new QLabel;
|
---|
176 | {
|
---|
177 | /* Add into layout: */
|
---|
178 | pMainLayout->addWidget(m_pTextLabel);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* Create layout: */
|
---|
182 | m_pIconLayout = new QHBoxLayout;
|
---|
183 | {
|
---|
184 | /* Configure layout: */
|
---|
185 | m_pIconLayout->setContentsMargins(0, 0, 0, 0);
|
---|
186 |
|
---|
187 | /* Add into layout: */
|
---|
188 | pMainLayout->addLayout(m_pIconLayout);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* Create hover-timer: */
|
---|
192 | m_pHoverTimer = new QTimer(this);
|
---|
193 | {
|
---|
194 | /* Configure timer: */
|
---|
195 | m_pHoverTimer->setInterval(200);
|
---|
196 | m_pHoverTimer->setSingleShot(true);
|
---|
197 | connect(m_pHoverTimer, &QTimer::timeout, this, &UISettingsWarningPane::sltHandleHoverTimer);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* Add right stretch: */
|
---|
201 | pMainLayout->addStretch();
|
---|
202 | }
|
---|
203 | }
|
---|