standardpanel.cpp 14.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * standardpanel.cpp : The "standard" playlist panel : just a treeview
 ****************************************************************************
4
 * Copyright © 2000-2010 VideoLAN
5
 * $Id$
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Jean-Baptiste Kempf <jb@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
24

25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
28

29
#include "components/playlist/standardpanel.hpp"
Clément Stenac's avatar
Clément Stenac committed
30

31
#include "components/playlist/vlc_model.hpp"      /* VLCModel */
32
#include "components/playlist/playlist_model.hpp" /* PLModel */
33
#include "components/playlist/ml_model.hpp"       /* MLModel */
34 35 36 37 38
#include "components/playlist/views.hpp"          /* 3 views */
#include "components/playlist/selector.hpp"       /* PLSelector */
#include "menus.hpp"                              /* Popup */
#include "input_manager.hpp"                      /* THEMIM */

39 40 41
#include "sorting.h"                              /* Columns order */

#include <vlc_services_discovery.h>               /* SD_CMD_SEARCH */
42

43
#include <QHeaderView>
Clément Stenac's avatar
Clément Stenac committed
44
#include <QModelIndexList>
45
#include <QMenu>
46
#include <QKeyEvent>
47
#include <QWheelEvent>
48
#include <QStackedLayout>
49
#include <QSignalMapper>
50
#include <QSettings>
51

Clément Stenac's avatar
Clément Stenac committed
52
#include <assert.h>
Clément Stenac's avatar
Clément Stenac committed
53

54
StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
55
                                  intf_thread_t *_p_intf,
56
                                  playlist_item_t *p_root,
57
                                  PLSelector *_p_selector,
58 59
                                  PLModel *_p_model,
                                  MLModel *_p_plmodel)
60
                : QWidget( _parent ), p_intf( _p_intf ),
61 62
                  p_selector( _p_selector ), model( _p_model ),
                  mlmodel( _p_plmodel)
63
{
64 65
    viewStack = new QStackedLayout( this );
    viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
66
    setMinimumWidth( 300 );
67

68 69 70 71
    iconView    = NULL;
    treeView    = NULL;
    listView    = NULL;
    picFlowView = NULL;
72

73 74
    currentRootIndexId  = -1;
    lastActivatedId     = -1;
Clément Stenac's avatar
Clément Stenac committed
75

76 77
    /* Saved Settings */
    getSettings()->beginGroup("Playlist");
78
    int i_savedViewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
79
    getSettings()->endGroup();
80 81 82
    /* Limit the saved value to a possible one inside [0, VIEW_COUNT[ */
    if(i_savedViewMode < 0 || i_savedViewMode >= VIEW_COUNT)
        i_savedViewMode = 0;
83

84
    showView( i_savedViewMode );
Jakob Leben's avatar
Jakob Leben committed
85

86 87
    DCONNECT( THEMIM, leafBecameParent( input_item_t *),
              this, browseInto( input_item_t * ) );
88 89 90

    CONNECT( model, currentChanged( const QModelIndex& ),
             this, handleExpansion( const QModelIndex& ) );
91
    CONNECT( model, rootChanged(), this, handleRootChange() );
92

93
    setRoot( p_root, false );
94 95
}

96 97 98
StandardPLPanel::~StandardPLPanel()
{
    getSettings()->beginGroup("Playlist");
99 100
    if( treeView )
        getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
Jakob Leben's avatar
Jakob Leben committed
101 102 103 104 105 106
    if( currentView == treeView )
        getSettings()->setValue( "view-mode", TREE_VIEW );
    else if( currentView == listView )
        getSettings()->setValue( "view-mode", LIST_VIEW );
    else if( currentView == iconView )
        getSettings()->setValue( "view-mode", ICON_VIEW );
107 108
    else if( currentView == picFlowView )
        getSettings()->setValue( "view-mode", PICTUREFLOW_VIEW );
109 110 111
    getSettings()->endGroup();
}

112
/* Unused anymore, but might be useful, like in right-click menu */
113 114
void StandardPLPanel::gotoPlayingItem()
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
115
    currentView->scrollTo( model->currentIndex() );
116 117
}

118
void StandardPLPanel::handleExpansion( const QModelIndex& index )
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
119
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
120
    assert( currentView );
121 122
    if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
        browseInto( index.parent() );
123
    currentView->scrollTo( index );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
124 125
}

