VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIPopupPaneMessage.cpp

Last change on this file was 100064, checked in by vboxsync, 12 months ago

FE/Qt: bugref:10421. Replacing VBOX_WS_X11 with VBOX_WS_NIX.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/* $Id: UIPopupPaneMessage.cpp 100064 2023-06-04 09:10:01Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIPopupPaneMessage class implementation.
4 */
5
6/*
7 * Copyright (C) 2013-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 <QLabel>
30#include <QCheckBox>
31
32/* GUI includes: */
33#include "UIAnimationFramework.h"
34#include "UIPopupPane.h"
35#include "UIPopupPaneMessage.h"
36
37/* Other VBox includes: */
38#include <iprt/assert.h>
39
40UIPopupPaneMessage::UIPopupPaneMessage(QWidget *pParent, const QString &strText, bool fFocused)
41 : QWidget(pParent)
42 , m_iLayoutMargin(0)
43 , m_iLayoutSpacing(10)
44 , m_strText(strText)
45 , m_pLabel(0)
46 , m_iDesiredLabelWidth(-1)
47 , m_fFocused(fFocused)
48 , m_pAnimation(0)
49{
50 /* Prepare: */
51 prepare();
52}
53
54void UIPopupPaneMessage::setText(const QString &strText)
55{
56 /* Make sure the text has changed: */
57 if (m_strText == strText)
58 return;
59
60 /* Fetch new text: */
61 m_strText = strText;
62 m_pLabel->setText(m_strText);
63
64 /* Update size-hint: */
65 updateSizeHint();
66}
67
68QSize UIPopupPaneMessage::minimumSizeHint() const
69{
70 /* Check if desired-width set: */
71 if (m_iDesiredLabelWidth >= 0)
72 /* Dependent size-hint: */
73 return m_minimumSizeHint;
74 /* Golden-rule size-hint by default: */
75 return QWidget::minimumSizeHint();
76}
77
78void UIPopupPaneMessage::setMinimumSizeHint(const QSize &minimumSizeHint)
79{
80 /* Make sure the size-hint has changed: */
81 if (m_minimumSizeHint == minimumSizeHint)
82 return;
83
84 /* Fetch new size-hint: */
85 m_minimumSizeHint = minimumSizeHint;
86
87 /* Notify parent popup-pane: */
88 emit sigSizeHintChanged();
89}
90
91void UIPopupPaneMessage::layoutContent()
92{
93 /* Variables: */
94 const int iWidth = width();
95 const int iHeight = height();
96 const int iLabelWidth = m_labelSizeHint.width();
97 const int iLabelHeight = m_labelSizeHint.height();
98
99 /* Label: */
100 m_pLabel->move(m_iLayoutMargin, m_iLayoutMargin);
101 m_pLabel->resize(qMin(iWidth, iLabelWidth), qMin(iHeight, iLabelHeight));
102}
103
104void UIPopupPaneMessage::sltHandleProposalForWidth(int iWidth)
105{
106 /* Make sure the desired-width has changed: */
107 if (m_iDesiredLabelWidth == iWidth)
108 return;
109
110 /* Fetch new desired-width: */
111 m_iDesiredLabelWidth = iWidth;
112
113 /* Update size-hint: */
114 updateSizeHint();
115}
116
117void UIPopupPaneMessage::sltFocusEnter()
118{
119 /* Ignore if already focused: */
120 if (m_fFocused)
121 return;
122
123 /* Update focus state: */
124 m_fFocused = true;
125
126 /* Notify listeners: */
127 emit sigFocusEnter();
128}
129
130void UIPopupPaneMessage::sltFocusLeave()
131{
132 /* Ignore if already unfocused: */
133 if (!m_fFocused)
134 return;
135
136 /* Update focus state: */
137 m_fFocused = false;
138
139 /* Notify listeners: */
140 emit sigFocusLeave();
141}
142
143void UIPopupPaneMessage::prepare()
144{
145 /* Prepare content: */
146 prepareContent();
147 /* Prepare animation: */
148 prepareAnimation();
149
150 /* Update size-hint: */
151 updateSizeHint();
152}
153
154void UIPopupPaneMessage::prepareContent()
155{
156 /* Create label: */
157 m_pLabel = new QLabel(this);
158 if (m_pLabel)
159 {
160 /* Configure label: */
161 m_pLabel->setFont(tuneFont(m_pLabel->font()));
162 m_pLabel->setWordWrap(true);
163 m_pLabel->setFocusPolicy(Qt::NoFocus);
164 m_pLabel->setText(m_strText);
165 }
166}
167
168void UIPopupPaneMessage::prepareAnimation()
169{
170 UIPopupPane *pPopupPane = qobject_cast<UIPopupPane*>(parent());
171 AssertReturnVoid(pPopupPane);
172 {
173 /* Propagate parent signals: */
174 connect(pPopupPane, &UIPopupPane::sigFocusEnter, this, &UIPopupPaneMessage::sltFocusEnter);
175 connect(pPopupPane, &UIPopupPane::sigFocusLeave, this, &UIPopupPaneMessage::sltFocusLeave);
176 }
177 /* Install geometry animation for 'minimumSizeHint' property: */
178 m_pAnimation = UIAnimation::installPropertyAnimation(this, "minimumSizeHint", "collapsedSizeHint", "expandedSizeHint",
179 SIGNAL(sigFocusEnter()), SIGNAL(sigFocusLeave()), m_fFocused);
180}
181
182void UIPopupPaneMessage::updateSizeHint()
183{
184 /* Recalculate collapsed size-hint: */
185 {
186 /* Collapsed size-hint contains only one-text-line label: */
187 QFontMetrics fm(m_pLabel->font(), m_pLabel);
188 m_collapsedSizeHint = QSize(m_iDesiredLabelWidth, fm.height());
189 }
190
191 /* Recalculate expanded size-hint: */
192 {
193 /* Recalculate label size-hint: */
194 m_labelSizeHint = QSize(m_iDesiredLabelWidth, m_pLabel->heightForWidth(m_iDesiredLabelWidth));
195 /* Expanded size-hint contains full-size label: */
196 m_expandedSizeHint = m_labelSizeHint;
197 }
198
199 /* Update current size-hint: */
200 m_minimumSizeHint = m_fFocused ? m_expandedSizeHint : m_collapsedSizeHint;
201
202 /* Update animation: */
203 if (m_pAnimation)
204 m_pAnimation->update();
205
206 /* Notify parent popup-pane: */
207 emit sigSizeHintChanged();
208}
209
210/* static */
211QFont UIPopupPaneMessage::tuneFont(QFont font)
212{
213#if defined(VBOX_WS_MAC)
214 font.setPointSize(font.pointSize() - 2);
215#elif defined(VBOX_WS_NIX)
216 font.setPointSize(font.pointSize() - 1);
217#endif
218 return font;
219}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use