forked from m-garanin/zgui-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolumewidget.cpp
162 lines (136 loc) · 5.1 KB
/
volumewidget.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
#include "volumewidget.h"
#include <QLayout>
#include <QPushButton>
#include <QSlider>
#include <QLabel>
#include <QDebug>
#include "IManager.h"
CVolumeWidget::CVolumeWidget(const QString &sourceKey, QWidget *parent) :
QWidget(parent),
_sourceKey(sourceKey),
_volume(.1)
{
init();
}
CVolumeWidget::CVolumeWidget(const QString &sourceKey, qreal volume, QWidget *parent) :
QWidget(parent),
_sourceKey(sourceKey),
_volume(volume)
{
Q_UNUSED(volume);
init();
}
void CVolumeWidget::init()
{
_isMute = true;
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setSpacing(6);
layout->setContentsMargins(0, 0, 0, 0);
QPushButton *pbMute = new QPushButton(this);
pbMute->setProperty("mute", "off");
pbMute->setObjectName("pbMute");
pbMute->setStyleSheet(QString("#pbMute { border-image: url(:/images/mute_off.png); background-color: transparent; max-width: 150px; max-height: 150px; margin-top: 0px; margin-left: 0px; margin-right: 0px;}"));
connect(pbMute, SIGNAL(clicked()), SLOT(onPbMuteClicked()));
layout->addWidget(pbMute);
QVBoxLayout *verticalLayout = new QVBoxLayout();
verticalLayout->setSpacing(0);
lSliderName = new QLabel("Empty", this);
lSliderName->setAlignment(Qt::AlignCenter);
verticalLayout->addWidget(lSliderName);
QString sliderStyle1 = "#volumeSlider::groove:horizontal { \
border: 1px solid #999999; \
height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */ \
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4); \
margin: 2px 0;\
}\
\
#volumeSlider::handle:horizontal {\
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);\
border: 1px solid #5c5c5c;\
width: 18px;\
margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */\
border-radius: 3px;\
}";
QString sliderStyle2 = "#volumeSlider::groove:horizontal { \
border: 0px solid rgb(152, 54, 29); \
height: 8px; \
background: qlineargradient(x1:1, y1:0, x2:0, y2:0, stop:0 rgba(152, 54, 29, 255), stop:1 rgba(254, 211, 59, 255)); \
border: 1px solid rgb(254, 211, 59); \
border-radius: 3px; \
margin: 2px 0; \
} \
\
#volumeSlider::::handle:horizontal { \
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 rgb(152, 54, 29), stop:1 rgb(254, 211, 59)); \
border: 1px solid rgb(254, 211, 59); \
width: 20px; \
margin: -2px 0; \
border-radius: 3px; \
}";
QString sliderStyle = "#volumeSlider::groove:horizontal { \
border: 0px solid rgb(152, 54, 29); \
height: 8px; \
background: qlineargradient(x1:1, y1:0, x2:0, y2:0, stop:0 rgba(255, 0, 0, 255), stop:1 rgba(0, 255, 127, 255)); \
border: 1px solid rgb(254, 211, 59); \
border-radius: 3px; \
margin: 2px 0; \
}\
\
#volumeSlider::handle:horizontal {\
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);\
border: 1px solid #5c5c5c;\
width: 18px;\
margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */\
border-radius: 3px;\
}";
QSlider *slider = new QSlider(this);
slider->setObjectName("volumeSlider");
slider->setStyleSheet(sliderStyle);
slider->setOrientation(Qt::Horizontal);
slider->setTickPosition(QSlider::TicksBothSides);
slider->setTickInterval(1);
slider->setMaximum(50);
connect(slider, SIGNAL(valueChanged(int)), SLOT(onSliderValueChanged(int)));
verticalLayout->addWidget(slider);
layout->addLayout(verticalLayout);
slider->setValue(_volume * 10);
}
void CVolumeWidget::onPbMuteClicked()
{
if(QPushButton * pb = dynamic_cast<QPushButton *>(sender()))
{
if(pb->property("mute").toString().compare("off") == 0)
pb->setProperty("mute", "on");
else
pb->setProperty("mute", "off");
pb->setStyleSheet(QString("#pbMute { border-image: url(:/images/mute_%1.png); background-color: transparent; max-width: 150px; max-height: 150px; margin-top: 0px; margin-left: 0px; margin-right: 0px;}").arg(pb->property("mute").toString()));
qDebug() << "mute: " << pb->property("mute").toString();
}
}
void CVolumeWidget::onSliderValueChanged(int value)
{
_volume = 0.1 * value;
global_manager->setVolume(_sourceKey.toUtf8().data(), _volume);
qDebug() << "Volume: " << _volume;
}
void CVolumeWidget::setText(const QString &text)
{
lSliderName->setText(text);
}
void CVolumeWidget::setVolume(qreal volume)
{
onSliderValueChanged(volume*10);
}
qreal CVolumeWidget::volume() const
{
return _volume;
}
void CVolumeWidget::setMute(bool mute)
{
_isMute = mute;
global_manager->setVolume(_sourceKey.toUtf8().data(), _isMute?0:_volume);
}
bool CVolumeWidget::isMute() const
{
return _isMute;
}