-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathosd.cpp
202 lines (159 loc) · 4.86 KB
/
osd.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
201
202
#include "osd.h"
OSD::OSD(QWidget *parent) :
QWidget(parent, Qt::Window |
Qt::X11BypassWindowManagerHint |
Qt::FramelessWindowHint),
osd_menu (new QMenu (this)),
m_dragging(false),
osd_interr (false),
osd_idx (0),
// 200k
osd_maxSpeed (200 * 1024)
{
memset (osd_data, 0, sizeof (osd_data));
setAcceptDrops(true);
setFixedSize(40 , 40);
// show at right corner
QDesktopWidget *desktop = QApplication::desktop();
move (desktop->width() - 100, 30);
show ();
QSettings settings;
settings.beginGroup("OSD");
restoreGeometry(settings.value("Geometry").toByteArray());
}
OSD::~OSD()
{
QSettings settings;
settings.beginGroup("OSD");
settings.setValue("Geometry", saveGeometry());
}
void OSD::setText(const QString &text)
{
m_text = text;
}
void OSD::mouseDoubleClickEvent(QMouseEvent *)
{
emit doubleClicked();
}
void OSD::dragEnterEvent(QDragEnterEvent *e)
{
emit requestDownload( e->mimeData()->text() );
}
void OSD::contextMenuEvent(QContextMenuEvent *e)
{
osd_menu->exec (e->globalPos());
}
void OSD::clear()
{
osd_interr = true;
update ();
}
void OSD::mousePressEvent( QMouseEvent* e )
{
m_dragOffset = e->pos();
if(e->button() == Qt::LeftButton && !m_dragging)
{
grabMouse( Qt::SizeAllCursor );
m_dragging = true;
}
}
void OSD::mouseReleaseEvent( QMouseEvent* )
{
if(m_dragging)
{
m_dragging = false;
releaseMouse();
}
}
void OSD::mouseMoveEvent( QMouseEvent* e )
{
if( m_dragging && this == mouseGrabber() ) {
// check if the osd has been dragged out of the current screen
int currentScreen = QApplication::desktop()->screenNumber( e->globalPos() );
if( currentScreen != -1 )
m_screen = currentScreen;
const QRect screen = QApplication::desktop()->screenGeometry( m_screen );
// make sure the position is valid
m_position = fixupPosition( e->globalPos() - m_dragOffset - screen.topLeft() );
// move us to the new position
move( m_position );
// fix the position
int midH = screen.width()/2;
int midV = screen.height()/2;
if( m_position.x() + width() > midH )
m_position.rx() += width();
if( m_position.y() + height() > midV )
m_position.ry() += height();
}
}
QPoint OSD::fixupPosition( const QPoint& pp )
{
QPoint p(pp);
const QRect& screen = QApplication::desktop()->screenGeometry( m_screen );
int maxY = screen.height() - height() - s_outerMargin;
int maxX = screen.width() - width() - s_outerMargin;
if( p.y() < s_outerMargin )
p.ry() = s_outerMargin;
else if( p.y() > maxY )
p.ry() = maxY;
if( p.x() < s_outerMargin )
p.rx() = s_outerMargin;
else if( p.x() > maxX )
p.rx() = screen.width() - s_outerMargin - width();
p += screen.topLeft();
return p;
}
void OSD::add(const int &speed)
{
if (osd_interr) osd_interr = false;
osd_data [osd_idx ++] = speed;
if (osd_idx > sizeof(osd_data)) osd_idx = 0;
update ();
}
void OSD::setMaxSpeedIndicator(const int &maxSpeed)
{
osd_maxSpeed = maxSpeed;
}
void OSD::paintEvent(QPaintEvent *)
{
QPainter painter (this);
painter.setPen( QColor (Qt::black) );
// draw the background and the frame
QRect thisRect(0, 0, width() - 1 , height() - 1);
painter.fillRect(thisRect, QColor::fromRgb(58, 117, 177) );
if (! osd_interr)
{
// progress graph
painter.setPen(QColor ("#3AD5FF"));
int square_width_each = ( width() - 2 ) / sizeof(osd_data);
int max_square_height = height() * 0.6;
int x = 0.5 * ( width() - sizeof(osd_data) * square_width_each ) , y = 0;
for (size_t i = osd_idx ; i < sizeof(osd_data) + osd_idx; ++ i)
{
if ( osd_data [i % sizeof(osd_data)] > osd_maxSpeed )
{
QRect rect (x, height() - 1 - max_square_height,
square_width_each , max_square_height);
painter.fillRect(rect, QBrush (QColor ("#3AD5FF")) );
}
else
{
y = max_square_height *
(double)osd_data [i % sizeof(osd_data)]
/ osd_maxSpeed;
QRect rect (x, height() - 1 - y , square_width_each , y );
painter.fillRect( rect , QBrush (QColor ("#3AD5FF")) );
}
x += square_width_each;
}
// progress text
painter.setPen(QColor(Qt::white));
QFontMetrics fm (font ());
QRect fontRect ( (width() - fm.width(m_text)) / 2,
2, fm.width(m_text), fm.height());
painter.drawText(fontRect, Qt::AlignCenter, m_text);
}
// frame
painter.setPen( QColor (Qt::black) );
painter.drawRect( thisRect );
}