summaryrefslogtreecommitdiffstats
path: root/kmix/mdwslider.cpp
blob: a4fa0bcc305c428e14d5f9c9ae177b7f0a225460 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*
 * KMix -- KDE's full featured mini mixer
 *
 *
 * Copyright (C) 1996-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 <klocale.h>
#include <kled.h>
#include <kiconloader.h>
#include <kconfig.h>
#include <kaction.h>
#include <kpopupmenu.h>
#include <kglobalaccel.h>
#include <kkeydialog.h>
#include <kdebug.h>

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

#include "mdwslider.h"
#include "mixer.h"
#include "viewbase.h"
#include "kledbutton.h"
#include "ksmallslider.h"
#include "verticaltext.h"

/**
 * MixDeviceWidget that represents a single mix device, inlcuding PopUp, muteLED, ...
 *
 * Used in KMix main window and DockWidget and PanelApplet.
 * It can be configured to include or exclude the recordLED and the muteLED.
 * The direction (horizontal, vertical) can be configured and whether it should
 * be "small"  (uses KSmallSlider instead of TQSlider then).
 *
 * Due to the many options, this is the most complicated MixDeviceWidget subclass.
 */
MDWSlider::MDWSlider(Mixer *mixer, MixDevice* md,
                                 bool showMuteLED, bool showRecordLED,
                                 bool small, Qt::Orientation orientation,
                                 TQWidget* parent, ViewBase* mw, const char* name) :
    MixDeviceWidget(mixer,md,small,orientation,parent,mw,name),
    m_linked(true), m_valueStyle( NNONE), m_iconLabel( 0 ), m_muteLED( 0 ), m_recordLED( 0 ), m_label( 0 ), _layout(0)
{
	// create actions (on _mdwActions, see MixDeviceWidget)

	new KToggleAction( i18n("&Split Channels"), 0, TQT_TQOBJECT(this), TQT_SLOT(toggleStereoLinked()),
			_mdwActions, "stereo" );
	new KToggleAction( i18n("&Hide"), 0, TQT_TQOBJECT(this), TQT_SLOT(setDisabled()), _mdwActions, "hide" );

	KToggleAction *a = new KToggleAction(i18n("&Muted"), 0, 0, 0, _mdwActions, "mute" );
	connect( a, TQT_SIGNAL(toggled(bool)), TQT_SLOT(toggleMuted()) );

	if( m_mixdevice->isRecordable() ) {
		a = new KToggleAction( i18n("Set &Record Source"), 0, 0, 0, _mdwActions, "recsrc" );
		connect( a, TQT_SIGNAL(toggled(bool)), TQT_SLOT( toggleRecsrc()) );
	}

	new KAction( i18n("C&onfigure Global Shortcuts..."), 0, TQT_TQOBJECT(this), TQT_SLOT(defineKeys()), _mdwActions, "keys" );

	// create widgets
	createWidgets( showMuteLED, showRecordLED );

	m_keys->insert( "Increase volume", i18n( "Increase Volume of '%1'" ).arg(m_mixdevice->name().utf8().data()), TQString(),
			KShortcut(), KShortcut(), TQT_TQOBJECT(this), TQT_SLOT( increaseVolume() ) );
	m_keys->insert( "Decrease volume", i18n( "Decrease Volume of '%1'" ).arg(m_mixdevice->name().utf8().data()), TQString(),
			KShortcut(), KShortcut(), TQT_TQOBJECT(this), TQT_SLOT( decreaseVolume() ) );
	m_keys->insert( "Toggle mute", i18n( "Toggle Mute of '%1'" ).arg(m_mixdevice->name().utf8().data()), TQString(),
			KShortcut(), KShortcut(), TQT_TQOBJECT(this), TQT_SLOT( toggleMuted() ) );

	installEventFilter( this ); // filter for popup

        update();
}


TQSizePolicy MDWSlider::sizePolicy() const
{
	if ( _orientation == Qt::Vertical ) {
		return TQSizePolicy(  TQSizePolicy::Fixed, TQSizePolicy::Expanding );
	}
	else {
		return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
	}
}


