1 | /* $Id: UISlidingAnimation.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UISlidingAnimation 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 <QLabel>
|
---|
22 | #include <QVBoxLayout>
|
---|
23 |
|
---|
24 | /* GUI includes: */
|
---|
25 | #include "UIAnimationFramework.h"
|
---|
26 | #include "UISlidingAnimation.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | UISlidingAnimation::UISlidingAnimation(Qt::Orientation enmOrientation, bool fReverse, QWidget *pParent /* = 0 */)
|
---|
30 | : QWidget(pParent)
|
---|
31 | , m_enmOrientation(enmOrientation)
|
---|
32 | , m_fReverse(fReverse)
|
---|
33 | , m_pAnimation(0)
|
---|
34 | , m_fIsInProgress(false)
|
---|
35 | , m_pWidget(0)
|
---|
36 | , m_pLabel1(0)
|
---|
37 | , m_pLabel2(0)
|
---|
38 | , m_pWidget1(0)
|
---|
39 | , m_pWidget2(0)
|
---|
40 | {
|
---|
41 | /* Prepare: */
|
---|
42 | prepare();
|
---|
43 | }
|
---|
44 |
|
---|
45 | void UISlidingAnimation::setWidgets(QWidget *pWidget1, QWidget *pWidget2)
|
---|
46 | {
|
---|
47 | /* Remember rendered widgets: */
|
---|
48 | m_pWidget1 = pWidget1;
|
---|
49 | m_pWidget2 = pWidget2;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void UISlidingAnimation::animate(SlidingDirection enmDirection)
|
---|
53 | {
|
---|
54 | /* Mark animation started: */
|
---|
55 | m_fIsInProgress = true;
|
---|
56 |
|
---|
57 | /* Acquire parent size: */
|
---|
58 | const QSize parentSize = parentWidget()->size();
|
---|
59 |
|
---|
60 | /* Update animation boundaries based on parent size: */
|
---|
61 | switch (m_enmOrientation)
|
---|
62 | {
|
---|
63 | case Qt::Horizontal:
|
---|
64 | {
|
---|
65 | m_startWidgetGeometry = QRect( 0, 0,
|
---|
66 | 2 * parentSize.width(), parentSize.height());
|
---|
67 | m_finalWidgetGeometry = QRect(- parentSize.width(), 0,
|
---|
68 | 2 * parentSize.width(), parentSize.height());
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | case Qt::Vertical:
|
---|
72 | {
|
---|
73 | m_startWidgetGeometry = QRect(0, 0,
|
---|
74 | parentSize.width(), 2 * parentSize.height());
|
---|
75 | m_finalWidgetGeometry = QRect(0, - parentSize.height(),
|
---|
76 | parentSize.width(), 2 * parentSize.height());
|
---|
77 | break;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if (m_pAnimation)
|
---|
81 | m_pAnimation->update();
|
---|
82 |
|
---|
83 | /* Update label content: */
|
---|
84 | QPixmap pixmap1(parentSize);
|
---|
85 | QPixmap pixmap2(parentSize);
|
---|
86 | m_pWidget1->render(&pixmap1);
|
---|
87 | m_pWidget2->render(&pixmap2);
|
---|
88 | m_pLabel1->setPixmap(pixmap1);
|
---|
89 | m_pLabel2->setPixmap(pixmap2);
|
---|
90 |
|
---|
91 | /* Update initial widget geometry: */
|
---|
92 | switch (enmDirection)
|
---|
93 | {
|
---|
94 | case SlidingDirection_Forward:
|
---|
95 | {
|
---|
96 | setWidgetGeometry(m_startWidgetGeometry);
|
---|
97 | emit sigForward();
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | case SlidingDirection_Reverse:
|
---|
101 | {
|
---|
102 | setWidgetGeometry(m_finalWidgetGeometry);
|
---|
103 | emit sigReverse();
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void UISlidingAnimation::sltHandleStateEnteredStart()
|
---|
110 | {
|
---|
111 | /* If animation started: */
|
---|
112 | if (m_fIsInProgress)
|
---|
113 | {
|
---|
114 | /* Mark animation finished: */
|
---|
115 | m_fIsInProgress = false;
|
---|
116 | /* And notify listeners: */
|
---|
117 | emit sigAnimationComplete(SlidingDirection_Reverse);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | void UISlidingAnimation::sltHandleStateEnteredFinal()
|
---|
122 | {
|
---|
123 | /* If animation started: */
|
---|
124 | if (m_fIsInProgress)
|
---|
125 | {
|
---|
126 | /* Mark animation finished: */
|
---|
127 | m_fIsInProgress = false;
|
---|
128 | /* And notify listeners: */
|
---|
129 | emit sigAnimationComplete(SlidingDirection_Forward);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | void UISlidingAnimation::prepare()
|
---|
134 | {
|
---|
135 | /* Create animation: */
|
---|
136 | m_pAnimation = UIAnimation::installPropertyAnimation(this,
|
---|
137 | "widgetGeometry",
|
---|
138 | "startWidgetGeometry", "finalWidgetGeometry",
|
---|
139 | SIGNAL(sigForward()), SIGNAL(sigReverse()), m_fReverse);
|
---|
140 | connect(m_pAnimation, &UIAnimation::sigStateEnteredStart, this, &UISlidingAnimation::sltHandleStateEnteredStart);
|
---|
141 | connect(m_pAnimation, &UIAnimation::sigStateEnteredFinal, this, &UISlidingAnimation::sltHandleStateEnteredFinal);
|
---|
142 |
|
---|
143 | /* Create private sliding widget: */
|
---|
144 | m_pWidget = new QWidget(this);
|
---|
145 | if (m_pWidget)
|
---|
146 | {
|
---|
147 | /* Create layout: */
|
---|
148 | QBoxLayout *pLayout = 0;
|
---|
149 | switch (m_enmOrientation)
|
---|
150 | {
|
---|
151 | case Qt::Horizontal: pLayout = new QHBoxLayout(m_pWidget); break;
|
---|
152 | case Qt::Vertical: pLayout = new QVBoxLayout(m_pWidget); break;
|
---|
153 | }
|
---|
154 | if (pLayout)
|
---|
155 | {
|
---|
156 | /* Configure layout: */
|
---|
157 | pLayout->setSpacing(0);
|
---|
158 | pLayout->setContentsMargins(0, 0, 0, 0);
|
---|
159 |
|
---|
160 | /* Create 1st label: */
|
---|
161 | m_pLabel1 = new QLabel;
|
---|
162 | if (m_pLabel1)
|
---|
163 | pLayout->addWidget(m_pLabel1);
|
---|
164 | /* Create 2nd label: */
|
---|
165 | m_pLabel2 = new QLabel;
|
---|
166 | if (m_pLabel2)
|
---|
167 | pLayout->addWidget(m_pLabel2);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Assign initial widget geometry: */
|
---|
172 | m_pWidget->setGeometry(0, 0, width(), height());
|
---|
173 | }
|
---|
174 |
|
---|
175 | void UISlidingAnimation::setWidgetGeometry(const QRect &rect)
|
---|
176 | {
|
---|
177 | /* Define widget geometry: */
|
---|
178 | if (m_pWidget)
|
---|
179 | m_pWidget->setGeometry(rect);
|
---|
180 | }
|
---|
181 |
|
---|
182 | QRect UISlidingAnimation::widgetGeometry() const
|
---|
183 | {
|
---|
184 | /* Return widget geometry: */
|
---|
185 | return m_pWidget ? m_pWidget->geometry() : QRect();
|
---|
186 | }
|
---|