-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvolumebutton.cpp
More file actions
81 lines (68 loc) · 1.81 KB
/
volumebutton.cpp
File metadata and controls
81 lines (68 loc) · 1.81 KB
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
#include "volumebutton.h"
VolumeButton::VolumeButton(QWidget *parent) :
QPushButton(parent)
{
this->setMouseTracking(true);
dialog = new VolumeSlider(this);
dialog->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
dialog->setSize(60, 210);
dialog->setMouseTracking(true);
dialog->hide();
volume = 1.0;
real_volume = 1.0;
connect(dialog, SIGNAL(volumeDragged(float)), this, SLOT(setVolume(float)));
connect (this, SIGNAL(toggled(bool)), SLOT(volumeToggled(bool)));
}
VolumeButton::~VolumeButton()
{
delete dialog;
}
void VolumeButton::setVolume(float vol)
{
if (vol < 0 || vol > 1)
return;
volume = vol;
real_volume = vol;
if (volume == 0)
this->setChecked(false);
else
this->setChecked(true);
emit volumeChanged(volume);
}
float VolumeButton::getVolume()
{
return volume;
}
void VolumeButton::leaveEvent(QEvent * e)
{
QPoint cur;
cur.setX(QCursor::pos().x() - this->mapToGlobal(this->rect().topLeft()).x());
cur.setY(QCursor::pos().y() - this->mapToGlobal(this->rect().topLeft()).y());
if (!(cur.x() > this->rect().left()
&& cur.x() < this->rect().right()
&& cur.y() < this->rect().top()))
dialog->hide();
}
void VolumeButton::volumeToggled(bool checked)
{
if (checked)
{
if (real_volume == 0)
real_volume = 0.1;
volume = real_volume;
emit volumeChanged(volume);
dialog->setCur(volume * dialog->MAX_VOL);
}
else
{
volume = 0;
emit volumeChanged(volume);
dialog->setCur(volume * dialog->MAX_VOL);
}
}
void VolumeButton::enterEvent(QEvent * e)
{
dialog->setCur(volume * dialog->MAX_VOL);
dialog->setPos(this->mapToGlobal(QPoint(this->rect().topLeft())), this->width());
dialog->show();
}