1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-12 09:04:33 +02:00

Tweak slider and OS X margin fixes

This commit is contained in:
Leo Franchi
2012-08-19 23:27:32 -04:00
parent ae152017dc
commit a5ec0aac42
6 changed files with 119 additions and 92 deletions

View File

@@ -153,5 +153,6 @@
<file>data/images/add-contact.png</file>
<file>data/images/account-none.png</file>
<file>data/images/green-dot.png</file>
<file>data/images/sliderbutton_knob.png</file>
</qresource>
</RCC>

View File

@@ -128,6 +128,11 @@ AccountWidget::AccountWidget( QWidget* parent )
m_inviteButton->setText( tr( "Invite" ) );
vLayout->addWidget( m_inviteButton, 1, 1 );
#ifdef Q_OS_MAC
layout()->setContentsMargins( 0, 0, 0, 0 );
#endif
setInviteWidgetsEnabled( false );
}

View File

@@ -47,6 +47,11 @@ AccountsPopupWidget::AccountsPopupWidget( QWidget* parent )
setContentsMargins( contentsMargins().left() + 2, contentsMargins().top() + 2 ,
contentsMargins().right(), contentsMargins().bottom() );
#ifdef Q_OS_MAC
setContentsMargins( 0, 0, 0, 0 );
layout()->setContentsMargins( 0, 0, 0, 0 );
#endif
}
void

View File

@@ -72,6 +72,11 @@ AccountsToolButton::AccountsToolButton( QWidget* parent )
separatorLine->setStyleSheet( "QWidget { border-top: 1px solid black; }" );
wMainLayout->addWidget( separatorLine );
#ifdef Q_OS_MAC
w->setContentsMargins( 4, 4, 2, 2 );
wMainLayout->setContentsMargins( 4, 4, 2, 2 );
#endif
m_popup->setWidget( w );
connect( m_popup, SIGNAL( hidden() ), SLOT( popupHidden() ) );

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012 Teo Mrnjavac <teo@kde.org>
* Copyright 2012 Leo Franchi <lfranchi@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,9 +19,12 @@
#include "SlideSwitchButton.h"
#include "utils/TomahawkUtils.h"
#include <QPainter>
#include <QPropertyAnimation>
#include <QStyleOptionButton>
#include <QPixmap>
SlideSwitchButton::SlideSwitchButton( QWidget* parent )
: QPushButton( parent )
@@ -44,11 +48,20 @@ void
SlideSwitchButton::init()
{
setCheckable( true );
#ifndef Q_OS_MAC
setMouseTracking( true );
#endif
m_backCheckedColor = QColor( 167, 183, 211 );
m_backUncheckedColor = QColor( "#d9d9d9" );
m_knob.load( RESPATH "images/sliderbutton_knob.png" );
m_backCheckedColorTop = QColor( 8, 54, 134 );
m_backCheckedColorBottom = QColor( 118, 172, 240 );
m_backUncheckedColorTop = QColor( 128, 128, 128 );
m_backUncheckedColorBottom = QColor( 179, 179, 179 );
m_baseColorTop = m_backUncheckedColorTop;
m_baseColorBottom = m_backUncheckedColorBottom;
m_baseColor = m_backUncheckedColor;
m_textColor = QColor( "#606060" );
setFocusPolicy( Qt::NoFocus );
@@ -64,15 +77,16 @@ SlideSwitchButton::init()
}
QSize
SlideSwitchButton::sizeHint()
SlideSwitchButton::sizeHint() const
{
QSize size = QPushButton::sizeHint();
QFontMetrics fm( m_textFont );
int maxTextLength = qMax( fm.boundingRect( m_checkedText ).width(),
fm.boundingRect( m_uncheckedText ).width() );
size.rwidth() = contentsMargins().left() + contentsMargins().right()
+ 2 /*a bit of margin*/ + maxTextLength + ( height() - 4 ) * 1.25;
return size;
// QSize size = QPushButton::sizeHint();
// QFontMetrics fm( m_textFont );
// int maxTextLength = qMax( fm.boundingRect( m_checkedText ).width(),
// fm.boundingRect( m_uncheckedText ).width() );
// size.rwidth() = contentsMargins().left() + contentsMargins().right()
// + 2 /*a bit of margin*/ + maxTextLength + ( height() - 4 ) * 1.25;
// return size;
return QSize( 70, 20 );
}
@@ -80,83 +94,63 @@ void
SlideSwitchButton::paintEvent( QPaintEvent* event )
{
QPainter painter( this );
painter.initFrom( this );
painter.setPen( m_baseColor );
QStyleOptionButton option;
option.initFrom( this );
painter.setRenderHint( QPainter::Antialiasing );
QPalette palette;
QStyleOptionButton option;
initStyleOption( &option );
QPen border;
border.setWidth( 1 );
#ifndef Q_OS_MAC
if ( option.state & QStyle::State_MouseOver )
painter.setPen( palette.color( QPalette::Highlight ) );
border.setColor( palette.color( QPalette::Highlight ) );
else
#endif
border.setColor( QColor( "#606060" ) );
painter.setPen( border );
//TODO: should the whole thing be highlighted or just the knob?
QLinearGradient gradient( 0, 0, 0, 1 );
gradient.setCoordinateMode( QGradient::ObjectBoundingMode );
gradient.setColorAt( 0, m_baseColor.lighter( 95 ) );
gradient.setColorAt( 1, m_baseColor.lighter( 115 ) );
gradient.setColorAt( 0, m_baseColorTop );
gradient.setColorAt( 1, m_baseColorBottom );
painter.setBrush( gradient );
painter.drawRoundedRect( QRect( 0, 0, width() - 0, height() - 0 ).adjusted( 3, 0, -3, 0 ), 2, 2 );
painter.setBrush( QBrush( gradient ) );
painter.setRenderHints( QPainter::Antialiasing, true );
painter.drawRoundedRect( QRect( 0, 0, width() - 0, height() - 0 ),
5, 5 );
painter.drawPixmap( m_knobX * ( width() - m_knob.width() ), 0, m_knob );
//knob
QRect knobRect( 2, 2, ( height() - 4 ) * 1.25, height() - 4 );
knobRect.moveTo( QPoint( 2 + m_knobX * ( width() - 4 - knobRect.width() ), knobRect.y() ) );
QLinearGradient knobGradient( 0, 0, 0, 1 );
knobGradient.setCoordinateMode( QGradient::ObjectBoundingMode );
knobGradient.setColorAt( 0, m_backUncheckedColor.lighter( 115 ) );
knobGradient.setColorAt( 1, m_backUncheckedColor.lighter( 95 ) );
painter.setBrush( QBrush( knobGradient ) );
qDebug() << "Drawn with knob at:" << m_knobX;
painter.drawRoundedRect( knobRect, 1, 1 );
// if we ever want to try with primitives...
// QStyleOptionButton option;
// option.initFrom( this );
// option.rect = QRect( m_knobX + 1, 1, ( height() - 2 ) * 1.25, height() - 2 );
// style()->drawPrimitive( QStyle::PE_PanelButtonBevel, &option, &painter, this );
if ( m_knobX != 1.0 && m_knobX != 0.0 )
return;
//let's draw some text...
QRect textRect = rect().adjusted( 4, 3, -4, -3 );
painter.setFont( m_textFont );
if ( m_baseColorTop == m_backUncheckedColorTop )
painter.setPen( m_textColor );
if ( m_knobX == 0. )
{
//draw on right
painter.drawText( textRect, Qt::AlignRight | Qt::AlignVCenter, m_uncheckedText );
}
else if ( m_knobX == 1. )
{
//draw on left
painter.drawText( textRect, Qt::AlignLeft | Qt::AlignVCenter, m_checkedText );
}
//otherwise don't draw because the knob is being animated
else
painter.setPen( Qt::white );
painter.end();
painter.setFont( m_textFont );
const QRect textRect( m_knobX == 0. ? m_knob.width() : 0, 0, width() - m_knob.width(), height() );
painter.drawText( textRect, Qt::AlignCenter, m_knobX == 0 ? m_uncheckedText : m_checkedText );
}
void
SlideSwitchButton::onCheckedStateChanged()
{
QPropertyAnimation *animation = new QPropertyAnimation( this, "knobX" );
animation->setDuration( 50 );
if ( !m_knobAnimation.isNull() )
m_knobAnimation.data()->stop();
if ( isChecked() )
{
animation->setStartValue( 0. );
animation->setEndValue( 1. );
}
else
{
animation->setStartValue( 1. );
animation->setEndValue( 0. );
}
animation->start( QAbstractAnimation::DeleteWhenStopped );
m_knobAnimation = QWeakPointer<QPropertyAnimation>( new QPropertyAnimation( this, "knobX" ) );
m_knobAnimation.data()->setDuration( 50 );
m_knobAnimation.data()->setStartValue( isChecked() ? 0 : 1 );
m_knobAnimation.data()->setEndValue( isChecked() ? 1 : 0 );
m_knobAnimation.data()->start( QAbstractAnimation::DeleteWhenStopped );
}
@@ -165,20 +159,24 @@ SlideSwitchButton::setBackChecked( bool state )
{
if ( state != m_backChecked )
{
if ( !m_backTopAnimation.isNull() )
m_backTopAnimation.data()->stop();
if ( !m_backBottomAnimation.isNull() )
m_backBottomAnimation.data()->stop();
m_backChecked = state;
QPropertyAnimation *animation = new QPropertyAnimation( this, "baseColor" );
animation->setDuration( 300 );
if ( state )
{
animation->setStartValue( m_backUncheckedColor );
animation->setEndValue( m_backCheckedColor );
}
else
{
animation->setStartValue( m_backCheckedColor );
animation->setEndValue( m_backUncheckedColor );
}
animation->start( QAbstractAnimation::DeleteWhenStopped );
m_backTopAnimation = QWeakPointer<QPropertyAnimation>( new QPropertyAnimation( this, "baseColorTop" ) );
m_backBottomAnimation = QWeakPointer<QPropertyAnimation>( new QPropertyAnimation( this, "baseColorBottom" ) );
m_backTopAnimation.data()->setDuration( 300 );
m_backBottomAnimation.data()->setDuration( 300 );
m_backTopAnimation.data()->setStartValue( state ? m_backUncheckedColorTop : m_backCheckedColorTop );
m_backTopAnimation.data()->setEndValue( state ? m_backCheckedColorTop : m_backUncheckedColorTop );
m_backBottomAnimation.data()->setStartValue( state ? m_backUncheckedColorBottom : m_backCheckedColorBottom );
m_backBottomAnimation.data()->setEndValue( state ? m_backCheckedColorBottom : m_backUncheckedColorBottom );
m_backTopAnimation.data()->start( QAbstractAnimation::DeleteWhenStopped );
m_backBottomAnimation.data()->start( QAbstractAnimation::DeleteWhenStopped );
}
}

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012 Teo Mrnjavac <teo@kde.org>
* Copyright 2012 Leo Franchi <lfranchi@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -23,10 +24,16 @@
#include <QColor>
#include <QPixmap>
#include <QFont>
#include <QWeakPointer>
class QPropertyAnimation;
class SlideSwitchButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY( qreal knobX READ knobX WRITE setKnobX )
Q_PROPERTY( QColor baseColorTop READ baseColorTop WRITE setBaseColorTop )
Q_PROPERTY( QColor baseColorBottom READ baseColorBottom WRITE setBaseColorBottom )
public:
explicit SlideSwitchButton( QWidget* parent = 0 );
explicit SlideSwitchButton( const QString& checkedText,
@@ -35,22 +42,22 @@ public:
void init();
virtual QSize sizeHint();
virtual QSize minimumSizeHint() { return sizeHint(); }
virtual QSize sizeHint() const;
virtual QSize minimumSizeHint() const { return sizeHint(); }
//the back check-state cannot be changed by the user, only programmatically
//to notify that the user-requested operation has completed
void setBackChecked( bool state );
bool backChecked() const;
void setKnobX( double x ) { m_knobX = x; repaint(); }
double knobX() const { return m_knobX; }
Q_PROPERTY( double knobX READ knobX WRITE setKnobX )
void setKnobX( qreal x ) { m_knobX = x; repaint(); }
qreal knobX() const { return m_knobX; }
void setBaseColor( const QColor& color ) { m_baseColor = color; repaint(); }
QColor baseColor() const { return m_baseColor; }
Q_PROPERTY( QColor baseColor READ baseColor WRITE setBaseColor )
void setBaseColorTop( const QColor& color ) { m_baseColorTop = color; repaint(); }
QColor baseColorTop() const { return m_baseColorTop; }
void setBaseColorBottom( const QColor& color ) { m_baseColorBottom = color; }
QColor baseColorBottom() const { return m_baseColorBottom; }
protected:
void paintEvent( QPaintEvent* event );
@@ -58,15 +65,21 @@ private slots:
void onCheckedStateChanged();
private:
QPixmap m_knob;
QString m_checkedText;
QString m_uncheckedText;
QColor m_baseColor;
QColor m_baseColorTop, m_baseColorBottom;
QColor m_textColor;
QColor m_backUncheckedColor;
QColor m_backCheckedColor;
QColor m_backUncheckedColorTop, m_backUncheckedColorBottom;
QColor m_backCheckedColorTop, m_backCheckedColorBottom;
QFont m_textFont; //needed for sizeHint
bool m_backChecked;
double m_knobX;
qreal m_knobX;
QWeakPointer<QPropertyAnimation> m_backTopAnimation;
QWeakPointer<QPropertyAnimation> m_backBottomAnimation;
QWeakPointer<QPropertyAnimation> m_knobAnimation;
};
#endif // SLIDESWITCHBUTTON_H