1 | /* $Id: UISlidingWidget.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UISlidingWidget class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2022 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 <QEvent>
|
---|
30 | #include <QHBoxLayout>
|
---|
31 | #include <QVBoxLayout>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "UIAnimationFramework.h"
|
---|
35 | #include "UISlidingWidget.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | UISlidingWidget::UISlidingWidget(Qt::Orientation enmOrientation, QWidget *pParent /* = 0 */)
|
---|
39 | : QWidget(pParent)
|
---|
40 | , m_enmOrientation(enmOrientation)
|
---|
41 | , m_enmState(State_Start)
|
---|
42 | , m_pAnimation(0)
|
---|
43 | , m_pWidget(0)
|
---|
44 | , m_pLayout(0)
|
---|
45 | , m_pWidget1(0)
|
---|
46 | , m_pWidget2(0)
|
---|
47 | {
|
---|
48 | /* Prepare: */
|
---|
49 | prepare();
|
---|
50 | }
|
---|
51 |
|
---|
52 | QSize UISlidingWidget::minimumSizeHint() const
|
---|
53 | {
|
---|
54 | /* Return maximum of minimum size-hints: */
|
---|
55 | QSize msh = QSize();
|
---|
56 | if (m_pWidget1)
|
---|
57 | msh = msh.expandedTo(m_pWidget1->minimumSizeHint());
|
---|
58 | if (m_pWidget2)
|
---|
59 | msh = msh.expandedTo(m_pWidget2->minimumSizeHint());
|
---|
60 | return msh;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void UISlidingWidget::setWidgets(QWidget *pWidget1, QWidget *pWidget2)
|
---|
64 | {
|
---|
65 | /* Clear animation/widgets if any: */
|
---|
66 | delete m_pAnimation;
|
---|
67 | delete m_pWidget1;
|
---|
68 | delete m_pWidget2;
|
---|
69 |
|
---|
70 | /* Remember widgets: */
|
---|
71 | m_pWidget1 = pWidget1;
|
---|
72 | m_pWidget2 = pWidget2;
|
---|
73 | m_pLayout->addWidget(m_pWidget1);
|
---|
74 | m_pLayout->addWidget(m_pWidget2);
|
---|
75 |
|
---|
76 | /* Install new animation: */
|
---|
77 | m_pAnimation = UIAnimation::installPropertyAnimation(this,
|
---|
78 | "widgetGeometry",
|
---|
79 | "startWidgetGeometry", "finalWidgetGeometry",
|
---|
80 | SIGNAL(sigForward()), SIGNAL(sigBackward()));
|
---|
81 | connect(m_pAnimation, &UIAnimation::sigStateEnteredStart, this, &UISlidingWidget::sltSetStateToStart);
|
---|
82 | connect(m_pAnimation, &UIAnimation::sigStateEnteredFinal, this, &UISlidingWidget::sltSetStateToFinal);
|
---|
83 |
|
---|
84 | /* Update animation: */
|
---|
85 | updateAnimation();
|
---|
86 | /* Update widget geometry: */
|
---|
87 | m_pWidget->setGeometry(m_enmState == State_Final ? m_finalWidgetGeometry : m_startWidgetGeometry);
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool UISlidingWidget::event(QEvent *pEvent)
|
---|
91 | {
|
---|
92 | /* Process desired events: */
|
---|
93 | switch (pEvent->type())
|
---|
94 | {
|
---|
95 | case QEvent::LayoutRequest:
|
---|
96 | {
|
---|
97 | // WORKAROUND:
|
---|
98 | // Since we are not connected to
|
---|
99 | // our children LayoutRequest,
|
---|
100 | // we should update geometry
|
---|
101 | // ourselves.
|
---|
102 | updateGeometry();
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | default:
|
---|
106 | break;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* Call to base class: */
|
---|
110 | return QWidget::event(pEvent);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void UISlidingWidget::resizeEvent(QResizeEvent *pEvent)
|
---|
114 | {
|
---|
115 | /* Call to base-class: */
|
---|
116 | QWidget::resizeEvent(pEvent);
|
---|
117 |
|
---|
118 | /* Update animation: */
|
---|
119 | updateAnimation();
|
---|
120 | /* Update widget geometry: */
|
---|
121 | m_pWidget->setGeometry(m_enmState == State_Final ? m_finalWidgetGeometry : m_startWidgetGeometry);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void UISlidingWidget::prepare()
|
---|
125 | {
|
---|
126 | /* Create private sliding widget: */
|
---|
127 | m_pWidget = new QWidget(this);
|
---|
128 | if (m_pWidget)
|
---|
129 | {
|
---|
130 | /* Create layout: */
|
---|
131 | switch (m_enmOrientation)
|
---|
132 | {
|
---|
133 | case Qt::Horizontal: m_pLayout = new QHBoxLayout(m_pWidget); break;
|
---|
134 | case Qt::Vertical: m_pLayout = new QVBoxLayout(m_pWidget); break;
|
---|
135 | }
|
---|
136 | if (m_pLayout)
|
---|
137 | {
|
---|
138 | /* Configure layout: */
|
---|
139 | m_pLayout->setSpacing(0);
|
---|
140 | m_pLayout->setContentsMargins(0, 0, 0, 0);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* Update animation: */
|
---|
145 | updateAnimation();
|
---|
146 | /* Update widget geometry: */
|
---|
147 | m_pWidget->setGeometry(m_enmState == State_Final ? m_finalWidgetGeometry : m_startWidgetGeometry);
|
---|
148 | }
|
---|
149 |
|
---|
150 | void UISlidingWidget::updateAnimation()
|
---|
151 | {
|
---|
152 | /* Recalculate sub-window geometry animation boundaries based on size-hint: */
|
---|
153 | switch (m_enmOrientation)
|
---|
154 | {
|
---|
155 | case Qt::Horizontal:
|
---|
156 | {
|
---|
157 | m_startWidgetGeometry = QRect( 0, 0,
|
---|
158 | 2 * width(), height());
|
---|
159 | m_finalWidgetGeometry = QRect(- width(), 0,
|
---|
160 | 2 * width(), height());
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | case Qt::Vertical:
|
---|
164 | {
|
---|
165 | m_startWidgetGeometry = QRect(0, 0,
|
---|
166 | width(), 2 * height());
|
---|
167 | m_finalWidgetGeometry = QRect(0, - height(),
|
---|
168 | width(), 2 * height());
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* Update animation finally: */
|
---|
174 | if (m_pAnimation)
|
---|
175 | m_pAnimation->update();
|
---|
176 | }
|
---|
177 |
|
---|
178 | void UISlidingWidget::setWidgetGeometry(const QRect &rect)
|
---|
179 | {
|
---|
180 | /* Apply widget geometry: */
|
---|
181 | m_pWidget->setGeometry(rect);
|
---|
182 | }
|
---|
183 |
|
---|
184 | QRect UISlidingWidget::widgetGeometry() const
|
---|
185 | {
|
---|
186 | /* Return widget geometry: */
|
---|
187 | return m_pWidget->geometry();
|
---|
188 | }
|
---|