1 | /* $Id: UIWarningPane.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIWarningPane class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
19 | # include <precomp.h>
|
---|
20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
21 |
|
---|
22 | /* Qt includes: */
|
---|
23 | # include <QHBoxLayout>
|
---|
24 | # include <QEvent>
|
---|
25 | # include <QLabel>
|
---|
26 | # include <QTimer>
|
---|
27 |
|
---|
28 | /* GUI includes: */
|
---|
29 | # include "QIWidgetValidator.h"
|
---|
30 | # include "UIWarningPane.h"
|
---|
31 |
|
---|
32 | /* Other VBox includes: */
|
---|
33 | # include <VBox/sup.h>
|
---|
34 |
|
---|
35 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
36 |
|
---|
37 |
|
---|
38 | UIWarningPane::UIWarningPane(QWidget *pParent)
|
---|
39 | : QWidget(pParent)
|
---|
40 | , m_pIconLayout(0)
|
---|
41 | , m_pTextLabel(0)
|
---|
42 | , m_pHoverTimer(0)
|
---|
43 | , m_iHoveredIconLabelPosition(-1)
|
---|
44 | {
|
---|
45 | /* Prepare: */
|
---|
46 | prepare();
|
---|
47 | }
|
---|
48 |
|
---|
49 | void UIWarningPane::setWarningLabel(const QString &strWarningLabel)
|
---|
50 | {
|
---|
51 | /* Assign passed text directly to warning-label: */
|
---|
52 | m_pTextLabel->setText(strWarningLabel);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void UIWarningPane::registerValidator(UIPageValidator *pValidator)
|
---|
56 | {
|
---|
57 | /* Make sure validator exists: */
|
---|
58 | AssertPtrReturnVoid(pValidator);
|
---|
59 |
|
---|
60 | /* Make sure validator is not registered yet: */
|
---|
61 | if (m_validators.contains(pValidator))
|
---|
62 | {
|
---|
63 | AssertMsgFailed(("Validator is registered already!\n"));
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* Register validator: */
|
---|
68 | m_validators << pValidator;
|
---|
69 |
|
---|
70 | /* Create icon-label for newly registered validator: */
|
---|
71 | QLabel *pIconLabel = new QLabel;
|
---|
72 | {
|
---|
73 | /* Configure icon-label: */
|
---|
74 | pIconLabel->setMouseTracking(true);
|
---|
75 | pIconLabel->installEventFilter(this);
|
---|
76 | pIconLabel->setPixmap(pValidator->warningPixmap());
|
---|
77 | connect(pValidator, &UIPageValidator::sigShowWarningIcon, pIconLabel, &QLabel::show);
|
---|
78 | connect(pValidator, &UIPageValidator::sigHideWarningIcon, pIconLabel, &QLabel::hide);
|
---|
79 |
|
---|
80 | /* Add icon-label into list: */
|
---|
81 | m_icons << pIconLabel;
|
---|
82 | /* Add icon-label into layout: */
|
---|
83 | m_pIconLayout->addWidget(pIconLabel);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* Mark icon as 'unhovered': */
|
---|
87 | m_hovered << false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool UIWarningPane::eventFilter(QObject *pObject, QEvent *pEvent)
|
---|
91 | {
|
---|
92 | /* Depending on event-type: */
|
---|
93 | switch (pEvent->type())
|
---|
94 | {
|
---|
95 | /* One of icons hovered: */
|
---|
96 | case QEvent::MouseMove:
|
---|
97 | {
|
---|
98 | /* Cast object to label: */
|
---|
99 | if (QLabel *pIconLabel = qobject_cast<QLabel*>(pObject))
|
---|
100 | {
|
---|
101 | /* Search for the corresponding icon: */
|
---|
102 | if (m_icons.contains(pIconLabel))
|
---|
103 | {
|
---|
104 | /* Mark icon-label hovered if not yet: */
|
---|
105 | int iIconLabelPosition = m_icons.indexOf(pIconLabel);
|
---|
106 | if (!m_hovered[iIconLabelPosition])
|
---|
107 | {
|
---|
108 | m_hovered[iIconLabelPosition] = true;
|
---|
109 | m_iHoveredIconLabelPosition = iIconLabelPosition;
|
---|
110 | m_pHoverTimer->start();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | break;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* One of icons unhovered: */
|
---|
118 | case QEvent::Leave:
|
---|
119 | {
|
---|
120 | /* Cast object to label: */
|
---|
121 | if (QLabel *pIconLabel = qobject_cast<QLabel*>(pObject))
|
---|
122 | {
|
---|
123 | /* Search for the corresponding icon: */
|
---|
124 | if (m_icons.contains(pIconLabel))
|
---|
125 | {
|
---|
126 | /* Mark icon-label unhovered if not yet: */
|
---|
127 | int iIconLabelPosition = m_icons.indexOf(pIconLabel);
|
---|
128 | if (m_hovered[iIconLabelPosition])
|
---|
129 | {
|
---|
130 | m_hovered[iIconLabelPosition] = false;
|
---|
131 | if (m_pHoverTimer->isActive())
|
---|
132 | {
|
---|
133 | m_pHoverTimer->stop();
|
---|
134 | m_iHoveredIconLabelPosition = -1;
|
---|
135 | }
|
---|
136 | else
|
---|
137 | emit sigHoverLeave(m_validators[iIconLabelPosition]);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | break;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* Default case: */
|
---|
145 | default:
|
---|
146 | break;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Call to base-class: */
|
---|
150 | return QWidget::eventFilter(pObject, pEvent);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void UIWarningPane::sltHandleHoverTimer()
|
---|
154 | {
|
---|
155 | /* Notify listeners about hovering: */
|
---|
156 | if (m_iHoveredIconLabelPosition >= 0 && m_iHoveredIconLabelPosition < m_validators.size())
|
---|
157 | emit sigHoverEnter(m_validators[m_iHoveredIconLabelPosition]);
|
---|
158 | }
|
---|
159 |
|
---|
160 | void UIWarningPane::prepare()
|
---|
161 | {
|
---|
162 | /* Create main-layout: */
|
---|
163 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
164 | {
|
---|
165 | /* Configure layout: */
|
---|
166 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
167 |
|
---|
168 | /* Add left stretch: */
|
---|
169 | pMainLayout->addStretch();
|
---|
170 |
|
---|
171 | /* Create text-label: */
|
---|
172 | m_pTextLabel = new QLabel;
|
---|
173 | {
|
---|
174 | /* Add into layout: */
|
---|
175 | pMainLayout->addWidget(m_pTextLabel);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Create layout: */
|
---|
179 | m_pIconLayout = new QHBoxLayout;
|
---|
180 | {
|
---|
181 | /* Configure layout: */
|
---|
182 | m_pIconLayout->setContentsMargins(0, 0, 0, 0);
|
---|
183 |
|
---|
184 | /* Add into layout: */
|
---|
185 | pMainLayout->addLayout(m_pIconLayout);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* Create hover-timer: */
|
---|
189 | m_pHoverTimer = new QTimer(this);
|
---|
190 | {
|
---|
191 | /* Configure timer: */
|
---|
192 | m_pHoverTimer->setInterval(200);
|
---|
193 | m_pHoverTimer->setSingleShot(true);
|
---|
194 | connect(m_pHoverTimer, &QTimer::timeout, this, &UIWarningPane::sltHandleHoverTimer);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* Add right stretch: */
|
---|
198 | pMainLayout->addStretch();
|
---|
199 | }
|
---|
200 | }
|
---|