/**
 * Creates up to 4 widgets - Icon, Mute-Button, Slider and Record-Button.
 *
 * Those widgets are placed into

*/
void MDWSlider::createWidgets( bool showMuteLED, bool showRecordLED )
{
    if ( _orientation == Qt::Vertical ) {
	_layout = new TQVBoxLayout( this );
	_layout->setAlignment(TQt::AlignCenter);
    }
    else {
	_layout = new TQHBoxLayout( this );
	_layout->setAlignment(TQt::AlignCenter);
    }

	 // -- MAIN SLIDERS LAYOUT  ---
	 TQBoxLayout *slidersLayout;
	 if ( _orientation == Qt::Vertical ) {
		 slidersLayout = new TQHBoxLayout( _layout );
		 slidersLayout->setAlignment(TQt::AlignVCenter);
	 }
	 else {
		 slidersLayout = new TQVBoxLayout( _layout );
		 slidersLayout->setAlignment(TQt::AlignHCenter);
	 }

	 /* cesken: This is inconsistent. Why should vertical and horizontal tqlayout differ?
	  *         Also it eats too much space - especially when you don't show sliders at all.
	  *         Even more on the vertical panel applet (see Bug #97667)
	     if ( _orientation == Qt::Horizontal )
		 slidersLayout->addSpacing( 10 );
	 */


	 // -- LABEL LAYOUT TO POSITION
	 TQBoxLayout *labelLayout;
	 if ( _orientation == Qt::Vertical ) {
		 labelLayout = new TQVBoxLayout( slidersLayout );
		 labelLayout->setAlignment(TQt::AlignHCenter);
	 }
	 else {
		 labelLayout = new TQHBoxLayout( slidersLayout );
		 labelLayout->setAlignment(TQt::AlignVCenter);
	 }
    if ( _orientation == Qt::Vertical ) {
		 m_label = new VerticalText( this, m_mixdevice->name().utf8().data() );
		TQToolTip::add( m_label, m_mixdevice->name() );

	 }
	 else {
		 m_label = new TQLabel(this);
		 static_cast<TQLabel*>(m_label) ->setText(m_mixdevice->name());
		TQToolTip::add( m_label, m_mixdevice->name() );
	 }

	 m_label->hide();

/* This addSpacing() looks VERY bizarre => removing it (cesken, 21.2.2006).
   Also horizontal and vertical spacing differs. This doesn't look sensible.
    if ( _orientation == Qt::Horizontal )
		 labelLayout->addSpacing( 36 );
*/
	 labelLayout->addWidget( m_label );
	 m_label->installEventFilter( this );

/* This addSpacing() looks VERY bizarre => removing it (cesken, 21.2.2006)
   Also horizontal and vertical spacing differs. This doesn't look sensible.
    if ( _orientation == Qt::Vertical ) {
		 labelLayout->addSpacing( 18 );
	 }
*/

	 // -- SLIDERS, LEDS AND ICON
	 TQBoxLayout *sliLayout;
	 if ( _orientation == Qt::Vertical ) {
		 sliLayout = new TQVBoxLayout( slidersLayout );
		 sliLayout->setAlignment(TQt::AlignHCenter);
    }
    else {
		 sliLayout = new TQHBoxLayout( slidersLayout );
		 sliLayout->setAlignment(TQt::AlignVCenter);
	 }

	 // --- ICON  ----------------------------
    TQBoxLayout *iconLayout;
    if ( _orientation == Qt::Vertical ) {
		 iconLayout = new TQHBoxLayout( sliLayout );
		 iconLayout->setAlignment(TQt::AlignVCenter);
	 }
	 else {
		 iconLayout = new TQVBoxLayout( sliLayout );
		 iconLayout->setAlignment(TQt::AlignHCenter);
	 }

	 m_iconLabel = 0L;
	 setIcon( m_mixdevice->type() );
	 iconLayout->addStretch();
	 iconLayout->addWidget( m_iconLabel );
	 iconLayout->addStretch();
	 m_iconLabel->installEventFilter( this );

	 sliLayout->addSpacing( 5 );


	 // --- MUTE LED
	 if ( showMuteLED ) {
		 TQBoxLayout *ledtqlayout;
		 if ( _orientation == Qt::Vertical ) {
			 ledtqlayout = new TQHBoxLayout( sliLayout );
			 ledtqlayout->setAlignment(TQt::AlignVCenter);
		 }
		 else {
			 ledtqlayout = new TQVBoxLayout( sliLayout );
			 ledtqlayout->setAlignment(TQt::AlignHCenter);
		 }

		 if( m_mixdevice->hasMute() )
		 {
			 ledtqlayout->addStretch();
			 // create mute LED
			 m_muteLED = new KLedButton( TQt::green, KLed::On, KLed::Sunken,
					 KLed::Circular, this, "MuteLED" );
			 m_muteLED->setFixedSize( TQSize(16, 16) );
			 m_muteLED->resize( TQSize(16, 16) );
			 ledtqlayout->addWidget( m_muteLED );
			 TQToolTip::add( m_muteLED, i18n( "Mute" ) );
			 connect( m_muteLED, TQT_SIGNAL(stateChanged(bool)), this, TQT_SLOT(toggleMuted()) );
			 m_muteLED->installEventFilter( this );
			 ledtqlayout->addStretch();
		 } // has Mute LED
		 else {
			 // we don't have a MUTE LED. We create a dummy widget
			 // !! possibly not neccesary any more (we are layouted)
			 TQWidget *qw = new TQWidget(this, "Spacer");
			 qw->setFixedSize( TQSize(16, 16) );
			 ledtqlayout->addWidget(qw);
			 qw->installEventFilter( this );
		 } // has no Mute LED

		 sliLayout->addSpacing( 3 );
    } // showMuteLED

    // --- SLIDERS ---------------------------
	 TQBoxLayout *volLayout;
	 if ( _orientation == Qt::Vertical ) {
		 volLayout = new TQHBoxLayout( sliLayout );
		 volLayout->setAlignment(TQt::AlignVCenter);
	 }
	 else {
		 volLayout = new TQVBoxLayout( sliLayout );
		 volLayout->setAlignment(TQt::AlignHCenter);
	 }

	 // Sliders and volume number indication
	 TQBoxLayout *slinumLayout;
	 for( int i = 0; i < m_mixdevice->getVolume().count(); i++ )
    {
		 Volume::ChannelID chid = Volume::ChannelID(i);
		 // @todo !! Normally the mixdevicewidget SHOULD know, which slider represents which channel.
		 // We should look up the mapping here, but for now, we simply assume "chid == i".

		 int maxvol = m_mixdevice->getVolume().maxVolume();
		 int minvol = m_mixdevice->getVolume().minVolume();

		 if ( _orientation == Qt::Vertical ) {
		   slinumLayout = new TQVBoxLayout( volLayout );
		   slinumLayout->setAlignment(TQt::AlignHCenter);
		 }
		 else {
		   slinumLayout = new TQHBoxLayout( volLayout );
		   slinumLayout->setAlignment(TQt::AlignVCenter);
		 }

		 // create labels to hold volume values (taken from qamix/kamix)
		 TQLabel *number = new TQLabel( "100", this );
		 slinumLayout->addWidget( number );
		 number->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
		 number->setLineWidth( 2 );
		 number->setMinimumWidth( number->sizeHint().width() );
		 number->setPaletteBackgroundColor( TQColor(190, 250, 190) );
		 // don't show the value by default
		 number->hide();
		 updateValue( number, chid );
		 _numbers.append( number );

		 TQWidget* slider;
		 if ( m_small ) {
			 slider = new KSmallSlider( minvol, maxvol, maxvol/10,
					 m_mixdevice->getVolume( chid ), _orientation,
					 this, m_mixdevice->name().ascii() );
		 }
		 else	{
			 slider = new TQSlider( 0, maxvol, maxvol/10,
					 maxvol - m_mixdevice->getVolume( chid ), _orientation,
					 this, m_mixdevice->name().ascii() );
			 slider->setMinimumSize( slider->sizeHint() );
		 }

		 slider->setBackgroundOrigin(AncestorOrigin);
		 slider->installEventFilter( this );
		 TQToolTip::add( slider, m_mixdevice->name() );

		 if( i>0 && isStereoLinked() ) {
			 // show only one (the first) slider, when the user wants it so
			 slider->hide();
			 number->hide();
		 }
		 slinumLayout->addWidget( slider );  // add to tqlayout
		 m_sliders.append ( slider );   // add to list
		 _slidersChids.append(chid);        // Remember slider-chid association
		 connect( slider, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(volumeChange(int)) );
	 } // for all channels of this device


    // --- RECORD SOURCE LED --------------------------
    if ( showRecordLED )
	 {
		 sliLayout->addSpacing( 5 );

		 // --- LED LAYOUT TO CENTER ---
		 TQBoxLayout *rectqlayout;
		 if ( _orientation == Qt::Vertical ) {
			 rectqlayout = new TQHBoxLayout( sliLayout );
			 rectqlayout->setAlignment(TQt::AlignVCenter);
		 }
		 else {
			 rectqlayout = new TQVBoxLayout( sliLayout );
			 rectqlayout->setAlignment(TQt::AlignHCenter);
		 }

		 if( m_mixdevice->isRecordable() ) {
			 rectqlayout->addStretch();
			 m_recordLED = new KLedButton( TQt::red,
					 m_mixdevice->isRecSource()?KLed::On:KLed::Off,
					 KLed::Sunken, KLed::Circular, this, "RecordLED" );
			 m_recordLED->setFixedSize( TQSize(16, 16) );
			 rectqlayout->addWidget( m_recordLED );
			 connect(m_recordLED, TQT_SIGNAL(stateChanged(bool)), this, TQT_SLOT(setRecsrc(bool)));
			 m_recordLED->installEventFilter( this );
                         TQToolTip::add( m_recordLED, i18n( "Record" ) );
			 rectqlayout->addStretch();
		 }
		 else
		 {
			 // we don't have a RECORD LED. We create a dummy widget
			 // !! possibly not neccesary any more (we are layouted)
			 TQWidget *qw = new TQWidget(this, "Spacer");
			 qw->setFixedSize( TQSize(16, 16) );
			 rectqlayout->addWidget(qw);
			 qw->installEventFilter( this );
		 } // has no Record LED
	 } // showRecordLED

	tqlayout()->activate();
}


