21#include <swvolumewidget.h>
25SWVolumeWidget::SWVolumeWidget( QWidget* parent )
30 setNumBarsBlocks( 32, 32 );
32 _valueList.resize( _numBars );
33 double dNb = double( _numBars );
34 double dSt = ( 1.0 / dNb ) / dNb;
35 for(
int i=0; i < _numBars; ++i )
36 _valueList[i] =
double( i ) / dNb + i * dSt + 0.03;
43SWVolumeWidget::~SWVolumeWidget()
49void SWVolumeWidget::mousePressEvent( QMouseEvent* event )
51 if( event->button() == Qt::LeftButton )
53 _lastPosX =
event->pos().x();
60void SWVolumeWidget::mouseMoveEvent( QMouseEvent* event )
62 if( ( event->buttons() & Qt::LeftButton ) && _moving )
65 double newvalue = 1.0 / (double) _rangeTo * event->pos().x();
67 _value = qBound( 0.0, newvalue, 1.0 );
68 int intvalue = 100 * _value;
73 emit valueChanged( intvalue );
78void SWVolumeWidget::mouseReleaseEvent( QMouseEvent* event )
81 if( event->button() == Qt::LeftButton && _moving )
87void SWVolumeWidget::paintEvent( QPaintEvent* event )
94 QPainter painter(
this );
99 pen.setWidth( _frameOffset );
101 painter.setPen( pen );
102 QRect frame = rect();
103 frame.adjust( 0, 0, -_frameOffset, -_frameOffset );
104 painter.fillRect( frame, QColor( 44,44,44 ) );
105 painter.drawRoundedRect( frame, 10, 10 );
110 int frameWidth = rect().width();
111 int frameHeight = rect().height();
115 int blockHeight = frameHeight / _numBlocks;
116 int blockWidth = frameWidth / _numBars;
119 int numColors = _colors.size();
120 double factorBars = (double) numColors / (
double) _numBars;
123 for(
int i=0; i<_numBars; ++i )
126 if( _value < _valueList[i] )
129 int barHeight = qRound( _valueList[i] * frameHeight ) - 4 * _frameOffset;
130 int blocks = barHeight / blockHeight;
131 int blockPosY = frameHeight - blocks * blockHeight - 2 * _frameOffset;
132 double factorBlocks= (double) _colors.size() / (double) blocks;
134 for(
int j = 0; j < blocks; ++j )
136 QRect block = QRect( blockPosX, blockPosY, blockWidth - _padding, blockHeight - _padding );
137 int colorIndex = _gradientMode ? numColors - 1 - factorBlocks * (double) j : qRound( factorBars * (
double) i ) ;
138 painter.fillRect( block, _colors[ colorIndex ] );
140 blockPosY += blockHeight;
143 blockPosX += blockWidth;