126 127
void StandardPLPanel::handleRootChange()
{
128
    browseInto();
129 130
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
131 132 133 134
void StandardPLPanel::popupPlView( const QPoint &point )
{
    QModelIndex index = currentView->indexAt( point );
    QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
135 136 137 138
    QItemSelectionModel *selection = currentView->selectionModel();
    QModelIndexList list = selection->selectedIndexes();

    if( !model->popup( index, globalPoint, list ) )
Erwan Tulou's avatar
Erwan Tulou committed
139
        QVLCMenu::PopupMenu( p_intf, true );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
140 141
}

142
void StandardPLPanel::popupSelectColumn( QPoint pos )
143
{
144
    QMenu menu;
145
    assert( treeView );
146

147 148
    /* We do not offer the option to hide index 0 column, or
    * QTreeView will behave weird */
149
    int i, j;
150
    for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
151
    {
152
        QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
153
        option->setCheckable( true );
154
        option->setChecked( !treeView->isColumnHidden( j ) );
155 156
        selectColumnsSigMapper->setMapping( option, j );
        CONNECT( option, triggered(), selectColumnsSigMapper, map() );
157
    }
158
    menu.exec( QCursor::pos() );
159 160
}

161
void StandardPLPanel::toggleColumnShown( int i )
162
{
163
    treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
164
}
165

166
/* Search in the playlist */
167
void StandardPLPanel::search( const QString& searchText )
Clément Stenac's avatar
Clément Stenac committed
168
{
169 170 171 172 173
    int type;
    QString name;
    p_selector->getCurrentSelectedItem( &type, &name );
    if( type != SD_TYPE )
    {
174
        bool flat = currentView == iconView || currentView == listView || currentView == picFlowView;
175 176 177 178
        model->search( searchText,
                       flat ? currentView->rootIndex() : QModelIndex(),
                       !flat );
    }
179 180
}

181
void StandardPLPanel::searchDelayed( const QString& searchText )
182 183 184 185 186 187
{
    int type;
    QString name;
    p_selector->getCurrentSelectedItem( &type, &name );

    if( type == SD_TYPE )
188
    {
189
        if( !name.isEmpty() && !searchText.isEmpty() )
Rémi Duraffort's avatar
Rémi Duraffort committed
190
            playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH, qtu( searchText ) );
191
    }
Clément Stenac's avatar
Clément Stenac committed
192 193
}

194 195
/* Set the root of the new Playlist */
/* This activated by the selector selection */
196
void StandardPLPanel::setRoot( playlist_item_t *p_item, bool b )
197
{
198
#ifdef MEDIA_LIBRARY
199 200 201 202 203 204
    if( b )
    {
        msg_Dbg( p_intf, "Setting the SQL ML" );
        currentView->setModel( mlmodel );
    }
    else
205
#endif
206 207 208 209 210 211
    {
        msg_Dbg( p_intf, "Normal PL/ML or SD" );
        if( currentView->model() != model )
            currentView->setModel( model );
        model->rebuild( p_item );
    }
212 213
}

214 215
void StandardPLPanel::browseInto( const QModelIndex &index )
{
216
    if( currentView == iconView || currentView == listView || currentView == picFlowView )
217
    {
Ilkka Ollakka's avatar
Ilkka Ollakka committed
218
        currentRootIndexId = model->itemId( index );
219 220 221
        currentView->setRootIndex( index );
    }

222
    emit viewChanged( index );
223 224 225 226 227 228 229 230 231
}

void StandardPLPanel::browseInto( )
{
    browseInto( currentRootIndexId != -1 && currentView != treeView ?
                model->index( currentRootIndexId, 0 ) :
                QModelIndex() );
}

232
void StandardPLPanel::wheelEvent( QWheelEvent *e )
Clément Stenac's avatar
Clément Stenac committed
233
{
234 235 236 237 238 239 240
    // Accept this event in order to prevent unwanted volume up/down changes
    e->accept();
}

bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
{
    if (event->type() == QEvent::KeyPress)
Clément Stenac's avatar
Clément Stenac committed
241
    {
242 243
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if( keyEvent->key() == Qt::Key_Delete ||
244
            keyEvent->key() == Qt::Key_Backspace )
245 246 247 248
        {
            deleteSelection();
            return true;
        }
Clément Stenac's avatar
Clément Stenac committed
249
    }
250
    return false;
Clément Stenac's avatar
Clément Stenac committed
251 252 253 254
}

void StandardPLPanel::deleteSelection()
{
255
    QItemSelectionModel *selection = currentView->selectionModel();
Clément Stenac's avatar
Clément Stenac committed
256 257 258 259
    QModelIndexList list = selection->selectedIndexes();
    model->doDelete( list );
}

