~ubuntu-branches/ubuntu/natty/kdemultimedia/natty-proposed

« back to all changes in this revision

Viewing changes to kmix/mdwswitch.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers
  • Date: 2011-05-26 02:41:36 UTC
  • mfrom: (0.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 108.
  • Revision ID: james.westby@ubuntu.com-20110526024136-jjwsigfy402jhupm
Tags: upstream-4.6.3
ImportĀ upstreamĀ versionĀ 4.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * KMix -- KDE's full featured mini mixer
3
 
 *
4
 
 *
5
 
 * Copyright (C) 2004 Christian Esken <esken@kde.org>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Library General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Library General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Library General Public
18
 
 * License along with this program; if not, write to the Free
19
 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 
 */
21
 
 
22
 
#include <qcursor.h>
23
 
#include <QLabel>
24
 
#include <QMouseEvent>
25
 
#include <QObject>
26
 
#include <qslider.h>
27
 
 
28
 
#include <klocale.h>
29
 
#include <kconfig.h>
30
 
#include <kaction.h>
31
 
#include <kmenu.h>
32
 
#include <kglobalaccel.h>
33
 
#include <kdebug.h>
34
 
#include <ktoggleaction.h>
35
 
#include <kactioncollection.h>
36
 
 
37
 
#include "kledbutton.h"
38
 
#include "mdwswitch.h"
39
 
#include "mixer.h"
40
 
#include "viewbase.h"
41
 
#include "verticaltext.h"
42
 
 
43
 
/**
44
 
 * Class that represents a single Switch
45
 
 * The orientation (horizontal, vertical) can be configured and whether it should
46
 
 * be "small"  (uses KSmallSlider instead of QSlider then).
47
 
 */
48
 
MDWSwitch::MDWSwitch(MixDevice* md,
49
 
                     bool small, Qt::Orientation orientation,
50
 
                     QWidget* parent, ViewBase* mw) :
51
 
    MixDeviceWidget(md,small,orientation,parent,mw),
52
 
    _label(0) , _labelV(0) , _switchLED(0), _layout(0)
53
 
{
54
 
    // create actions (on _mdwActions, see MixDeviceWidget)
55
 
 
56
 
    // KStandardAction::showMenubar() is in MixDeviceWidget now
57
 
    KToggleAction *action = _mdwActions->add<KToggleAction>( "hide" );
58
 
    action->setText( i18n("&Hide") );
59
 
    connect(action, SIGNAL(triggered(bool) ), SLOT(setDisabled()));
60
 
    KAction *b = _mdwActions->addAction( "keys" );
61
 
    b->setText( i18n("C&onfigure Shortcuts...") );
62
 
    connect(b, SIGNAL(triggered(bool) ), SLOT(defineKeys()));
63
 
 
64
 
    // create widgets
65
 
    createWidgets();
66
 
 
67
 
    KAction *a = _mdwActions->addAction( "Toggle switch" );
68
 
    a->setText( i18n( "Toggle Switch" ) );
69
 
    connect(a, SIGNAL(triggered(bool) ), SLOT( toggleSwitch() ));
70
 
 
71
 
    // The accel keys are loaded in KMixerWidget::loadConfig, see kmixtoolbox.cpp
72
 
 
73
 
    installEventFilter( this ); // filter for popup
74
 
}
75
 
 
76
 
MDWSwitch::~MDWSwitch()
77
 
{
78
 
}
79
 
 
80
 
 
81
 
void MDWSwitch::createWidgets()
82
 
{
83
 
   if ( _orientation == Qt::Vertical ) {
84
 
      _layout = new QVBoxLayout( this );
85
 
      _layout->setAlignment(Qt::AlignHCenter);
86
 
   }
87
 
   else {
88
 
      _layout = new QHBoxLayout( this );
89
 
      _layout->setAlignment(Qt::AlignVCenter);
90
 
   }
91
 
   this->setToolTip( m_mixdevice->readableName() );
92
 
 
93
 
 
94
 
   _layout->addSpacing( 4 );
95
 
   // --- LEDS --------------------------
96
 
   if ( _orientation == Qt::Vertical ) {
97
 
      if( m_mixdevice->captureVolume().hasSwitch() )
98
 
         _switchLED = new KLedButton( Qt::red,
99
 
               m_mixdevice->isRecSource()?KLed::On:KLed::Off,
100
 
               KLed::Sunken, KLed::Circular, this, "RecordLED" );
101
 
      else
102
 
         _switchLED = new KLedButton( Qt::yellow, KLed::On, KLed::Sunken, KLed::Circular, this, "SwitchLED" );
103
 
         _switchLED->setFixedSize(16,16);
104
 
         _labelV = new VerticalText( this, m_mixdevice->readableName().toUtf8().data() );
105
 
 
106
 
         _layout->addWidget( _switchLED );
107
 
         _layout->addSpacing( 2 );
108
 
         _layout->addWidget( _labelV );
109
 
 
110
 
         _switchLED->installEventFilter( this );
111
 
         _labelV->installEventFilter( this );
112
 
      }
113
 
      else
114
 
      {
115
 
      if( m_mixdevice->captureVolume().hasSwitch() )
116
 
         _switchLED = new KLedButton( Qt::red,
117
 
               m_mixdevice->isRecSource()?KLed::On:KLed::Off,
118
 
               KLed::Sunken, KLed::Circular, this, "RecordLED" );
119
 
      else
120
 
         _switchLED = new KLedButton( Qt::yellow, KLed::On, KLed::Sunken, KLed::Circular, this, "SwitchLED" );
121
 
         _switchLED->setFixedSize(16,16);
122
 
         _label  = new QLabel(m_mixdevice->readableName(), this );
123
 
         _label->setObjectName("SwitchName");
124
 
 
125
 
         _layout->addWidget( _switchLED );
126
 
         _layout->addSpacing( 1 );
127
 
         _layout->addWidget( _label );
128
 
         _switchLED->installEventFilter( this );
129
 
         _label->installEventFilter( this );
130
 
      }
131
 
      connect( _switchLED, SIGNAL(stateChanged(bool)), this, SLOT(toggleSwitch()) );
132
 
      _layout->addSpacing( 4 );
133
 
}
134
 
 
135
 
void MDWSwitch::update()
136
 
{
137
 
   if ( _switchLED != 0 ) {
138
 
      _switchLED->blockSignals( true );
139
 
      if( m_mixdevice->captureVolume().hasSwitch() )
140
 
         _switchLED->setState( m_mixdevice->isRecSource() ? KLed::On : KLed::Off );
141
 
      else
142
 
         _switchLED->setState( m_mixdevice->isMuted() ? KLed::Off : KLed::On );
143
 
 
144
 
      _switchLED->blockSignals( false );
145
 
   }
146
 
}
147
 
 
148
 
void MDWSwitch::setBackgroundRole(QPalette::ColorRole m)
149
 
{
150
 
   if ( _label != 0 ){
151
 
      _label->setBackgroundRole(m);
152
 
   }
153
 
   if ( _labelV != 0 ){
154
 
      _labelV->setBackgroundRole(m);
155
 
   }
156
 
   _switchLED->setBackgroundRole(m);
157
 
   MixDeviceWidget::setBackgroundRole(m);
158
 
}
159
 
 
160
 
void MDWSwitch::showContextMenu()
161
 
{
162
 
   if( m_view == 0 )
163
 
   return;
164
 
 
165
 
    KMenu *menu = m_view->getPopup();
166
 
 
167
 
    QPoint pos = QCursor::pos();
168
 
    menu->popup( pos );
169
 
}
170
 
 
171
 
 
172
 
QSizePolicy MDWSwitch::sizePolicy() const
173
 
{
174
 
    if ( _orientation == Qt::Vertical ) {
175
 
        return QSizePolicy(  QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
176
 
    }
177
 
    else {
178
 
        return QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
179
 
    }
180
 
}
181
 
 
182
 
/**
183
 
   This slot is called, when a user has clicked the mute button. Also it is called by any other
184
 
    associated KAction like the context menu.
185
 
*/
186
 
void MDWSwitch::toggleSwitch() {
187
 
   if( m_mixdevice->captureVolume().hasSwitch() )
188
 
      setSwitch( !m_mixdevice->isRecSource() );
189
 
   else
190
 
      setSwitch( !m_mixdevice->isMuted() );
191
 
}
192
 
 
193
 
void MDWSwitch::setSwitch(bool value)
194
 
{
195
 
   if (  m_mixdevice->playbackVolume().hasSwitch() ) {
196
 
      if ( m_mixdevice->captureVolume().hasSwitch() ) {
197
 
         m_mixdevice->mixer()->setRecordSource( m_mixdevice->id(), value );
198
 
      }
199
 
      else {
200
 
         m_mixdevice->setMuted( value );
201
 
         m_mixdevice->mixer()->commitVolumeChange( m_mixdevice );
202
 
      }
203
 
   }
204
 
}
205
 
 
206
 
void MDWSwitch::setDisabled()
207
 
{
208
 
   setDisabled( true );
209
 
}
210
 
 
211
 
void MDWSwitch::setDisabled( bool value ) {
212
 
   if ( m_disabled!=value)
213
 
   {
214
 
      value ? hide() : show();
215
 
      m_disabled = value;
216
 
   }
217
 
}
218
 
 
219
 
/**
220
 
 * An event filter for the various QWidgets. We watch for Mouse press Events, so
221
 
 * that we can popup the context menu.
222
 
 */
223
 
bool MDWSwitch::eventFilter( QObject* obj, QEvent* e )
224
 
{
225
 
   if (e->type() == QEvent::MouseButtonPress) {
226
 
      QMouseEvent *qme = static_cast<QMouseEvent*>(e);
227
 
      if (qme->button() == Qt::RightButton) {
228
 
            showContextMenu();
229
 
            return true;
230
 
      }
231
 
   }
232
 
   return QWidget::eventFilter(obj,e);
233
 
}
234
 
 
235
 
#include "mdwswitch.moc"