TQPixmap
MDWSlider::icon( int icontype )
{
   TQPixmap miniDevPM;
   switch (icontype) {
      case MixDevice::AUDIO:
         miniDevPM = UserIcon("mix_audio"); break;
      case MixDevice::BASS:
      case MixDevice::SURROUND_LFE:  // "LFE" SHOULD have an own icon
         miniDevPM = UserIcon("mix_bass"); break;
      case MixDevice::CD:
         miniDevPM = UserIcon("mix_cd"); break;
      case MixDevice::EXTERNAL:
         miniDevPM = UserIcon("mix_ext"); break;
      case MixDevice::MICROPHONE:
         miniDevPM = UserIcon("mix_microphone");break;
      case MixDevice::MIDI:
         miniDevPM = UserIcon("mix_midi"); break;
      case MixDevice::RECMONITOR:
         miniDevPM = UserIcon("mix_recmon"); break;
      case MixDevice::TREBLE:
         miniDevPM = UserIcon("mix_treble"); break;
      case MixDevice::UNKNOWN:
         miniDevPM = UserIcon("mix_unknown"); break;
      case MixDevice::VOLUME:
         miniDevPM = UserIcon("mix_volume"); break;
      case MixDevice::VIDEO:
         miniDevPM = UserIcon("mix_video"); break;
      case MixDevice::SURROUND:
      case MixDevice::SURROUND_BACK:
      case MixDevice::SURROUND_CENTERFRONT:
      case MixDevice::SURROUND_CENTERBACK:
         miniDevPM = UserIcon("mix_surround"); break;
      case MixDevice::HEADPHONE:
         miniDevPM = UserIcon( "mix_headphone" ); break;
      case MixDevice::DIGITAL:
         miniDevPM = UserIcon( "mix_digital" ); break;
      case MixDevice::AC97:
         miniDevPM = UserIcon( "mix_ac97" ); break;
      default:
         miniDevPM = UserIcon("mix_unknown"); break;
   }

   return miniDevPM;
}

