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