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
|
#include "volumebar.h"
#include "playerbutton.h"
#include <QVBoxLayout>
#include <QSettings>
#include <QMouseEvent>
#include <QApplication>
#include <QSettings>
VolumeButton::VolumeButton (QWidget *parent, XClient *client) : QWidget (parent)
{
m_client = client;
setMaximumSize (100, 20);
setToolTip ("Volumebar");
/* Create the slider */
m_slider = new QSlider (Qt::Horizontal, this);
m_slider->hide ();
m_slider->setMaximum (100);
m_slider->setMinimum (0);
m_slider->setTickInterval (5);
m_volbar = new VolumeBar (this);
connect (m_client, SIGNAL (gotConnection (XClient *)), this, SLOT (got_connection (XClient *)));
connect (m_slider, SIGNAL (valueChanged (int)), this, SLOT (set_volume (int)));
/* we add both and let change_settings decide */
m_hbox = new QHBoxLayout (this);
m_hbox->setMargin (0);
m_button = new PlayerButton (this, ":images/volume.png");
m_button->hide ();
connect (m_button, SIGNAL (clicked (QMouseEvent *)),
this, SLOT (volume_pressed (QMouseEvent *)));
m_hbox->addWidget (m_button);
m_hbox->addWidget (m_slider);
connect (m_client->settings (), SIGNAL (settingsChanged ()),
this, SLOT (changed_settings ()));
changed_settings ();
}
void
VolumeButton::changed_settings ()
{
QSettings s;
if (s.value ("ui/volumepopup").toBool ()) {
m_slider->hide ();
m_button->show ();
} else {
m_button->hide ();
m_slider->show ();
}
if (s.value ("ui/volumeinteractive").toBool ()) {
m_slider->setTracking (true);
} else {
m_slider->setTracking (false);
}
}
void
VolumeButton::volume_pressed (QMouseEvent *ev)
{
m_volbar->move (ev->globalPos ());
m_volbar->show ();
}
void
VolumeButton::got_connection (XClient *c)
{
m_client->playback.volumeGet (Xmms::bind (&VolumeButton::handle_volume, this));
m_client->playback.broadcastVolumeChanged (Xmms::bind (&VolumeButton::handle_volume, this));
}
bool
VolumeButton::handle_volume (const Xmms::Dict &d)
{
/* XXX: this function really should do each and sum / split the channels */
if (d.contains ("master")) {
if (!m_slider->isSliderDown ()) {
m_slider->setValue (d.get<uint32_t> ("master"));
m_volbar->setValue (d.get<uint32_t> ("master"));
}
m_channels = 1;
} else if (d.contains ("left") && d.contains ("right")) {
if (!m_slider->isSliderDown ()) {
m_slider->setValue ((d.get<uint32_t> ("left") + d.get<uint32_t> ("right")) / 2);
m_volbar->setValue ((d.get<uint32_t> ("left") + d.get<uint32_t> ("right")) / 2);
}
m_channels = 2;
}
return true;
}
void
VolumeButton::set_volume (int vol)
{
if (m_channels == 1) {
m_client->playback.volumeSet ("master", vol, &XClient::log);
} else {
m_client->playback.volumeSet ("left", vol, &XClient::log);
m_client->playback.volumeSet ("right", vol, &XClient::log);
}
}
VolumeBar::VolumeBar (QWidget *parent) : QWidget (NULL)
{
setWindowFlags (Qt::FramelessWindowHint | Qt::Popup);
m_parent = parent;
m_box = new QVBoxLayout (this);
m_slider = new QSlider (Qt::Vertical, this);
m_slider->setMaximum (100);
m_slider->setMinimum (0);
m_slider->setTickInterval (5);
m_slider->setTracking (false);
connect (m_slider, SIGNAL (valueChanged (int)), parent, SLOT (set_volume (int)));
resize (m_slider->sizeHint ());
m_box->addWidget (m_slider);
setFocusPolicy (Qt::StrongFocus);
hide ();
}
bool
VolumeBar::eventFilter (QObject *obj, QEvent *ev)
{
if (isVisible ()) {
if (ev->type () == QEvent::MouseButtonPress) {
QMouseEvent *mev = (QMouseEvent *) ev;
QPoint globalPos = mapToGlobal (mev->pos ());
if (!(QApplication::widgetAt (globalPos) == this ||
QApplication::widgetAt (globalPos) == m_slider)) {
hide ();
return true;
}
}
}
return QWidget::eventFilter (obj, ev);
}
void
VolumeBar::showEvent (QShowEvent *ev)
{
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
widget->installEventFilter (this);
}
raise ();
}
void
VolumeBar::hideEvent (QHideEvent *ev)
{
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
widget->removeEventFilter (this);
}
}
/*
void
VolumeBar::focusOutEvent (QFocusEvent *ev)
{
hide ();
}
*/
|