Project

General

Profile

equalizer-qt.cc

(libaudqt) - Jim Turner, December 03, 2019 05:30

 
1
/*
2
 * equalizer.cc
3
 * Copyright 2014 William Pitcock
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions, and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 *    this list of conditions, and the following disclaimer in the documentation
13
 *    provided with the distribution.
14
 *
15
 * This software is provided "as is" and without any warranty, express or
16
 * implied. In no event shall the authors be liable for any damages arising from
17
 * the use of this software.
18
 */
19

    
20
#include <QCheckBox>
21
#include <QDialog>
22
#include <QFrame>
23
#include <QHBoxLayout>
24
#include <QLabel>
25
#include <QPainter>
26
#include <QPushButton>
27
#include <QToolButton>
28
#include <QSlider>
29
#include <QStyle>
30
#include <QVBoxLayout>
31

    
32
#include <libfauxdcore/equalizer.h>
33
#include <libfauxdcore/hook.h>
34
#include <libfauxdcore/i18n.h>
35
#include <libfauxdcore/runtime.h>
36

    
37
#include "libfauxdqt.h"
38

    
39
class VLabel : public QLabel
40
{
41
public:
42
    VLabel (const QString & text, QWidget * parent = nullptr) :
43
        QLabel (text, parent) {}
44

    
45
    QSize minimumSizeHint () const
46
    {
47
        QSize s = QLabel::minimumSizeHint ();
48
        return QSize (s.height (), s.width ());
49
    }
50

    
51
    QSize sizeHint () const
52
    {
53
        QSize s = QLabel::sizeHint ();
54
        return QSize (s.height (), s.width ());
55
    }
56

    
57
    void paintEvent (QPaintEvent *)
58
    {
59
        QPainter p (this);
60
        p.rotate (270);
61

    
62
        QRect box (-height (), 0, height (), width ());
63
        style ()->drawItemText (& p, box, (int) alignment (), palette (),
64
         isEnabled (), text (), QPalette::Foreground);
65
    }
66
};
67

    
68
class EqualizerSlider : public QWidget
69
{
70
public:
71
    QSlider slider;
72

    
73
    EqualizerSlider (const char * label, QWidget * parent) :
74
        QWidget (parent),
75
        slider (Qt::Vertical)
76
    {
77
        slider.setMinimumHeight (audqt::sizes.OneInch);
78
        slider.setRange (-AUD_EQ_MAX_GAIN, AUD_EQ_MAX_GAIN);
79
        slider.setTickInterval (AUD_EQ_MAX_GAIN >> 1);
80
        slider.setTickPosition (QSlider::TicksBothSides);
81

    
82
        auto layout = audqt::make_vbox (this);
83
        auto value_label = new QLabel ("0");
84

    
85
        layout->addWidget (new VLabel (label, this), 1, Qt::AlignCenter);
86
        layout->addWidget (& slider, 0, Qt::AlignCenter);
87
        layout->addWidget (value_label, 0, Qt::AlignCenter);
88

    
89
        connect (& slider, & QSlider::valueChanged, [value_label] (int value) {
90
            value_label->setText (QString::number (value));
91
        });
92
    }
93
};
94

    
95
class EqualizerWindow : public QDialog
96
{
97
public:
98
    EqualizerWindow ();
99

    
100
private:
101
    QCheckBox m_onoff_checkbox;
102
    QToolButton * preset_button;
103
    EqualizerSlider * m_preamp_slider;
104
    EqualizerSlider * m_sliders[AUD_EQ_NBANDS];
105

    
106
    void updateActive ();
107
    void updatePreamp ();
108
    void updateBands ();
109
    void updateSongAuto ();
110

    
111
    const HookReceiver<EqualizerWindow>
112
     hook1 {"set equalizer_active", this, & EqualizerWindow::updateActive},
113
     hook2 {"set equalizer_preamp", this, & EqualizerWindow::updatePreamp},
114
     hook3 {"set equalizer_bands", this, & EqualizerWindow::updateBands},
115
     hook4 {"set equalizer_songauto", this, & EqualizerWindow::updateSongAuto};
116
};
117

    
118
EqualizerWindow::EqualizerWindow () :
119
    m_onoff_checkbox (audqt::translate_str (N_("_Enable")))