260 261 262 263 264 265
void StandardPLPanel::createIconView()
{
    iconView = new PlIconView( model, this );
    iconView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( iconView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
266 267
    CONNECT( iconView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
268
    iconView->installEventFilter( this );
269
    viewStack->addWidget( iconView );
270 271
}

Jakob Leben's avatar
Jakob Leben committed
272 273 274 275 276 277 278 279
void StandardPLPanel::createListView()
{
    listView = new PlListView( model, this );
    listView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( listView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
    CONNECT( listView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
280
    listView->installEventFilter( this );
281
    viewStack->addWidget( listView );
Jakob Leben's avatar
Jakob Leben committed
282 283
}

284 285 286 287 288 289 290 291 292 293 294 295
void StandardPLPanel::createCoverView()
{
    picFlowView = new PicFlowView( model, this );
    picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
    CONNECT( picFlowView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
    viewStack->addWidget( picFlowView );
    picFlowView->installEventFilter( this );
}

296 297 298
void StandardPLPanel::createTreeView()
{
    /* Create and configure the QTreeView */
299
    treeView = new PlTreeView;
300 301 302 303 304 305

    treeView->setIconSize( QSize( 20, 20 ) );
    treeView->setAlternatingRowColors( true );
    treeView->setAnimated( true );
    treeView->setUniformRowHeights( true );
    treeView->setSortingEnabled( true );
306
    treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
307 308 309 310 311 312 313 314 315 316 317 318
    treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
    treeView->header()->setSortIndicatorShown( true );
    treeView->header()->setClickable( true );
    treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );

    treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
    treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
    treeView->setDragEnabled( true );
    treeView->setAcceptDrops( true );
    treeView->setDropIndicatorShown( true );
    treeView->setContextMenuPolicy( Qt::CustomContextMenu );

319 320
    /* setModel after setSortingEnabled(true), or the model will sort immediately! */

Jakob Leben's avatar
Jakob Leben committed
321 322
    getSettings()->beginGroup("Playlist");

323 324 325 326 327 328 329 330 331
    if( getSettings()->contains( "headerStateV2" ) )
    {
        treeView->header()->restoreState(
                getSettings()->value( "headerStateV2" ).toByteArray() );
    }
    else
    {
        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
        {
332
            treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
333 334 335 336 337
            if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
            else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
        }
    }

Jakob Leben's avatar
Jakob Leben committed
338 339
    getSettings()->endGroup();

340 341
    /* Connections for the TreeView */
    CONNECT( treeView, activated( const QModelIndex& ),
342
             this, activate( const QModelIndex& ) );
343 344 345 346
    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
             this, popupSelectColumn( QPoint ) );
    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
347
    treeView->installEventFilter( this );
348 349 350 351 352 353

    /* SignalMapper for columns */
    selectColumnsSigMapper = new QSignalMapper( this );
    CONNECT( selectColumnsSigMapper, mapped( int ),
             this, toggleColumnShown( int ) );

354
    viewStack->addWidget( treeView );
355 356
}

357 358
void StandardPLPanel::changeModel( bool b_ml )
{
359
#ifdef MEDIA_LIBRARY
360 361 362 363 364 365 366
    VLCModel *mod;
    if( b_ml )
        mod = mlmodel;
    else
        mod = model;
    if( currentView->model() != mod )
        currentView->setModel( mod );
367
#endif
368 369
}

370
void StandardPLPanel::showView( int i_view )
371
{
372

373 374 375 376 377 378 379 380 381 382
    switch( i_view )
    {
    case TREE_VIEW:
    {
        if( treeView == NULL )
            createTreeView();
        currentView = treeView;
        break;
    }
    case ICON_VIEW:
383
    {
384
        if( iconView == NULL )
385
            createIconView();
386
        currentView = iconView;
Jakob Leben's avatar
Jakob Leben committed
387 388 389 390 391 392 393
        break;
    }
    case LIST_VIEW:
    {
        if( listView == NULL )
            createListView();
        currentView = listView;
394
        break;
395
    }
396 397 398 399 400 401 402
    case PICTUREFLOW_VIEW:
    {
        if( picFlowView == NULL )
            createCoverView();
        currentView = picFlowView;
        break;
    }
403
    default: return;
404
    }
405

406 407
    changeModel( false );

408
    viewStack->setCurrentWidget( currentView );
409
    browseInto();
410
    gotoPlayingItem();
411
}
412

413
const int StandardPLPanel::currentViewIndex()
414 415 416 417 418 419 420
{
    if( currentView == treeView )
        return TREE_VIEW;
    else if( currentView == iconView )
        return ICON_VIEW;
    else if( currentView == listView )
        return LIST_VIEW;
421 422
    else
        return PICTUREFLOW_VIEW;
423 424
}

425 426 427 428 429
void StandardPLPanel::cycleViews()
{
    if( currentView == iconView )
        showView( TREE_VIEW );
    else if( currentView == treeView )
Jakob Leben's avatar
Jakob Leben committed
430 431
        showView( LIST_VIEW );
    else if( currentView == listView )
432 433
        showView( PICTUREFLOW_VIEW  );
    else if( currentView == picFlowView )
434 435 436 437 438
        showView( ICON_VIEW );
    else
        assert( 0 );
}

439 440
void StandardPLPanel::activate( const QModelIndex &index )
{
441
    if( currentView->model() == model )
442
    {
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
        /* If we are not a leaf node */
        if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
        {
            if( currentView != treeView )
                browseInto( index );
        }
        else
        {
            playlist_Lock( THEPL );
            playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
            p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
            lastActivatedId = p_item->p_input->i_id;
            playlist_Unlock( THEPL );
            model->activateItem( index );
        }
458 459
    }
}
460

461
void StandardPLPanel::browseInto( input_item_t *p_input )
462
{
463
    if( p_input->i_id != lastActivatedId ) return;
464 465 466

    playlist_Lock( THEPL );

467
    playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
468 469 470 471 472
    if( !p_item )
    {
        playlist_Unlock( THEPL );
        return;
    }
473

474
    QModelIndex index = model->index( p_item->i_id, 0 );
475 476
    playlist_Unlock( THEPL );

477
    if( currentView == treeView )
478
        treeView->setExpanded( index, true );
479 480
    else
        browseInto( index );
481

482
    lastActivatedId = -1;
483
}