1 /****************************************************************************************
2  * Copyright (c) 2008-2010 Soren Harward <stharward@gmail.com>                          *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #define DEBUG_PREFIX "TrackSelectWidget"
18 
19 #include "TrackSelectWidget.h"
20 
21 #include "amarokconfig.h"
22 #include "browsers/CollectionTreeItem.h"
23 #include "browsers/CollectionTreeItemModel.h"
24 #include "browsers/CollectionTreeView.h"
25 #include "core/meta/Meta.h"
26 #include "core/support/Amarok.h"
27 #include "core/support/Debug.h"
28 #include "widgets/PrettyTreeDelegate.h"
29 
30 #include <KLocalizedString>
31 #include <KSqueezedTextLabel>
32 
33 #include <QLabel>
34 #include <QVBoxLayout>
35 
TrackSelectWidget(QWidget * parent)36 TrackSelectWidget::TrackSelectWidget( QWidget* parent )
37     : BoxWidget( true, parent )
38 {
39     DEBUG_BLOCK
40 
41     m_label = new KSqueezedTextLabel( this );
42     m_label->hide(); // TODO: decide whether the label should be shown or not
43     m_label->setTextElideMode( Qt::ElideRight );
44     setData( Meta::DataPtr() );
45 
46     m_view = new CollectionTreeView( this );
47     m_view->setRootIsDecorated( false );
48     m_view->setFrameShape( QFrame::NoFrame );
49 
50     m_view->setItemDelegate( new PrettyTreeDelegate( m_view ) );
51 
52     QList<int> levelNumbers = Amarok::config( "Collection Browser" ).readEntry( "TreeCategory", QList<int>() );
53     QList<CategoryId::CatMenuId> levels;
54     foreach( int levelNumber, levelNumbers )
55         levels << CategoryId::CatMenuId( levelNumber );
56     if ( levels.isEmpty() )
57         levels << CategoryId::Artist << CategoryId::Album;
58     m_model = new CollectionTreeItemModel( levels );
59     m_model->setParent( this );
60     m_view->setModel( m_model );
61 
62     connect( m_view, &CollectionTreeView::itemSelected,
63              this, &TrackSelectWidget::recvNewSelection );
64 }
65 
~TrackSelectWidget()66 TrackSelectWidget::~TrackSelectWidget() {}
67 
setData(const Meta::DataPtr & data)68 void TrackSelectWidget::setData( const Meta::DataPtr& data )
69 {
70     debug() << "setting label to" << dataToLabel( data );
71     m_label->setText( i18n("Checkpoint: <b>%1</b>", dataToLabel( data ) ) );
72 }
73 
74 void
recvNewSelection(CollectionTreeItem * item)75 TrackSelectWidget::recvNewSelection( CollectionTreeItem* item )
76 {
77     if ( item && item->isDataItem() ) {
78         Meta::DataPtr data = item->data();
79         if ( data != Meta::DataPtr() ) {
80             setData( data );
81             debug() << "new selection" << data->prettyName();
82             Q_EMIT selectionChanged( data );
83         }
84     }
85 }
86 
dataToLabel(const Meta::DataPtr & data) const87 const QString TrackSelectWidget::dataToLabel( const Meta::DataPtr& data ) const
88 {
89     if ( data != Meta::DataPtr() ) {
90         if ( Meta::TrackPtr track = Meta::TrackPtr::dynamicCast( data ) ) {
91             return i18n("Track: %1", track->prettyName() );
92         } else if ( Meta::AlbumPtr album = Meta::AlbumPtr::dynamicCast( data ) ) {
93             return i18n("Album: %1", album->prettyName() );
94         } else if ( Meta::ArtistPtr artist = Meta::ArtistPtr::dynamicCast( data ) ) {
95             return i18n("Artist: %1", artist->prettyName() );
96         }
97         // TODO: can things other than tracks, artists, and albums end up here?
98     }
99     return i18n("empty");
100 }
101