summaryrefslogtreecommitdiffstats
path: root/kmix/mdwswitch.cpp
blob: 2fcd228ca998cf138fa12a1df64d1c586e469b66 (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
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
/*
 * KMix -- KDE's full featured mini mixer
 *
 *
 * Copyright (C) 2004 Christian Esken <esken@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <tqcursor.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqobject.h>
#include <tqslider.h>
#include <tqtooltip.h>

#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdeaction.h>
#include <tdepopupmenu.h>
#include <kglobalaccel.h>
#include <kkeydialog.h>
#include <kdebug.h>

#include "kledbutton.h"
#include "mdwswitch.h"
#include "mixer.h"
#include "viewbase.h"
#include "verticaltext.h"

/**
 * Class that represents a single Switch
 * The orientation (horizontal, vertical) can be configured and whether it should
 * be "small"  (uses KSmallSlider instead of TQSlider then).
 */
MDWSwitch::MDWSwitch(Mixer *mixer, MixDevice* md,
                                 bool small, TQt::Orientation orientation,
                                 TQWidget* parent, ViewBase* mw, const char* name) :
    MixDeviceWidget(mixer,md,small,orientation,parent,mw,name),
    _label(0) , _labelV(0) , _switchLED(0), _layout(0)
{
    // create actions (on _mdwActions, see MixDeviceWidget)

    // KStdAction::showMenubar() is in MixDeviceWidget now
    new TDEToggleAction( i18n("&Hide"), 0, this, TQ_SLOT(setDisabled()), _mdwActions, "hide" );
    new TDEAction( i18n("C&onfigure Shortcuts..."), 0, this, TQ_SLOT(defineKeys()), _mdwActions, "keys" );

    // create widgets
    createWidgets();

    m_keys->insert( "Toggle switch", i18n( "Toggle Switch" ), TQString(),
		    TDEShortcut(), TDEShortcut(), this, TQ_SLOT( toggleSwitch() ) );

    // The keys are loaded in KMixerWidget::loadConfig, see kmixerwidget.cpp (now: kmixtoolbox.cpp)
    //m_keys->readSettings();
    //m_keys->updateConnections();

    installEventFilter( this ); // filter for popup
}

MDWSwitch::~MDWSwitch()
{
}


void MDWSwitch::createWidgets()
{
	if ( _orientation == TQt::Vertical ) {
		_layout = new TQVBoxLayout( this );
		_layout->setAlignment(TQt::AlignHCenter);
	}
	else {
		_layout = new TQHBoxLayout( this );
		_layout->setAlignment(TQt::AlignVCenter);
	}
	TQToolTip::add( this, m_mixdevice->name() );
	
	
	_layout->addSpacing( 4 );
	// --- LEDS --------------------------
	if ( _orientation == TQt::Vertical ) {
		if( m_mixdevice->isRecordable() )
		 _switchLED = new KLedButton( TQt::red,
				 m_mixdevice->isRecSource()?KLed::On:KLed::Off,
				 KLed::Sunken, KLed::Circular, this, "RecordLED" );
		else
		 _switchLED = new KLedButton( TQt::yellow, KLed::On, KLed::Sunken, KLed::Circular, this, "SwitchLED" );
		 _switchLED->setFixedSize(16,16); 
		 _labelV = new VerticalText( this, m_mixdevice->name().utf8().data() );
		 
		 _layout->addWidget( _switchLED );
		 _layout->addSpacing( 2 );
		 _layout->addWidget( _labelV );
		 
		 _switchLED->installEventFilter( this );
		 _labelV->installEventFilter( this );
	 }
	 else
	 {
		if( m_mixdevice->isRecordable() )
			_switchLED = new KLedButton( TQt::red,
					m_mixdevice->isRecSource()?KLed::On:KLed::Off,
					KLed::Sunken, KLed::Circular, this, "RecordLED" );
		else
		 _switchLED = new KLedButton( TQt::yellow, KLed::On, KLed::Sunken, KLed::Circular, this, "SwitchLED" );
		 _switchLED->setFixedSize(16,16);
		 _label  = new TQLabel(m_mixdevice->name(), this, "SwitchName");
		 
		 _layout->addWidget( _switchLED );
		 _layout->addSpacing( 1 );
		 _layout->addWidget( _label );
		 _switchLED->installEventFilter( this );
		 _label->installEventFilter( this );
	 }
    connect( _switchLED, TQ_SIGNAL(stateChanged(bool)), this, TQ_SLOT(toggleSwitch()) );
    _layout->addSpacing( 4 );
}

void MDWSwitch::update()
{
	if ( _switchLED != 0 ) {
		_switchLED->blockSignals( true );
		if( m_mixdevice->isRecordable() )
			_switchLED->setState( m_mixdevice->isRecSource() ? KLed::On : KLed::Off );
		else
			_switchLED->setState( m_mixdevice->isMuted() ? KLed::Off : KLed::On );
		
		_switchLED->blockSignals( false );
	}
}

void MDWSwitch::setBackgroundMode(BackgroundMode m)
{
    if ( _label != 0 ){
	_label->setBackgroundMode(m);
    }
    if ( _labelV != 0 ){
	_labelV->setBackgroundMode(m);
    }
    _switchLED->setBackgroundMode(m);
    MixDeviceWidget::setBackgroundMode(m);
}

void MDWSwitch::showContextMenu()
{
    if( m_mixerwidget == NULL )
	return;

    TDEPopupMenu *menu = m_mixerwidget->getPopup();

    TQPoint pos = TQCursor::pos();
    menu->popup( pos );
}

TQSize MDWSwitch::sizeHint() const {
    if ( _layout != 0 ) {
	return _layout->sizeHint();
    }
    else {
	// layout not (yet) created
	return TQWidget::sizeHint();
    }
}


/**
   This slot is called, when a user has clicked the mute button. Also it is called by any other
    associated TDEAction like the context menu.
*/
void MDWSwitch::toggleSwitch() {
	if( m_mixdevice->isRecordable() )
		setSwitch( !m_mixdevice->isRecSource() );
	else
		setSwitch( !m_mixdevice->isMuted() );
}

void MDWSwitch::setSwitch(bool value)
{
	if (  m_mixdevice->isSwitch() ) {
		if ( m_mixdevice->isRecordable() ) {
			m_mixer->setRecordSource( m_mixdevice->num(), value );
		}
		else {
			m_mixdevice->setMuted( value );
			m_mixer->commitVolumeChange( m_mixdevice );
		}
	}
}

void MDWSwitch::setDisabled()
{
    setDisabled( true );
}

void MDWSwitch::setDisabled( bool value ) {
    if ( m_disabled!=value)
    {
	value ? hide() : show();
	m_disabled = value;
    }
}

/**
 * An event filter for the various TQWidgets. We watch for Mouse press Events, so
 * that we can popup the context menu.
 */
bool MDWSwitch::eventFilter( TQObject* obj, TQEvent* e )
{
    if (e->type() == TQEvent::MouseButtonPress) {
	TQMouseEvent *qme = static_cast<TQMouseEvent*>(e);
	if (qme->button() == TQt::RightButton) {
	    showContextMenu();
	    return true;
	}
    }
    return TQWidget::eventFilter(obj,e);
}

#include "mdwswitch.moc"