20 #include <qstyleoption.h>
29 knobWidth = qMax( 3 * knob->
markerSize(), min );
33 const int d = 2 * ( extent + 4 ) + knobWidth;
35 const QMargins m = knob->contentsMargins();
36 return QSize(
d + m.left() + m.right(),
d + m.top() + m.bottom() );
44 double a = 90.0 - angle;
47 else if ( a >= 180.0 )
115 setSizePolicy( QSizePolicy::MinimumExpanding,
116 QSizePolicy::MinimumExpanding );
187 angle = qBound( 10.0, angle, 360.0 );
226 const double angle =
numTurns * 360.0;
294 const QRect cr = contentsRect();
302 const int dim = qMin( cr.width(), cr.height() );
308 QRect r( 0, 0, w, w );
312 r.moveLeft( cr.left() +
d );
316 r.moveRight( cr.right() -
d );
320 r.moveCenter( QPoint( cr.center().x(), r.center().y() ) );
325 r.moveTop( cr.top() +
d );
329 r.moveBottom( cr.bottom() -
d );
333 r.moveCenter( QPoint( r.center().x(), cr.center().y() ) );
351 const QRegion region( kr, QRegion::Ellipse );
352 if ( region.contains( pos ) && ( pos != kr.center() ) )
354 const double angle = QLineF( kr.center(), pos ).angle();
375 double angle = QLineF( rect().center(), pos ).angle();
387 if ( qAbs( valueAngle - angle ) > 180.0 )
389 numTurns += ( angle > valueAngle ) ? -1 : 1;
396 const double boundedAngle =
400 angle = boundedAngle;
413 if ( ( currentAngle > 90.0 ) && ( boundedAngle < -90.0 ) )
415 else if ( ( currentAngle < -90.0 ) && ( boundedAngle > 90.0 ) )
421 angle = boundedAngle;
433 switch( event->type() )
435 case QEvent::StyleChange:
436 case QEvent::FontChange:
455 QPainter painter(
this );
456 painter.setClipRegion( event->region() );
460 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter,
this);
462 painter.setRenderHint( QPainter::Antialiasing,
true );
464 if ( !
knobRect.contains( event->region().boundingRect() ) )
477 painter.setRenderHint( QPainter::Antialiasing,
false );
494 QRectF aRect( 0, 0, dim, dim );
495 aRect.moveCenter(
knobRect.center() );
497 QPen pen( Qt::NoPen );
500 QColor c1 = palette().color( QPalette::Light );
501 QColor c2 = palette().color( QPalette::Dark );
503 QLinearGradient gradient( aRect.topLeft(), aRect.bottomRight() );
504 gradient.setColorAt( 0.0, c1 );
505 gradient.setColorAt( 0.3, c1 );
506 gradient.setColorAt( 0.7, c2 );
507 gradient.setColorAt( 1.0, c2 );
517 double off = 0.3 *
knobRect.width();
518 QRadialGradient gradient(
knobRect.center(),
521 gradient.setColorAt( 0.0, palette().
color( QPalette::Midlight ) );
522 gradient.setColorAt( 1.0, palette().
color( QPalette::Button ) );
524 brush = QBrush( gradient );
536 const QColor c = palette().color( QPalette::Button );
537 gradient.setColorAt(0, c.lighter(110) );
538 gradient.setColorAt( 0.5, c);
539 gradient.setColorAt( 0.501, c.darker(102) );
540 gradient.setColorAt(1, c.darker(115) );
542 brush = QBrush( gradient );
548 QLinearGradient gradient(
550 gradient.setColorAt( 0.0, palette().
color( QPalette::Mid ) );
551 gradient.setColorAt( 0.5, palette().
color( QPalette::Button ) );
552 gradient.setColorAt( 1.0, palette().
color( QPalette::Midlight ) );
553 brush = QBrush( gradient );
559 brush = palette().brush( QPalette::Button );
562 painter->setPen( pen );
563 painter->setBrush( brush );
564 painter->drawEllipse( aRect );
577 const QRectF& rect,
double angle )
const
583 const double sinA = -qFastSin( radians );
584 const double cosA = qFastCos( radians );
586 const double xm = rect.center().x();
587 const double ym = rect.center().y();
588 const double margin = 4.0;
605 const double dotCenterDist = radius - 0.5 * dotWidth;
606 if ( dotCenterDist > 0.0 )
608 const QPointF center( xm - sinA * dotCenterDist,
609 ym - cosA * dotCenterDist );
611 QRectF ellipse( 0.0, 0.0, dotWidth, dotWidth );
612 ellipse.moveCenter( center );
614 QColor c1 = palette().color( QPalette::Light );
615 QColor c2 = palette().color( QPalette::Mid );
620 QLinearGradient gradient(
621 ellipse.topLeft(), ellipse.bottomRight() );
622 gradient.setColorAt( 0.0, c1 );
623 gradient.setColorAt( 1.0, c2 );
625 painter->setPen( Qt::NoPen );
626 painter->setBrush( gradient );
628 painter->drawEllipse( ellipse );
636 const double dotCenterDist = radius - 0.5 * dotWidth;
637 if ( dotCenterDist > 0.0 )
639 const QPointF center( xm - sinA * dotCenterDist,
640 ym - cosA * dotCenterDist );
642 QRectF ellipse( 0.0, 0.0, dotWidth, dotWidth );
643 ellipse.moveCenter( center );
645 painter->setPen( Qt::NoPen );
646 painter->setBrush( palette().
color( QPalette::ButtonText ) );
647 painter->drawEllipse( ellipse );
655 const double re = radius;
657 const QLineF line( xm - sinA * rb, ym - cosA * rb,
658 xm - sinA * re, ym - cosA * re );
660 QPen pen( palette().
color( QPalette::ButtonText ), 0 );
661 pen.setCapStyle( Qt::FlatCap );
662 painter->setPen( pen );
663 painter->drawLine ( line );
670 const double re = radius;
672 painter->translate( rect.center() );
673 painter->rotate( angle - 90.0 );
676 polygon += QPointF( re, 0.0 );
677 polygon += QPointF( rb, 0.5 * ( re - rb ) );
678 polygon += QPointF( rb, -0.5 * ( re - rb ) );
680 painter->setPen( Qt::NoPen );
681 painter->setBrush( palette().
color( QPalette::ButtonText ) );
682 painter->drawPolygon( polygon );
684 painter->resetTransform();
699 const QRect cr = contentsRect();
704 w = qMin( cr.width(), cr.height() );
708 const int extent = qCeil(
scaleDraw()->extent( font() ) );
712 QRect focusRect( 0, 0, w, w );
713 focusRect.moveCenter( cr.center() );
761 width = qMax( width, 0 );
765 QSizePolicy::Policy policy;
767 policy = QSizePolicy::Minimum;
769 policy = QSizePolicy::MinimumExpanding;
771 setSizePolicy( policy, policy );
849 #include "moc_qwt_knob.cpp"