1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#include <QPainter>
#include <QGraphicsProxyWidget>
#include <QGraphicsRotation>
#include <QList>
#include <QGraphicsTransform>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QState>
#include <QEventTransition>
#include <QGraphicsSceneHoverEvent>
#include "graphicstoolbar.h"
bool HoverOutEventTransition::eventTest(QEvent *e) {
if (! QEventTransition::eventTest(e))
return false;
QGraphicsWidget *toolbar = qobject_cast<QGraphicsWidget*>(eventSource());
// if focus is now on a widget contained within the toolbar, don't transition
if (toolbar && toolbar->scene() && toolbar->isAncestorOf(toolbar->scene()->focusItem()))
return false;
return true;
}
GraphicsToolBar::GraphicsToolBar(QGraphicsScene *scene, QGraphicsItem * parent, Qt::WindowFlags wFlags)
: QGraphicsWidget(parent, wFlags)
, m_scene(scene)
, m_fillLevel(100)
{
m_scene->addItem(this);
m_layout = new QGraphicsLinearLayout(Qt::Horizontal, this);
setLayout(m_layout);
layout()->setContentsMargins(20, 20, 20, 20);
setFlag(QGraphicsItem::ItemIsMovable);
setAcceptHoverEvents(true);
m_fillLevelBrush = QBrush(Qt::transparent);
QLinearGradient bgGradient;
bgGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
bgGradient.setStart(0, 0);
bgGradient.setFinalStop(0, 1);
bgGradient.setColorAt(0, QColor(Qt::gray).lighter(100));
bgGradient.setColorAt(1, QColor(Qt::gray).lighter(200));
m_bgBrush = bgGradient;
QPalette palette;
palette.setBrush(QPalette::Background, Qt::transparent);
setPalette(palette);
setOpacity(0.7);
m_blurEffect = new QGraphicsBlurEffect(this);
m_blurEffect->setProperty("blurRadius", 5);
setGraphicsEffect(m_blurEffect);
setGraphicsEffectsEnabled(true);
m_rotation = new QGraphicsRotation(this);
m_rotation->setAxis(Qt::YAxis);
m_rotation->setAngle(0);
QList<QGraphicsTransform *> txs;
txs << m_rotation;
setTransformations(txs);
QState *blurredState = new QState;
QState *normalState = new QState;
m_stateMachine.addState(blurredState);
m_stateMachine.addState(normalState);
m_stateMachine.setInitialState(normalState);
m_makeBlurred = new HoverOutEventTransition(this, QEvent::GraphicsSceneHoverLeave, normalState);
m_makeBlurred->setTargetState(blurredState);
m_makeNormal = new QEventTransition(this, QEvent::GraphicsSceneHoverEnter, blurredState);
m_makeNormal->setTargetState(normalState);
QParallelAnimationGroup *makeBlurAnimation = new QParallelAnimationGroup(this);
QParallelAnimationGroup *makeNormalAnimation = new QParallelAnimationGroup(this);
QPropertyAnimation *blurAnimation = new QPropertyAnimation(m_blurEffect, "blurRadius", makeBlurAnimation);
blurAnimation->setEndValue(5);
QPropertyAnimation *transparentizeAnimation = new QPropertyAnimation(this, "opacity", makeBlurAnimation);
transparentizeAnimation->setEndValue(0.7);
makeBlurAnimation->addAnimation(blurAnimation);
makeBlurAnimation->addAnimation(transparentizeAnimation);
QPropertyAnimation *unblurAnimation = new QPropertyAnimation(m_blurEffect, "blurRadius", makeNormalAnimation);
unblurAnimation->setEndValue(0);
QPropertyAnimation *untransparentizeAnimation = new QPropertyAnimation(this, "opacity", makeBlurAnimation);
untransparentizeAnimation->setEndValue(1.0);
makeNormalAnimation->addAnimation(unblurAnimation);
makeNormalAnimation->addAnimation(untransparentizeAnimation);
m_makeBlurred->addAnimation(makeBlurAnimation);
m_makeNormal->addAnimation(makeNormalAnimation);
m_stateMachine.start();
}
void GraphicsToolBar::setGraphicsEffectsEnabled(bool enabled) {
m_isGraphicsEffectsEnabled = enabled;
if (!enabled)
setGraphicsEffect(0);
else
setGraphicsEffect(m_blurEffect);
}
bool GraphicsToolBar::graphicsEffectsEnabled() const {
return m_isGraphicsEffectsEnabled;
}
QGraphicsRotation* GraphicsToolBar::rotation() const {
return m_rotation;
}
void GraphicsToolBar::addWidget(QWidget *widget) {
QGraphicsWidget *gw = m_scene->addWidget(widget);
m_layout->addItem(gw);
}
void GraphicsToolBar::setBackgroundBrush(QBrush brush) {
m_bgBrush = brush;
}
QBrush GraphicsToolBar::backgroundBrush() const {
return m_bgBrush;
}
void GraphicsToolBar::setFillLevelBrush(QBrush brush) {
m_fillLevelBrush = brush;
}
QBrush GraphicsToolBar::fillLevelBrush() const {
return m_fillLevelBrush;
}
void GraphicsToolBar::setFillLevel(int percent) {
m_fillLevel = qMax(0, qMin(percent, 100));
}
void GraphicsToolBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(m_bgBrush);
painter->drawRoundedRect(boundingRect(), 10, 10);
if (m_fillLevel < 100) {
painter->setClipRect(boundingRect().adjusted(boundingRect().width() * (m_fillLevel / 100.0), 0, 0, 0));
painter->setBrush(m_fillLevelBrush);
painter->drawRoundedRect(boundingRect(), 10, 10);
}
painter->restore();
}
void GraphicsToolBar::transitionBlurEnabledTo(bool enabled) {
if (! graphicsEffectsEnabled())
return;
QParallelAnimationGroup *animGroup = new QParallelAnimationGroup(this);
QPropertyAnimation *blurAnimation = new QPropertyAnimation(m_blurEffect, "blurRadius", animGroup);
blurAnimation->setStartValue(enabled ? 0 : 5);
blurAnimation->setEndValue(enabled? 5 : 0);
blurAnimation->setDuration(enabled? 200 : 200);
QPropertyAnimation *opacityAnimation = new QPropertyAnimation(this, "opacity", animGroup);
opacityAnimation->setStartValue(enabled? 1.0 : 0.7);
opacityAnimation->setEndValue(enabled? 0.7 : 1.0);
animGroup->addAnimation(blurAnimation);
animGroup->addAnimation(opacityAnimation);
animGroup->start(QAbstractAnimation::DeleteWhenStopped);
}
void GraphicsToolBar::show() {
QGraphicsWidget::show();
setOpacity(1.0);
m_blurEffect->setProperty("blurRadius", 0);
}
void GraphicsToolBar::hide() {
QGraphicsWidget::hide();
}
|