summaryrefslogtreecommitdiff
path: root/src/libaudqt/volumebutton.cc
blob: ca253102d528e10c12e70e90b352def53e693a9c (plain)
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
/*
 * volumebutton.cc
 * Copyright 2014 William Pitcock
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "libaudqt.h"

#include <QFrame>
#include <QIcon>
#include <QSlider>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWheelEvent>

#include <libaudcore/drct.h>
#include <libaudcore/hook.h>

namespace audqt {

class VolumeButton : public QToolButton
{
public:
    VolumeButton (QWidget * parent = nullptr);

private:
    void updateIcon (int val);
    void updateVolume ();
    void showSlider ();
    void setVolume (int val);
    QToolButton * newSliderButton (int delta);

    void wheelEvent (QWheelEvent * e);

    QSlider * m_slider;
    QFrame * m_container;
};

VolumeButton::VolumeButton (QWidget * parent) :
    QToolButton (parent)
{
    setFocusPolicy (Qt::NoFocus);

    m_container = new QFrame (this, Qt::Popup);
    m_container->setFrameShape (QFrame::StyledPanel);

    auto layout = new QVBoxLayout (m_container);
    layout->setSpacing (0);
    layout->setMargin (2);

    m_slider = new QSlider (Qt::Vertical, this);
    m_slider->setRange (0, 100);
    m_slider->setSingleStep (2);
    m_slider->setPageStep (20);

    layout->addWidget (newSliderButton (5));
    layout->addWidget (m_slider);
    layout->addWidget (newSliderButton (-5));

    int val = aud_drct_get_volume_main ();
    m_slider->setValue (val);
    updateIcon (val);

    connect (this, & QAbstractButton::clicked, this, & VolumeButton::showSlider);
    connect (m_slider, & QAbstractSlider::valueChanged, this, & VolumeButton::setVolume);

    auto timer = new Timer<VolumeButton> (TimerRate::Hz4, this, & VolumeButton::updateVolume);
    connect (this, & QObject::destroyed, [timer] () { delete timer; });

    timer->start ();
}

void VolumeButton::updateIcon (int val)
{
    if (val == 0)
        setIcon (QIcon::fromTheme ("audio-volume-muted"));
    else if (val > 0 && val < 35)
        setIcon (QIcon::fromTheme ("audio-volume-low"));
    else if (val >= 35 && val < 70)
        setIcon (QIcon::fromTheme ("audio-volume-medium"));
    else if (val >= 70)
        setIcon (QIcon::fromTheme ("audio-volume-high"));

    setToolTip (QString ("%1 %").arg (val));
}

void VolumeButton::updateVolume ()
{
    if (m_slider->isSliderDown ())
        return;

    int val = aud_drct_get_volume_main ();
    if (val != m_slider->value ())
    {
        disconnect (m_slider, nullptr, this, nullptr);
        m_slider->setValue (val);
        updateIcon (val);
        connect (m_slider, & QAbstractSlider::valueChanged, this, & VolumeButton::setVolume);
    }
}

void VolumeButton::showSlider ()
{
    QSize button_size = sizeHint ();
    QSize container_size = m_container->sizeHint ();

    int dx = container_size.width () / 2 - button_size.width () / 2;
    int dy = container_size.height () / 2 - button_size.height () / 2;

    QPoint pos = mapToGlobal (QPoint (0, 0));
    pos += QPoint (-dx, -dy);

    m_container->move (pos);
    window_bring_to_front (m_container);
}

void VolumeButton::setVolume (int val)
{
    aud_drct_set_volume_main (val);
    updateIcon (val);
}

QToolButton * VolumeButton::newSliderButton (int delta)
{
    auto button = new QToolButton (this);
    button->setText (delta < 0 ? "-" : "+");
    button->setAutoRaise (true);
    button->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Preferred);

    connect (button, & QAbstractButton::clicked, [this, delta] () {
        int val = aud_drct_get_volume_main ();
        m_slider->setValue (val + delta);
    });

    return button;
}

void VolumeButton::wheelEvent (QWheelEvent * e)
{
    int val = m_slider->value ();
    int y = e->angleDelta ().y ();

    if (y < 0)
        m_slider->setValue (-- val);
    else
        m_slider->setValue (++ val);
}

EXPORT QToolButton * volume_button_new (QWidget * parent)
{
    return new VolumeButton (parent);
}

} // namespace audqt