void
MDWSlider::setIcon( int icontype )
{
   if( !m_iconLabel )
   {
      m_iconLabel = new TQLabel(this);
      m_iconLabel->setBackgroundOrigin(AncestorOrigin);
      installEventFilter( m_iconLabel );
   }

   TQPixmap miniDevPM = icon( icontype );
   if ( !miniDevPM.isNull() )
   {
      if ( m_small )
      {
         // scale icon
         TQWMatrix t;
         t = t.scale( 10.0/miniDevPM.width(), 10.0/miniDevPM.height() );
         m_iconLabel->setPixmap( miniDevPM.xForm( t ) );
         m_iconLabel->resize( 10, 10 );
      } else
         m_iconLabel->setPixmap( miniDevPM );
      m_iconLabel->setAlignment( TQt::AlignCenter );
   } else
   {
      kdError(67100) << "Pixmap missing." << endl;
   }

   tqlayout()->activate();
}

bool
MDWSlider::isLabeled() const
{
    if ( m_label == 0 )
	return false;
    else
	return !m_label->isHidden();
}

void
MDWSlider::toggleStereoLinked()
{
   setStereoLinked( !isStereoLinked() );
}

void
MDWSlider::setStereoLinked(bool value)
{
   m_linked = value;

   TQWidget *slider = m_sliders.first();
   TQLabel *number = _numbers.first();
   TQString qs = number->text();

   /***********************************************************
      Remember value of first slider, so that it can be copied
      to the other sliders.
    ***********************************************************/
   int firstSliderValue = 0;
   bool firstSliderValueValid = false;
   if (slider->isA(TQSLIDER_OBJECT_NAME_STRING) ) {
      TQSlider *sld = static_cast<TQSlider*>(slider);
      firstSliderValue = sld->value();
      firstSliderValueValid = true;
   }
   else if ( slider->isA("KSmallSlider") ) {
      KSmallSlider *sld = static_cast<KSmallSlider*>(slider);
      firstSliderValue = sld->value();
      firstSliderValueValid = true;
   }

   for( slider=m_sliders.next(), number=_numbers.next();  slider!=0 && number!=0; slider=m_sliders.next(), number=_numbers.next() ) {
      if ( m_linked ) {
         slider->hide();
	 number->hide();
      }
      else {
         // When splitting, make the next sliders show the same value as the first.
         // This might not be entirely true, but better than showing the random value
         // that was used to be shown before hot-fixing this. !! must be revised
         if ( firstSliderValueValid ) {
            // Remark: firstSlider== 0 could happen, if the static_cast<TQRangeControl*> above fails.
            //         It's a safety measure, if we got other Slider types in the future.
            if (slider->isA(TQSLIDER_OBJECT_NAME_STRING) ) {
               TQSlider *sld = static_cast<TQSlider*>(slider);
               sld->setValue( firstSliderValue );
            }
            if (slider->isA("KSmallSlider") ) {
               KSmallSlider *sld = static_cast<KSmallSlider*>(slider);
               sld->setValue( firstSliderValue );
            }
         }
         slider->show();
	 number->setText(qs);
         if (m_valueStyle != NNONE)
	     number->show();
      }
   }

   slider = m_sliders.last();
   if( slider && static_cast<TQSlider *>(slider)->tickmarks() )
      setTicks( true );

   tqlayout()->activate();
}


void
MDWSlider::setLabeled(bool value)
{
    if ( m_label == 0 )
	return;

   if (value )
      m_label->show();
   else
      m_label->hide();

   tqlayout()->activate();
}

void
MDWSlider::setTicks( bool ticks )
{
	TQWidget* slider;

		slider = m_sliders.first();

	if ( slider->inherits( TQSLIDER_OBJECT_NAME_STRING ) )
	{
		if( ticks )
			if( isStereoLinked() )
				static_cast<TQSlider *>(slider)->setTickmarks( TQSlider::Right );
			else
			{
				static_cast<TQSlider *>(slider)->setTickmarks( TQSlider::NoMarks );
				slider = m_sliders.last();
				static_cast<TQSlider *>(slider)->setTickmarks( TQSlider::Left );
			}
		else
		{
			static_cast<TQSlider *>(slider)->setTickmarks( TQSlider::NoMarks );
			slider = m_sliders.last();
			static_cast<TQSlider *>(slider)->setTickmarks( TQSlider::NoMarks );
		}
	}

	tqlayout()->activate();
}

void
MDWSlider::setValueStyle( ValueStyle valueStyle )
{
    m_valueStyle = valueStyle;

    int i = 0;
    TQValueList<Volume::ChannelID>::Iterator it = _slidersChids.begin();
    for(TQLabel *number = _numbers.first(); number!=0; number = _numbers.next(), ++i, ++it) {
        Volume::ChannelID chid = *it;
        switch ( m_valueStyle ) {
	case NNONE: number->hide(); break;
	default: 
	    if ( !isStereoLinked() || (i == 0)) {
	      updateValue( number, chid );
	      number->show();
	    }
	}
    }
    tqlayout()->activate();
}

