Commit a569b096 authored by Ludovic Fauvet's avatar Ludovic Fauvet Committed by Jean-Baptiste Kempf

Qt: use a QStyle to draw the seek slider

This should be much cleaner than drawing ourselves inside the paintEvent
of the widget. Furthermore it is easier to fallback to the native slider
available in pre 2.0.
The use of a QStyle also fixes the clipping issue where the handle was
cropped while being at the start or the end of the slider.
Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 31f985a5
......@@ -96,6 +96,7 @@ nodist_SOURCES_qt4 = \
ui/messages_panel.h \
ui/about.h \
ui/update.h \
styles/seekstyle.moc.cpp \
dialogs/ml_configuration.moc.cpp \
components/playlist/ml_model.moc.cpp \
ui/sout.h
......@@ -315,7 +316,8 @@ SOURCES_qt4 = qt4.cpp \
util/pictureflow.cpp \
util/buttons/BrowseButton.cpp \
util/buttons/DeckButtonsLayout.cpp \
util/buttons/RoundButton.cpp
util/buttons/RoundButton.cpp \
styles/seekstyle.cpp
if HAVE_DARWIN
SOURCES_qt4 += util/searchlineedit_mac.mm
......@@ -398,7 +400,8 @@ noinst_HEADERS = \
util/singleton.hpp \
util/buttons/RoundButton.hpp \
util/buttons/DeckButtonsLayout.hpp \
util/buttons/BrowseButton.hpp
util/buttons/BrowseButton.hpp \
styles/seekstyle.hpp
EXTRA_DIST += \
......
This diff is collapsed.
/*****************************************************************************
* seekstyle.hpp : Seek slider style
****************************************************************************
* Copyright (C) 2011-2012 VLC authors and VideoLAN
*
* Authors: Ludovic Fauvet <etix@videolan.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.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU 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.
*****************************************************************************/
#ifndef SEEKSTYLE_HPP
#define SEEKSTYLE_HPP
#include <QWindowsStyle>
class SeekStyle : public QWindowsStyle
{
Q_OBJECT
public:
SeekStyle() { }
virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0) const;
virtual void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const;
};
#endif // SEEKSTYLE_HPP
......@@ -36,7 +36,6 @@
#include <QPaintEvent>
#include <QPainter>
#include <QBitmap>
#include <QPainter>
#include <QStyleOptionSlider>
#include <QLinearGradient>
#include <QTimer>
......@@ -62,6 +61,7 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
f_buffering = 1.0;
mHandleOpacity = 1.0;
chapters = NULL;
mHandleLength = -1;
// prepare some static colors
QPalette p = palette();
......@@ -109,6 +109,10 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
setTracking( true );
setFocusPolicy( Qt::NoFocus );
/* Use the new/classic style */
if( !b_classic )
setStyle( new SeekStyle );
/* Init to 0 */
setPosition( -1.0, 0, 0 );
secstotimestr( psz_length, 0 );
......@@ -253,7 +257,8 @@ void SeekSlider::mousePressEvent( QMouseEvent* event )
}
isSliding = true ;
setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
emit sliderMoved( value() );
event->accept();
}
......@@ -262,14 +267,15 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event )
{
if( isSliding )
{
setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
emit sliderMoved( value() );
}
/* Tooltip */
if ( inputLength > 0 )
{
int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
int margin = handleLength() / 2;
int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
QString chapterLabel;
......@@ -292,7 +298,7 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event )
QPoint target( event->globalX() - ( event->x() - posX ),
QWidget::mapToGlobal( pos() ).y() );
secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
secstotimestr( psz_length, ( ( posX - margin ) * inputLength ) / ( size().width() - handleLength() ) );
mTimeTooltip->setTip( target, psz_length, chapterLabel );
}
event->accept();
......@@ -368,178 +374,12 @@ bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
QSize SeekSlider::sizeHint() const
{
if ( b_classic )
return QSlider::sizeHint();
return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
: QSize( 18, 100 );
}
QSize SeekSlider::handleSize() const
{
const int size = ( orientation() == Qt::Horizontal ? height() : width() );
return QSize( size, size );
}
void SeekSlider::paintEvent( QPaintEvent *event )
{
if( b_classic )
return QSlider::paintEvent( event );
QStyleOptionSlider option;
initStyleOption( &option );
/* */
QPainter painter( this );
painter.setRenderHints( QPainter::Antialiasing );
// draw bar
const int barCorner = 3;
qreal sliderPos = -1;
int range = MAXIMUM;
QRect barRect = rect();
// adjust positions based on the current orientation
if ( option.sliderPosition != 0 )
{
switch ( orientation() )
{
case Qt::Horizontal:
sliderPos = ( ( (qreal)width() ) / (qreal)range )
* (qreal)option.sliderPosition;
break;
case Qt::Vertical:
sliderPos = ( ( (qreal)height() ) / (qreal)range )
* (qreal)option.sliderPosition;
break;
}
}
switch ( orientation() )
{
case Qt::Horizontal:
barRect.setHeight( height() /2 );
break;
case Qt::Vertical:
barRect.setWidth( width() /2 );
break;
}
barRect.moveCenter( rect().center() );
QSize hSize( handleSize() - QSize( 6, 6 ) );
QSize sSize( handleSize() - QSize( 2, 2 ) );
if ( gradientsTargetSize != size() )
{
/* Need to fix gradients */
gradientsTargetSize = size();
backgroundGradient.setFinalStop( 0, height() );
foregroundGradient.setFinalStop( 0, height() );
handleGradient.setFinalStop( 0, hSize.height() );
}
// draw a slight 3d effect on the bottom
painter.setPen( QColor( 230, 230, 230 ) );
painter.setBrush( Qt::NoBrush );
painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
// draw background
painter.setPen( Qt::NoPen );
painter.setBrush( backgroundGradient );
painter.drawRoundedRect( barRect, barCorner, barCorner );
// adjusted foreground rectangle
QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
switch ( orientation() )
{
case Qt::Horizontal:
valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
break;
case Qt::Vertical:
valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
valueRect.moveBottom( rect().bottom() );
break;
}
if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
{
// draw foreground
painter.setPen( Qt::NoPen );
painter.setBrush( foregroundGradient );
painter.drawRoundedRect( valueRect, barCorner, barCorner );
}
// draw buffering overlay
if ( f_buffering < 1.0 )
{
QRect innerRect = barRect.adjusted( 1, 1,
barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
QColor overlayColor = QColor( "Orange" );
overlayColor.setAlpha( 128 );
painter.setBrush( overlayColor );
painter.drawRoundedRect( innerRect, barCorner, barCorner );
}
if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
{
/* draw chapters tickpoints */
if ( chapters && inputLength && size().width() )
{
if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
{
QList<SeekPoint> points = chapters->getPoints();
painter.setPen( tickpointForeground );
painter.setBrush( Qt::NoBrush );
foreach( SeekPoint point, points )
{
int x = point.time / 1000000.0 / inputLength * size().width();
painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
}
}
}
// draw handle
if ( sliderPos != -1 )
{
const int margin = 0;
QPoint pos;
switch ( orientation() )
{
case Qt::Horizontal:
pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
pos.rx() = qMax( margin, pos.x() );
pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
break;
case Qt::Vertical:
pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
pos.ry() = qMax( margin, pos.y() );
pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
break;
}
QPoint shadowPos( pos - QPoint( 2, 2 ) );
/* FIXME: precompute gradient. Anim Compatible ? */
QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
shadowPos.y() + ( sSize.height() / 2 ),
qMax( sSize.width(), sSize.height() ) / 2 );
shadowGradient.setColorAt( 0.4, shadowDark );
shadowGradient.setColorAt( 1.0, shadowLight );
painter.setPen( Qt::NoPen );
painter.setOpacity( mHandleOpacity );
// draw the handle's shadow
painter.setBrush( shadowGradient );
painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );
// finally draw the handle
painter.setBrush( handleGradient );
painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
}
}
}
qreal SeekSlider::handleOpacity() const
{
return mHandleOpacity;
......@@ -552,6 +392,18 @@ void SeekSlider::setHandleOpacity(qreal opacity)
update();
}
inline int SeekSlider::handleLength()
{
if ( mHandleLength > 0 )
return mHandleLength;
/* Ask for the length of the handle to the underlying style */
QStyleOptionSlider option;
initStyleOption( &option );
mHandleLength = style()->pixelMetric( QStyle::PM_SliderLength, &option );
return mHandleLength;
}
void SeekSlider::hideHandle()
{
/* If pause is called while not running Qt will complain */
......@@ -568,6 +420,7 @@ bool SeekSlider::isAnimationRunning() const
|| hideHandleTimer->isActive();
}
/* This work is derived from Amarok's work under GPLv2+
- Mark Kretschmann
- Gábor Lehel
......
......@@ -32,6 +32,7 @@
#include <vlc_common.h>
#include "timetooltip.hpp"
#include "styles/seekstyle.hpp"
#include <QSlider>
#include <QPainter>
......@@ -44,6 +45,7 @@ class QHideEvent;
class QTimer;
class SeekPoints;
class QPropertyAnimation;
class QStyleOption;
/* Input Slider derived from QSlider */
class SeekSlider : public QSlider
......@@ -64,15 +66,14 @@ protected:
virtual void leaveEvent( QEvent * );
virtual void hideEvent( QHideEvent * );
virtual void paintEvent( QPaintEvent* event );
virtual bool eventFilter( QObject *obj, QEvent *event );
QSize handleSize() const;
virtual QSize sizeHint() const;
bool isAnimationRunning() const;
qreal handleOpacity() const;
void setHandleOpacity( qreal opacity );
int handleLength();
private:
bool isSliding; /* Whether we are currently sliding by user action */
......@@ -84,6 +85,7 @@ private:
float f_buffering;
SeekPoints* chapters;
bool b_classic;
int mHandleLength;
/* Colors & gradients */
QSize gradientsTargetSize;
......@@ -109,9 +111,11 @@ private slots:
signals:
void sliderDragged( float );
};
friend class SeekStyle;
};
/* Sound Slider inherited directly from QAbstractSlider */
class QPaintEvent;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment