1 /****************************************************************************************
2  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
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 #include "ServiceCollectionTreeView.h"
18 
19 #include "browsers/CollectionTreeItem.h"
20 #include "core/capabilities/ActionsCapability.h"
21 #include "core/meta/Meta.h"
22 #include "core/support/Debug.h"
23 
24 #include <QMenu>
25 
26 #include <QAction>
27 #include <QContextMenuEvent>
28 #include <QSortFilterProxyModel>
29 
ServiceCollectionTreeView(QWidget * parent)30 ServiceCollectionTreeView::ServiceCollectionTreeView( QWidget *parent )
31     : CollectionTreeView( parent )
32     , m_playableTracks( true ) //per default, act just like a normal CollectionTreeView
33 {
34 }
35 
~ServiceCollectionTreeView()36 ServiceCollectionTreeView::~ServiceCollectionTreeView()
37 {}
38 
39 void
mouseDoubleClickEvent(QMouseEvent * event)40 ServiceCollectionTreeView::mouseDoubleClickEvent( QMouseEvent* event )
41 {
42     if ( m_playableTracks )
43         CollectionTreeView::mouseDoubleClickEvent( event );
44 }
45 
46 void
contextMenuEvent(QContextMenuEvent * event)47 ServiceCollectionTreeView::contextMenuEvent( QContextMenuEvent * event )
48 {
49     if ( m_playableTracks )
50         CollectionTreeView::contextMenuEvent( event );
51     else
52     {
53         QModelIndexList indices = selectedIndexes();
54         if( filterModel() )
55         {
56             QModelIndexList tmp;
57             foreach( const QModelIndex &idx, indices )
58             {
59                 tmp.append( filterModel()->mapToSource( idx ) );
60             }
61             indices = tmp;
62         }
63 
64         if( !indices.isEmpty() )
65         {
66             QMenu menu;
67             if( indices.count() == 1 )
68             {
69                 if( indices.first().isValid() && indices.first().internalPointer() )
70                 {
71                     Meta::DataPtr data = static_cast<CollectionTreeItem*>( indices.first().internalPointer() )->data();
72                     if( data )
73                     {
74                         QScopedPointer< Capabilities::ActionsCapability > ac( data->create<Capabilities::ActionsCapability>() );
75                         if( ac )
76                         {
77                             QList<QAction*> actions = ac->actions();
78                             if( !actions.isEmpty() )
79                                 menu.addSeparator();
80                             foreach( QAction *action, actions )
81                             {
82                                 if( !action->parent() )
83                                     action->setParent( &menu );
84                                 menu.addAction( action );
85                             }
86                         }
87                     }
88                 }
89             }
90 
91             if( menu.actions().count() > 0 )
92             {
93                 (void)menu.exec( event->globalPos() );
94                 QSet<CollectionTreeItem*> items;
95                 foreach( const QModelIndex &index, indices )
96                 {
97                     if( index.isValid() && index.internalPointer() )
98                         items.insert( static_cast<CollectionTreeItem*>( index.internalPointer() ) );
99                 }
100             }
101         }
102         else
103             debug() << "invalid index or null internalPointer";
104     }
105 }
106 
107 bool
playableTracks() const108 ServiceCollectionTreeView::playableTracks() const
109 {
110     return m_playableTracks;
111 }
112 
113 
114 void
setPlayableTracks(bool playable)115 ServiceCollectionTreeView::setPlayableTracks( bool playable )
116 {
117     m_playableTracks = playable;
118 }
119 
120