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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
#include "devicespage.h"
#include "../model/pulsemodel.h"
#include <tqscrollview.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqgroupbox.h>
#include <tqstringlist.h>
#include <tqpixmap.h>
#include <tqbitmap.h>
#include <tqpainter.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <pulse/pulseaudio.h>
static TQString portTypeIcon( uint32_t type )
{
switch ( type ) {
case PA_DEVICE_PORT_TYPE_HEADPHONES: return "audio-headphones";
case PA_DEVICE_PORT_TYPE_HEADSET: return "audio-headset";
case PA_DEVICE_PORT_TYPE_MIC: return "audio-input-microphone";
case PA_DEVICE_PORT_TYPE_SPEAKER: return "audio-speakers";
case PA_DEVICE_PORT_TYPE_HDMI: return "video-display";
case PA_DEVICE_PORT_TYPE_SPDIF: return "audio-card";
case PA_DEVICE_PORT_TYPE_LINE: return "audio-card";
case PA_DEVICE_PORT_TYPE_USB: return "audio-card";
case PA_DEVICE_PORT_TYPE_BLUETOOTH: return "bluetooth";
default: return "audio-card";
}
}
static TQPixmap availDot( bool plugged, int size )
{
TQPixmap pm( size, size );
TQBitmap mask( size, size, true );
{
TQPainter mp( &mask );
mp.setBrush( Qt::color1 );
mp.setPen( Qt::NoPen );
mp.drawEllipse( 1, 1, size-2, size-2 );
}
pm.setMask( mask );
pm.fill( plugged ? TQColor(80, 200, 80) : TQColor(160, 160, 160) );
{
TQPainter p( &pm );
p.setBrush( plugged ? TQColor(80, 200, 80) : TQColor(160, 160, 160) );
p.setPen( Qt::NoPen );
p.drawEllipse( 1, 1, size-2, size-2 );
}
return pm;
}
DevicesPage::DevicesPage( PulseModel *model, TQWidget *parent )
: TQWidget(parent), m_model(model), m_container(0)
{
TQVBoxLayout *outer = new TQVBoxLayout( this, 0, 0 );
m_scroll = new TQScrollView( this );
m_scroll->setFrameStyle( TQFrame::NoFrame );
m_scroll->setResizePolicy( TQScrollView::AutoOneFit );
outer->addWidget( m_scroll );
rebuild();
connect( model, TQ_SIGNAL(cardAdded(uint32_t)), this, TQ_SLOT(onCardAdded(uint32_t)) );
connect( model, TQ_SIGNAL(cardRemoved(uint32_t)), this, TQ_SLOT(onCardRemoved(uint32_t)) );
connect( model, TQ_SIGNAL(cardUpdated(uint32_t)), this, TQ_SLOT(onCardUpdated(uint32_t)) );
connect( model, TQ_SIGNAL(sinkUpdated(uint32_t)), this, TQ_SLOT(onSinkUpdated(uint32_t)) );
connect( model, TQ_SIGNAL(sourceUpdated(uint32_t)),this, TQ_SLOT(onSourceUpdated(uint32_t)) );
}
void DevicesPage::rebuild()
{
m_comboCard.clear();
m_profileNames.clear();
m_cardCombo.clear();
m_cardPorts.clear();
m_sinkPortComboSink.clear();
m_sinkPortComboNames.clear();
m_sinkPortCombos.clear();
m_sourcePortComboSource.clear();
m_sourcePortComboNames.clear();
m_sourcePortCombos.clear();
delete m_container;
m_container = new TQWidget( m_scroll->viewport() );
m_scroll->addChild( m_container );
TQVBoxLayout *vbox = new TQVBoxLayout( m_container, 8, 6 );
TQValueList<PulseCardInfo> cards = m_model->cards();
for ( TQValueList<PulseCardInfo>::Iterator it = cards.begin(); it != cards.end(); ++it ) {
PulseCardInfo &info = *it;
TQGroupBox *grp = new TQGroupBox( info.description, m_container );
grp->setColumnLayout( 0, Qt::Vertical );
grp->layout()->setSpacing( 4 );
grp->layout()->setMargin( 8 );
vbox->addWidget( grp );
TQGridLayout *grid = new TQGridLayout( grp->layout(), 0, 2, 4 );
grid->setColStretch( 1, 1 );
int gridRow = 0;
auto addRow = [&]( const TQString &labelText, TQWidget *valueWidget ) {
TQLabel *lbl = new TQLabel( labelText, grp );
lbl->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
grid->addWidget( lbl, gridRow, 0 );
grid->addWidget( valueWidget, gridRow, 1 );
++gridRow;
};
auto addTextRow = [&]( const TQString &labelText, const TQString &valueText ) {
addRow( labelText, new TQLabel( valueText, grp ) );
};
if ( !info.vendor.isEmpty() )
addTextRow( i18n("Vendor:"), info.vendor );
if ( !info.product.isEmpty() )
addTextRow( i18n("Product:"), info.product );
TQStringList typeparts;
if ( !info.formFactor.isEmpty() ) typeparts << info.formFactor;
if ( !info.busType.isEmpty() ) typeparts << info.busType;
if ( !typeparts.isEmpty() )
addTextRow( i18n("Type:"), typeparts.join(" / ") );
// Profile dropdown
TQComboBox *combo = new TQComboBox( false, grp );
combo->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Fixed );
addRow( i18n("Profile:"), combo );
TQValueList<TQString> names;
int activeIdx = 0, i = 0;
for ( TQValueList<PulseCardProfile>::Iterator pit = info.profiles.begin();
pit != info.profiles.end(); ++pit, ++i ) {
combo->insertItem( pit->description );
names.append( pit->name );
if ( pit->name == info.activeProfile )
activeIdx = i;
}
if ( info.profiles.count() > 0 )
combo->setCurrentItem( activeIdx );
m_profileNames[combo] = names;
m_comboCard[combo] = info.index;
m_cardCombo[info.index] = combo;
connect( combo, TQ_SIGNAL(activated(int)), this, TQ_SLOT(onProfileActivated(int)) );
// Sink port dropdowns — only when a sink has 2+ ports
TQValueList<PulseModel::DevicePortInfo> sinks = m_model->sinksForCard( info.index );
bool multiSink = false;
for ( TQValueList<PulseModel::DevicePortInfo>::Iterator sit = sinks.begin();
sit != sinks.end(); ++sit )
if ( (int)sit->ports.count() >= 2 ) { multiSink = ( sinks.count() > 1 ); break; }
for ( TQValueList<PulseModel::DevicePortInfo>::Iterator sit = sinks.begin();
sit != sinks.end(); ++sit ) {
if ( (int)sit->ports.count() < 2 ) continue;
TQComboBox *pc = new TQComboBox( false, grp );
pc->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Fixed );
TQString label = multiSink
? sit->description + i18n(" port:")
: i18n("Output port:");
addRow( label, pc );
TQValueList<TQString> portNames;
int activePort = 0, pi = 0;
for ( TQValueList<PulseDevicePort>::ConstIterator pit = sit->ports.begin();
pit != sit->ports.end(); ++pit, ++pi ) {
pc->insertItem( (*pit).description );
portNames.append( (*pit).name );
if ( (*pit).name == sit->activePort )
activePort = pi;
}
pc->setCurrentItem( activePort );
m_sinkPortComboNames[pc] = portNames;
m_sinkPortComboSink[pc] = sit->paIndex;
m_sinkPortCombos[sit->paIndex] = pc;
connect( pc, TQ_SIGNAL(activated(int)), this, TQ_SLOT(onSinkPortActivated(int)) );
}
// Source port dropdowns — only when a source has 2+ ports
TQValueList<PulseModel::DevicePortInfo> sources = m_model->sourcesForCard( info.index );
bool multiSource = false;
for ( TQValueList<PulseModel::DevicePortInfo>::Iterator sit = sources.begin();
sit != sources.end(); ++sit )
if ( (int)sit->ports.count() >= 2 ) { multiSource = ( sources.count() > 1 ); break; }
for ( TQValueList<PulseModel::DevicePortInfo>::Iterator sit = sources.begin();
sit != sources.end(); ++sit ) {
if ( (int)sit->ports.count() < 2 ) continue;
TQComboBox *pc = new TQComboBox( false, grp );
pc->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Fixed );
TQString label = multiSource
? sit->description + i18n(" port:")
: i18n("Input port:");
addRow( label, pc );
TQValueList<TQString> portNames;
int activePort = 0, pi = 0;
for ( TQValueList<PulseDevicePort>::ConstIterator pit = sit->ports.begin();
pit != sit->ports.end(); ++pit, ++pi ) {
pc->insertItem( (*pit).description );
portNames.append( (*pit).name );
if ( (*pit).name == sit->activePort )
activePort = pi;
}
pc->setCurrentItem( activePort );
m_sourcePortComboNames[pc] = portNames;
m_sourcePortComboSource[pc] = sit->paIndex;
m_sourcePortCombos[sit->paIndex] = pc;
connect( pc, TQ_SIGNAL(activated(int)), this, TQ_SLOT(onSourcePortActivated(int)) );
}
// Ports — 2-column grid to keep tall cards compact
if ( !info.ports.isEmpty() ) {
TQLabel *portsHdr = new TQLabel( i18n("Ports:"), grp );
portsHdr->setAlignment( TQt::AlignRight | TQt::AlignTop );
TQWidget *portsWidget = new TQWidget( grp );
TQGridLayout *portsGrid = new TQGridLayout( portsWidget, 0, 2, 0, 4 );
portsGrid->setColStretch( 0, 1 );
portsGrid->setColStretch( 1, 1 );
TQValueList<PortWidgets> portWidgetList;
int portIdx = 0;
for ( TQValueList<PulseCardPort>::Iterator pit = info.ports.begin();
pit != info.ports.end(); ++pit, ++portIdx ) {
bool plugged = ( pit->available != PA_PORT_AVAILABLE_NO );
TQWidget *row = new TQWidget( portsWidget );
TQHBoxLayout *hbox = new TQHBoxLayout( row, 0, 4 );
TQLabel *dot = new TQLabel( row );
dot->setPixmap( availDot( plugged, 10 ) );
dot->setFixedSize( 10, 10 );
TQLabel *icon = new TQLabel( row );
TQPixmap px = TDEGlobal::iconLoader()->loadIcon(
portTypeIcon( pit->type ), TDEIcon::Small, 16,
TDEIcon::DefaultState, 0, true );
if ( !px.isNull() )
icon->setPixmap( px );
icon->setFixedSize( 16, 16 );
TQLabel *name = new TQLabel( pit->description, row );
hbox->addWidget( dot );
hbox->addWidget( icon );
hbox->addWidget( name );
hbox->addStretch( 1 );
portsGrid->addWidget( row, portIdx / 2, portIdx % 2 );
PortWidgets pw;
pw.dot = dot;
portWidgetList.append( pw );
}
m_cardPorts[info.index] = portWidgetList;
grid->addWidget( portsHdr, gridRow, 0, TQt::AlignRight | TQt::AlignTop );
grid->addWidget( portsWidget, gridRow, 1 );
++gridRow;
}
}
vbox->addStretch( 1 );
m_container->show();
}
void DevicesPage::onCardAdded( uint32_t ) { rebuild(); }
void DevicesPage::onCardRemoved( uint32_t ) { rebuild(); }
void DevicesPage::onCardUpdated( uint32_t ) { rebuild(); }
void DevicesPage::onProfileActivated( int idx )
{
TQComboBox *combo = dynamic_cast<TQComboBox*>( const_cast<TQObject*>( sender() ) );
if ( !combo ) return;
TQMap<TQComboBox*, uint32_t>::Iterator cit = m_comboCard.find( combo );
if ( cit == m_comboCard.end() ) return;
uint32_t cardIndex = cit.data();
TQMap<TQComboBox*, TQValueList<TQString>>::Iterator nit = m_profileNames.find( combo );
if ( nit == m_profileNames.end() || idx >= (int)nit.data().count() ) return;
m_model->setCardProfile( cardIndex, nit.data()[idx] );
}
void DevicesPage::onSinkPortActivated( int idx )
{
TQComboBox *combo = dynamic_cast<TQComboBox*>( const_cast<TQObject*>( sender() ) );
if ( !combo ) return;
TQMap<TQComboBox*, uint32_t>::Iterator cit = m_sinkPortComboSink.find( combo );
if ( cit == m_sinkPortComboSink.end() ) return;
TQMap<TQComboBox*, TQValueList<TQString>>::Iterator nit = m_sinkPortComboNames.find( combo );
if ( nit == m_sinkPortComboNames.end() || idx >= (int)nit.data().count() ) return;
m_model->setSinkPort( cit.data(), nit.data()[idx] );
}
void DevicesPage::onSourcePortActivated( int idx )
{
TQComboBox *combo = dynamic_cast<TQComboBox*>( const_cast<TQObject*>( sender() ) );
if ( !combo ) return;
TQMap<TQComboBox*, uint32_t>::Iterator cit = m_sourcePortComboSource.find( combo );
if ( cit == m_sourcePortComboSource.end() ) return;
TQMap<TQComboBox*, TQValueList<TQString>>::Iterator nit = m_sourcePortComboNames.find( combo );
if ( nit == m_sourcePortComboNames.end() || idx >= (int)nit.data().count() ) return;
m_model->setSourcePort( cit.data(), nit.data()[idx] );
}
void DevicesPage::onSinkUpdated( uint32_t paIndex )
{
TQMap<uint32_t, TQComboBox*>::Iterator cit = m_sinkPortCombos.find( paIndex );
if ( cit == m_sinkPortCombos.end() ) {
// Sink with ports appeared after last rebuild — refresh.
rebuild();
return;
}
TQComboBox *combo = cit.data();
TQMap<TQComboBox*, TQValueList<TQString>>::Iterator nit = m_sinkPortComboNames.find( combo );
if ( nit == m_sinkPortComboNames.end() ) return;
// Find current active port from the model's sinksForCard lookup.
// Walk all cards to find this sink.
TQValueList<PulseCardInfo> cards = m_model->cards();
for ( TQValueList<PulseCardInfo>::ConstIterator it = cards.begin(); it != cards.end(); ++it ) {
TQValueList<PulseModel::DevicePortInfo> sinks = m_model->sinksForCard( (*it).index );
for ( TQValueList<PulseModel::DevicePortInfo>::ConstIterator sit = sinks.begin();
sit != sinks.end(); ++sit ) {
if ( (*sit).paIndex != paIndex ) continue;
TQValueList<TQString> &names = nit.data();
for ( int i = 0; i < (int)names.count(); ++i ) {
if ( names[i] == (*sit).activePort ) {
combo->blockSignals( true );
combo->setCurrentItem( i );
combo->blockSignals( false );
return;
}
}
}
}
}
void DevicesPage::onSourceUpdated( uint32_t paIndex )
{
TQMap<uint32_t, TQComboBox*>::Iterator cit = m_sourcePortCombos.find( paIndex );
if ( cit == m_sourcePortCombos.end() ) {
rebuild();
return;
}
TQComboBox *combo = cit.data();
TQMap<TQComboBox*, TQValueList<TQString>>::Iterator nit = m_sourcePortComboNames.find( combo );
if ( nit == m_sourcePortComboNames.end() ) return;
TQValueList<PulseCardInfo> cards = m_model->cards();
for ( TQValueList<PulseCardInfo>::ConstIterator it = cards.begin(); it != cards.end(); ++it ) {
TQValueList<PulseModel::DevicePortInfo> sources = m_model->sourcesForCard( (*it).index );
for ( TQValueList<PulseModel::DevicePortInfo>::ConstIterator sit = sources.begin();
sit != sources.end(); ++sit ) {
if ( (*sit).paIndex != paIndex ) continue;
TQValueList<TQString> &names = nit.data();
for ( int i = 0; i < (int)names.count(); ++i ) {
if ( names[i] == (*sit).activePort ) {
combo->blockSignals( true );
combo->setCurrentItem( i );
combo->blockSignals( false );
return;
}
}
}
}
}
#include "devicespage.moc"
|