VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/settings/UISettingsWarningPane.cpp@ 103977

Last change on this file since 103977 was 100967, checked in by vboxsync, 16 months ago

FE/Qt: UISettingsDialog: Adjusting UISettingsWarningPane to have more obvious show/hide API; S.a. r158899.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette