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