void
MDWSlider::setIcons(bool value)
{
    if ( m_iconLabel != 0 ) {
	if ( ( !m_iconLabel->isHidden()) !=value ) {
	    if (value)
		m_iconLabel->show();
	    else
		m_iconLabel->hide();

	    tqlayout()->activate();
	}
    } // if it has an icon
}

void
MDWSlider::setColors( TQColor high, TQColor low, TQColor back )
{
    for( TQWidget *slider=m_sliders.first(); slider!=0; slider=m_sliders.next() ) {
        KSmallSlider *smallSlider = dynamic_cast<KSmallSlider*>(slider);
        if ( smallSlider ) smallSlider->setColors( high, low, back );
    }
}

void
MDWSlider::setMutedColors( TQColor high, TQColor low, TQColor back )
{
    for( TQWidget *slider=m_sliders.first(); slider!=0; slider=m_sliders.next() ) {
        KSmallSlider *smallSlider = dynamic_cast<KSmallSlider*>(slider);
        if ( smallSlider ) smallSlider->setGrayColors( high, low, back );
    }
}

void 
MDWSlider::updateValue( TQLabel *value, Volume::ChannelID chid ) {
    TQString qs;
    Volume& vol = m_mixdevice->getVolume();

    if (m_valueStyle == NABSOLUTE )
      qs.sprintf("%3d",  (int) vol.getVolume( chid ) );
    else
        qs.sprintf("%3d", (int)( vol.getVolume( chid ) / (double)vol.maxVolume() * 100 ) );
    value->setText(qs);
}


/** This slot is called, when a user has changed the volume via the KMix Slider */
void MDWSlider::volumeChange( int )
{
   // --- Step 1: Get a REFERENCE of the volume Object ---
   Volume& vol = m_mixdevice->getVolume();

   // --- Step 2: Change the volumes directly in the Volume object to reflect the Sliders ---
   if ( isStereoLinked() )
   {
      TQWidget *slider = m_sliders.first();
      Volume::ChannelID chid  = _slidersChids.first();

      int sliderValue = 0;
      if ( slider->inherits( "KSmallSlider" ) )
      {
         KSmallSlider *slider = dynamic_cast<KSmallSlider *>(m_sliders.first());
         if (slider) {
	    sliderValue= slider->value();
	 }
      }
      else {
         TQSlider *slider = dynamic_cast<TQSlider *>(m_sliders.first());
         if (slider) {
				if ( _orientation == Qt::Vertical )
					sliderValue= slider->maxValue() - slider->value();
				else
					sliderValue= slider->value();

         }
      }

		// With balance proper working, we must change relative volumes,
		// not absolute, which leads a make some difference calc related
		// to new sliders position against the top volume on channels
		long volumeDif =  sliderValue - vol.getTopStereoVolume( Volume::MMAIN );

      if ( chid == Volume::LEFT ) {
			vol.setVolume( Volume::LEFT , vol.getVolume( Volume::LEFT ) + volumeDif );
			vol.setVolume( Volume::RIGHT, vol.getVolume( Volume::RIGHT ) + volumeDif );
      }
      else {
         kdDebug(67100) << "MDWSlider::volumeChange(), unknown chid " << chid << endl;
      }

      updateValue( _numbers.first(), Volume::LEFT );
   } // joined
   else {
      int n = 0;
      TQValueList<Volume::ChannelID>::Iterator it = _slidersChids.begin();
      TQLabel *number = _numbers.first();
      for( TQWidget *slider=m_sliders.first(); slider!=0 && number!=0; slider=m_sliders.next(), number=_numbers.next(), ++it )
      {
          Volume::ChannelID chid = *it;
	  if ( slider->inherits( "KSmallSlider" ) )
	  {
	      KSmallSlider *smallSlider = dynamic_cast<KSmallSlider *>(slider);
	      if (smallSlider)
		  vol.setVolume( chid, smallSlider->value() );
	  }
	  else
	  {
	      TQSlider *bigSlider = dynamic_cast<TQSlider *>(slider);
	      if (bigSlider)
				if ( _orientation == Qt::Vertical )
					vol.setVolume( chid, bigSlider->maxValue() - bigSlider->value() );
				else
					vol.setVolume( chid, bigSlider->value() );
	  }
	  updateValue( number, chid );
	  n++;
      }
   }

   // --- Step 3: Write back the new volumes to the HW ---
   m_mixer->commitVolumeChange(m_mixdevice);
}


