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

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2010 Soren Harward <stharward@gmail.com>                          *
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 #define DEBUG_PREFIX "TrackSelectWidget"
0018 
0019 #include "TrackSelectWidget.h"
0020 
0021 #include "amarokconfig.h"
0022 #include "browsers/CollectionTreeItem.h"
0023 #include "browsers/CollectionTreeItemModel.h"
0024 #include "browsers/CollectionTreeView.h"
0025 #include "core/meta/Meta.h"
0026 #include "core/support/Amarok.h"
0027 #include "core/support/Debug.h"
0028 #include "widgets/PrettyTreeDelegate.h"
0029 
0030 #include <KLocalizedString>
0031 #include <KSqueezedTextLabel>
0032 
0033 #include <QLabel>
0034 #include <QVBoxLayout>
0035 
0036 TrackSelectWidget::TrackSelectWidget( QWidget* parent )
0037     : BoxWidget( true, parent )
0038 {
0039     DEBUG_BLOCK
0040 
0041     m_label = new KSqueezedTextLabel( this );
0042     m_label->hide(); // TODO: decide whether the label should be shown or not
0043     m_label->setTextElideMode( Qt::ElideRight );
0044     setData( Meta::DataPtr() );
0045 
0046     m_view = new CollectionTreeView( this );
0047     m_view->setRootIsDecorated( false );
0048     m_view->setFrameShape( QFrame::NoFrame );
0049 
0050     m_view->setItemDelegate( new PrettyTreeDelegate( m_view ) );
0051 
0052     QList<int> levelNumbers = Amarok::config( "Collection Browser" ).readEntry( "TreeCategory", QList<int>() );
0053     QList<CategoryId::CatMenuId> levels;
0054     foreach( int levelNumber, levelNumbers )
0055         levels << CategoryId::CatMenuId( levelNumber );
0056     if ( levels.isEmpty() )
0057         levels << CategoryId::Artist << CategoryId::Album;
0058     m_model = new CollectionTreeItemModel( levels );
0059     m_model->setParent( this );
0060     m_view->setModel( m_model );
0061 
0062     connect( m_view, &CollectionTreeView::itemSelected,
0063              this, &TrackSelectWidget::recvNewSelection );
0064 }
0065 
0066 TrackSelectWidget::~TrackSelectWidget() {}
0067 
0068 void TrackSelectWidget::setData( const Meta::DataPtr& data )
0069 {
0070     debug() << "setting label to" << dataToLabel( data );
0071     m_label->setText( i18n("Checkpoint: <b>%1</b>", dataToLabel( data ) ) );
0072 }
0073 
0074 void
0075 TrackSelectWidget::recvNewSelection( CollectionTreeItem* item )
0076 {
0077     if ( item && item->isDataItem() ) {
0078         Meta::DataPtr data = item->data();
0079         if ( data != Meta::DataPtr() ) {
0080             setData( data );
0081             debug() << "new selection" << data->prettyName();
0082             Q_EMIT selectionChanged( data );
0083         }
0084     }
0085 }
0086 
0087 const QString TrackSelectWidget::dataToLabel( const Meta::DataPtr& data ) const
0088 {
0089     if ( data != Meta::DataPtr() ) {
0090         if ( Meta::TrackPtr track = Meta::TrackPtr::dynamicCast( data ) ) {
0091             return i18n("Track: %1", track->prettyName() );
0092         } else if ( Meta::AlbumPtr album = Meta::AlbumPtr::dynamicCast( data ) ) {
0093             return i18n("Album: %1", album->prettyName() );
0094         } else if ( Meta::ArtistPtr artist = Meta::ArtistPtr::dynamicCast( data ) ) {
0095             return i18n("Artist: %1", artist->prettyName() );
0096         }
0097         // TODO: can things other than tracks, artists, and albums end up here?
0098     }
0099     return i18n("empty");
0100 }