standardpanel.cpp 14.3 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
    CONNECT( model, currentIndexChanged( const QModelIndex& ),
86
             this, handleExpansion( const QModelIndex& ) );
87
    CONNECT( model, rootIndexChanged(), this, browseInto() );
88

89
    setRootItem( 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 ) )
123
        VLCMenuBar::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::setRootItem( 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! */

309 310
    /* Connections for the TreeView */
    CONNECT( treeView, activated( const QModelIndex& ),
311
             this, activate( const QModelIndex& ) );
312 313 314 315
    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
             this, popupSelectColumn( QPoint ) );
    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
316
    treeView->installEventFilter( this );
317 318 319 320 321 322

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

323
    viewStack->addWidget( treeView );
324 325
}

326 327
void StandardPLPanel::changeModel( bool b_ml )
{
328
#ifdef MEDIA_LIBRARY
329 330 331 332 333 334 335
    VLCModel *mod;
    if( b_ml )
        mod = mlmodel;
    else
        mod = model;
    if( currentView->model() != mod )
        currentView->setModel( mod );
336
#else
337
    Q_UNUSED( b_ml );
338 339
    if( currentView->model() != model )
        currentView->setModel( model );
340
#endif
341 342
}

343
void StandardPLPanel::showView( int i_view )
344
{
345
    bool b_treeViewCreated = false;
346

347 348 349
    switch( i_view )
    {
    case ICON_VIEW:
350
    {
351
        if( iconView == NULL )
352
            createIconView();
353
        currentView = iconView;
Jakob Leben's avatar
Jakob Leben committed
354 355 356 357 358 359 360
        break;
    }
    case LIST_VIEW:
    {
        if( listView == NULL )
            createListView();
        currentView = listView;
361
        break;
362
    }
363 364 365 366 367 368 369
    case PICTUREFLOW_VIEW:
    {
        if( picFlowView == NULL )
            createCoverView();
        currentView = picFlowView;
        break;
    }
370 371 372 373
    default:
    case TREE_VIEW:
    {
        if( treeView == NULL )
374
        {
375
            createTreeView();
376 377
            b_treeViewCreated = true;
        }
378 379 380
        currentView = treeView;
        break;
    }
381
    }
382

383 384
    changeModel( false );

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    /* Restoring the header Columns must come after changeModel */
    if( b_treeViewCreated )
    {
        assert( treeView );
        if( getSettings()->contains( "Playlist/headerStateV2" ) )
        {
            treeView->header()->restoreState(getSettings()
                    ->value( "Playlist/headerStateV2" ).toByteArray() );
        }
        else
        {
            for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
            {
                treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
                if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
                else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
            }
        }
    }

405
    viewStack->setCurrentWidget( currentView );
406
    browseInto();
407
    gotoPlayingItem();
408
}
409

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

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

438 439
void StandardPLPanel::activate( const QModelIndex &index )
{
440
    if( currentView->model() == model )
441
    {
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        /* 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 );
        }
457 458
    }
}
459

460
void StandardPLPanel::browseInto( int i_id )
461
{
462
    if( i_id != lastActivatedId ) return;
463

464
    QModelIndex index = model->index( i_id, 0 );
465 466
    playlist_Unlock( THEPL );

467
    if( currentView == treeView )
468
        treeView->setExpanded( index, true );
469 470
    else
        browseInto( index );
471

472
    lastActivatedId = -1;
473
}