summaryrefslogtreecommitdiff
path: root/src/ui/alsa/alsadevicespage.cpp
blob: 2b7ede0afb1973ac9f2b3d35d1bee1d5b42c9db7 (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
#include "alsadevicespage.h"
#include "../../model/alsamodel.h"
#include "../../model/alsadevice.h"
#include "../kledbutton.h"

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqscrollview.h>
#include <tqframe.h>
#include <tdelocale.h>

// ── AlsaSwitchRow ─────────────────────────────────────────────────────────

AlsaSwitchRow::AlsaSwitchRow( AlsaDevice *dev, TQWidget *parent )
    : TQWidget(parent), m_dev(dev)
{
    TQColor ledColor = ( dev->category() == AlsaDevice::Input ) ? TQt::red : TQt::yellow;
    m_led = new KLedButton( ledColor,
        dev->muted() ? KLed::Off : KLed::On,
        KLed::Sunken, KLed::Circular, this );
    m_led->setFixedSize(14, 14);

    TQHBoxLayout *hbox = new TQHBoxLayout(this, 2, 4);
    hbox->addWidget(m_led);
    hbox->addWidget( new TQLabel(dev->name(), this) );
    hbox->addStretch(1);

    connect(dev,   TQ_SIGNAL(muteChanged(bool)),   this, TQ_SLOT(onMuteChanged(bool)));
    connect(m_led, TQ_SIGNAL(stateChanged(bool)),  this, TQ_SLOT(onLedToggled(bool)));
}

void AlsaSwitchRow::onMuteChanged( bool m )
{
    m_led->blockSignals(true);
    m_led->setState( m ? KLed::Off : KLed::On );
    m_led->blockSignals(false);
}

void AlsaSwitchRow::onLedToggled( bool on )
{
    m_dev->setMuted( !on );
}

// ── AlsaEnumRow ──────────────────────────────────────────────────────────────

AlsaEnumRow::AlsaEnumRow( AlsaDevice *dev, TQWidget *parent )
    : TQWidget(parent), m_dev(dev)
{
    m_combo = new TQComboBox(false, this);
    TQStringList vals = dev->enumValues();
    for ( TQStringList::ConstIterator it = vals.begin(); it != vals.end(); ++it )
        m_combo->insertItem(*it);
    m_combo->setCurrentItem( dev->enumId() );

    TQHBoxLayout *hbox = new TQHBoxLayout(this, 2, 6);
    hbox->addWidget( new TQLabel(dev->name() + ":", this) );
    hbox->addWidget(m_combo, 1);

    connect(dev,     TQ_SIGNAL(enumChanged(int)), this, TQ_SLOT(onEnumChanged(int)));
    connect(m_combo, TQ_SIGNAL(activated(int)),   this, TQ_SLOT(onComboActivated(int)));
}

void AlsaEnumRow::onEnumChanged( int id )
{
    m_combo->blockSignals(true);
    m_combo->setCurrentItem(id);
    m_combo->blockSignals(false);
}

void AlsaEnumRow::onComboActivated( int idx )
{
    m_dev->setEnumId(idx);
}

// ── AlsaDevicesPage ──────────────────────────────────────────────────────────

AlsaDevicesPage::AlsaDevicesPage( AlsaModel *model, TQWidget *parent )
    : TQWidget(parent), m_model(model), m_cardIndex(-1), m_content(0)
{
    TQVBoxLayout *outer = new TQVBoxLayout(this, 8, 4);

    m_scroll = new TQScrollView(this);
    m_scroll->setResizePolicy( TQScrollView::AutoOneFit );
    m_scroll->setFrameStyle( TQFrame::NoFrame );
    outer->addWidget(m_scroll, 1);

    m_content = new TQWidget( m_scroll->viewport() );
    m_scroll->addChild(m_content);

    connect(model, TQ_SIGNAL(deviceAdded(AlsaDevice*)), this, TQ_SLOT(onDeviceAdded(AlsaDevice*)));
}

void AlsaDevicesPage::setCard( int cardIndex )
{
    m_cardIndex = cardIndex;
    rebuild();
}

void AlsaDevicesPage::rebuild()
{
    delete m_content;
    m_content = new TQWidget( m_scroll->viewport() );
    m_scroll->addChild(m_content);

    TQVBoxLayout *vbox = new TQVBoxLayout(m_content, 6, 4);

    if ( m_cardIndex < 0 ) {
        vbox->addWidget( new TQLabel( i18n("No card selected."), m_content ) );
        vbox->addStretch(1);
        m_content->show();
        return;
    }

    TQPtrList<AlsaDevice> devs = m_model->devicesForCard(m_cardIndex);
    bool hasSwitches = false, hasEnums = false;

    for ( TQPtrListIterator<AlsaDevice> it(devs); *it; ++it ) {
        if ( (*it)->type() == AlsaDevice::Switch ) hasSwitches = true;
        if ( (*it)->type() == AlsaDevice::Enum   ) hasEnums    = true;
    }

    if ( hasSwitches ) {
        TQLabel *hdr = new TQLabel( TQString("<b>%1</b>").arg(i18n("Switches")), m_content );
        vbox->addWidget(hdr);
        for ( TQPtrListIterator<AlsaDevice> it(devs); *it; ++it ) {
            if ( (*it)->type() == AlsaDevice::Switch ) {
                AlsaSwitchRow *row = new AlsaSwitchRow( *it, m_content );
                vbox->addWidget(row);
            }
        }
        if ( hasEnums )
            vbox->addSpacing(8);
    }

    if ( hasEnums ) {
        TQLabel *hdr = new TQLabel( TQString("<b>%1</b>").arg(i18n("Routing")), m_content );
        vbox->addWidget(hdr);
        for ( TQPtrListIterator<AlsaDevice> it(devs); *it; ++it ) {
            if ( (*it)->type() == AlsaDevice::Enum ) {
                AlsaEnumRow *row = new AlsaEnumRow( *it, m_content );
                vbox->addWidget(row);
            }
        }
    }

    if ( !hasSwitches && !hasEnums ) {
        vbox->addWidget( new TQLabel( i18n("No device settings available for this card."), m_content ) );
    }

    vbox->addStretch(1);
    m_content->show();
    m_content->adjustSize();
}

void AlsaDevicesPage::onDeviceAdded( AlsaDevice * )
{
    rebuild();
}

#include "alsadevicespage.moc"