1 | /* $Id: UIGraphicsZoomButton.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGraphicsZoomButton class definition.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2023 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 <QStateMachine>
|
---|
30 | #include <QSignalTransition>
|
---|
31 | #include <QPropertyAnimation>
|
---|
32 | #include <QPainter>
|
---|
33 | #include <QStyleOptionGraphicsItem>
|
---|
34 |
|
---|
35 | /* GUI includes: */
|
---|
36 | #include "UIGraphicsZoomButton.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | UIGraphicsZoomButton::UIGraphicsZoomButton(QIGraphicsWidget *pParent, const QIcon &icon, int iDirection)
|
---|
40 | : UIGraphicsButton(pParent, icon)
|
---|
41 | , m_iIndent(4)
|
---|
42 | , m_iDirection(iDirection)
|
---|
43 | , m_iAnimationDuration(200)
|
---|
44 | , m_pStateMachine(0)
|
---|
45 | , m_pForwardAnimation(0)
|
---|
46 | , m_pBackwardAnimation(0)
|
---|
47 | , m_fStateDefault(true)
|
---|
48 | {
|
---|
49 | /* Setup: */
|
---|
50 | setAcceptHoverEvents(true);
|
---|
51 |
|
---|
52 | /* Create state machine: */
|
---|
53 | m_pStateMachine = new QStateMachine(this);
|
---|
54 |
|
---|
55 | /* Create 'default' state: */
|
---|
56 | QState *pStateDefault = new QState(m_pStateMachine);
|
---|
57 | pStateDefault->assignProperty(this, "stateDefault", true);
|
---|
58 |
|
---|
59 | /* Create 'zoomed' state: */
|
---|
60 | QState *pStateZoomed = new QState(m_pStateMachine);
|
---|
61 | pStateZoomed->assignProperty(this, "stateDefault", false);
|
---|
62 |
|
---|
63 | /* Initial state is 'default': */
|
---|
64 | m_pStateMachine->setInitialState(pStateDefault);
|
---|
65 |
|
---|
66 | /* Zoom animation: */
|
---|
67 | m_pForwardAnimation = new QPropertyAnimation(this, "geometry", this);
|
---|
68 | m_pForwardAnimation->setDuration(m_iAnimationDuration);
|
---|
69 |
|
---|
70 | /* Unzoom animation: */
|
---|
71 | m_pBackwardAnimation = new QPropertyAnimation(this, "geometry", this);
|
---|
72 | m_pBackwardAnimation->setDuration(m_iAnimationDuration);
|
---|
73 |
|
---|
74 | /* Add state transitions: */
|
---|
75 | QSignalTransition *pDefaultToZoomed = pStateDefault->addTransition(this, SIGNAL(sigHoverEnter()), pStateZoomed);
|
---|
76 | pDefaultToZoomed->addAnimation(m_pForwardAnimation);
|
---|
77 |
|
---|
78 | QSignalTransition *pZoomedToDefault = pStateZoomed->addTransition(this, SIGNAL(sigHoverLeave()), pStateDefault);
|
---|
79 | pZoomedToDefault->addAnimation(m_pBackwardAnimation);
|
---|
80 |
|
---|
81 | /* Start state-machine: */
|
---|
82 | m_pStateMachine->start();
|
---|
83 | }
|
---|
84 |
|
---|
85 | int UIGraphicsZoomButton::indent() const
|
---|
86 | {
|
---|
87 | return m_iIndent;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void UIGraphicsZoomButton::setIndent(int iIndent)
|
---|
91 | {
|
---|
92 | m_iIndent = iIndent;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void UIGraphicsZoomButton::updateAnimation()
|
---|
96 | {
|
---|
97 | QRectF oldRect = geometry();
|
---|
98 | QRectF newRect = oldRect;
|
---|
99 | if (m_iDirection & UIGraphicsZoomDirection_Top)
|
---|
100 | newRect.setTop(newRect.top() - m_iIndent);
|
---|
101 | if (m_iDirection & UIGraphicsZoomDirection_Bottom)
|
---|
102 | newRect.setBottom(newRect.bottom() + m_iIndent);
|
---|
103 | if (m_iDirection & UIGraphicsZoomDirection_Left)
|
---|
104 | newRect.setLeft(newRect.left() - m_iIndent);
|
---|
105 | if (m_iDirection & UIGraphicsZoomDirection_Right)
|
---|
106 | newRect.setRight(newRect.right() + m_iIndent);
|
---|
107 | if (!(m_iDirection & UIGraphicsZoomDirection_Left) &&
|
---|
108 | !(m_iDirection & UIGraphicsZoomDirection_Right))
|
---|
109 | {
|
---|
110 | newRect.setLeft(newRect.left() - m_iIndent / 2);
|
---|
111 | newRect.setRight(newRect.right() + m_iIndent / 2);
|
---|
112 | }
|
---|
113 | if (!(m_iDirection & UIGraphicsZoomDirection_Top) &&
|
---|
114 | !(m_iDirection & UIGraphicsZoomDirection_Bottom))
|
---|
115 | {
|
---|
116 | newRect.setTop(newRect.top() - m_iIndent / 2);
|
---|
117 | newRect.setBottom(newRect.bottom() + m_iIndent / 2);
|
---|
118 | }
|
---|
119 | m_pForwardAnimation->setStartValue(oldRect);
|
---|
120 | m_pForwardAnimation->setEndValue(newRect);
|
---|
121 | m_pBackwardAnimation->setStartValue(newRect);
|
---|
122 | m_pBackwardAnimation->setEndValue(oldRect);
|
---|
123 | }
|
---|
124 |
|
---|
125 | QVariant UIGraphicsZoomButton::data(int iKey) const
|
---|
126 | {
|
---|
127 | /* Known key? */
|
---|
128 | switch (iKey)
|
---|
129 | {
|
---|
130 | case GraphicsButton_Margin: return 1;
|
---|
131 | default: break;
|
---|
132 | }
|
---|
133 | /* Call to base-class: */
|
---|
134 | return UIGraphicsButton::data(iKey);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void UIGraphicsZoomButton::hoverEnterEvent(QGraphicsSceneHoverEvent*)
|
---|
138 | {
|
---|
139 | emit sigHoverEnter();
|
---|
140 | }
|
---|
141 |
|
---|
142 | void UIGraphicsZoomButton::hoverLeaveEvent(QGraphicsSceneHoverEvent*)
|
---|
143 | {
|
---|
144 | emit sigHoverLeave();
|
---|
145 | }
|
---|
146 |
|
---|
147 | void UIGraphicsZoomButton::paint(QPainter *pPainter, const QStyleOptionGraphicsItem *pOption, QWidget*)
|
---|
148 | {
|
---|
149 | /* Save painter: */
|
---|
150 | pPainter->save();
|
---|
151 |
|
---|
152 | /* Prepare variables: */
|
---|
153 | int iMargin = data(GraphicsButton_Margin).toInt();
|
---|
154 | QRect paintRect = pOption->rect;
|
---|
155 | paintRect.setTopLeft(paintRect.topLeft() + QPoint(iMargin, iMargin));
|
---|
156 | paintRect.setBottomRight(paintRect.bottomRight() - QPoint(iMargin, iMargin));
|
---|
157 | QIcon icon = data(GraphicsButton_Icon).value<QIcon>();
|
---|
158 | QSize iconSize = data(GraphicsButton_IconSize).toSize();
|
---|
159 |
|
---|
160 | /* Make painter beauty: */
|
---|
161 | pPainter->setRenderHint(QPainter::SmoothPixmapTransform);
|
---|
162 |
|
---|
163 | /* Draw pixmap: */
|
---|
164 | pPainter->drawPixmap(/* Pixmap rectangle: */
|
---|
165 | paintRect,
|
---|
166 | /* Pixmap size: */
|
---|
167 | icon.pixmap(iconSize));
|
---|
168 |
|
---|
169 | /* Restore painter: */
|
---|
170 | pPainter->restore();
|
---|
171 | }
|
---|
172 |
|
---|
173 | bool UIGraphicsZoomButton::isAnimationRunning() const
|
---|
174 | {
|
---|
175 | return m_pForwardAnimation->state() == QAbstractAnimation::Running ||
|
---|
176 | m_pBackwardAnimation->state() == QAbstractAnimation::Running;
|
---|
177 | }
|
---|
178 |
|
---|
179 | bool UIGraphicsZoomButton::stateDefault() const
|
---|
180 | {
|
---|
181 | return m_fStateDefault;
|
---|
182 | }
|
---|
183 |
|
---|
184 | void UIGraphicsZoomButton::setStateDefault(bool fStateDefault)
|
---|
185 | {
|
---|
186 | m_fStateDefault = fStateDefault;
|
---|
187 | }
|
---|
188 |
|
---|