File indexing completed on 2025-01-05 04:25:40

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Seb Ruiz <ruiz@kde.org>                                           *
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 "TrackItem.h"
0018 
0019 #include "AlbumsDefs.h"
0020 #include "core/meta/Meta.h"
0021 #include "core/meta/support/MetaUtility.h"
0022 
0023 #include <QCollator>
0024 #include <QFont>
0025 #include <QMutexLocker>
0026 
0027 TrackItem::TrackItem()
0028     : QStandardItem()
0029 {
0030     setEditable( false );
0031 }
0032 
0033 TrackItem::~TrackItem()
0034 {
0035     QMutexLocker locker( &m_mutex );
0036     if( m_track )
0037         unsubscribeFrom( m_track );
0038 }
0039 
0040 void
0041 TrackItem::setTrack( const Meta::TrackPtr &trackPtr )
0042 {
0043     if( m_track )
0044         unsubscribeFrom( m_track );
0045     m_track = trackPtr;
0046     subscribeTo( m_track );
0047 
0048     metadataChanged( m_track );
0049 }
0050 
0051 void
0052 TrackItem::metadataChanged(const Meta::TrackPtr &track )
0053 {
0054     QMutexLocker locker( &m_mutex );
0055     if( !track )
0056         return;
0057 
0058     Meta::ArtistPtr artist = track->artist();
0059     Meta::AlbumPtr  album = track->album();
0060 
0061     setData( track->prettyName(), NameRole );
0062     setData( track->trackNumber(), TrackNumberRole );
0063     setData( track->length(), TrackLengthRole );
0064 
0065     if( artist )
0066         setData( artist->prettyName(), TrackArtistRole );
0067 
0068     if( album )
0069     {
0070         setData( album->isCompilation(), AlbumCompilationRole );
0071         int num = 0;
0072         foreach( const Meta::TrackPtr &track, album->tracks() )
0073         {
0074             if( num < track->trackNumber() )
0075                 num = track->trackNumber();
0076         }
0077         setData( num, AlbumMaxTrackNumberRole );
0078     }
0079     setToolTip( QString( "%1 (%2)" ).arg( track->name(), Meta::msToPrettyTime(track->length()) ) );
0080 }
0081 
0082 void
0083 TrackItem::italicise()
0084 {
0085     QFont f = font();
0086     f.setItalic( true );
0087     setFont( f );
0088 }
0089 
0090 void
0091 TrackItem::bold()
0092 {
0093     QFont f = font();
0094     f.setBold( true );
0095     setFont( f );
0096 }
0097 
0098 int
0099 TrackItem::type() const
0100 {
0101     return TrackType;
0102 }
0103 
0104 bool
0105 TrackItem::operator<( const QStandardItem &other ) const
0106 {
0107     int trackA = data( TrackNumberRole ).toInt();
0108     int trackB = other.data( TrackNumberRole ).toInt();
0109     if( trackA < trackB )
0110         return true;
0111     else if( trackA == trackB )
0112     {
0113         const QString nameA = data( NameRole ).toString();
0114         const QString nameB = other.data( NameRole ).toString();
0115         QCollator collator;
0116         collator.setNumericMode( true );
0117         collator.setCaseSensitivity( Qt::CaseInsensitive );
0118         return collator.compare( nameA, nameB ) < 0;
0119     }
0120     else
0121         return false;
0122 }