/**
   This slot is called, when a user has clicked the recsrc button. Also it is called by any other
    associated KAction like the context menu.
*/
void MDWSlider::toggleRecsrc() {
    setRecsrc( !m_mixdevice->isRecSource() );
}


void MDWSlider::setRecsrc(bool value )
{
	if (  m_mixdevice->isRecordable() ) {
		m_mixer->setRecordSource( m_mixdevice->num(), value );
	}
}


/**
   This slot is called, when a user has clicked the mute button. Also it is called by any other
    associated KAction like the context menu.
*/
void MDWSlider::toggleMuted() {
    setMuted( !m_mixdevice->isMuted() );
}

void MDWSlider::setMuted(bool value)
{
    if (  m_mixdevice->hasMute() ) {
	m_mixdevice->setMuted( value );
	m_mixer->commitVolumeChange(m_mixdevice);
    }
}


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

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


/**
   This slot is called on a MouseWheel event. Also it is called by any other
    associated KAction like the context menu.
*/
void MDWSlider::increaseVolume()
{
	Volume vol = m_mixdevice->getVolume();
	long inc = vol.maxVolume() / 20;
	if ( inc == 0 )
		inc = 1;
	for ( int i = 0; i < vol.count(); i++ ) {
		long newVal = (vol[i]) + inc;
		m_mixdevice->setVolume( i, newVal < vol.maxVolume() ? newVal : vol.maxVolume() );
	}
	m_mixer->commitVolumeChange(m_mixdevice);
}

/**
   This slot is called on a MouseWheel event. Also it is called by any other
    associated KAction like the context menu.
*/
void MDWSlider::decreaseVolume()
{
	Volume vol = m_mixdevice->getVolume();
	long inc = vol.maxVolume() / 20;
	if ( inc == 0 )
		inc = 1;
	for ( int i = 0; i < vol.count(); i++ ) {
		long newVal = (vol[i]) - inc;
		m_mixdevice->setVolume( i, newVal > 0 ? newVal : 0 );
	}
	m_mixer->commitVolumeChange(m_mixdevice);
}


