File indexing completed on 2024-05-19 04:50:24

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Bart Cerneels <bart.cerneels@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 "OpmlDirectoryView.h"
0018 
0019 #include "OpmlDirectoryModel.h"
0020 
0021 #include "core/support/Debug.h"
0022 
0023 #include <QMenu>
0024 
0025 #include <QContextMenuEvent>
0026 
0027 OpmlDirectoryView::OpmlDirectoryView( QWidget *parent ) :
0028     Amarok::PrettyTreeView(parent)
0029 {
0030 }
0031 
0032 void
0033 OpmlDirectoryView::contextMenuEvent( QContextMenuEvent *event )
0034 {
0035     DEBUG_BLOCK
0036 
0037     QModelIndex idx = indexAt( event->pos() );
0038 
0039     debug() << idx;
0040 
0041     event->accept();
0042 
0043     QVariant data = model()->data( idx, OpmlDirectoryModel::ActionRole );
0044     QActionList actions = data.value<QActionList>();
0045 
0046     if( actions.isEmpty() )
0047     {
0048         warning() << "no actions for index:" << idx;
0049         return;
0050     }
0051 
0052     QMenu menu;
0053     foreach( QAction *action, actions )
0054     {
0055         if( action )
0056             menu.addAction( action );
0057     }
0058 
0059     menu.exec( mapToGlobal( event->pos() ) );
0060 
0061     //We keep the items that the actions need to be applied to in the actions private data.
0062     //Clear the data from all actions now that the context menu has executed.
0063     foreach( QAction *action, actions )
0064         action->setData( QVariant() );
0065 }
0066 
0067 void
0068 OpmlDirectoryView::keyPressEvent( QKeyEvent *event )
0069 {
0070     switch( event->key() )
0071     {
0072         case Qt::Key_Delete:
0073         {
0074             foreach( const QItemSelectionRange &range, selectionModel()->selection() )
0075                 model()->removeRows( range.top(), range.height(), range.parent() );
0076             event->accept();
0077             return;
0078         }
0079      }
0080      Amarok::PrettyTreeView::keyPressEvent( event );
0081 }
0082 
0083 QItemSelectionModel::SelectionFlags
0084 OpmlDirectoryView::selectionCommand ( const QModelIndex &index, const QEvent *event ) const
0085 {
0086     if( model()->hasChildren( index ) )
0087         return QItemSelectionModel::ClearAndSelect;
0088 
0089     return Amarok::PrettyTreeView::selectionCommand( index, event );
0090 }