Commit 033c64ed authored by Filipe Azevedo's avatar Filipe Azevedo Committed by Jean-Baptiste Kempf

Qt: Round Buttons

Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent cce2ffdb
......@@ -70,6 +70,9 @@ nodist_SOURCES_qt4 = \
util/searchlineedit.moc.cpp \
util/qvlcapp.moc.cpp \
util/pictureflow.moc.cpp \
util/buttons/RoundButton.moc.cpp \
util/buttons/DeckButtonsLayout.moc.cpp \
util/buttons/BrowseButton.moc.cpp \
resources.cpp \
ui/equalizer.h \
ui/video_effects.h \
......@@ -297,7 +300,10 @@ SOURCES_qt4 = qt4.cpp \
util/searchlineedit.cpp \
util/registry.cpp \
util/qt_dirs.cpp \
util/pictureflow.cpp
util/pictureflow.cpp \
util/buttons/BrowseButton.cpp \
util/buttons/DeckButtonsLayout.cpp \
util/buttons/RoundButton.cpp
if HAVE_DARWIN
SOURCES_qt4 += util/searchlineedit_mac.mm
......@@ -370,7 +376,10 @@ noinst_HEADERS = \
util/qt_dirs.hpp \
util/registry.hpp \
util/pictureflow.hpp \
util/singleton.hpp
util/singleton.hpp \
util/buttons/RoundButton.hpp \
util/buttons/DeckButtonsLayout.hpp \
util/buttons/BrowseButton.hpp
EXTRA_DIST += \
......
/*****************************************************************************
* Copyright © 2011 VideoLAN
* $Id$
*
* Authors: Filipe Azevedo, aka PasNox
*
* 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.
*****************************************************************************/
#include "BrowseButton.hpp"
#include <QPainter>
#include <QStyleOptionToolButton>
BrowseButton::BrowseButton( QWidget* parent, BrowseButton::Type type )
: RoundButton( parent )
{
setIconSize( QSize( 16, 16 ) );
setType( type );
}
BrowseButton::Type BrowseButton::type() const
{
return mType;
}
void BrowseButton::setType( BrowseButton::Type type )
{
//FIXME
switch ( type ) {
case BrowseButton::Backward:
setIcon( QIcon::fromTheme( "media-seek-backward" ) );
break;
case BrowseButton::Forward:
setIcon( QIcon::fromTheme( "media-seek-forward" ) );
break;
}
mType = type;
}
QSize BrowseButton::sizeHint() const
{
return QSize( 50, 26 );
}
void BrowseButton::paintEvent( QPaintEvent* event )
{
/*RoundButton::paintEvent( event );
return;*/
Q_UNUSED( event );
const int corner = 5;
const int margin = 5;
QPainter painter( this );
QStyleOptionToolButton option;
initStyleOption( &option );
painter.setRenderHint( QPainter::Antialiasing );
painter.setPen( QPen( pen( &option ), 1 ) );
painter.setBrush( brush( &option ) );
painter.drawRoundedRect( rect().adjusted( 1, 1, -1, -1 ), corner, corner );
switch ( mType ) {
case BrowseButton::Backward:
option.rect = option.rect.adjusted( 0, 0, -height() +margin, 0 );
break;
case BrowseButton::Forward:
option.rect = option.rect.adjusted( height() -margin, 0, 0, 0 );
break;
}
style()->drawControl( QStyle::CE_ToolButtonLabel, &option, &painter, this );
}
/*****************************************************************************
* Copyright © 2011 VideoLAN
* $Id$
*
* Authors: Filipe Azevedo, aka PasNox
*
* 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 BROWSEBUTTON_H
#define BROWSEBUTTON_H
#include "RoundButton.hpp"
class BrowseButton : public RoundButton
{
Q_OBJECT
public:
enum Type {
Backward = 0,
Forward = 1
};
BrowseButton( QWidget* parent = 0, BrowseButton::Type type = BrowseButton::Forward );
virtual QSize sizeHint() const;
BrowseButton::Type type() const;
public slots:
void setType( BrowseButton::Type type );
protected:
BrowseButton::Type mType;
virtual void paintEvent( QPaintEvent* event );
};
#endif // BROWSEBUTTON_H
/*****************************************************************************
* Copyright © 2011 VideoLAN
* $Id$
*
* Authors: Filipe Azevedo, aka PasNox
*
* 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.
*****************************************************************************/
#include "DeckButtonsLayout.hpp"
DeckButtonsLayout::DeckButtonsLayout( QWidget* parent )
: QLayout( parent )
{
backwardItem = 0;
goItem = 0;
forwardItem = 0;
setContentsMargins( 0, 0, 0, 0 );
setSpacing( 0 );
setBackwardButton( 0 );
setRoundButton( 0 );
setForwardButton( 0 );
}
DeckButtonsLayout::~DeckButtonsLayout()
{
delete backwardItem;
delete goItem;
delete forwardItem;
}
QSize DeckButtonsLayout::sizeHint() const
{
const int bbw = backwardButton ? backwardButton->sizeHint().width() : 0;
const int fbw = forwardButton ? forwardButton->sizeHint().width() : 0;
const int gbw = bbw +fbw == 0 ? ( RoundButton ? RoundButton->sizeHint().width() : 0 ) : bbw +fbw;
int left; int top; int right; int bottom;
QSize sh = QSize( gbw, 0 );
getContentsMargins( &left, &top, &right, &bottom );
sh.setHeight( qMax( sh.height(), backwardButton ? backwardButton->sizeHint().height() : 0 ) );
sh.setHeight( qMax( sh.height(), RoundButton ? RoundButton->sizeHint().height() : 0 ) );
sh.setHeight( qMax( sh.height(), forwardButton ? forwardButton->sizeHint().height() : 0 ) );
sh.rwidth() += left +right;
sh.rheight() += top +bottom;
return sh;
}
int DeckButtonsLayout::count() const
{
return 3;
}
void DeckButtonsLayout::setGeometry( const QRect& _r )
{
QLayout::setGeometry( _r );
int left; int top; int right; int bottom;
getContentsMargins( &left, &top, &right, &bottom );
const QRect r = _r.adjusted( left, top, right, bottom );
const QAbstractButton* button = backwardButton ? backwardButton : forwardButton;
qreal factor = 1;
if ( !button ) {
if ( RoundButton ) {
const int min = qMin( r.height(), r.width() );
QRect rect = QRect( QPoint(), QSize( min, min ) );
rect.moveCenter( r.center() );
RoundButton->setGeometry( rect );
}
return;
}
else if ( backwardButton && forwardButton ) {
factor = (qreal)r.width() /(qreal)( button->sizeHint().width() *2 );
}
else if ( RoundButton ) {
factor = (qreal)r.width() /(qreal)( button->sizeHint().width() +( RoundButton->sizeHint().width() /2 ) );
}
else {
factor = (qreal)r.width() /(qreal)( button->sizeHint().width() );
}
if ( RoundButton ) {
int height = (qreal)RoundButton->sizeHint().height() *factor;
while ( height > r.height() ) {
factor -= 0.1;
height = (qreal)RoundButton->sizeHint().height() *factor;
}
QRect rect( QPoint(), QSize( height, height ) );
rect.moveCenter( r.center() );
if ( backwardButton && forwardButton ) {
// nothing to do
}
else if ( backwardButton ) {
rect.moveRight( r.right() );
}
else if ( forwardButton ) {
rect.moveLeft( r.left() );
}
RoundButton->setGeometry( rect );
}
else {
int height = (qreal)button->sizeHint().height() *factor;
while ( height > r.height() ) {
factor -= 0.1;
height = (qreal)button->sizeHint().height() *factor;
}
}
const QSize bs = QSize( (qreal)button->sizeHint().width() *factor, (qreal)button->sizeHint().height() *factor );
if ( backwardButton ) {
QRect gr = RoundButton ? QRect( QPoint(), RoundButton->size() ) : r;
QRect rect = QRect( QPoint(), bs );
if ( RoundButton ) {
gr.moveTopLeft( RoundButton->pos() );
}
rect.moveCenter( gr.center() );
rect.moveRight( gr.center().x() +1 );
backwardButton->setGeometry( rect );
}
if ( forwardButton ) {
QRect gr = RoundButton ? QRect( QPoint(), RoundButton->size() ) : r;
QRect rect = QRect( QPoint(), bs );
if ( RoundButton ) {
gr.moveTopLeft( RoundButton->pos() );
}
rect.moveCenter( gr.center() );
rect.moveLeft( gr.center().x() );
forwardButton->setGeometry( rect );
}
if ( RoundButton ) {
RoundButton->raise();
}
}
void DeckButtonsLayout::addItem( QLayoutItem* item )
{
Q_UNUSED( item );
}
QLayoutItem* DeckButtonsLayout::itemAt( int index ) const
{
switch ( index ) {
case 0:
return backwardItem;
case 1:
return goItem;
case 2:
return forwardItem;
}
return 0;
}
QLayoutItem* DeckButtonsLayout::takeAt( int index )
{
QLayoutItem* item = itemAt( index );
switch ( index ) {
case 0: {
backwardItem = 0;
if ( backwardButton ) {
backwardButton->setParent( 0 );
}
backwardButton = 0;
break;
}
case 1: {
goItem = 0;
if ( RoundButton ) {
RoundButton->setParent( 0 );
}
RoundButton = 0;
break;
}
case 2: {
forwardItem = 0;
if ( forwardButton ) {
forwardButton->setParent( 0 );
}
forwardButton = 0;
break;
}
}
update();
return item;
}
void DeckButtonsLayout::setBackwardButton( QAbstractButton* button )
{
if ( backwardButton && button == backwardButton ) {
return;
}
if ( backwardItem ) {
delete takeAt( 0 );
}
if ( button ) {
addChildWidget( button );
}
backwardItem = new QWidgetItem( button );
backwardButton = button;
update();
}
void DeckButtonsLayout::setRoundButton( QAbstractButton* button )
{
if ( RoundButton && button == RoundButton ) {
return;
}
if ( goItem ) {
delete takeAt( 1 );
}
if ( button ) {
addChildWidget( button );
}
goItem = new QWidgetItem( button );
RoundButton = button;
update();
}
void DeckButtonsLayout::setForwardButton( QAbstractButton* button )
{
if ( forwardButton && button == forwardButton ) {
return;
}
if ( forwardItem ) {
delete takeAt( 2 );
}
if ( button ) {
addChildWidget( button );
}
forwardItem = new QWidgetItem( button );
forwardButton = button;
update();
}
/*****************************************************************************
* Copyright © 2011 VideoLAN
* $Id$
*
* Authors: Filipe Azevedo, aka PasNox
*
* 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 DECKBUTTONLAYOUT_H
#define DECKBUTTONLAYOUT_H
#include <QLayout>
#include <QPointer>
#include <QAbstractButton>
class QWidget;
class QAbstractButton;
class DeckButtonsLayout : public QLayout
{
Q_OBJECT
public:
DeckButtonsLayout( QWidget* parent = 0 );
virtual ~DeckButtonsLayout();
virtual QSize sizeHint() const;
virtual int count() const;
void setBackwardButton( QAbstractButton* button );
void setRoundButton( QAbstractButton* button );
void setForwardButton( QAbstractButton* button );
protected:
QWidgetItem* backwardItem;
QWidgetItem* goItem;
QWidgetItem* forwardItem;
QPointer<QAbstractButton> backwardButton;
QPointer<QAbstractButton> RoundButton;
QPointer<QAbstractButton> forwardButton;
virtual void setGeometry( const QRect& r );
virtual void addItem( QLayoutItem* item );
virtual QLayoutItem* itemAt( int index ) const;
virtual QLayoutItem* takeAt( int index );
};
#endif // DECKBUTTONLAYOUT_H
/*****************************************************************************
* Copyright © 2011 VideoLAN
* $Id$
*
* Authors: Filipe Azevedo, aka PasNox
*
* 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.
*****************************************************************************/
#include "RoundButton.hpp"
#include <QPainter>
#include <QStyleOptionToolButton>
RoundButton::RoundButton( QWidget* parent )
: QToolButton( parent )
{
setIconSize( QSize( 24, 24 ) );
setIcon( QIcon::fromTheme( "media-playback-start" ) );
}
QSize RoundButton::sizeHint() const
{
return QSize( 38, 38 );
}
QBrush RoundButton::pen( QStyleOptionToolButton* option ) const
{
const bool over = option->state & QStyle::State_MouseOver;
return QBrush( over ? QColor( 61, 165, 225 ) : QColor( 109, 106, 102 ) );
}
QBrush RoundButton::brush( QStyleOptionToolButton* option ) const
{
const bool over = option->state & QStyle::State_MouseOver;
const bool pressed = option->state & QStyle::State_Sunken;
QColor g1 = QColor( 219, 217, 215 );
QColor g2 = QColor( 205, 202, 199 );
QColor g3 = QColor( 187, 183, 180 );
if ( pressed ) {
g1 = g1.darker( 120 );
g2 = g2.darker( 120 );
g3 = g3.darker( 120 );
}
else if ( over ) {
g1 = g1.lighter( 110 );
g2 = g2.lighter( 110 );
g3 = g3.lighter( 110 );
}
QLinearGradient gradient( 0, 0, 0, height() );
gradient.setColorAt( 0.0, g1 );
gradient.setColorAt( 0.40, g2 );
gradient.setColorAt( 1.0, g3 );
return QBrush( gradient );
}
void RoundButton::paintEvent( QPaintEvent* event )
{
/*QToolButton::paintEvent( event );
return;*/
Q_UNUSED( event );
QPainter painter( this );
QStyleOptionToolButton option;
initStyleOption( &option );
painter.setRenderHint( QPainter::Antialiasing );
painter.setPen( QPen( pen( &option ), 1.5 ) );
painter.setBrush( brush( &option ) );
painter.drawEllipse( rect().adjusted( 1, 1, -1, -1 ) );
style()->drawControl( QStyle::CE_ToolButtonLabel, &option, &painter, this );
}
/*****************************************************************************
* Copyright © 2011 VideoLAN
* $Id$
*
* Authors: Filipe Azevedo, aka PasNox
*
* 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 ROUNDBUTTON_H
#define ROUNDBUTTON_H
#include <QToolButton>
class RoundButton : public QToolButton
{
Q_OBJECT
public:
RoundButton( QWidget* parent = 0 );
virtual QSize sizeHint() const;
protected:
QBrush pen( QStyleOptionToolButton* option ) const;
QBrush brush( QStyleOptionToolButton* option ) const;
virtual void paintEvent( QPaintEvent* event );
};
#endif // ROUNDBUTTON_H
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