-
Notifications
You must be signed in to change notification settings - Fork 11
/
applet.cpp
142 lines (121 loc) · 3.53 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
135
136
137
138
139
140
141
142
#include "applet.h"
#include <QtCore/QTimer>
#include <QtGui/QPainter>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsSceneMouseEvent>
#include "panelwindow.h"
#include "animationutils.h"
Applet::Applet(PanelWindow* panelWindow)
: m_panelWindow(panelWindow), m_highlightIntensity(0.0), m_interactive(false)
{
setZValue(-1.0);
setAcceptedMouseButtons(Qt::RightButton);
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)
{
m_interactive = interactive;
if(m_interactive)
{
setAcceptsHoverEvents(true);
setAcceptedMouseButtons(Qt::RightButton | Qt::LeftButton);
}
else
{
setAcceptsHoverEvents(false);
setAcceptedMouseButtons(Qt::RightButton);
}
}
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)
{
if(m_size.width() < 32)
return; // Too small to draw a background (don't want to deal with weird corner cases).
if(!m_interactive)
return; // Currently, background is only used for highlight on interactive applets.
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);
QColor highlightColor(255, 255, 255, static_cast<int>(150*m_highlightIntensity));
gradient.setColorAt(0.0, highlightColor);
gradient.setColorAt((radius - m_size.height()/2.0)/(radius + radiusInc), highlightColor);
gradient.setColorAt(1.0, QColor(255, 255, 255, 0));
painter->setBrush(QBrush(gradient));
painter->drawRect(boundingRect());
}
void Applet::animateHighlight()
{
static const qreal highlightAnimationSpeed = 0.15;
qreal targetIntensity = isHighlighted() ? 1.0 : 0.0;
bool needAnotherStep = false;
m_highlightIntensity = AnimationUtils::animate(m_highlightIntensity, targetIntensity, highlightAnimationSpeed, needAnotherStep);
if(needAnotherStep)
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())
{
if(event->button() == Qt::LeftButton)
{
// 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()));
}
if(event->button() == Qt::RightButton)
{
m_panelWindow->showPanelContextMenu(m_position + QPoint(static_cast<int>(event->pos().x()), static_cast<int>(event->pos().y())));
}
}
}