120
{
121
    const char * const names[AUD_EQ_NBANDS] = {N_("31 Hz"), N_("63 Hz"),
122
     N_("125 Hz"), N_("250 Hz"), N_("500 Hz"), N_("1 kHz"), N_("2 kHz"),
123
     N_("4 kHz"), N_("8 kHz"), N_("16 kHz")};
124

    
125
    auto slider_container = new QWidget (this);
126
    auto slider_layout = audqt::make_hbox (slider_container, audqt::sizes.TwoPt);
127

    
128
    m_preamp_slider = new EqualizerSlider (_("Preamp"), this);
129
    slider_layout->addWidget (m_preamp_slider);
130

    
131
    auto line = new QFrame (this);
132
    line->setFrameShape (QFrame::VLine);
133
    line->setFrameShadow (QFrame::Sunken);
134
    slider_layout->addWidget (line);
135

    
136
    for (int i = 0; i < AUD_EQ_NBANDS; i ++)
137
    {
138
        m_sliders[i] = new EqualizerSlider (_(names[i]), this);
139
        slider_layout->addWidget (m_sliders[i]);
140
    }
141

    
142
    auto hide_button = new QPushButton (_("Hide"), this);
143
    auto zero_button = new QPushButton (_("Flat"), this);
144
    preset_button = new QToolButton (this);
145
    preset_button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
146
    if (aud_get_bool (nullptr, "equalizer_songauto"))
147
    {
148
        preset_button->setIcon(audqt::get_icon ("list-add"));
149
        preset_button->setText(_("PreSETs!"));
150
    }
151
    else
152
    {
153
        preset_button->setIcon(audqt::get_icon ("list-remove"));
154
        preset_button->setText(_("Presets"));
155
    }
156
    auto auto_checkbox = new QCheckBox (_("Auto"), this);
157
    auto hbox = audqt::make_hbox (nullptr);
158

    
159
    hbox->addWidget (& m_onoff_checkbox);
160
    hbox->addStretch (1);
161
    hbox->addWidget (hide_button);
162
    hbox->addWidget (zero_button);
163
    hbox->addWidget (preset_button);
164
    hbox->addWidget (auto_checkbox);
165

    
166
    auto layout = audqt::make_vbox (this);
167
    layout->setSizeConstraint (QLayout::SetFixedSize);
168
    layout->addLayout (hbox);
169
    layout->addWidget (slider_container);
170

    
171
    setWindowTitle (_("Equalizer"));
172
    setContentsMargins (audqt::margins.EightPt);
173

    
174
    m_onoff_checkbox.setFocus ();
175

    
176
    updateActive ();
177
    updatePreamp ();
178
    updateBands ();
179

    
180
    auto_checkbox->setCheckState (aud_get_bool (nullptr, "equalizer_autoload") 
181
            ? Qt::Checked : Qt::Unchecked);
182

    
183
    connect (& m_onoff_checkbox, & QCheckBox::stateChanged, [] (int state) {
184
        aud_set_bool (nullptr, "equalizer_active", (state == Qt::Checked));
185
    });
186

    
187
    connect (hide_button, & QPushButton::clicked, [] () {
188
        audqt::equalizer_hide ();
189
    });
190

    
191
    connect (zero_button, & QPushButton::clicked, [] () {
192
        aud_eq_apply_preset (EqualizerPreset ());
193
    });
194

    
195
    connect (preset_button, & QPushButton::clicked, audqt::eq_presets_show);
196

    
197
    connect (auto_checkbox, & QCheckBox::stateChanged, [] (int state) {
198
        aud_set_bool (nullptr, "equalizer_autoload", (state == Qt::Checked));
199
    });
200

    
201
    connect (& m_preamp_slider->slider, & QSlider::valueChanged, [] (int value) {
202
        aud_set_int (nullptr, "equalizer_preamp", value);
203
    });
204

    
205
    for (int i = 0; i < AUD_EQ_NBANDS; i ++)
206
    {
207
        connect (& m_sliders[i]->slider, & QSlider::valueChanged, [i] (int value) {
208
            aud_eq_set_band (i, value);
209
        });
210
    }
211
}
212

    
213
void EqualizerWindow::updateActive ()
214
{
215
    bool active = aud_get_bool (nullptr, "equalizer_active");
216
    m_onoff_checkbox.setCheckState (active ? Qt::Checked : Qt::Unchecked);
217
}
218

    
219
void EqualizerWindow::updatePreamp ()
220
{
221
    m_preamp_slider->slider.setValue (aud_get_int (nullptr, "equalizer_preamp"));
222
}
223

    
224
void EqualizerWindow::updateBands ()
225
{
226
    double values[AUD_EQ_NBANDS];
227
    aud_eq_get_bands (values);
228

    
229
    for (int i = 0; i < AUD_EQ_NBANDS; i ++)
230
        m_sliders[i]->slider.setValue (values[i]);
231
}
232

    
233
void EqualizerWindow::updateSongAuto ()
234
{
235
    if (aud_get_bool (nullptr, "equalizer_songauto"))
236
    {
237
        preset_button->setIcon(audqt::get_icon ("list-add"));
238
        preset_button->setText(_("PreSETs!"));
239
    }
240
    else
241
    {
242
        preset_button->setIcon(audqt::get_icon ("list-remove"));
243
        preset_button->setText(_("Presets"));
244
    }
245
}
246

    
247
static EqualizerWindow * s_equalizer = nullptr;
248

    
249
namespace audqt {
250

    
251
EXPORT void equalizer_show ()
252
{
253
    if (! s_equalizer)
254
    {
255
        s_equalizer = new EqualizerWindow;
256
        s_equalizer->setAttribute (Qt::WA_DeleteOnClose);
257

    
258
        QObject::connect (s_equalizer, & QObject::destroyed, [] () {
259
            s_equalizer = nullptr;
260
        });
261
    }
262

    
263
    window_bring_to_front (s_equalizer);
264
}
265

    
266
EXPORT void equalizer_hide ()
267
{
268
    delete s_equalizer;
269
}
270

    
271
} // namespace audqt