summaryrefslogtreecommitdiffstats
path: root/arts/gui/kde/kpoti.cpp
blob: c0a3b5d2e6fd6d14f1bbb88b57e3bb123c197dad (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
/***************************************************************************
                          kpoti.cpp  -  potentiometer widget
                             -------------------
    begin                : Wed Apr 28 23:05:05 MEST 1999

    copyright            : (C) 1999 by Martin Lorenz
    email                : lorenz@ch.tum.de
                           (C) 2002-2003 Matthias Kretz <kretz@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kpoti.h"
#include "kpoti.moc"

#include <tqbitmap.h>
#include <tqpainter.h>
#include <tqcolor.h>
#include <tqdrawutil.h>
#include <tqtimer.h>
#include <tqkeycode.h>
#include <tqpen.h>
#include <tqstring.h>
#include <tqstyle.h>

#include <math.h>

#include <kpixmap.h>
#include <kpixmapeffect.h>
#include <kglobal.h>
#include <kdebug.h>
#include <tdeapplication.h>

#define PI 3.1415926
static const int thresholdTime = 500;
static const int repeatTime    = 100;
static const float maxAngle = PI*135/180; // 140 degrees to both sides
static const float tickLength = 3;

struct KPoti::KPotiPrivate
{
	KPotiPrivate()
		: bgDirty( false )
		, potiDirty( false )
	{}

	bool bgDirty;
	KPixmap bgdb;
	KPixmap bgPixmap( const TQColorGroup & colorGroup )
	{
		if( bgDirty || bgdb.isNull() )
		{
			bgdb.resize( buttonRect.size() );
			TQPainter dbp( &bgdb );
			dbp.setPen( TQt::NoPen );
			TQRect drawRect = bgdb.rect();

			// create mask
			TQBitmap mask( bgdb.size(), true );
			TQPainter maskpainter( &mask );
			maskpainter.setPen( TQt::NoPen );
			maskpainter.setBrush( TQt::color1 );
			maskpainter.drawEllipse( drawRect );
			maskpainter.end();
			bgdb.setMask( mask );

			// inset shadow
			KPixmap gradient( bgdb.size() );
			KPixmapEffect::gradient( gradient, colorGroup.light(), colorGroup.dark(), KPixmapEffect::DiagonalGradient );
			dbp.setBrush( TQBrush( colorGroup.button(), gradient ) );
			dbp.drawEllipse( drawRect );

			potiRect.setSize( drawRect.size() * 0.9 );
			if( potiRect.width() + 6 > drawRect.width() )
			{
				potiRect.setWidth( drawRect.width() - 6 );
				potiRect.setHeight( drawRect.height() - 6 );
			}
			potiRect.moveCenter( center );

			bgDirty = false;
		}
		return bgdb;
	}

	TQColor potiColor;
	bool potiDirty;
	KPixmap potidb;
	KPixmap potiPixmap()
	{
		if( ( potiDirty || potidb.isNull() ) && ! potiRect.size().isEmpty() )
		{
			potidb.resize( potiRect.size() );
			TQPainter dbp( &potidb );
			dbp.setPen( TQt::NoPen );
			TQRect drawRect( potidb.rect() );

			// create mask
			TQBitmap mask( potidb.size(), true );
			TQPainter maskpainter( &mask );
			maskpainter.setPen( TQt::NoPen );
			maskpainter.setBrush( TQt::color1 );
			maskpainter.drawEllipse( drawRect );
			maskpainter.end();
			potidb.setMask( mask );

			KPixmap gradient( potidb.size() );
			KPixmapEffect::gradient( gradient, potiColor.dark( 130 ), potiColor.light( 130 ), KPixmapEffect::DiagonalGradient );
			dbp.setBrush( TQBrush( potiColor, gradient ) );
			dbp.drawEllipse( drawRect );

			potiDirty = false;
		}
		return potidb;
	}

	TQRect buttonRect;
	TQRect potiRect;
	TQRect labelRect;
	TQString label;
	TQPoint center;
};

TQSizePolicy KPoti::sizePolicy() const
{
	return TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Preferred );
}

TQSize KPoti::sizeHint() const
{
	return minimumSizeHint();
}

TQSize KPoti::minimumSizeHint() const
{
	int width = 40;
	int height = 40;
	if( m_bLabel )
	{
		TQFontMetrics metrics( font() );
		d->labelRect = metrics.boundingRect( d->label );
		d->labelRect.setHeight( metrics.lineSpacing() );
		width = KMAX( width, d->labelRect.width() + frameRect().width() - contentsRect().width() );
		height += metrics.lineSpacing();
	}
	//kdDebug() << k_funcinfo << "return " <<  width << "x" << height << endl;
	return TQSize( width, height );
}

TQString KPoti::text() const
{
	return d->label;
}

void KPoti::setText( const TQString & text )
{
	d->label = text;
	setMinimumSize( minimumSizeHint() );
	updateGeometry();
}

/**
  Constructs a poti.

  The \e parent and \e name arguments are sent to the TQWidget constructor.
*/
KPoti::KPoti( TQWidget *parent, const char *name )
    : TQFrame( parent, name, WResizeNoErase | WRepaintNoErase )
	, d( 0 )
{
    init();
}



/**
  Constructs a poti.

  \arg \e minValue is the minimum slider value.
  \arg \e maxValue is the maximum slider value.
  \arg \e step is the page step value.
  \arg \e value is the initial value.

  The \e parent and \e name arguments are sent to the TQWidget constructor.
*/

KPoti::KPoti( int minValue, int maxValue, int step,
		  int value, TQWidget *parent, const char *name )
    : TQFrame( parent, name, WResizeNoErase | WRepaintNoErase )
	, TQRangeControl( minValue, maxValue, 1, step, value )
	, d( 0 )
{
    init(value);
}

KPoti::~KPoti()
{
	delete d;
	d = 0;
}

void KPoti::init(int value)
{
	d = new KPotiPrivate;
	TQFont pifnt = font();
	const_cast<TQFont*>(&pifnt)->setPointSize( 8 );
	d->potiColor.setNamedColor( "red" );

  timer = 0;
  potiVal = value;
  potiPos = positionFromValue(value);
  clickOffset = 0;
  state = Idle;
  track = TRUE;
  ticks = TRUE;
  m_bLabel = true;
  tickInt = 0;

  setFocusPolicy( TQ_TabFocus );
  initTicks();
}


/**
  Does what's needed when someone changes the tickmark status
*/

void KPoti::initTicks()
{
	TQRect available = contentsRect();
	if( m_bLabel )
		available.rTop() += d->labelRect.height();
	d->center = available.center();
	// make the width and height equal
	if( available.width() > available.height() )
		available.setWidth( available.height() );
	else if( available.height() > available.width() )
		available.setHeight( available.width() );
	available.moveCenter( d->center );
	d->buttonRect = available;

	buttonRadius = available.width() / 2.0;
	if( ticks )
	{
		buttonRadius -= tickLength;
		int tickSpace = static_cast<int>( tickLength );
		d->buttonRect.rTop() += tickSpace;
		d->buttonRect.rLeft() += tickSpace;
		d->buttonRect.rRight() -= tickSpace;
		d->buttonRect.rBottom() -= tickSpace;
	}

	d->potiDirty = true;
	d->bgDirty = true;
}


/**
  Enables slider tracking if \e enable is TRUE, or disables tracking
  if \e enable is FALSE.

  If tracking is enabled (default), the slider emits the
  valueChanged() signal whenever the slider is being dragged.  If
  tracking is disabled, the slider emits the valueChanged() signal
  when the user releases the mouse button (unless the value happens to
  be the same as before).

  \sa tracking()
*/

void KPoti::setTracking( bool enable )
{
    track = enable;
}


/**
  \fn bool KPoti::tracking() const
  Returns TRUE if tracking is enabled, or FALSE if tracking is disabled.

  Tracking is initially enabled.

  \sa setTracking()
*/


/**
  \fn void KPoti::valueChanged( int value )
  This signal is emitted when the slider value is changed, with the
  new slider value as an argument.
*/

/**
  \fn void KPoti::sliderPressed()
  This signal is emitted when the user presses the slider with the mouse.
*/

/**
  \fn void KPoti::sliderMoved( int value )
  This signal is emitted when the slider is dragged, with the
  new slider value as an argument.
*/

/**
  \fn void KPoti::sliderReleased()
  This signal is emitted when the user releases the slider with the mouse.
*/

/**
  Calculates slider position corresponding to value \a v. Does not perform
  rounding.
*/

float KPoti::positionFromValue( int v ) const
{

    int range = maxValue() - minValue();
    return ( (v - minValue() ) *2* maxAngle) / range - maxAngle;
}


/**
  Calculates value corresponding to poti position \a p. Performs rounding.
*/

int KPoti::valueFromPosition( float p ) const
{
    int range = maxValue() - minValue();
    return (int) (minValue() + ((p+maxAngle)*range)/(2*maxAngle));
}

/*!
  Implements the virtual TQRangeControl function.
*/

void KPoti::rangeChange()
{
	float newPos = positionFromValue( value() );
	if ( newPos != potiPos ) {
		reallyMovePoti( newPos );
	}
}

void KPoti::paletteChange( const TQPalette & )
{
	d->bgDirty = true;
	d->potiDirty = true;
}

/*!
  Changes the value (slot)
*/

void KPoti::valueChange()
{
    if ( potiVal != value() ) {
	float newPos = positionFromValue( value() );
	potiVal = value();
	reallyMovePoti( newPos );
    }
    emit valueChanged(value());
}


/*!
  Handles resize events for the poti.
*/

void KPoti::resizeEvent( TQResizeEvent * )
{
    rangeChange();
    initTicks();
}

void KPoti::setLabel(bool s)
{
	m_bLabel = s;
	initTicks();
}

/**
  Sets the color of the button
  */
void KPoti::setColor( const TQColor &c )
{
	d->potiColor = c;
	d->potiDirty = true;
	repaint();
}


void KPoti::paintPoti( TQPainter * p )
{
	if( isVisible() )
	{
		KPixmap db = d->potiPixmap();
		if( db.isNull() )
			return;

		TQPainter p2( &db );
		p2.translate( db.rect().center().x(), db.rect().center().y() );
		p2.rotate( potiPos * 180.0 / PI );
		TQRect pointer( db.width() / -20, db.width() / -2, db.width() / 10, db.width() / 2 );
		TQBrush buttonbrush( colorGroup().button() );
		qDrawShadePanel( &p2, pointer, colorGroup(), true, 1, &buttonbrush );
		p2.end();

		p->drawPixmap( d->potiRect, db );
	}
}

/*!
  Performs the actual moving of the slider.
*/

void KPoti::reallyMovePoti( float newPos )
{
  TQPainter p;
  p.begin( this );
  p.setPen(NoPen);
  potiPos = newPos;
  paintPoti(&p);
  p.end();
}




/**
  Handles paint events for the slider.
*/

void KPoti::drawContents( TQPainter * p )
{
	TQPixmap doublebuffer( contentsRect().size() );
	doublebuffer.fill( colorGroup().background() );
	TQPainter dbp( &doublebuffer );
	if( m_bLabel )
	{
		dbp.setFont( font() );
		TQFontMetrics metrics = dbp.fontMetrics();
		dbp.drawText( contentsRect().x() - metrics.leftBearing( d->label[ 0 ] ) + ( contentsRect().width() - d->labelRect.width() ) / 2, metrics.height(), d->label );
	}

    int interval = tickInt;
    if( interval <= 0 )
		interval = 12;
    if( ticks )
		drawTicks( &dbp, buttonRadius, tickLength, interval );

	dbp.drawPixmap( d->buttonRect, d->bgPixmap( colorGroup() ) );

	if( hasFocus() )
		style().tqdrawPrimitive( TQStyle::PE_FocusRect, &dbp, d->buttonRect, colorGroup() );

    paintPoti( &dbp );
	dbp.end();
	p->drawPixmap( contentsRect(), doublebuffer );
}


/*!
  Handles mouse press events for the slider.
*/

void KPoti::mousePressEvent( TQMouseEvent *e )
{
  resetState();

  if ( e->button() == Qt::MidButton ) {
    double pos = atan2( double(e->pos().x()-d->center.x()),
                       double(- e->pos().y() + d->center.y()) );
    movePoti( pos );
    return;
  }
  if ( e->button() != Qt::LeftButton )
    return;


  int dx=e->pos().x()-d->center.x(), dy=e->pos().y()-d->center.y();

  if ( dx*dx+dy*dy < buttonRadius*buttonRadius ) {
    state = Dragging;
    clickOffset =  potiVal + (e->pos().y() ) ;
    emit potiPressed();
  }  else if ( e->pos().x() < width()/2 ) {
    state = TimingDown;
    subtractPage();
    if ( !timer )
      timer = new TQTimer( this );
    connect( timer, TQT_SIGNAL(timeout()), TQT_SLOT(repeatTimeout()) );
    timer->start( thresholdTime, TRUE );
  } else  {
    state = TimingUp;
    addPage();
    if ( !timer )
      timer = new TQTimer( this );
    connect( timer, TQT_SIGNAL(timeout()), TQT_SLOT(repeatTimeout()) );
    timer->start( thresholdTime, TRUE );
  }
}

/*!
  Handles mouse move events for the slider.
*/
void KPoti::mouseMoveEvent( TQMouseEvent *e )
{

    if ( (e->state() & Qt::MidButton) ) { 		// middle button wins
      double pos = atan2( double(e->pos().x()-d->center.x()),
                          double(- e->pos().y()+d->center.y()) );
      movePoti( pos );
      return;
    }
    if ( !(e->state() & Qt::LeftButton) )
	return;					// left mouse button is up
    if ( state != Dragging )
	return;


    movePoti( positionFromValue(- e->pos().y() + clickOffset ));
}


/*!
  Handles mouse release events for the slider.
*/

void KPoti::mouseReleaseEvent( TQMouseEvent *e )
{
    if ( !(e->button() & Qt::LeftButton) )
	return;
    resetState();
}

void KPoti::focusInEvent( TQFocusEvent * e )
{
	//setFrameStyle( Raised | Box );
	//setLineWidth( 1 );
	TQFrame::focusInEvent( e );
}

void KPoti::focusOutEvent( TQFocusEvent * e )
{
	//setFrameStyle( NoFrame );
	//setLineWidth( 0 );
	TQFrame::focusOutEvent( e );
}


void KPoti::enterEvent( TQEvent * )
{
    emit mouseEntered( potiVal );
}


/*!
  Moves the left (or top) edge of the slider to position
  \a pos. Performs snapping.
*/

void KPoti::movePoti( float pos )
{
    float newPos = TQMIN( maxAngle, TQMAX( -maxAngle, pos ) );
    int newVal = valueFromPosition( newPos );
    if ( potiVal != newVal ) {
	potiVal = newVal;
	emit potiMoved( potiVal );
    }
    if ( tracking() && potiVal != value() ) {
	directSetValue( potiVal );
	emit valueChanged( potiVal );
    }


    if ( potiPos != newPos )
	reallyMovePoti( newPos );
}


/*!
  Resets all state information and stops my timer.
*/

void KPoti::resetState()
{
    if ( timer ) {
	timer->stop();
	timer->disconnect();
    }
    switch ( state ) {
    case TimingUp:
    case TimingDown:
	break;
    case Dragging: {
	setValue( valueFromPosition( potiPos ) );
	emit potiReleased();
	break;
    }
    case Idle:
	break;
    default:
	kdWarning() << "KPoti: in wrong state" << endl;
    }
    state = Idle;
}


/*!
  Handles key press events for the slider.
*/

void KPoti::keyPressEvent( TQKeyEvent *e )
{

    switch ( e->key() ) {
    case Key_Left:
      subtractLine();
      break;
    case Key_Right:
      addLine();
      break;
    case Key_Up:
      addLine();
      break;
    case Key_Down:
      subtractLine();
      break;
    case Key_Prior:
      subtractPage();
      break;
    case Key_Next:
      addPage();
      break;
    case Key_Home:
      setValue( minValue() );
      break;
    case Key_End:
      setValue( maxValue() );
      break;
    default:
	e->ignore();
	return;
    }
    e->accept();
}





/*!
  Makes TQRangeControl::setValue() available as a slot.
*/

void KPoti::setValue( int value )
{
    TQRangeControl::setValue( value );
}


/*!
  Moves the slider one pageStep() upwards.
*/

void KPoti::addStep()
{
    addPage();
}


/*!
  Moves the slider one pageStep() downwards.
*/

void KPoti::subtractStep()
{
    subtractPage();
}


/*!
  Waits for autorepeat.
*/

void KPoti::repeatTimeout()
{
    Q_ASSERT( timer );
    timer->disconnect();
    if ( state == TimingDown )
	connect( timer, TQT_SIGNAL(timeout()), TQT_SLOT(subtractStep()) );
    else if ( state == TimingUp )
	connect( timer, TQT_SIGNAL(timeout()), TQT_SLOT(addStep()) );
    timer->start( repeatTime, FALSE );
}




/*!
  Using \a p, draws tickmarks at a distance of \a dist from the edge
  of the widget, using \a w pixels and \a i intervals.
  */

void KPoti::drawTicks( TQPainter *p, double dist, double w, int i ) const
{
	p->setPen( colorGroup().foreground() );
	double angle,s,c;
	double x, y;
	for (int v=0; v<=i; v++)
	{
		angle = -maxAngle+2*maxAngle*v/i;
		s = sin( angle );
		c = cos( angle );
		x = d->center.x() - s * dist;
		y = d->center.y() - c * dist;

		p->drawLine( (int)x, (int)y, (int)(x - s * w), (int)(y - c * w) );
	}
}

void KPoti::wheelEvent(TQWheelEvent *e)
{
	setValue(value()+e->delta()/120*8);
}


/*!
  Sets the way tickmarks are displayed by the slider. \a s can take
  the following values:
  <ul>
  <li> \c NoMarks
  <li> \c Above
  <li> \c Left
  <li> \c Below
  <li> \c Right
  <li> \c Both
  </ul>
  The initial value is \c NoMarks.
  \sa tickmarks(), setTickInterval()
*/

void KPoti::setTickmarks( bool s )
{
    ticks = s;
    initTicks();
    update();
}



/*!
  Sets the interval between tickmarks to \a i. This is a value interval,
  not a pixel interval. If \a i is 0, the slider
  will choose between lineStep() and pageStep(). The initial value of
  tickInterval() is 0.
  \sa tickInterval(), TQRangeControl::lineStep(), TQRangeControl::pageStep()
*/

void KPoti::setTickInterval( int i )
{
    tickInt = TQMAX( 0, i );
    update();
}


/*!
  \fn int KPoti::tickInterval() const
  Returns the interval between tickmarks. Returns 0 if the slider
  chooses between pageStep() and lineStep().
  \sa setTickInterval()
*/

// vim: sw=4 ts=4