File indexing completed on 2024-05-05 04:48:43

0001 /****************************************************************************************
0002  * Copyright (c) 2011 Kevin Funk <krf@electrostorm.net>                                 *
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 "PlaylistInfoWidget.h"
0018 
0019 #include "core/meta/Meta.h"
0020 #include "core/meta/support/MetaUtility.h"
0021 #include "core/support/Debug.h"
0022 #include "playlist/PlaylistActions.h"
0023 #include "playlist/PlaylistModelStack.h"
0024 
0025 #include "QEvent"
0026 #include "QHelpEvent"
0027 #include "QToolTip"
0028 
0029 PlaylistInfoWidget::PlaylistInfoWidget( QWidget *parent )
0030     : QLabel( parent )
0031 {
0032     connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::dataChanged,
0033              this, &PlaylistInfoWidget::updateTotalPlaylistLength );
0034     // Ignore The::playlist() layoutChanged: rows moving around does not change the total playlist length.
0035     connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::modelReset,
0036              this, &PlaylistInfoWidget::updateTotalPlaylistLength );
0037     connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::rowsInserted,
0038              this, &PlaylistInfoWidget::updateTotalPlaylistLength );
0039     connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::rowsRemoved,
0040              this, &PlaylistInfoWidget::updateTotalPlaylistLength );
0041 
0042     updateTotalPlaylistLength();
0043 }
0044 
0045 PlaylistInfoWidget::~PlaylistInfoWidget()
0046 {}
0047 
0048 void
0049 PlaylistInfoWidget::updateTotalPlaylistLength() //SLOT
0050 {
0051     const quint64 totalLength = The::playlist()->totalLength();
0052     const int trackCount = The::playlist()->qaim()->rowCount();
0053 
0054     if( totalLength > 0 && trackCount > 0 )
0055     {
0056         const QString prettyTotalLength = Meta::msToPrettyTime( totalLength );
0057         setText( i18ncp( "%1 is number of tracks, %2 is time",
0058                          "%1 track (%2)", "%1 tracks (%2)",
0059                          trackCount, prettyTotalLength ) );
0060     }
0061     else if( ( totalLength == 0 ) && ( trackCount > 0 ) )
0062     {
0063         setText( i18ncp( "%1 is number of tracks", "%1 track", "%1 tracks", trackCount ) );
0064     }
0065     else // Total Length will not be > 0 if trackCount is 0, so we can ignore it
0066     {
0067         setText( i18n( "No tracks" ) );
0068     }
0069 }
0070 
0071 bool
0072 PlaylistInfoWidget::event( QEvent *event )
0073 {
0074     if( event->type() == QEvent::ToolTip ) {
0075         QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
0076         const quint64 totalLength = The::playlist()->totalLength();
0077         const int trackCount = The::playlist()->qaim()->rowCount();
0078 
0079         if( totalLength == 0 || trackCount == 0 )
0080         {
0081             QToolTip::hideText();
0082             event->ignore();
0083         }
0084         else
0085         {
0086             // - determine the queued tracks
0087             quint64 queuedTotalLength( 0 );
0088             quint64 queuedTotalSize( 0 );
0089             int queuedCount( 0 );
0090 
0091             QQueue<quint64> queue = The::playlistActions()->queue();
0092             foreach( quint64 id, queue )
0093             {
0094                 Meta::TrackPtr track = The::playlist()->trackForId( id );
0095                 queuedTotalLength += track->length();
0096                 queuedTotalSize += track->filesize();
0097                 ++queuedCount;
0098             }
0099 
0100             // - set the tooltip
0101             const quint64 totalSize = The::playlist()->totalSize();
0102             const QString prettyTotalSize = Meta::prettyFilesize( totalSize );
0103             const QString prettyQueuedTotalLength = Meta::msToPrettyTime( queuedTotalLength );
0104             const QString prettyQueuedTotalSize   = Meta::prettyFilesize( queuedTotalSize );
0105 
0106             QString tooltipLabel;
0107             if( queuedCount > 0 && queuedTotalLength > 0 )
0108             {
0109                 tooltipLabel = i18n( "Total playlist size: %1", prettyTotalSize )       + '\n'
0110                     + i18n( "Queue size: %1",          prettyQueuedTotalSize ) + '\n'
0111                     + i18n( "Queue length: %1",        prettyQueuedTotalLength );
0112             }
0113             else
0114             {
0115                 tooltipLabel = i18n( "Total playlist size: %1", prettyTotalSize );
0116             }
0117 
0118             QToolTip::showText( helpEvent->globalPos(), tooltipLabel );
0119         }
0120 
0121         return true;
0122     }
0123     return QWidget::event(event);
0124 }
0125