/
psitrayicon.cpp
200 lines (167 loc) · 4.37 KB
/
psitrayicon.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <QPixmap>
#include <QPixmapCache>
#include <QApplication> // old
#include <QSystemTrayIcon>
#include "psitrayicon.h"
#include "iconset.h"
#include "alerticon.h"
#include "psioptions.h"
// TODO: remove the QPoint parameter from the signals when we finally move
// to the new system.
PsiTrayIcon::PsiTrayIcon(const QString &tip, QMenu *popup, QObject *parent)
: QObject(parent)
{
icon_ = NULL;
trayicon_ = NULL;
trayicon_ = new QSystemTrayIcon();
trayicon_->setContextMenu(popup);
setToolTip(tip);
connect(trayicon_,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),SLOT(trayicon_activated(QSystemTrayIcon::ActivationReason)));
}
PsiTrayIcon::~PsiTrayIcon()
{
delete trayicon_;
//delete icon_;
if ( icon_ ) {
icon_->disconnect();
icon_->stop();
}
}
void PsiTrayIcon::setContextMenu(QMenu* menu)
{
trayicon_->setContextMenu(menu);
}
void PsiTrayIcon::setToolTip(const QString &str)
{
trayicon_->setToolTip(str);
}
void PsiTrayIcon::setIcon(const PsiIcon *icon, bool /*alert*/)
{
if ( icon_ ) {
icon_->disconnect();
icon_->stop();
// delete icon_;
// icon_ = 0;
}
icon_ = (PsiIcon*)icon;
if ( icon ) {
connect(icon_, SIGNAL(pixmapChanged()), SLOT(animate()));
icon_->activated();
// if ( !alert )
// icon_ = new PsiIcon(*icon);
// else
// icon_ = new AlertIcon(icon);
// connect(icon_, SIGNAL(pixmapChanged()), SLOT(animate()));
// icon_->activated();
}
//else
//icon_ = new PsiIcon();
animate();
}
void PsiTrayIcon::setAlert(const PsiIcon *icon)
{
setIcon(icon, true);
}
bool PsiTrayIcon::isAnimating() const
{
return icon_->isAnimated();
}
bool PsiTrayIcon::isWMDock()
{
return false;
}
void PsiTrayIcon::show()
{
trayicon_->show();
}
void PsiTrayIcon::hide()
{
trayicon_->hide();
}
// a function to blend 2 pixels taking their alpha channels
// into consideration
// p1 is in the 1st layer, p2 is in the 2nd layer (over p1)
QRgb PsiTrayIcon::pixelBlend(QRgb p1, QRgb p2)
{
int a2 = qAlpha(p2);
if (a2 == 255) return p2; // don't calculate anything if p2 is completely opaque
int a1 = qAlpha(p1);
double prop1 = double(a1*(255-a2))/double(255*255);
double prop2 = double(a2)/255.0;
int r = int( qRed(p1)*prop1 + qRed(p2)*prop2 );
int g = int( qGreen(p1)*prop1 + qGreen(p2)*prop2 );
int b = int( qBlue(p1)*prop1 + qBlue(p2)*prop2 );
return qRgba(r, g, b, (a1>a2) ? a1:a2);
}
QPixmap PsiTrayIcon::makeIcon()
{
if ( !icon_ )
return QPixmap();
#ifdef HAVE_X11
// on X11, the KDE dock is 22x22. let's make our icon_ "seem" bigger.
QImage real(22,22,QImage::Format_ARGB32);
QImage in = icon_->image();
in.detach();
// make sure it is no bigger than 16x16
if(in.width() > 16 || in.height() > 16)
in = in.scaled(16,16,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
int xo = (real.width() - in.width()) / 2;
int yo = (real.height() - in.height()) / 2;
int n, n2;
// clear the output and make it transparent
// deprecates real.fill(0)
for(n2 = 0; n2 < real.height(); ++n2)
for(n = 0; n < real.width(); ++n)
real.setPixel(n, n2, qRgba(0,0,0,0));
// draw a dropshadow
for(n2 = 0; n2 < in.height(); ++n2) {
for(n = 0; n < in.width(); ++n) {
if(int a = qAlpha(in.pixel(n,n2))) {
int x = n + xo + 2;
int y = n2 + yo + 2;
real.setPixel(x, y, qRgba(0x80,0x80,0x80,a));
}
}
}
// draw the image
for(n2 = 0; n2 < in.height(); ++n2) {
for(n = 0; n < in.width(); ++n) {
if(qAlpha(in.pixel(n,n2))) {
QRgb pold = real.pixel(n + xo , n2 + yo);
QRgb pnew = in.pixel(n , n2);
real.setPixel(n + xo, n2 + yo, pixelBlend(pold, pnew));
}
}
}
return QPixmap::fromImage(real);
#else
return icon_->pixmap();
#endif
}
void PsiTrayIcon::trayicon_activated(QSystemTrayIcon::ActivationReason reason)
{
#ifdef Q_OS_MAC
Q_UNUSED(reason)
#else
if (reason == QSystemTrayIcon::Trigger)
emit clicked(QPoint(),Qt::LeftButton);
else if (reason == QSystemTrayIcon::MiddleClick)
emit clicked(QPoint(),Qt::MidButton);
else if (reason == QSystemTrayIcon::DoubleClick)
emit doubleClicked(QPoint());
#endif
}
void PsiTrayIcon::animate()
{
if ( !icon_ )
return;
QString cachedName = "PsiTray/" + PsiOptions::instance()->getOption("options.iconsets.status").toString()
+ "/" + icon_->name() + "/" + QString::number(intptr_t(icon_)) + "/"
+ QString::number( icon_->frameNumber() );
QPixmap p;
if ( !QPixmapCache::find(cachedName, p) ) {
p = makeIcon();
QPixmapCache::insert( cachedName, p );
}
trayicon_->setIcon(p);
}