Qt: correctly display the right treeView columns
[vlc.git] / modules / gui / qt4 / components / playlist / playlist_model.cpp
blob8b3e089282067204a80179e44f8f7362e6a5e6cc
1 /*****************************************************************************
2 * playlist_model.cpp : Manage playlist model
3 ****************************************************************************
4 * Copyright (C) 2006-2011 the VideoLAN team
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Ilkka Ollakkka <ileoo (at) videolan dot org>
9 * Jakob Leben <jleben@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "qt4.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "dialogs_provider.hpp" /* THEDP */
33 #include "input_manager.hpp" /* THEMIM */
34 #include "dialogs/mediainfo.hpp" /* MediaInfo Dialog */
35 #include "dialogs/playlist.hpp" /* Playlist Dialog */
37 #include <vlc_intf_strings.h> /* I_DIR */
39 #include "pixmaps/types/type_unknown.xpm"
40 #include "sorting.h"
42 #include <assert.h>
43 #include <QIcon>
44 #include <QFont>
45 #include <QMenu>
46 #include <QUrl>
47 #include <QFileInfo>
48 #include <QDesktopServices>
49 #include <QInputDialog>
50 #include <QSignalMapper>
52 #define I_NEW_DIR \
53 I_DIR_OR_FOLDER( N_("Create Directory"), N_( "Create Folder" ) )
54 #define I_NEW_DIR_NAME \
55 I_DIR_OR_FOLDER( N_( "Enter name for new directory:" ), \
56 N_( "Enter name for new folder:" ) )
58 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
60 /*************************************************************************
61 * Playlist model implementation
62 *************************************************************************/
64 PLModel::PLModel( playlist_t *_p_playlist, /* THEPL */
65 intf_thread_t *_p_intf, /* main Qt p_intf */
66 playlist_item_t * p_root,
67 QObject *parent ) /* Basic Qt parent */
68 : VLCModel( _p_intf, parent )
70 p_playlist = _p_playlist;
71 i_cached_id = -1;
72 i_cached_input_id = -1;
73 i_popup_item = i_popup_parent = -1;
74 sortingMenu = NULL;
76 rootItem = NULL; /* PLItem rootItem, will be set in rebuild( ) */
77 latestSearch = QString();
79 /* Icons initialization */
80 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
81 ADD_ICON( UNKNOWN , type_unknown_xpm );
82 ADD_ICON( FILE, ":/type/file" );
83 ADD_ICON( DIRECTORY, ":/type/directory" );
84 ADD_ICON( DISC, ":/type/disc" );
85 ADD_ICON( CDDA, ":/type/cdda" );
86 ADD_ICON( CARD, ":/type/capture-card" );
87 ADD_ICON( NET, ":/type/net" );
88 ADD_ICON( PLAYLIST, ":/type/playlist" );
89 ADD_ICON( NODE, ":/type/node" );
90 #undef ADD_ICON
92 i_zoom = getSettings()->value( "Playlist/zoom", 0 ).toInt();
94 rebuild( p_root );
95 DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
96 this, processInputItemUpdate( input_item_t *) );
97 DCONNECT( THEMIM, inputChanged( input_thread_t * ),
98 this, processInputItemUpdate( input_thread_t* ) );
99 CONNECT( THEMIM, playlistItemAppended( int, int ),
100 this, processItemAppend( int, int ) );
101 CONNECT( THEMIM, playlistItemRemoved( int ),
102 this, processItemRemoval( int ) );
105 PLModel::~PLModel()
107 getSettings()->setValue( "Playlist/zoom", i_zoom );
108 delete rootItem;
109 delete sortingMenu;
112 Qt::DropActions PLModel::supportedDropActions() const
114 return Qt::CopyAction | Qt::MoveAction;
117 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
119 Qt::ItemFlags flags = QAbstractItemModel::flags( index );
121 const PLItem *item = index.isValid() ? getItem( index ) : rootItem;
123 if( canEdit() )
125 PL_LOCK;
126 playlist_item_t *plItem =
127 playlist_ItemGetById( p_playlist, item->i_id );
129 if ( plItem && ( plItem->i_children > -1 ) )
130 flags |= Qt::ItemIsDropEnabled;
132 PL_UNLOCK;
135 flags |= Qt::ItemIsDragEnabled;
137 return flags;
140 QStringList PLModel::mimeTypes() const
142 QStringList types;
143 types << "vlc/qt-input-items";
144 return types;
147 bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
149 if( !i1.isValid() || !i2.isValid() ) return false;
150 PLItem *item1 = static_cast<PLItem*>( i1.internalPointer() );
151 PLItem *item2 = static_cast<PLItem*>( i2.internalPointer() );
152 if( item1->parent() == item2->parent() ) return i1.row() < i2.row();
153 else return *item1 < *item2;
156 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
158 PlMimeData *plMimeData = new PlMimeData();
159 QModelIndexList list;
161 foreach( const QModelIndex &index, indexes ) {
162 if( index.isValid() && index.column() == 0 )
163 list.append(index);
166 qSort(list.begin(), list.end(), modelIndexLessThen);
168 PLItem *item = NULL;
169 foreach( const QModelIndex &index, list ) {
170 if( item )
172 PLItem *testee = getItem( index );
173 while( testee->parent() )
175 if( testee->parent() == item ||
176 testee->parent() == item->parent() ) break;
177 testee = testee->parent();
179 if( testee->parent() == item ) continue;
180 item = getItem( index );
182 else
183 item = getItem( index );
185 plMimeData->appendItem( item->inputItem() );
188 return plMimeData;
191 /* Drop operation */
192 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
193 int row, int, const QModelIndex &parent )
195 bool copy = action == Qt::CopyAction;
196 if( !copy && action != Qt::MoveAction )
197 return true;
199 const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
200 if( plMimeData )
202 if( copy )
203 dropAppendCopy( plMimeData, getItem( parent ), row );
204 else
205 dropMove( plMimeData, getItem( parent ), row );
207 return true;
210 void PLModel::dropAppendCopy( const PlMimeData *plMimeData, PLItem *target, int pos )
212 PL_LOCK;
214 playlist_item_t *p_parent =
215 playlist_ItemGetByInput( p_playlist, target->inputItem() );
216 if( !p_parent ) return;
218 if( pos == -1 ) pos = PLAYLIST_END;
220 QList<input_item_t*> inputItems = plMimeData->inputItems();
222 foreach( input_item_t* p_input, inputItems )
224 playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
225 if( !p_item ) continue;
226 pos = playlist_NodeAddCopy( p_playlist, p_item, p_parent, pos );
229 PL_UNLOCK;
232 void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row )
234 QList<input_item_t*> inputItems = plMimeData->inputItems();
235 QList<PLItem*> model_items;
236 playlist_item_t *pp_items[inputItems.count()];
238 PL_LOCK;
240 playlist_item_t *p_parent =
241 playlist_ItemGetByInput( p_playlist, target->inputItem() );
243 if( !p_parent || row > p_parent->i_children )
245 PL_UNLOCK; return;
248 int new_pos = row == -1 ? p_parent->i_children : row;
249 int model_pos = new_pos;
250 int i = 0;
252 foreach( input_item_t *p_input, inputItems )
254 playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
255 if( !p_item ) continue;
257 PLItem *item = findByInput( rootItem, p_input->i_id );
258 if( !item ) continue;
260 /* Better not try to move a node into itself.
261 Abort the whole operation in that case,
262 because it is ambiguous. */
263 PLItem *climber = target;
264 while( climber )
266 if( climber == item )
268 PL_UNLOCK; return;
270 climber = climber->parent();
273 if( item->parent() == target &&
274 target->children.indexOf( item ) < new_pos )
275 model_pos--;
277 model_items.append( item );
278 pp_items[i] = p_item;
279 i++;
282 if( model_items.isEmpty() )
284 PL_UNLOCK; return;
287 playlist_TreeMoveMany( p_playlist, i, pp_items, p_parent, new_pos );
289 PL_UNLOCK;
291 foreach( PLItem *item, model_items )
292 takeItem( item );
294 insertChildren( target, model_items, model_pos );
297 /* remove item with its id */
298 void PLModel::removeItem( int i_id )
300 PLItem *item = findById( rootItem, i_id );
301 removeItem( item );
304 void PLModel::activateItem( const QModelIndex &index )
306 assert( index.isValid() );
307 const PLItem *item = getItem( index );
308 assert( item );
309 PL_LOCK;
310 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
311 activateItem( p_item );
312 PL_UNLOCK;
315 /* Convenient overloaded private version of activateItem
316 * Must be entered with PL lock */
317 void PLModel::activateItem( playlist_item_t *p_item )
319 if( !p_item ) return;
320 playlist_item_t *p_parent = p_item;
321 while( p_parent )
323 if( p_parent->i_id == rootItem->id() ) break;
324 p_parent = p_parent->p_parent;
326 if( p_parent )
327 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
328 p_parent, p_item );
331 /****************** Base model mandatory implementations *****************/
332 QVariant PLModel::data( const QModelIndex &index, const int role ) const
334 if( !index.isValid() ) return QVariant();
335 const PLItem *item = getItem( index );
336 if( role == Qt::DisplayRole )
338 int metadata = columnToMeta( index.column() );
339 if( metadata == COLUMN_END ) return QVariant();
341 QString returninfo;
342 if( metadata == COLUMN_NUMBER )
343 returninfo = QString::number( index.row() + 1 );
344 else if( metadata == COLUMN_COVER )
346 QString artUrl;
347 artUrl = InputManager::decodeArtURL( item->inputItem() );
348 if( artUrl.isEmpty() )
350 for( int i = 0; i < item->childCount(); i++ )
352 artUrl = InputManager::decodeArtURL( item->child( i )->inputItem() );
353 if( !artUrl.isEmpty() )
354 break;
357 return QVariant( artUrl );
359 else
361 char *psz = psz_column_meta( item->inputItem(), metadata );
362 returninfo = qfu( psz );
363 free( psz );
365 return QVariant( returninfo );
367 else if( role == Qt::DecorationRole && index.column() == 0 )
369 /* Used to segfault here because i_type wasn't always initialized */
370 return QVariant( PLModel::icons[item->inputItem()->i_type] );
372 else if( role == Qt::FontRole )
374 QFont f;
375 f.setPointSize( __MAX( f.pointSize() - 1 + i_zoom, 4 ) );
376 if( isCurrent( index ) )
377 f.setBold( true );
378 return QVariant( f );
380 else if( role == Qt::BackgroundRole && isCurrent( index ) )
382 return QVariant( QBrush( Qt::gray ) );
384 else if( role == IsCurrentRole )
386 return QVariant( isCurrent( index ) );
388 else if( role == IsLeafNodeRole )
390 QVariant isLeaf;
391 PL_LOCK;
392 playlist_item_t *plItem =
393 playlist_ItemGetById( p_playlist, item->i_id );
395 if( plItem )
396 isLeaf = plItem->i_children == -1;
398 PL_UNLOCK;
399 return isLeaf;
401 else if( role == IsCurrentsParentNodeRole )
403 return QVariant( isParent( index, currentIndex() ) );
405 return QVariant();
408 /* Seek from current index toward the top and see if index is one of parent nodes */
409 bool PLModel::isParent( const QModelIndex &index, const QModelIndex &current ) const
411 if( !index.isValid() )
412 return false;
414 if( index == current )
415 return true;
417 if( !current.isValid() || !current.parent().isValid() )
418 return false;
420 return isParent( index, current.parent() );
423 bool PLModel::isCurrent( const QModelIndex &index ) const
425 return getItem( index )->inputItem() == THEMIM->currentInputItem();
428 int PLModel::itemId( const QModelIndex &index ) const
430 return getItem( index )->id();
433 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
434 int role ) const
436 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
437 return QVariant();
439 int meta_col = columnToMeta( section );
441 if( meta_col == COLUMN_END ) return QVariant();
443 return QVariant( qfu( psz_column_title( meta_col ) ) );
446 QModelIndex PLModel::index( const int row, const int column, const QModelIndex &parent )
447 const
449 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
451 PLItem *childItem = parentItem->child( row );
452 if( childItem )
453 return createIndex( row, column, childItem );
454 else
455 return QModelIndex();
458 QModelIndex PLModel::index( const int i_id, const int c )
460 return index( findById( rootItem, i_id ), c );
463 /* Return the index of a given item */
464 QModelIndex PLModel::index( PLItem *item, int column ) const
466 if( !item ) return QModelIndex();
467 const PLItem *parent = item->parent();
468 if( parent )
469 return createIndex( parent->children.lastIndexOf( item ),
470 column, item );
471 return QModelIndex();
474 QModelIndex PLModel::currentIndex() const
476 input_thread_t *p_input_thread = THEMIM->getInput();
477 if( !p_input_thread ) return QModelIndex();
478 PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
479 return index( item, 0 );
482 QModelIndex PLModel::parent( const QModelIndex &index ) const
484 if( !index.isValid() ) return QModelIndex();
486 PLItem *childItem = getItem( index );
487 if( !childItem )
489 msg_Err( p_playlist, "NULL CHILD" );
490 return QModelIndex();
493 PLItem *parentItem = childItem->parent();
494 if( !parentItem || parentItem == rootItem ) return QModelIndex();
495 if( !parentItem->parent() )
497 msg_Err( p_playlist, "No parent parent, trying row 0 " );
498 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
499 return createIndex( 0, 0, parentItem );
501 return createIndex(parentItem->row(), 0, parentItem);
504 int PLModel::columnCount( const QModelIndex &) const
506 return columnFromMeta( COLUMN_END );
509 int PLModel::rowCount( const QModelIndex &parent ) const
511 const PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
512 return parentItem->childCount();
515 QStringList PLModel::selectedURIs()
517 QStringList lst;
518 for( int i = 0; i < current_selection.count(); i++ )
520 const PLItem *item = getItem( current_selection[i] );
521 if( item )
523 PL_LOCK;
524 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
525 if( p_item )
527 char *psz = input_item_GetURI( p_item->p_input );
528 if( psz )
530 lst.append( qfu(psz) );
531 free( psz );
534 PL_UNLOCK;
537 return lst;
540 /************************* Lookups *****************************/
541 PLItem *PLModel::findById( PLItem *root, int i_id ) const
543 return findInner( root, i_id, false );
546 PLItem *PLModel::findByInput( PLItem *root, int i_id ) const
548 PLItem *result = findInner( root, i_id, true );
549 return result;
552 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input ) const
554 if( !root ) return NULL;
556 if( !b_input && root->id() == i_id )
557 return root;
559 else if( b_input && root->inputItem()->i_id == i_id )
560 return root;
562 QList<PLItem *>::iterator it = root->children.begin();
563 while ( it != root->children.end() )
565 if( !b_input && (*it)->id() == i_id )
566 return (*it);
568 else if( b_input && (*it)->inputItem()->i_id == i_id )
569 return (*it);
571 if( (*it)->childCount() )
573 PLItem *childFound = findInner( (*it), i_id, b_input );
574 if( childFound )
575 return childFound;
577 ++it;
579 return NULL;
582 bool PLModel::canEdit() const
584 return (
585 rootItem != NULL &&
587 rootItem->inputItem() == p_playlist->p_playing->p_input ||
588 ( p_playlist->p_media_library &&
589 rootItem->inputItem() == p_playlist->p_media_library->p_input )
594 /************************* Updates handling *****************************/
596 /**** Events processing ****/
597 void PLModel::processInputItemUpdate( input_thread_t *p_input )
599 if( !p_input ) return;
600 if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
602 PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
603 if( item ) emit currentIndexChanged( index( item, 0 ) );
605 processInputItemUpdate( input_GetItem( p_input ) );
608 void PLModel::processInputItemUpdate( input_item_t *p_item )
610 if( !p_item || p_item->i_id <= 0 ) return;
611 PLItem *item = findByInput( rootItem, p_item->i_id );
612 if( item )
613 updateTreeItem( item );
616 void PLModel::processItemRemoval( int i_id )
618 if( i_id <= 0 ) return;
619 removeItem( i_id );
622 void PLModel::processItemAppend( int i_item, int i_parent )
624 playlist_item_t *p_item = NULL;
625 PLItem *newItem = NULL;
626 int pos;
628 /* Find the Parent */
629 PLItem *nodeParentItem = findById( rootItem, i_parent );
630 if( !nodeParentItem ) return;
632 /* Search for an already matching children */
633 foreach( const PLItem *existing, nodeParentItem->children )
634 if( existing->i_id == i_item ) return;
636 /* Find the child */
637 PL_LOCK;
638 p_item = playlist_ItemGetById( p_playlist, i_item );
639 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
641 PL_UNLOCK; return;
644 for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
645 if( p_item->p_parent->pp_children[pos] == p_item ) break;
647 newItem = new PLItem( p_item, nodeParentItem );
648 PL_UNLOCK;
650 /* We insert the newItem (children) inside the parent */
651 beginInsertRows( index( nodeParentItem, 0 ), pos, pos );
652 nodeParentItem->insertChild( newItem, pos );
653 endInsertRows();
655 if( newItem->inputItem() == THEMIM->currentInputItem() )
656 emit currentIndexChanged( index( newItem, 0 ) );
658 search( latestSearch, index( rootItem, 0), false /*FIXME*/ );
661 void PLModel::rebuild( playlist_item_t *p_root )
663 /* Invalidate cache */
664 i_cached_id = i_cached_input_id = -1;
666 if( rootItem ) rootItem->removeChildren();
668 PL_LOCK;
669 if( p_root ) // Can be NULL
671 delete rootItem;
672 rootItem = new PLItem( p_root );
674 assert( rootItem );
675 /* Recreate from root */
676 updateChildren( rootItem );
677 PL_UNLOCK;
679 /* And signal the view */
680 reset();
682 if( p_root ) emit rootIndexChanged();
685 void PLModel::takeItem( PLItem *item )
687 assert( item );
688 PLItem *parent = item->parent();
689 assert( parent );
690 int i_index = parent->children.indexOf( item );
692 beginRemoveRows( index( parent, 0 ), i_index, i_index );
693 parent->takeChildAt( i_index );
694 endRemoveRows();
697 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
699 assert( node );
700 int count = items.count();
701 if( !count ) return;
702 printf( "Here I am\n");
703 beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
704 for( int i = 0; i < count; i++ )
706 node->children.insert( i_pos + i, items[i] );
707 items[i]->parentItem = node;
709 endInsertRows();
712 void PLModel::removeItem( PLItem *item )
714 if( !item ) return;
716 i_cached_id = -1;
717 i_cached_input_id = -1;
719 if( item->parent() ) {
720 int i = item->parent()->children.indexOf( item );
721 beginRemoveRows( index( item->parent(), 0), i, i );
722 item->parent()->children.removeAt(i);
723 delete item;
724 endRemoveRows();
726 else delete item;
728 if(item == rootItem)
730 rootItem = NULL;
731 rebuild( p_playlist->p_playing );
735 /* This function must be entered WITH the playlist lock */
736 void PLModel::updateChildren( PLItem *root )
738 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->id() );
739 updateChildren( p_node, root );
742 /* This function must be entered WITH the playlist lock */
743 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
745 for( int i = 0; i < p_node->i_children ; i++ )
747 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
748 PLItem *newItem = new PLItem( p_node->pp_children[i], root );
749 root->appendChild( newItem );
750 if( p_node->pp_children[i]->i_children != -1 )
751 updateChildren( p_node->pp_children[i], newItem );
755 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
756 void PLModel::updateTreeItem( PLItem *item )
758 if( !item ) return;
759 emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
762 /************************* Actions ******************************/
765 * Deletion, don't delete items childrens if item is going to be
766 * delete allready, so we remove childrens from selection-list.
768 void PLModel::doDelete( QModelIndexList selected )
770 if( !canEdit() ) return;
772 while( !selected.isEmpty() )
774 QModelIndex index = selected[0];
775 selected.removeAt( 0 );
777 if( index.column() != 0 ) continue;
779 PLItem *item = getItem( index );
780 if( item->childCount() )
781 recurseDelete( item->children, &selected );
783 PL_LOCK;
784 playlist_DeleteFromInput( p_playlist, item->inputItem(), pl_Locked );
785 PL_UNLOCK;
787 removeItem( item );
791 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
793 for( int i = children.count() - 1; i >= 0 ; i-- )
795 PLItem *item = children[i];
796 if( item->childCount() )
797 recurseDelete( item->children, fullList );
798 fullList->removeAll( index( item, 0 ) );
802 /******* Volume III: Sorting and searching ********/
803 void PLModel::sort( const int column, Qt::SortOrder order )
805 sort( rootItem->id(), column, order );
808 void PLModel::sort( const int i_root_id, const int column, Qt::SortOrder order )
810 msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
812 int meta = columnToMeta( column );
813 if( meta == COLUMN_END ) return;
815 PLItem *item = findById( rootItem, i_root_id );
816 if( !item ) return;
817 QModelIndex qIndex = index( item, 0 );
818 int count = item->childCount();
819 if( count )
821 beginRemoveRows( qIndex, 0, count - 1 );
822 item->removeChildren();
823 endRemoveRows( );
826 PL_LOCK;
828 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
829 i_root_id );
830 if( p_root )
832 playlist_RecursiveNodeSort( p_playlist, p_root,
833 i_column_sorting( meta ),
834 order == Qt::AscendingOrder ?
835 ORDER_NORMAL : ORDER_REVERSE );
839 i_cached_id = i_cached_input_id = -1;
841 if( count )
843 beginInsertRows( qIndex, 0, count - 1 );
844 updateChildren( item );
845 endInsertRows( );
847 PL_UNLOCK;
848 /* if we have popup item, try to make sure that you keep that item visible */
849 if( i_popup_item > -1 )
851 PLItem *popupitem = findById( rootItem, i_popup_item );
852 if( popupitem ) emit currentIndexChanged( index( popupitem, 0 ) );
853 /* reset i_popup_item as we don't show it as selected anymore anyway */
854 i_popup_item = -1;
856 else if( currentIndex().isValid() ) emit currentIndexChanged( currentIndex() );
859 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
861 latestSearch = search_text;
863 /** \todo Fire the search with a small delay ? */
864 PL_LOCK;
866 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
867 itemId( idx ) );
868 assert( p_root );
869 playlist_LiveSearchUpdate( p_playlist, p_root, qtu( search_text ),
870 b_recursive );
871 if( idx.isValid() )
873 PLItem *searchRoot = getItem( idx );
875 beginRemoveRows( idx, 0, searchRoot->childCount() - 1 );
876 searchRoot->removeChildren();
877 endRemoveRows();
879 beginInsertRows( idx, 0, searchRoot->childCount() - 1 );
880 updateChildren( searchRoot ); // The PL_LOCK is needed here
881 endInsertRows();
883 PL_UNLOCK;
884 return;
887 PL_UNLOCK;
888 rebuild();
891 /*********** Popup *********/
892 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
894 int i_id = index.isValid() ? itemId( index ) : rootItem->id();
896 PL_LOCK;
897 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
898 if( !p_item )
900 PL_UNLOCK;
901 return false;
904 i_popup_item = index.isValid() ? p_item->i_id : -1;
905 i_popup_parent = index.isValid() ?
906 ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
907 ( rootItem->id() );
909 bool tree = ( rootItem && rootItem->id() != p_playlist->p_playing->i_id ) ||
910 var_InheritBool( p_intf, "playlist-tree" );
912 input_item_t *p_input = p_item->p_input;
913 vlc_gc_incref( p_input );
914 PL_UNLOCK;
916 /* */
917 QMenu menu;
919 /* Play/Stream/Info static actions */
920 if( i_popup_item > -1 )
922 menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
923 menu.addAction( QIcon( ":/menu/stream" ),
924 qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
925 menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
926 menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
927 if( p_input->psz_uri && !strncasecmp( p_input->psz_uri, "file://", 7 ) )
929 menu.addAction( QIcon( ":/type/folder-grey" ),
930 qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
932 menu.addSeparator();
934 vlc_gc_decref( p_input );
936 /* In PL or ML, allow to add a file/folder */
937 if( canEdit() )
939 QIcon addIcon( ":/buttons/playlist/playlist_add" );
940 menu.addSeparator();
941 if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
942 if( rootItem->id() == THEPL->p_playing->i_id )
944 menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
945 menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
946 menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
948 else if( THEPL->p_media_library &&
949 rootItem->id() == THEPL->p_media_library->i_id )
951 menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
952 menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
953 menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
957 /* Item removal */
958 if( i_popup_item > -1 )
960 if( rootItem->id() != THEPL->p_playing->i_id )
961 menu.addAction( qtr( "Add to playlist"), this, SLOT( popupAddToPlaylist() ) );
962 menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
963 qtr(I_POP_DEL), this, SLOT( popupDel() ) );
966 menu.addSeparator();
967 /* Playlist sorting */
968 if( !sortingMenu )
970 sortingMenu = new QMenu( qtr( "Sort by" ) );
971 sortingMapper = new QSignalMapper( this );
972 /* Choose what columns to show in sorting menu, not sure if this should be configurable*/
973 QList<int> sortingColumns;
974 sortingColumns << COLUMN_TITLE << COLUMN_ARTIST << COLUMN_ALBUM << COLUMN_TRACK_NUMBER << COLUMN_URI;
975 foreach( int Column, sortingColumns )
977 QAction *asc = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Ascending") );
978 QAction *desc = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Descending") );
979 sortingMapper->setMapping( asc, columnFromMeta(Column) + 1 );
980 sortingMapper->setMapping( desc, -1 * (columnFromMeta(Column)+1) );
981 CONNECT( asc, triggered(), sortingMapper, map() );
982 CONNECT( desc, triggered(), sortingMapper, map() );
984 CONNECT( sortingMapper, mapped( int ), this, popupSort( int ) );
986 menu.addMenu( sortingMenu );
988 /* Zoom */
989 QMenu *zoomMenu = new QMenu( qtr( "Display size" ) );
990 zoomMenu->addAction( qtr( "Increase" ), this, SLOT( increaseZoom() ) );
991 zoomMenu->addAction( qtr( "Decrease" ), this, SLOT( decreaseZoom() ) );
992 menu.addMenu( zoomMenu );
994 /* Store the current selected item for popup*() methods */
995 current_selection = list;
997 /* Display and forward the result */
998 if( !menu.isEmpty() )
1000 menu.exec( point ); return true;
1002 else return false;
1005 void PLModel::popupDel()
1007 doDelete( current_selection );
1010 void PLModel::popupPlay()
1012 PL_LOCK;
1014 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1015 i_popup_item );
1016 activateItem( p_item );
1018 PL_UNLOCK;
1021 void PLModel::popupAddToPlaylist()
1023 playlist_Lock( THEPL );
1025 foreach( QModelIndex currentIndex, current_selection )
1027 playlist_item_t *p_item = playlist_ItemGetById( THEPL, itemId( currentIndex ) );
1028 if( !p_item ) continue;
1030 playlist_NodeAddCopy( THEPL, p_item,
1031 THEPL->p_playing,
1032 PLAYLIST_END );
1034 playlist_Unlock( THEPL );
1037 void PLModel::popupInfo()
1039 PL_LOCK;
1040 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1041 i_popup_item );
1042 if( p_item )
1044 input_item_t* p_input = p_item->p_input;
1045 vlc_gc_incref( p_input );
1046 PL_UNLOCK;
1047 MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
1048 vlc_gc_decref( p_input );
1049 mid->setParent( PlaylistDialog::getInstance( p_intf ),
1050 Qt::Dialog );
1051 mid->show();
1052 } else
1053 PL_UNLOCK;
1056 void PLModel::popupStream()
1058 QStringList mrls = selectedURIs();
1059 if( !mrls.isEmpty() )
1060 THEDP->streamingDialog( NULL, mrls[0], false );
1063 void PLModel::popupSave()
1065 QStringList mrls = selectedURIs();
1066 if( !mrls.isEmpty() )
1067 THEDP->streamingDialog( NULL, mrls[0] );
1070 void PLModel::popupExplore()
1072 char *uri = NULL, *path = NULL;
1074 PL_LOCK;
1075 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1076 if( p_item )
1078 input_item_t *p_input = p_item->p_input;
1079 uri = input_item_GetURI( p_input );
1081 PL_UNLOCK;
1083 if( uri != NULL )
1085 path = make_path( uri );
1086 free( uri );
1088 if( path == NULL )
1089 return;
1091 QFileInfo info( qfu( path ) );
1092 free( path );
1094 QDesktopServices::openUrl( QUrl::fromLocalFile( info.absolutePath() ) );
1097 void PLModel::popupAddNode()
1099 bool ok;
1100 QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1101 qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
1102 QLineEdit::Normal, QString(), &ok);
1103 if( !ok || name.isEmpty() ) return;
1105 PL_LOCK;
1106 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1107 i_popup_parent );
1108 if( p_item )
1109 playlist_NodeCreate( p_playlist, qtu( name ), p_item, PLAYLIST_END, 0, NULL );
1110 PL_UNLOCK;
1113 void PLModel::popupSort( int column )
1115 sort( i_popup_parent,
1116 column > 0 ? column - 1 : -column - 1,
1117 column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
1120 /* */
1121 void PLModel::increaseZoom()
1123 i_zoom++;
1124 emit layoutChanged();
1127 void PLModel::decreaseZoom()
1129 i_zoom--;
1130 emit layoutChanged();
1133 /******************* Drag and Drop helper class ******************/
1134 PlMimeData::~PlMimeData()
1136 foreach( input_item_t *p_item, _inputItems )
1137 vlc_gc_decref( p_item );
1140 void PlMimeData::appendItem( input_item_t *p_item )
1142 vlc_gc_incref( p_item );
1143 _inputItems.append( p_item );
1146 QList<input_item_t*> PlMimeData::inputItems() const
1148 return _inputItems;
1151 QStringList PlMimeData::formats () const
1153 QStringList fmts;
1154 fmts << "vlc/qt-input-items";
1155 return fmts;