standardpanel.cpp 14 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 61 62 63 64
                : QWidget( _parent ),
                  model( _p_model ),
                  mlmodel( _p_plmodel),
                  p_intf( _p_intf ),
                  p_selector( _p_selector )
65
{
66 67
    viewStack = new QStackedLayout( this );
    viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
68
    setMinimumWidth( 300 );
69

70 71 72 73
    iconView    = NULL;
    treeView    = NULL;
    listView    = NULL;
    picFlowView = NULL;
74

75 76
    currentRootIndexId  = -1;
    lastActivatedId     = -1;
Clément Stenac's avatar
Clément Stenac committed
77

78
    /* Saved Settings */
79
    int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
80
    showView( i_savedViewMode );
Jakob Leben's avatar
Jakob Leben committed
81

82 83
    DCONNECT( THEMIM, leafBecameParent( int ),
              this, browseInto( int ) );
84 85 86

    CONNECT( model, currentChanged( const QModelIndex& ),
             this, handleExpansion( const QModelIndex& ) );
87
    CONNECT( model, rootChanged(), this, browseInto() );
88

89
    setRoot( p_root, false );
90 91
}

92 93 94
StandardPLPanel::~StandardPLPanel()
{
    getSettings()->beginGroup("Playlist");
95 96
    if( treeView )
        getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
97
    getSettings()->setValue( "view-mode", currentViewIndex() );
98 99 100
    getSettings()->endGroup();
}

101
/* Unused anymore, but might be useful, like in right-click menu */
102 103
void StandardPLPanel::gotoPlayingItem()
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
104
    currentView->scrollTo( model->currentIndex() );
105 106
}

107
void StandardPLPanel::handleExpansion( const QModelIndex& index )
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
108
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
109
    assert( currentView );
110 111
    if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
        browseInto( index.parent() );
112
    currentView->scrollTo( index );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
113 114
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
115 116 117 118
void StandardPLPanel::popupPlView( const QPoint &point )
{
    QModelIndex index = currentView->indexAt( point );
    QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
119 120 121 122
    QItemSelectionModel *selection = currentView->selectionModel();
    QModelIndexList list = selection->selectedIndexes();

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

126
void StandardPLPanel::popupSelectColumn( QPoint )
127
{
128
    QMenu menu;
129
    assert( treeView );
130

131
    /* We do not offer the option to hide index 0 column, or
132 133
     * QTreeView will behave weird */
    for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
134
    {
135
        QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
136
        option->setCheckable( true );
137
        option->setChecked( !treeView->isColumnHidden( j ) );
138 139
        selectColumnsSigMapper->setMapping( option, j );
        CONNECT( option, triggered(), selectColumnsSigMapper, map() );
140
    }
141
    menu.exec( QCursor::pos() );
142 143
}

144
void StandardPLPanel::toggleColumnShown( int i )
145
{
146
    treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
147
}
148

149
/* Search in the playlist */
150
void StandardPLPanel::search( const QString& searchText )
Clément Stenac's avatar
Clément Stenac committed
151
{
152 153 154 155 156
    int type;
    QString name;
    p_selector->getCurrentSelectedItem( &type, &name );
    if( type != SD_TYPE )
    {
157 158 159
        bool flat = ( currentView == iconView ||
                      currentView == listView ||
                      currentView == picFlowView );
160 161 162 163
        model->search( searchText,
                       flat ? currentView->rootIndex() : QModelIndex(),
                       !flat );
    }
164 165
}

166
void StandardPLPanel::searchDelayed( const QString& searchText )
167 168 169 170 171 172
{
    int type;
    QString name;
    p_selector->getCurrentSelectedItem( &type, &name );

    if( type == SD_TYPE )
173
    {
174
        if( !name.isEmpty() && !searchText.isEmpty() )
175 176
            playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
                                              qtu( searchText ) );
177
    }
Clément Stenac's avatar
Clément Stenac committed
178 179
}

180 181
/* Set the root of the new Playlist */
/* This activated by the selector selection */
182
void StandardPLPanel::setRoot( playlist_item_t *p_item, bool b )
183
{
184
#ifdef MEDIA_LIBRARY
185 186 187 188 189 190
    if( b )
    {
        msg_Dbg( p_intf, "Setting the SQL ML" );
        currentView->setModel( mlmodel );
    }
    else
191 192
#else
    Q_UNUSED( b );
193
#endif
194 195 196 197 198 199
    {
        msg_Dbg( p_intf, "Normal PL/ML or SD" );
        if( currentView->model() != model )
            currentView->setModel( model );
        model->rebuild( p_item );
    }
200 201
}

202 203
void StandardPLPanel::browseInto( const QModelIndex &index )
{
204
    if( currentView == iconView || currentView == listView || currentView == picFlowView )
205
    {
Ilkka Ollakka's avatar
Ilkka Ollakka committed
206
        currentRootIndexId = model->itemId( index );
207 208 209
        currentView->setRootIndex( index );
    }

210
    emit viewChanged( index );
211 212
}

213
void StandardPLPanel::browseInto()
214
{
215 216 217
    browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
                 model->index( currentRootIndexId, 0 ) :
                 QModelIndex() );
218 219
}

220
void StandardPLPanel::wheelEvent( QWheelEvent *e )
Clément Stenac's avatar
Clément Stenac committed
221
{
222 223 224 225
    // Accept this event in order to prevent unwanted volume up/down changes
    e->accept();
}

