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

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@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 "ServiceCollectionTreeView.h"
0018 
0019 #include "browsers/CollectionTreeItem.h"
0020 #include "core/capabilities/ActionsCapability.h"
0021 #include "core/meta/Meta.h"
0022 #include "core/support/Debug.h"
0023 
0024 #include <QMenu>
0025 
0026 #include <QAction>
0027 #include <QContextMenuEvent>
0028 #include <QSortFilterProxyModel>
0029 
0030 ServiceCollectionTreeView::ServiceCollectionTreeView( QWidget *parent )
0031     : CollectionTreeView( parent )
0032     , m_playableTracks( true ) //per default, act just like a normal CollectionTreeView
0033 {
0034 }
0035 
0036 ServiceCollectionTreeView::~ServiceCollectionTreeView()
0037 {}
0038 
0039 void
0040 ServiceCollectionTreeView::mouseDoubleClickEvent( QMouseEvent* event )
0041 {
0042     if ( m_playableTracks )
0043         CollectionTreeView::mouseDoubleClickEvent( event );
0044 }
0045 
0046 void
0047 ServiceCollectionTreeView::contextMenuEvent( QContextMenuEvent * event )
0048 {
0049     if ( m_playableTracks )
0050         CollectionTreeView::contextMenuEvent( event );
0051     else
0052     {
0053         QModelIndexList indices = selectedIndexes();
0054         if( filterModel() )
0055         {
0056             QModelIndexList tmp;
0057             foreach( const QModelIndex &idx, indices )
0058             {
0059                 tmp.append( filterModel()->mapToSource( idx ) );
0060             }
0061             indices = tmp;
0062         }
0063 
0064         if( !indices.isEmpty() )
0065         {
0066             QMenu menu;
0067             if( indices.count() == 1 )
0068             {
0069                 if( indices.first().isValid() && indices.first().internalPointer() )
0070                 {
0071                     Meta::DataPtr data = static_cast<CollectionTreeItem*>( indices.first().internalPointer() )->data();
0072                     if( data )
0073                     {
0074                         QScopedPointer< Capabilities::ActionsCapability > ac( data->create<Capabilities::ActionsCapability>() );
0075                         if( ac )
0076                         {
0077                             QList<QAction*> actions = ac->actions();
0078                             if( !actions.isEmpty() )
0079                                 menu.addSeparator();
0080                             foreach( QAction *action, actions )
0081                             {
0082                                 if( !action->parent() )
0083                                     action->setParent( &menu );
0084                                 menu.addAction( action );
0085                             }
0086                         }
0087                     }
0088                 }
0089             }
0090 
0091             if( menu.actions().count() > 0 )
0092             {
0093                 (void)menu.exec( event->globalPos() );
0094                 QSet<CollectionTreeItem*> items;
0095                 foreach( const QModelIndex &index, indices )
0096                 {
0097                     if( index.isValid() && index.internalPointer() )
0098                         items.insert( static_cast<CollectionTreeItem*>( index.internalPointer() ) );
0099                 }
0100             }
0101         }
0102         else
0103             debug() << "invalid index or null internalPointer";
0104     }
0105 }
0106 
0107 bool
0108 ServiceCollectionTreeView::playableTracks() const
0109 {
0110     return m_playableTracks;
0111 }
0112 
0113 
0114 void
0115 ServiceCollectionTreeView::setPlayableTracks( bool playable )
0116 {
0117     m_playableTracks = playable;
0118 }
0119