/
applet.cpp
134 lines (116 loc) · 2.97 KB
/
applet.cpp
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
#include "applet.h"
#include <QtCore/QTimer>
#include <QtGui/QPainter>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsSceneMouseEvent>
#include "panelwindow.h"
Applet::Applet(PanelWindow* panelWindow)
: m_panelWindow(panelWindow), m_highlightIntensity(0.0)
{
setZValue(-1.0);
setParentItem(m_panelWindow->panelItem());
}
Applet::~Applet()
{
}
bool Applet::init()
{
m_panelWindow->updateLayout();
return true;
}
void Applet::setPosition(const QPoint& position)
{
m_position = position;
setPos(m_position);
}
void Applet::setSize(const QSize& size)
{
m_size = size;
layoutChanged();
}
void Applet::setInteractive(bool interactive)
{
if(interactive)
{
setAcceptsHoverEvents(true);
setAcceptedMouseButtons(Qt::LeftButton);
}
else
{
setAcceptsHoverEvents(false);
setAcceptedMouseButtons(Qt::NoButton);
}
}
QRectF Applet::boundingRect() const
{
return QRectF(0.0, 0.0, m_size.width(), m_size.height());
}
void Applet::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->setPen(Qt::NoPen);
qreal radius = (m_size.width()*m_size.width() + m_size.height()*m_size.height()) / (4.0 * m_size.height());
QPointF center(m_size.width()/2.0, m_size.height() + radius - m_size.height()/2.0);
static const qreal radiusInc = 10.0;
QRadialGradient gradient(center, radius + radiusInc, center);
gradient.setColorAt((radius - m_size.height()/2.0)/(radius + radiusInc), QColor(255, 255, 255, static_cast<int>(150*m_highlightIntensity)));
gradient.setColorAt(1, QColor(255, 255, 255, 0));
painter->setBrush(QBrush(gradient));
painter->drawRect(boundingRect());
}
void Applet::animateHighlight()
{
static const qreal highlightAnimationSpeed = 0.15;
if(isHighlighted())
{
m_highlightIntensity += highlightAnimationSpeed;
if(m_highlightIntensity > 1.0)
m_highlightIntensity = 1.0;
else
QTimer::singleShot(20, this, SLOT(animateHighlight()));
}
else
{
m_highlightIntensity -= highlightAnimationSpeed;
if(m_highlightIntensity < 0.0)
m_highlightIntensity = 0.0;
else
QTimer::singleShot(20, this, SLOT(animateHighlight()));
}
update();
}
void Applet::clicked()
{
}
void Applet::layoutChanged()
{
}
QPoint Applet::localToScreen(const QPoint& point)
{
return m_panelWindow->pos() + m_position + point;
}
bool Applet::isHighlighted()
{
return isUnderMouse();
}
void Applet::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
animateHighlight();
}
void Applet::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
animateHighlight();
}
void Applet::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
}
void Applet::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
if(isUnderMouse())
{
// FIXME: Workaround.
// For some weird reason, if clicked() function is called directly, and menu is opened,
// this item will receive hover enter event on menu close. But it shouldn't (mouse is outside).
// Probably somehow related to taking a mouse grab when one is already active.
QTimer::singleShot(1, this, SLOT(clicked()));
}
}