summaryrefslogtreecommitdiffstats
path: root/arts/gui/kde/kvolumefader_impl.cpp
blob: fca3466eb16888a9d443054636b031763fc1fdbb (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
232
233
234
235
236
237
238
239
240
241
242
243
/*
    Copyright (  C ) 2003 Arnold Krille <arnold@arnoldarts.de>

    This library 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;
    version 2 of the License.

    This library 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 library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "kvolumefader_impl.h"

#include <kdebug.h>
#include <tqpainter.h>
#include <tqstyle.h>
#include <tqfont.h>
#include <tqfontmetrics.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kaction.h>
#include <kinputdialog.h>

KVolumeFader_impl::KVolumeFader_impl( TQFrame* w )
  : Arts::KFrame_impl( w ? w : new KVolumeFader_Widget( 0 ) )
  , dB2VolCalc( -36, 6 )
  , _dir( Arts::BottomToTop )
  , dbmin_inupdate( false )
  , dbmax_inupdate( false )
  , direction_inupdate( false )
  , ignoreUpdates( 0 )
{
	//kdDebug()<<"KVolumeFader_impl::KVolumeFader_impl( TQFrame* w="<<w<<" )"<<endl;
	_vfwidget = static_cast<KVolumeFader_Widget*>( _qwidget );
	_vfwidget->setImpl( this );
}

KVolumeFader_impl::~KVolumeFader_impl() {
}

float KVolumeFader_impl::dbmin() { return dB2VolCalc::dbmin; }
void KVolumeFader_impl::dbmin( float n ) {
	if ( n != dB2VolCalc::dbmin && !dbmin_inupdate ) {
		dbmin_inupdate = true;
		dB2VolCalc::dbmin = n;
		dbmin_changed( dbmin() );
		dbmin_inupdate = false;
	}
}
float KVolumeFader_impl::dbmax() { return dB2VolCalc::dbmax; }
void KVolumeFader_impl::dbmax( float n ) {
	if ( n != dB2VolCalc::dbmax && !dbmax_inupdate ) {
		dbmax_inupdate = true;
		dB2VolCalc::dbmax = n;
		dbmax_changed( dbmax() );
		dbmax_inupdate = false;
	}
}

Arts::Direction KVolumeFader_impl::direction() { return _dir; }
void KVolumeFader_impl::direction( Arts::Direction n ) {
	if ( n != _dir && !direction_inupdate ) {
		direction_inupdate = true;
		_dir = n;
		_vfwidget->setDirection( _dir );
//		direction_changed( direction() );
		direction_inupdate = false;
	}
}

float KVolumeFader_impl::volume() { return _volume; }
void KVolumeFader_impl::volume( float n ) {
	//kdDebug() << k_funcinfo << n << " ignore: " << ignoreUpdates << endl;
	if ( ignoreUpdates > 0 ) {
		--ignoreUpdates;
		return;
	}
	_volume = n;
	_vfwidget->setValue( amptondb( _volume ) );
}

float KVolumeFader_impl::dbvolume() { return amptodb( _volume ); }
void KVolumeFader_impl::dbvolume( float n ) {
	//kdDebug() << k_funcinfo << n << endl;
	normalizedvolume( dbtondb( n ) );
}

void KVolumeFader_impl::normalizedvolume( float n ) {
	if ( ( ndbtodb( n ) > dbmin() ) && ( ndbtodb( n ) < dbmax() ) ) {
		float amp = ndbtoamp( n );
		if ( amp != _volume ) {
			++ignoreUpdates;
			//kdDebug() << k_funcinfo << ignoreUpdates << endl;
			_volume = amp;
			_vfwidget->setValue( n );
			volume_changed( _volume );
		}
	}
}

void KVolumeFader_impl::constructor( float dbmin, float dbmax, Arts::Direction dir ) {
	this->dbmin( dbmin ); this->dbmax( dbmax ); direction( dir );
}



KVolumeFader_Widget::KVolumeFader_Widget( TQWidget* p, const char* n )
  : TQFrame( p,n )
  , _impl( 0 )
  , _inupdate( false )
  , _value( -1 )
  , _dir( Arts::BottomToTop )
  , _menu( new KPopupMenu( this ) )
  , _aExactValue( new KAction( i18n( "Set Exact Value..." ), KShortcut(), TQT_TQOBJECT(this), TQT_SLOT( exactValue() ), TQT_TQOBJECT(this) ) )
{
	//kdDebug() << k_funcinfo << endl;
	setMinimumSize( 10,10 );
	_aExactValue->plug( _menu );
}

void KVolumeFader_Widget::setImpl( KVolumeFader_impl* n ) { _impl = n; update(); }

KVolumeFader_Widget::~KVolumeFader_Widget() {
	//kdDebug() << k_funcinfo << endl;
}

void KVolumeFader_Widget::setDirection( Arts::Direction n ) { _dir = n; update(); }

void KVolumeFader_Widget::setValue( float n ) {
	//kdDebug() << k_funcinfo << n << endl;
	if ( n != _value ) {
		_value = n;
		update();
	}
}

TQColor KVolumeFader_Widget::interpolate(  TQColor low, TQColor high, float percent ) {
	if (  percent<=0 ) return low; else
	if (  percent>=1 ) return high; else
		return TQColor(
			int( low.red() + ( high.red()-low.red() ) * percent ),
			int( low.green() + ( high.green()-low.green() ) * percent ),
			int( low.blue() + ( high.blue()-low.blue() ) * percent ) );
}

void KVolumeFader_Widget::drawContents( TQPainter* p ){
	if ( _dir==Arts::BottomToTop || _dir==Arts::BottomToTop ) {
		float h;
		if ( _dir==Arts::BottomToTop ) h = contentsRect().height() * ( 1 - _value );
			else h = contentsRect().height() * _value;
		for ( int i=int( h ); i<contentsRect().height(); i++ ) {
			p->setPen( interpolate( tqcolorGroup().light(), tqcolorGroup().highlight(), float( i )/contentsRect().height() ) );
			p->drawLine( contentsRect().left(), this->frameWidth() + i, contentsRect().right(), this->frameWidth() + i );
		}
		p->setPen( tqcolorGroup().dark() );
		p->drawLine( contentsRect().left(), this->frameWidth() + int( h ), contentsRect().right(), this->frameWidth() + int( h ) );
	} else {
		float w;
		p->translate( this->width(),0 );
		if ( _dir==Arts::LeftToRight ) w = - contentsRect().width() * ( 1 - _value );
			else w = - contentsRect().width() * _value;
		for ( int i=int( w ); i>=-contentsRect().width(); i-- ) {
			p->setPen( interpolate( tqcolorGroup().light(), tqcolorGroup().highlight(), float( -i )/contentsRect().width() ) );
			p->drawLine( this->frameWidth() + i, contentsRect().top(), this->frameWidth() + i, contentsRect().bottom() );
		}
		p->setPen( tqcolorGroup().dark() );
		p->drawLine( this->frameWidth() + int( w ), contentsRect().top(), this->frameWidth() + int( w ), contentsRect().bottom() );
	}
}

void KVolumeFader_Widget::mousePressEvent( TQMouseEvent* ){
	//kdDebug() << k_funcinfo << endl;
}

void KVolumeFader_Widget::mouseReleaseEvent( TQMouseEvent* qme ){
	bool setValue = false;
	if ( KGlobalSettings::mouseSettings().handed == 0 && qme->button() == Qt::LeftButton ) setValue=true;
	if ( KGlobalSettings::mouseSettings().handed == 1 && qme->button() == Qt::RightButton ) setValue=true;
	if ( setValue )
	{
		switch ( _dir ) {
			default:
			case Arts::BottomToTop:
				if ( _impl ) _impl->normalizedvolume( 1 - float( qme->y() ) / contentsRect().height() );
				break;
			case Arts::TopToBottom:
				if ( _impl ) _impl->normalizedvolume( float( qme->y() ) / contentsRect().height() );
				break;
			case Arts::LeftToRight:
				if ( _impl ) _impl->normalizedvolume( float( qme->x() ) / contentsRect().width() );
				break;
			case Arts::RightToLeft:
				if ( _impl ) _impl->normalizedvolume( 1 - float( qme->x() ) / contentsRect().width() );
				break;
		}
	} else _menu->exec( qme->globalPos() );
}

void KVolumeFader_Widget::mouseMoveEvent( TQMouseEvent* qme ){
	switch ( _dir ) {
		default:
		case Arts::BottomToTop:
			if ( _impl ) _impl->normalizedvolume( 1 - float( qme->y() ) / contentsRect().height() );
			break;
		case Arts::TopToBottom:
			if ( _impl ) _impl->normalizedvolume( float( qme->y() ) / contentsRect().height() );
			break;
		case Arts::LeftToRight:
			if ( _impl ) _impl->normalizedvolume( float( qme->x() ) / contentsRect().width() );
			break;
		case Arts::RightToLeft:
			if ( _impl ) _impl->normalizedvolume( 1 - float( qme->x() ) / contentsRect().width() );
			break;
	}
}

void KVolumeFader_Widget::wheelEvent( TQWheelEvent* qwe ){
	//kdDebug() << k_funcinfo << endl;
	if ( qwe->delta() < 0 ) { if ( _impl ) _impl->normalizedvolume( _impl->dbtondb( _impl->dbvolume() - 1 ) ); }
	if ( qwe->delta() > 0 ) { if ( _impl ) _impl->normalizedvolume( _impl->dbtondb( _impl->dbvolume() + 1 ) ); }
}

void KVolumeFader_Widget::exactValue() {
	//kdDebug() << k_funcinfo << endl;
	bool ok=false;
	double n = KInputDialog::getDouble( i18n( "Set Exact Volume Value" ), i18n( "Exact volume (dB):" ), _impl->dbvolume(), _impl->dbmin(), _impl->dbmax(), 1, &ok, this );
	if ( ok ) _impl->dbvolume( n );
}

REGISTER_IMPLEMENTATION( KVolumeFader_impl );

#include "kvolumefader_impl.moc"
// vim: sw=4 ts=4