VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIPopupPaneDetails.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/* $Id: UIPopupPaneDetails.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIPopupPaneDetails 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 <QCheckBox>
23# include <QTextDocument>
24# include <QTextEdit>
25
26/* GUI includes: */
27# include "UIAnimationFramework.h"
28# include "UIPopupPaneDetails.h"
29
30#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
31
32
33UIPopupPaneDetails::UIPopupPaneDetails(QWidget *pParent, const QString &strText, bool fFocused)
34 : QWidget(pParent)
35 , m_iLayoutMargin(5)
36 , m_iLayoutSpacing(10)
37 , m_strText(strText)
38 , m_pTextEdit(0)
39 , m_iDesiredTextEditWidth(-1)
40 , m_iMaximumPaneHeight(-1)
41 , m_iMaximumTextEditHeight(0)
42 , m_iTextContentMargin(5)
43 , m_fFocused(fFocused)
44 , m_pAnimation(0)
45{
46 /* Prepare: */
47 prepare();
48}
49
50void UIPopupPaneDetails::setText(const QString &strText)
51{
52 /* Make sure the text has changed: */
53 if (m_strText == strText)
54 return;
55
56 /* Fetch new text: */
57 m_strText = strText;
58 m_pTextEdit->setText(m_strText);
59
60 /* Update size-hint/visibility: */
61 updateSizeHint();
62 updateVisibility();
63}
64
65QSize UIPopupPaneDetails::minimumSizeHint() const
66{
67 /* Check if desired-width set: */
68 if (m_iDesiredTextEditWidth >= 0)
69 /* Dependent size-hint: */
70 return m_minimumSizeHint;
71 /* Golden-rule size-hint by default: */
72 return QWidget::minimumSizeHint();
73}
74
75void UIPopupPaneDetails::setMinimumSizeHint(const QSize &minimumSizeHint)
76{
77 /* Make sure the size-hint has changed: */
78 if (m_minimumSizeHint == minimumSizeHint)
79 return;
80
81 /* Fetch new size-hint: */
82 m_minimumSizeHint = minimumSizeHint;
83
84 /* Notify parent popup-pane: */
85 emit sigSizeHintChanged();
86}
87
88void UIPopupPaneDetails::layoutContent()
89{
90 /* Variables: */
91 const int iWidth = width();
92 const int iHeight = height();
93 const int iTextEditWidth = m_textEditSizeHint.width();
94 const int iTextEditHeight = m_textEditSizeHint.height();
95
96 /* TextEdit: */
97 m_pTextEdit->move(m_iLayoutMargin, m_iLayoutMargin);
98 m_pTextEdit->resize(qMin(iWidth, iTextEditWidth), qMin(iHeight, iTextEditHeight));
99 /* Text-document: */
100 QTextDocument *pTextDocument = m_pTextEdit->document();
101 if (pTextDocument)
102 {
103 pTextDocument->adjustSize();
104 pTextDocument->setTextWidth(m_pTextEdit->width() - m_iTextContentMargin);
105 }
106}
107
108void UIPopupPaneDetails::sltHandleProposalForWidth(int iWidth)
109{
110 /* Make sure the desired-width has changed: */
111 if (m_iDesiredTextEditWidth == iWidth)
112 return;
113
114 /* Fetch new desired-width: */
115 m_iDesiredTextEditWidth = iWidth;
116
117 /* Update size-hint: */
118 updateSizeHint();
119}
120
121void UIPopupPaneDetails::sltHandleProposalForHeight(int iHeight)
122{
123 /* Make sure the desired-height has changed: */
124 if (m_iMaximumPaneHeight == iHeight)
125 return;
126
127 /* Fetch new desired-height: */
128 m_iMaximumPaneHeight = iHeight;
129 m_iMaximumTextEditHeight = m_iMaximumPaneHeight - 2 * m_iLayoutMargin;
130
131 /* Update size-hint: */
132 updateSizeHint();
133}
134
135void UIPopupPaneDetails::sltFocusEnter()
136{
137 /* Ignore if already focused: */
138 if (m_fFocused)
139 return;
140
141 /* Update focus state: */
142 m_fFocused = true;
143
144 /* Update visibility: */
145 updateVisibility();
146
147 /* Notify listeners: */
148 emit sigFocusEnter();
149}
150
151void UIPopupPaneDetails::sltFocusLeave()
152{
153 /* Ignore if already unfocused: */
154 if (!m_fFocused)
155 return;
156
157 /* Update focus state: */
158 m_fFocused = false;
159
160 /* Update visibility: */
161 updateVisibility();
162
163 /* Notify listeners: */
164 emit sigFocusLeave();
165}
166
167void UIPopupPaneDetails::prepare()
168{
169 /* Prepare content: */
170 prepareContent();
171 /* Prepare animation: */
172 prepareAnimation();
173
174 /* Update size-hint/visibility: */
175 updateSizeHint();
176 updateVisibility();
177}
178
179void UIPopupPaneDetails::prepareContent()
180{
181 /* Create text-editor: */
182 m_pTextEdit = new QTextEdit(this);
183 if (m_pTextEdit)
184 {
185 /* Configure text-editor: */
186 m_pTextEdit->setFont(tuneFont(m_pTextEdit->font()));
187 m_pTextEdit->setText(m_strText);
188 m_pTextEdit->setFocusProxy(this);
189 }
190}
191
192void UIPopupPaneDetails::prepareAnimation()
193{
194 /* Propagate parent signals: */
195 connect(parent(), SIGNAL(sigFocusEnter()), this, SLOT(sltFocusEnter()));
196 connect(parent(), SIGNAL(sigFocusLeave()), this, SLOT(sltFocusLeave()));
197 /* Install geometry animation for 'minimumSizeHint' property: */
198 m_pAnimation = UIAnimation::installPropertyAnimation(this, "minimumSizeHint", "collapsedSizeHint", "expandedSizeHint",
199 SIGNAL(sigFocusEnter()), SIGNAL(sigFocusLeave()), m_fFocused);
200}
201
202void UIPopupPaneDetails::updateSizeHint()
203{
204 /* Recalculate collapsed size-hint: */
205 {
206 /* Collapsed size-hint with 0 height: */
207 m_collapsedSizeHint = QSize(m_iDesiredTextEditWidth, 0);
208 }
209
210 /* Recalculate expanded size-hint: */
211 {
212 int iNewHeight = m_iMaximumPaneHeight;
213 QTextDocument *pTextDocument = m_pTextEdit->document();
214 if (pTextDocument)
215 {
216 /* Adjust text-edit size: */
217 pTextDocument->adjustSize();
218 /* Get corresponding QTextDocument size: */
219 QSize textSize = pTextDocument->size().toSize();
220 /* Make sure the text edits height is no larger than that of container widget: */
221 iNewHeight = qMin(m_iMaximumTextEditHeight, textSize.height() + 2 * m_iLayoutMargin);
222 }
223 /* Recalculate label size-hint: */
224 m_textEditSizeHint = QSize(m_iDesiredTextEditWidth, iNewHeight);
225 /* Expanded size-hint contains full-size label: */
226 m_expandedSizeHint = m_textEditSizeHint;
227 }
228
229 /* Update current size-hint: */
230 m_minimumSizeHint = m_fFocused ? m_expandedSizeHint : m_collapsedSizeHint;
231
232 /* Update animation: */
233 if (m_pAnimation)
234 m_pAnimation->update();
235
236 /* Notify parent popup-pane: */
237 emit sigSizeHintChanged();
238}
239
240void UIPopupPaneDetails::updateVisibility()
241{
242 if (m_fFocused && !m_strText.isEmpty())
243 show();
244 else
245 hide();
246}
247
248/* static */
249QFont UIPopupPaneDetails::tuneFont(QFont font)
250{
251#if defined(VBOX_WS_MAC)
252 font.setPointSize(font.pointSize() - 2);
253#elif defined(VBOX_WS_X11)
254 font.setPointSize(font.pointSize() - 1);
255#endif
256 return font;
257}
258
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use