/**
   This is called whenever there are volume updates pending from the hardware for this MDW.
   At the moment it is called regulary via a TQTimer (implicitely).
*/
void MDWSlider::update()
{
	// update volumes
	Volume vol = m_mixdevice->getVolume();
	if( isStereoLinked() )
	{
		TQValueList<Volume::ChannelID>::Iterator it = _slidersChids.begin();

		long avgVol = vol.getAvgVolume( Volume::MMAIN );

		TQWidget *slider =  m_sliders.first();
		if ( slider == 0 ) {
			return; // !!! Development version, check this !!!
		}
		slider->blockSignals( true );
		if ( slider->inherits( "KSmallSlider" ) )
		{
			KSmallSlider *smallSlider = dynamic_cast<KSmallSlider *>(slider);
			if (smallSlider) {
				smallSlider->setValue( avgVol ); // !! inverted ?!?
				smallSlider->setGray( m_mixdevice->isMuted() );
			}
		} // small slider
		else {
			TQSlider *bigSlider = dynamic_cast<TQSlider *>(slider);
			if (bigSlider)
			{
				// In case of stereo linked and single slider, slider must
				// show the top of both volumes, and not strangely low down
				// the main volume by half

				if ( _orientation == Qt::Vertical )
					bigSlider->setValue( vol.maxVolume() - vol.getTopStereoVolume( Volume::MMAIN ) );
				else
					bigSlider->setValue( vol.getTopStereoVolume( Volume::MMAIN ) );
			}
		} // big slider

		updateValue( _numbers.first(), Volume::LEFT );
		slider->blockSignals( false );
	} // only 1 slider (stereo-linked)
	else {
		TQValueList<Volume::ChannelID>::Iterator it = _slidersChids.begin();
		for( int i=0; i<vol.count(); i++, ++it ) {
			TQWidget *slider = m_sliders.at( i );
			Volume::ChannelID chid = *it;
			if (slider == 0) {
				// !!! not implemented !!!
				// not implemented: happens if there are record and playback
				// sliders in the same device. Or if you only show
				// the right slider (or any other fancy occasion)
				continue;
			}
			slider->blockSignals( true );

			if ( slider->inherits( "KSmallSlider" ) )
			{
				KSmallSlider *smallSlider = dynamic_cast<KSmallSlider *>(slider);
				if (smallSlider) {
					smallSlider->setValue( vol[chid] );
					smallSlider->setGray( m_mixdevice->isMuted() );
				}
			}
			else
			{
				TQSlider *bigSlider = dynamic_cast<TQSlider *>(slider);
				if (bigSlider)
					if ( _orientation == Qt::Vertical ) {
						bigSlider->setValue( vol.maxVolume() - vol[i] );
					}
					else {
						bigSlider->setValue( vol[i] );
				}
			}

			updateValue( _numbers.at ( i ), chid );

			slider->blockSignals( false );
		} // for all sliders
	} // more than 1 slider

	// update mute led
	if ( m_muteLED ) {
		m_muteLED->blockSignals( true );
		m_muteLED->setState( m_mixdevice->isMuted() ? KLed::Off : KLed::On );
		m_muteLED->blockSignals( false );
	}

	// update recsrc
	if( m_recordLED ) {
		m_recordLED->blockSignals( true );
		m_recordLED->setState( m_mixdevice->isRecSource() ? KLed::On : KLed::Off );
		m_recordLED->blockSignals( false );
	}
}

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

	KPopupMenu *menu = m_mixerwidget->getPopup();
	menu->insertTitle( SmallIcon( "kmix" ), m_mixdevice->name() );

	if ( m_sliders.count()>1 ) {
		KToggleAction *stereo = (KToggleAction *)_mdwActions->action( "stereo" );
		if ( stereo ) {
			stereo->setChecked( !isStereoLinked() );
			stereo->plug( menu );
		}
	}

	KToggleAction *ta = (KToggleAction *)_mdwActions->action( "recsrc" );
	if ( ta ) {
		ta->setChecked( m_mixdevice->isRecSource() );
		ta->plug( menu );
	}

	if ( m_mixdevice->hasMute() ) {
		ta = ( KToggleAction* )_mdwActions->action( "mute" );
		if ( ta ) {
			ta->setChecked( m_mixdevice->isMuted() );
			ta->plug( menu );
		}
	}

	KAction *a = _mdwActions->action(  "hide" );
	if ( a )
		a->plug(  menu );

	a = _mdwActions->action( "keys" );
	if ( a && m_keys ) {
		KActionSeparator sep( TQT_TQOBJECT(this) );
		sep.plug( menu );
		a->plug( menu );
	}

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

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

/**
 * An event filter for the various TQWidgets. We watch for Mouse press Events, so
 * that we can popup the context menu.
 */
bool MDWSlider::eventFilter( TQObject* obj, TQEvent* e )
{
    if (e->type() == TQEvent::MouseButtonPress) {
	TQMouseEvent *qme = TQT_TQMOUSEEVENT(e);
	if (qme->button() == Qt::RightButton) {
	    showContextMenu();
	    return true;
	}
    }
    // Attention: We don't filter WheelEvents for KSmallSlider, because it handles WheelEvents itself
    else if ( (e->type() == TQEvent::Wheel) && !obj->isA("KSmallSlider") ) {
	TQWheelEvent *qwe = TQT_TQWHEELEVENT(e);
	if (qwe->delta() > 0) {
	    increaseVolume();
	}
	else {
	    decreaseVolume();
	}
	return true;
    }
    return TQWidget::eventFilter(obj,e);
}

#include "mdwslider.moc"