226
bool StandardPLPanel::eventFilter ( QObject *, QEvent * event )
227 228
{
    if (event->type() == QEvent::KeyPress)
Clément Stenac's avatar
Clément Stenac committed
229
    {
230 231
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if( keyEvent->key() == Qt::Key_Delete ||
232
            keyEvent->key() == Qt::Key_Backspace )
233 234 235 236
        {
            deleteSelection();
            return true;
        }
Clément Stenac's avatar
Clément Stenac committed
237
    }
238
    return false;
Clément Stenac's avatar
Clément Stenac committed
239 240 241 242
}

void StandardPLPanel::deleteSelection()
{
243
    QItemSelectionModel *selection = currentView->selectionModel();
Clément Stenac's avatar
Clément Stenac committed
244 245 246 247
    QModelIndexList list = selection->selectedIndexes();
    model->doDelete( list );
}

248 249 250 251 252 253
void StandardPLPanel::createIconView()
{
    iconView = new PlIconView( model, this );
    iconView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( iconView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
254 255
    CONNECT( iconView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
256
    iconView->installEventFilter( this );
257
    viewStack->addWidget( iconView );
258 259
}

Jakob Leben's avatar
Jakob Leben committed
260 261 262 263 264 265 266 267
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 & ) );
268
    listView->installEventFilter( this );
269
    viewStack->addWidget( listView );
Jakob Leben's avatar
Jakob Leben committed
270 271
}

272 273 274 275 276 277 278 279 280 281 282 283
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 );
}

284 285 286
void StandardPLPanel::createTreeView()
{
    /* Create and configure the QTreeView */
287
    treeView = new PlTreeView;
288 289 290 291 292 293

    treeView->setIconSize( QSize( 20, 20 ) );
    treeView->setAlternatingRowColors( true );
    treeView->setAnimated( true );
    treeView->setUniformRowHeights( true );
    treeView->setSortingEnabled( true );
294
    treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
295 296 297 298 299 300 301 302 303 304 305 306
    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 );

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

Jakob Leben's avatar
Jakob Leben committed
309 310
    getSettings()->beginGroup("Playlist");

311 312 313 314 315 316 317 318 319
    if( getSettings()->contains( "headerStateV2" ) )
    {
        treeView->header()->restoreState(
                getSettings()->value( "headerStateV2" ).toByteArray() );
    }
    else
    {
        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
        {
320
            treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
321 322 323 324 325
            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
326 327
    getSettings()->endGroup();

328 329
    /* Connections for the TreeView */
    CONNECT( treeView, activated( const QModelIndex& ),
330
             this, activate( const QModelIndex& ) );
331 332 333 334
    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
             this, popupSelectColumn( QPoint ) );
    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
335
    treeView->installEventFilter( this );
336 337 338 339 340 341

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

342
    viewStack->addWidget( treeView );
343 344
}

345 346
void StandardPLPanel::changeModel( bool b_ml )
{
347
#ifdef MEDIA_LIBRARY
348 349 350 351 352 353 354
    VLCModel *mod;
    if( b_ml )
        mod = mlmodel;
    else
        mod = model;
    if( currentView->model() != mod )
        currentView->setModel( mod );
355
#else
356
    Q_UNUSED( b_ml );
357 358
    if( currentView->model() != model )
        currentView->setModel( model );
359
#endif
360 361
}

362
void StandardPLPanel::showView( int i_view )
363
{
364

365 366 367
    switch( i_view )
    {
    case ICON_VIEW:
368
    {
369
        if( iconView == NULL )
370
            createIconView();
371
        currentView = iconView;
Jakob Leben's avatar
Jakob Leben committed
372 373 374 375 376 377 378
        break;
    }
    case LIST_VIEW:
    {
        if( listView == NULL )
            createListView();
        currentView = listView;
379
        break;
380
    }
381 382 383 384 385 386 387
    case PICTUREFLOW_VIEW:
    {
        if( picFlowView == NULL )
            createCoverView();
        currentView = picFlowView;
        break;
    }
388 389 390 391 392 393 394 395
    default:
    case TREE_VIEW:
    {
        if( treeView == NULL )
            createTreeView();
        currentView = treeView;
        break;
    }
396
    }
397

398 399
    changeModel( false );

400
    viewStack->setCurrentWidget( currentView );
401
    browseInto();
402
    gotoPlayingItem();
403
}
404

405
int StandardPLPanel::currentViewIndex() const
406 407 408 409 410 411 412
{
    if( currentView == treeView )
        return TREE_VIEW;
    else if( currentView == iconView )
        return ICON_VIEW;
    else if( currentView == listView )
        return LIST_VIEW;
413 414
    else
        return PICTUREFLOW_VIEW;
415 416
}

417 418 419 420 421
void StandardPLPanel::cycleViews()
{
    if( currentView == iconView )
        showView( TREE_VIEW );
    else if( currentView == treeView )
Jakob Leben's avatar
Jakob Leben committed
422 423
        showView( LIST_VIEW );
    else if( currentView == listView )
424 425
        showView( PICTUREFLOW_VIEW  );
    else if( currentView == picFlowView )
426 427 428 429 430
        showView( ICON_VIEW );
    else
        assert( 0 );
}

431 432
void StandardPLPanel::activate( const QModelIndex &index )
{
433
    if( currentView->model() == model )
434
    {
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        /* 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 );
        }
450 451
    }
}
452

453
void StandardPLPanel::browseInto( int i_id )
454
{
455
    if( i_id != lastActivatedId ) return;
456

457
    QModelIndex index = model->index( i_id, 0 );
458 459
    playlist_Unlock( THEPL );

460
    if( currentView == treeView )
461
        treeView->setExpanded( index, true );
462 463
    else
        browseInto( index );
464

465
    lastActivatedId = -1;
466
}