OSDN Git Service

dc8e4b34935275719f67fce923a9b622d37db6c5
[kde/kde-extraapps.git] / kmix / gui / mdwenum.cpp
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
23 // KMix
24 #include "mdwenum.h"
25 #include "core/mixer.h"
26 #include "viewbase.h"
27
28 // KDE
29 #include <kaction.h>
30 #include <kactioncollection.h>
31 #include <kconfig.h>
32 #include <kcombobox.h>
33 #include <kdebug.h>
34 #include <kglobalaccel.h>
35 #include <klocale.h>
36 #include <kmenu.h>
37 #include <ktoggleaction.h>
38
39 // Qt
40 #include <QCursor>
41 #include <QLabel>
42 #include <QtGui/qevent.h>
43 #include <QObject>
44 #include <QBoxLayout>
45
46 /**
47  * Class that represents an Enum element (a select one-from-many selector)
48  * The orientation (horizontal, vertical) is ignored
49  */
50 MDWEnum::MDWEnum( std::shared_ptr<MixDevice> md,
51                  Qt::Orientation orientation,
52                  QWidget* parent, ViewBase* view, ProfControl* par_pctl) :
53    MixDeviceWidget(md, false, orientation, parent, view, par_pctl),
54    _label(0), _enumCombo(0), _layout(0)
55 {
56    // create actions (on _mdwActions, see MixDeviceWidget)
57
58    // KStandardAction::showMenubar() is in MixDeviceWidget now
59    KToggleAction *action = _mdwActions->add<KToggleAction>( "hide" );
60    action->setText( i18n("&Hide") );
61    connect(action, SIGNAL(triggered(bool)), SLOT(setDisabled(bool)));
62    QAction *c = _mdwActions->addAction( "keys" );
63    c->setText( i18n("C&onfigure Shortcuts...") );
64    connect(c, SIGNAL(triggered(bool)), SLOT(defineKeys()));
65
66    // create widgets
67    createWidgets();
68
69    /* remove this for production version
70      QAction *a = _mdwActions->addAction( "Next Value" );
71      c->setText( i18n( "Next Value" ) );
72      connect(a, SIGNAL(triggered(bool)), SLOT(nextEnumId()));
73    */
74
75    installEventFilter( this ); // filter for popup
76 }
77
78 MDWEnum::~MDWEnum()
79 {
80 }
81
82
83 void MDWEnum::createWidgets()
84 {
85    if ( _orientation == Qt::Vertical ) {
86       _layout = new QVBoxLayout( this );
87           _layout->setAlignment(Qt::AlignLeft);
88    }
89    else {
90       _layout = new QHBoxLayout( this );
91           _layout->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
92    }
93
94    _label = new QLabel( m_mixdevice->readableName(), this);
95    _layout->addWidget(_label);
96    _enumCombo = new KComboBox( false, this);
97    _enumCombo->installEventFilter(this);
98    // ------------ fill ComboBox start ------------
99    int maxEnumId= m_mixdevice->enumValues().count();
100    for (int i=0; i<maxEnumId; i++ ) {
101       _enumCombo->addItem( m_mixdevice->enumValues().at(i));
102    }
103    // ------------ fill ComboBox end --------------
104    _layout->addWidget(_enumCombo);
105    connect( _enumCombo, SIGNAL(activated(int)), this, SLOT(setEnumId(int)) );
106    _enumCombo->setToolTip( m_mixdevice->readableName() );
107         _layout->addStretch(1);
108 }
109
110 void MDWEnum::update()
111 {
112   if ( m_mixdevice->isEnum() ) {
113     //kDebug(67100) << "MDWEnum::update() enumID=" << m_mixdevice->enumId();
114     _enumCombo->setCurrentIndex( m_mixdevice->enumId() );
115   }
116   else {
117     kError(67100) << "MDWEnum::update() enumID=" << m_mixdevice->enumId() << " is no Enum ... skipped";
118   }
119 }
120
121 void MDWEnum::showContextMenu(const QPoint& pos )
122 {
123    if( m_view == 0 )
124       return;
125
126    KMenu *menu = m_view->getPopup();
127
128    menu->popup( pos );
129 }
130
131
132 QSizePolicy MDWEnum::sizePolicy() const
133 {
134     return QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
135 }
136
137 /**
138    This slot is called, when a user has clicked the mute button. Also it is called by any other
139     associated KAction like the context menu.
140 */
141 void MDWEnum::nextEnumId() {
142    if( m_mixdevice->isEnum() ) {
143       int curEnum = enumId();
144       if ( curEnum < m_mixdevice->enumValues().count() ) {
145          // next enum value
146          setEnumId(curEnum+1);
147       }
148       else {
149          // wrap around
150          setEnumId(0);
151       }
152    } // isEnum
153 }
154
155 void MDWEnum::setEnumId(int value)
156 {
157    if (  m_mixdevice->isEnum() ) {
158       m_mixdevice->setEnumId( value );
159       m_mixdevice->mixer()->commitVolumeChange( m_mixdevice );
160    }
161 }
162
163 int MDWEnum::enumId()
164 {
165    if (  m_mixdevice->isEnum() ) {
166       return m_mixdevice->enumId();
167    }
168    else {
169       return 0;
170    }
171 }
172
173
174 void MDWEnum::setDisabled( bool hide )
175 {
176         emit guiVisibilityChange(this, !hide);
177 }
178
179 /**
180  * An event filter for the various QWidgets. We watch for Mouse press Events, so
181  * that we can popup the context menu.
182  */
183 bool MDWEnum::eventFilter( QObject* obj, QEvent* e )
184 {
185    if (e->type() == QEvent::MouseButtonPress) {
186       QMouseEvent *qme = static_cast<QMouseEvent*>(e);
187       if (qme->button() == Qt::RightButton) {
188          showContextMenu();
189          return true;
190       }
191    } else if (e->type() == QEvent::ContextMenu) {
192       QPoint pos = reinterpret_cast<QWidget *>(obj)->mapToGlobal(QPoint(0, 0));
193       showContextMenu(pos);
194       return true;
195    }
196     return QWidget::eventFilter(obj,e);
197 }
198
199 #include "moc_mdwenum.cpp"