author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /core/applets/volumeapplet/volume.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'core/applets/volumeapplet/volume.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/applets/volumeapplet/volume.cpp | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/core/applets/volumeapplet/volume.cpp b/core/applets/volumeapplet/volume.cpp new file mode 100644 index 0000000..35dbf22 --- a/dev/null +++ b/core/applets/volumeapplet/volume.cpp | |||
@@ -0,0 +1,199 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "volume.h" | ||
22 | |||
23 | #include <qpe/resource.h> | ||
24 | #include <qpe/qpeapplication.h> | ||
25 | #include <qpe/config.h> | ||
26 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | ||
27 | #include <qpe/qcopenvelope_qws.h> | ||
28 | #endif | ||
29 | |||
30 | #include <qpainter.h> | ||
31 | #include <qcheckbox.h> | ||
32 | #include <qslider.h> | ||
33 | #include <qlayout.h> | ||
34 | #include <qframe.h> | ||
35 | #include <qpixmap.h> | ||
36 | |||
37 | |||
38 | VolumeControl::VolumeControl( QWidget *parent, const char *name ) | ||
39 | : QFrame( parent, name, WDestructiveClose | WStyle_StaysOnTop | WType_Popup ) | ||
40 | { | ||
41 | setFrameStyle( QFrame::PopupPanel | QFrame::Raised ); | ||
42 | |||
43 | QVBoxLayout *vbox = new QVBoxLayout( this ); | ||
44 | slider = new QSlider( this ); | ||
45 | muteBox = new QCheckBox( tr("Mute"), this ); | ||
46 | slider->setRange( 0, 100 ); | ||
47 | slider->setTickmarks( QSlider::Both ); | ||
48 | slider->setTickInterval( 20 ); | ||
49 | slider->setFocusPolicy( QWidget::NoFocus ); | ||
50 | muteBox->setFocusPolicy( QWidget::NoFocus ); | ||
51 | vbox->setMargin( 6 ); | ||
52 | vbox->setSpacing( 3 ); | ||
53 | vbox->addWidget( slider, 0, Qt::AlignVCenter | Qt::AlignHCenter ); | ||
54 | vbox->addWidget( muteBox ); | ||
55 | setFixedHeight( 100 ); | ||
56 | setFixedWidth( sizeHint().width() ); | ||
57 | setFocusPolicy(QWidget::NoFocus); | ||
58 | } | ||
59 | |||
60 | void VolumeControl::keyPressEvent( QKeyEvent *e) | ||
61 | { | ||
62 | switch(e->key()) { | ||
63 | case Key_Up: | ||
64 | slider->subtractStep(); | ||
65 | break; | ||
66 | case Key_Down: | ||
67 | slider->addStep(); | ||
68 | break; | ||
69 | case Key_Space: | ||
70 | muteBox->toggle(); | ||
71 | break; | ||
72 | case Key_Escape: | ||
73 | close(); | ||
74 | break; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | //=========================================================================== | ||
79 | |||
80 | VolumeApplet::VolumeApplet( QWidget *parent, const char *name ) | ||
81 | : QWidget( parent, name ) | ||
82 | { | ||
83 | setFixedHeight( 18 ); | ||
84 | setFixedWidth( 14 ); | ||
85 | volumePixmap = Resource::loadPixmap( "volume" ); | ||
86 | muted = FALSE; // ### read from pref | ||
87 | volumePercent = 50; // ### read from pref | ||
88 | connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) ); | ||
89 | writeSystemVolume(); | ||
90 | } | ||
91 | |||
92 | VolumeApplet::~VolumeApplet() | ||
93 | { | ||
94 | } | ||
95 | |||
96 | void VolumeApplet::mousePressEvent( QMouseEvent *) | ||
97 | { | ||
98 | // Create a small volume control window to adjust the volume with | ||
99 | VolumeControl *vc = new VolumeControl; | ||
100 | vc->slider->setValue( 100 - volumePercent ); | ||
101 | vc->muteBox->setChecked( muted ); | ||
102 | connect( vc->slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderMoved( int ) ) ); | ||
103 | connect( vc->muteBox, SIGNAL( toggled( bool ) ), this, SLOT( mute( bool ) ) ); | ||
104 | QPoint curPos = mapToGlobal( rect().topLeft() ); | ||
105 | vc->move( curPos.x()-(vc->sizeHint().width()-width())/2, curPos.y() - 100 ); | ||
106 | vc->show(); | ||
107 | } | ||
108 | |||
109 | void VolumeApplet::volumeChanged( bool nowMuted ) | ||
110 | { | ||
111 | int previousVolume = volumePercent; | ||
112 | |||
113 | if ( !nowMuted ) | ||
114 | readSystemVolume(); | ||
115 | |||
116 | // Handle case where muting it toggled | ||
117 | if ( muted != nowMuted ) { | ||
118 | muted = nowMuted; | ||
119 | repaint( TRUE ); | ||
120 | return; | ||
121 | } | ||
122 | |||
123 | // Avoid over repainting | ||
124 | if ( previousVolume != volumePercent ) | ||
125 | repaint( 2, height() - 3, width() - 4, 2, FALSE ); | ||
126 | } | ||
127 | |||
128 | |||
129 | void VolumeApplet::mute( bool toggled ) | ||
130 | { | ||
131 | muted = toggled; | ||
132 | // clear if removing mute | ||
133 | repaint( !toggled ); | ||
134 | writeSystemVolume(); | ||
135 | } | ||
136 | |||
137 | |||
138 | void VolumeApplet::sliderMoved( int percent ) | ||
139 | { | ||
140 | setVolume( 100 - percent ); | ||
141 | } | ||
142 | |||
143 | |||
144 | void VolumeApplet::readSystemVolume() | ||
145 | { | ||
146 | Config cfg("Sound"); | ||
147 | cfg.setGroup("System"); | ||
148 | volumePercent = cfg.readNumEntry("Volume"); | ||
149 | } | ||
150 | |||
151 | |||
152 | void VolumeApplet::setVolume( int percent ) | ||
153 | { | ||
154 | // clamp volume percent to be between 0 and 100 | ||
155 | volumePercent = (percent < 0) ? 0 : ((percent > 100) ? 100 : percent); | ||
156 | // repaint just the little volume rectangle | ||
157 | repaint( 2, height() - 3, width() - 4, 2, FALSE ); | ||
158 | writeSystemVolume(); | ||
159 | } | ||
160 | |||
161 | |||
162 | void VolumeApplet::writeSystemVolume() | ||
163 | { | ||
164 | { | ||
165 | Config cfg("Sound"); | ||
166 | cfg.setGroup("System"); | ||
167 | cfg.writeEntry("Volume",volumePercent); | ||
168 | } | ||
169 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | ||
170 | // Send notification that the volume has changed | ||
171 | QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << muted; | ||
172 | #endif | ||
173 | } | ||
174 | |||
175 | |||
176 | void VolumeApplet::paintEvent( QPaintEvent* ) | ||
177 | { | ||
178 | QPainter p(this); | ||
179 | |||
180 | if (volumePixmap.isNull()) | ||
181 | volumePixmap = Resource::loadPixmap( "volume" ); | ||
182 | p.drawPixmap( 0, 1, volumePixmap ); | ||
183 | p.setPen( darkGray ); | ||
184 | p.drawRect( 1, height() - 4, width() - 2, 4 ); | ||
185 | |||
186 | int pixelsWide = volumePercent * (width() - 4) / 100; | ||
187 | p.fillRect( 2, height() - 3, pixelsWide, 2, red ); | ||
188 | p.fillRect( pixelsWide + 2, height() - 3, width() - 4 - pixelsWide, 2, lightGray ); | ||
189 | |||
190 | if ( muted ) { | ||
191 | p.setPen( red ); | ||
192 | p.drawLine( 1, 2, width() - 2, height() - 5 ); | ||
193 | p.drawLine( 1, 3, width() - 2, height() - 4 ); | ||
194 | p.drawLine( width() - 2, 2, 1, height() - 5 ); | ||
195 | p.drawLine( width() - 2, 3, 1, height() - 4 ); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | |||