File indexing completed on 2024-05-05 04:49:27

0001 /****************************************************************************************
0002 * Copyright (c) 2009 Thomas Luebking <thomas.luebking@web.de>                          *
0003 *                                                                                      *
0004 * This program is free software; you can redistribute it and/or modify it under        *
0005 * the terms of the GNU General Public License as published by the Free Software        *
0006 * Foundation; either version 2 of the License, or (at your option) any later           *
0007 * version.                                                                             *
0008 *                                                                                      *
0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012 *                                                                                      *
0013 * You should have received a copy of the GNU General Public License along with         *
0014 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015 ****************************************************************************************/
0016 
0017 #include "PlayPauseButton.h"
0018 
0019 #include "SvgHandler.h"
0020 
0021 #include <KLocalizedString>
0022 
0023 #include <QMouseEvent>
0024 #include <QPainter>
0025 
0026 
0027 PlayPauseButton::PlayPauseButton( QWidget *parent ) : IconButton( parent )
0028     , m_isPlaying( false )
0029 {
0030     connect (this, &PlayPauseButton::clicked, this, &PlayPauseButton::toggle );
0031     setToolTip( i18n( "Play" ) );
0032 }
0033 
0034 void PlayPauseButton::enterEvent( QEvent * )
0035 {
0036     setIcon( m_isPlaying ? m_icon.pause[1] : m_icon.play[1], 3 );
0037 }
0038 
0039 void PlayPauseButton::leaveEvent( QEvent * )
0040 {
0041     setIcon( m_isPlaying ? m_icon.pause[0] : m_icon.play[0], 6 );
0042 }
0043 
0044 void PlayPauseButton::mousePressEvent( QMouseEvent *me )
0045 {
0046     setIcon( m_isPlaying ? m_icon.pause[0] : m_icon.play[0] );
0047     IconButton::mousePressEvent( me );
0048 }
0049 
0050 void PlayPauseButton::toggle()
0051 {
0052     Q_EMIT toggled( !m_isPlaying );
0053 }
0054 
0055 void PlayPauseButton::reloadContent( const QSize &sz )
0056 {
0057     const int width  = sz.width()*2; //double size svg render to have better looking high-dpi toolbar
0058     const int height = sz.height()*2;
0059     //NOTICE this is a bit cumbersome, as Qt renders faster to images than to pixmaps
0060     // However we need the Image and generate the pixmap ourself - maybe extend the SvgHandler API
0061     m_icon.play[0] = The::svgHandler()->renderSvg( "PLAYpause", width, height, "PLAYpause", true ).toImage();
0062     m_icon.play[1] = The::svgHandler()->renderSvg( "PLAYpause_active", width, height, "PLAYpause_active", true ).toImage();
0063     m_icon.pause[0] = The::svgHandler()->renderSvg( "playPAUSE", width, height, "playPAUSE", true ).toImage();
0064     m_icon.pause[1] = The::svgHandler()->renderSvg( "playPAUSE_active", width, height, "playPAUSE_active", true ).toImage();
0065     if( layoutDirection() == Qt::RightToLeft )
0066     {
0067         for ( int i = 0; i < 2; ++i )
0068         {
0069             m_icon.play[i] = m_icon.play[i].mirrored( true, false );
0070             m_icon.pause[i] = m_icon.pause[i].mirrored( true, false );
0071         }
0072     }
0073     setIcon( m_isPlaying ? m_icon.pause[underMouse()] : m_icon.play[underMouse()] );
0074 }
0075 
0076 void PlayPauseButton::setPlaying( bool playing )
0077 {
0078     if ( m_isPlaying == playing )
0079         return;
0080 
0081     setToolTip( playing ? i18n( "Pause" ) : i18n( "Play" ) );
0082 
0083     m_isPlaying = playing;
0084     setIcon( m_isPlaying ? m_icon.pause[underMouse()] : m_icon.play[underMouse()], 4 );
0085 }
0086