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