File indexing completed on 2025-01-05 04:25:39
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 "AlbumItem.h" 0018 0019 #include "SvgHandler.h" 0020 #include "AlbumsDefs.h" 0021 #include "core/meta/Meta.h" 0022 #include "core/meta/support/MetaUtility.h" 0023 0024 #include <KLocalizedString> 0025 0026 #include <QCollator> 0027 #include <QIcon> 0028 #include <QPixmap> 0029 0030 AlbumItem::AlbumItem() 0031 : QStandardItem() 0032 , m_iconSize( 40 ) 0033 , m_showArtist( false ) 0034 { 0035 setEditable( false ); 0036 } 0037 0038 AlbumItem::~AlbumItem() 0039 { 0040 if( m_album ) 0041 unsubscribeFrom( m_album ); 0042 } 0043 0044 void 0045 AlbumItem::setAlbum( const Meta::AlbumPtr &albumPtr ) 0046 { 0047 if( m_album ) 0048 unsubscribeFrom( m_album ); 0049 m_album = albumPtr; 0050 subscribeTo( m_album ); 0051 update(); 0052 } 0053 0054 void 0055 AlbumItem::setIconSize( const int iconSize ) 0056 { 0057 static const int padding = 5; 0058 0059 m_iconSize = iconSize; 0060 0061 QSize size = sizeHint(); 0062 size.setHeight( iconSize + padding*2 ); 0063 setSizeHint( size ); 0064 } 0065 0066 void 0067 AlbumItem::setShowArtist( const bool showArtist ) 0068 { 0069 if( showArtist != m_showArtist ) 0070 { 0071 m_showArtist = showArtist; 0072 metadataChanged( m_album ); 0073 } 0074 } 0075 0076 void 0077 AlbumItem::metadataChanged(const Meta::AlbumPtr &album ) 0078 { 0079 Q_UNUSED( album ); 0080 QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection); 0081 } 0082 0083 void 0084 AlbumItem::update() 0085 { 0086 if( !m_album ) 0087 return; 0088 0089 Meta::TrackList tracks = m_album->tracks(); 0090 if( !tracks.isEmpty() ) 0091 { 0092 Meta::TrackPtr first = tracks.first(); 0093 Meta::YearPtr year = first->year(); 0094 if( year ) 0095 setData( year->year(), AlbumYearRole ); 0096 } 0097 0098 QString albumName = m_album->name(); 0099 albumName = albumName.isEmpty() ? i18n("Unknown") : albumName; 0100 QString name = ( m_showArtist && m_album->hasAlbumArtist() ) 0101 ? QString( "%1 - %2" ).arg( m_album->albumArtist()->name(), albumName ) 0102 : albumName; 0103 setData( name, NameRole ); 0104 0105 qint64 totalTime = 0; 0106 foreach( Meta::TrackPtr item, tracks ) 0107 totalTime += item->length(); 0108 0109 QString trackCount = i18np( "%1 track", "%1 tracks", tracks.size() ); 0110 QString lengthText = QString( "%1, %2" ).arg( trackCount, Meta::msToPrettyTime( totalTime ) ); 0111 setData( lengthText, AlbumLengthRole ); 0112 0113 QPixmap cover = The::svgHandler()->imageWithBorder( m_album, m_iconSize, 3 ); 0114 setIcon( QIcon( cover ) ); 0115 setData( cover, AlbumCoverRole ); 0116 } 0117 0118 int 0119 AlbumItem::type() const 0120 { 0121 return AlbumType; 0122 } 0123 0124 bool 0125 AlbumItem::operator<( const QStandardItem &other ) const 0126 { 0127 // compare the opposite for descending year order 0128 int yearA = data( AlbumYearRole ).toInt(); 0129 int yearB = other.data( AlbumYearRole ).toInt(); 0130 if( yearA > yearB ) 0131 return true; 0132 else if( yearA == yearB ) 0133 { 0134 const QString nameA = data( NameRole ).toString(); 0135 const QString nameB = other.data( NameRole ).toString(); 0136 QCollator collator; 0137 collator.setNumericMode( true ); 0138 collator.setCaseSensitivity( Qt::CaseInsensitive ); 0139 return collator.compare( nameA, nameB ) < 0; 0140 } 0141 else 0142 return false; 0143 } 0144