| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 26 |
# include "config.h" |
|---|
| 27 |
#endif |
|---|
| 28 |
|
|---|
| 29 |
#include "util/input_slider.hpp" |
|---|
| 30 |
|
|---|
| 31 |
#include <QPaintEvent> |
|---|
| 32 |
#include <QPainter> |
|---|
| 33 |
#include <QBitmap> |
|---|
| 34 |
#include <QStyle> |
|---|
| 35 |
|
|---|
| 36 |
InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent ) |
|---|
| 37 |
{ |
|---|
| 38 |
InputSlider::InputSlider( Qt::Horizontal, _parent ); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) : |
|---|
| 42 |
QSlider( q, _parent ) |
|---|
| 43 |
{ |
|---|
| 44 |
b_sliding = false; |
|---|
| 45 |
setMinimum( 0 ); |
|---|
| 46 |
setMouseTracking(true); |
|---|
| 47 |
setMaximum( 1000 ); |
|---|
| 48 |
setSingleStep( 2 ); |
|---|
| 49 |
setPageStep( 10 ); |
|---|
| 50 |
setTracking( true ); |
|---|
| 51 |
setPosition( -1.0, 0, 0 ); |
|---|
| 52 |
secstotimestr( psz_length, 0 ); |
|---|
| 53 |
setFocusPolicy( Qt::NoFocus ); |
|---|
| 54 |
CONNECT( this, valueChanged(int), this, userDrag( int ) ); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
void InputSlider::setPosition( float pos, int a, int b ) |
|---|
| 58 |
{ |
|---|
| 59 |
if( pos == -1.0 ) |
|---|
| 60 |
setEnabled( false ); |
|---|
| 61 |
else |
|---|
| 62 |
setEnabled( true ); |
|---|
| 63 |
|
|---|
| 64 |
if( !b_sliding ) |
|---|
| 65 |
setValue( (int)(pos * 1000.0 ) ); |
|---|
| 66 |
inputLength = b; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
void InputSlider::userDrag( int new_value ) |
|---|
| 70 |
{ |
|---|
| 71 |
if( b_sliding ) |
|---|
| 72 |
{ |
|---|
| 73 |
float f_pos = (float)(new_value)/1000.0; |
|---|
| 74 |
emit sliderDragged( f_pos ); |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
void InputSlider::mouseReleaseEvent( QMouseEvent *event ) |
|---|
| 79 |
{ |
|---|
| 80 |
b_sliding = false; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
void InputSlider::mousePressEvent(QMouseEvent* event) |
|---|
| 84 |
{ |
|---|
| 85 |
b_sliding = true ; |
|---|
| 86 |
if( event->button() != Qt::LeftButton && |
|---|
| 87 |
event->button() != Qt::MidButton ) |
|---|
| 88 |
{ |
|---|
| 89 |
QSlider::mousePressEvent( event ); |
|---|
| 90 |
return; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(), |
|---|
| 94 |
Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ), |
|---|
| 95 |
Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ), |
|---|
| 96 |
event->modifiers() ); |
|---|
| 97 |
QSlider::mousePressEvent( &newEvent ); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
void InputSlider::mouseMoveEvent(QMouseEvent *event) |
|---|
| 101 |
{ |
|---|
| 102 |
if( b_sliding ) |
|---|
| 103 |
{ |
|---|
| 104 |
QSlider::mouseMoveEvent( event ); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
secstotimestr( psz_length, ( event->x() * inputLength) / size().width() ); |
|---|
| 108 |
setToolTip( psz_length ); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
void InputSlider::wheelEvent( QWheelEvent *event) |
|---|
| 112 |
{ |
|---|
| 113 |
|
|---|
| 114 |
if( !b_sliding ) |
|---|
| 115 |
{ |
|---|
| 116 |
setValue( value() + event->delta()/12 ); |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
emit sliderDragged( value()/1000.0 ); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
event->accept(); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
#define WLENGTH 80 // px |
|---|
| 132 |
#define WHEIGHT 22 // px |
|---|
| 133 |
#define SOUNDMIN 0 // % |
|---|
| 134 |
#define SOUNDMAX 200 // % OR 400 ? |
|---|
| 135 |
|
|---|
| 136 |
SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard, |
|---|
| 137 |
char *psz_colors ) |
|---|
| 138 |
: QAbstractSlider( _parent ) |
|---|
| 139 |
{ |
|---|
| 140 |
f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ; |
|---|
| 141 |
setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX ); |
|---|
| 142 |
setMouseTracking( true ); |
|---|
| 143 |
b_sliding = false; |
|---|
| 144 |
|
|---|
| 145 |
pixOutside = QPixmap( ":/volslide-outside" ); |
|---|
| 146 |
|
|---|
| 147 |
const QPixmap temp( ":/volslide-inside" ); |
|---|
| 148 |
const QBitmap mask( temp.createHeuristicMask() ); |
|---|
| 149 |
|
|---|
| 150 |
setMinimumSize( pixOutside.size() ); |
|---|
| 151 |
|
|---|
| 152 |
pixGradient = QPixmap( mask.size() ); |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 ); |
|---|
| 156 |
|
|---|
| 157 |
QStringList colorList = qfu( psz_colors ).split( ";" ); |
|---|
| 158 |
free( psz_colors ); |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
if( colorList.size() < 12 ) |
|---|
| 162 |
for( int i = colorList.size(); i < 12; i++) |
|---|
| 163 |
colorList.append( "255" ); |
|---|
| 164 |
|
|---|
| 165 |
#define c(i) colorList.at(i).toInt() |
|---|
| 166 |
gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) ); |
|---|
| 167 |
gradient.setColorAt( 0.2, QColor( c(3), c(4), c(5) ) ); |
|---|
| 168 |
gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) ); |
|---|
| 169 |
gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) ); |
|---|
| 170 |
|
|---|
| 171 |
QPainter painter( &pixGradient ); |
|---|
| 172 |
painter.setPen( Qt::NoPen ); |
|---|
| 173 |
painter.setBrush( gradient ); |
|---|
| 174 |
painter.drawRect( pixGradient.rect() ); |
|---|
| 175 |
painter.end(); |
|---|
| 176 |
|
|---|
| 177 |
pixGradient.setMask( mask ); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
void SoundSlider::wheelEvent( QWheelEvent *event ) |
|---|
| 181 |
{ |
|---|
| 182 |
int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step; |
|---|
| 183 |
setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) ); |
|---|
| 184 |
|
|---|
| 185 |
emit sliderReleased(); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
void SoundSlider::mousePressEvent( QMouseEvent *event ) |
|---|
| 189 |
{ |
|---|
| 190 |
if( event->button() != Qt::RightButton ) |
|---|
| 191 |
{ |
|---|
| 192 |
|
|---|
| 193 |
b_sliding = true; |
|---|
| 194 |
i_oldvalue = value(); |
|---|
| 195 |
emit sliderPressed(); |
|---|
| 196 |
changeValue( event->x() - paddingL ); |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
void SoundSlider::mouseReleaseEvent( QMouseEvent *event ) |
|---|
| 201 |
{ |
|---|
| 202 |
if( event->button() != Qt::RightButton ) |
|---|
| 203 |
{ |
|---|
| 204 |
if( !b_outside && value() != i_oldvalue ) |
|---|
| 205 |
{ |
|---|
| 206 |
emit sliderReleased(); |
|---|
| 207 |
setValue( value() ); |
|---|
| 208 |
} |
|---|
| 209 |
b_sliding = false; |
|---|
| 210 |
b_outside = false; |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
void SoundSlider::mouseMoveEvent( QMouseEvent *event ) |
|---|
| 215 |
{ |
|---|
| 216 |
if( b_sliding ) |
|---|
| 217 |
{ |
|---|
| 218 |
QRect rect( paddingL - 15, -1, |
|---|
| 219 |
WLENGTH + 15 * 2 , WHEIGHT + 5 ); |
|---|
| 220 |
if( !rect.contains( event->pos() ) ) |
|---|
| 221 |
{ |
|---|
| 222 |
if ( !b_outside ) |
|---|
| 223 |
setValue( i_oldvalue ); |
|---|
| 224 |
b_outside = true; |
|---|
| 225 |
} |
|---|
| 226 |
else |
|---|
| 227 |
{ |
|---|
| 228 |
b_outside = false; |
|---|
| 229 |
changeValue( event->x() - paddingL ); |
|---|
| 230 |
emit sliderMoved( value() ); |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
else |
|---|
| 234 |
{ |
|---|
| 235 |
int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH; |
|---|
| 236 |
i = __MIN( __MAX( 0, i ), maximum() ); |
|---|
| 237 |
setToolTip( QString("%1 \%" ).arg( i ) ); |
|---|
| 238 |
} |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
void SoundSlider::changeValue( int x ) |
|---|
| 242 |
{ |
|---|
| 243 |
setValue( (x * maximum() + 40 ) / WLENGTH ); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
void SoundSlider::paintEvent(QPaintEvent *e) |
|---|
| 247 |
{ |
|---|
| 248 |
QPainter painter( this ); |
|---|
| 249 |
const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL; |
|---|
| 250 |
|
|---|
| 251 |
const QRectF boundsG( 0, 0, offset , pixGradient.height() ); |
|---|
| 252 |
painter.drawPixmap( boundsG, pixGradient, boundsG ); |
|---|
| 253 |
|
|---|
| 254 |
const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() ); |
|---|
| 255 |
painter.drawPixmap( boundsO, pixOutside, boundsO ); |
|---|
| 256 |
|
|---|
| 257 |
painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) ); |
|---|
| 258 |
QFont font; font.setPixelSize( 9 ); |
|---|
| 259 |
painter.setFont( font ); |
|---|
| 260 |
const QRect rect( 0, 0, 34, 15 ); |
|---|
| 261 |
painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter, |
|---|
| 262 |
QString::number( value() ) + '%' ); |
|---|
| 263 |
|
|---|
| 264 |
painter.end(); |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|