standardpanel.cpp 17.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * standardpanel.cpp : The "standard" playlist panel : just a treeview
 ****************************************************************************
4
 * Copyright (C) 2000-2009 VideoLAN
5
 * $Id$
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          JB 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 "dialogs_provider.hpp"
30

31
#include "components/playlist/playlist_model.hpp"
32
#include "components/playlist/standardpanel.hpp"
33
#include "components/playlist/icon_view.hpp"
Clément Stenac's avatar
Clément Stenac committed
34
#include "util/customwidgets.hpp"
35
#include "menus.hpp"
Clément Stenac's avatar
Clément Stenac committed
36

37 38
#include <vlc_intf_strings.h>

Clément Stenac's avatar
Clément Stenac committed
39
#include <QPushButton>
40
#include <QHeaderView>
Clément Stenac's avatar
Clément Stenac committed
41 42
#include <QKeyEvent>
#include <QModelIndexList>
Clément Stenac's avatar
Clément Stenac committed
43
#include <QLabel>
44
#include <QMenu>
45
#include <QSignalMapper>
46
#include <QWheelEvent>
47 48
#include <QToolButton>
#include <QFontMetrics>
49
#include <QPainter>
50

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

Rafaël Carré's avatar
Rafaël Carré committed
53 54
#include "sorting.h"

Jakob Leben's avatar
Jakob Leben committed
55 56 57 58
static const QString viewNames[] = { qtr( "Detailed View" ),
                                     qtr( "Icon View" ),
                                     qtr( "List View" ) };

59
StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
60
                                  intf_thread_t *_p_intf,
Clément Stenac's avatar
Clément Stenac committed
61
                                  playlist_t *p_playlist,
Clément Stenac's avatar
Clément Stenac committed
62
                                  playlist_item_t *p_root ):
63
                                  QWidget( _parent ), p_intf( _p_intf )
64
{
65
    layout = new QGridLayout( this );
66
    layout->setSpacing( 0 ); layout->setMargin( 0 );
67
    setMinimumWidth( 300 );
68

69
    iconView = NULL;
70
    treeView = NULL;
Jakob Leben's avatar
Jakob Leben committed
71
    listView = NULL;
72

73
    model = new PLModel( p_playlist, p_intf, p_root, this );
74
    currentRootId = -1;
75 76
    currentRootIndexId = -1;
    lastActivatedId = -1;
Clément Stenac's avatar
Clément Stenac committed
77

78
    locationBar = new LocationBar( model );
79 80 81 82 83
    locationBar->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
    layout->addWidget( locationBar, 0, 1 );
    layout->setColumnStretch( 1, 100 );
    CONNECT( locationBar, invoked( const QModelIndex & ),
             this, browseInto( const QModelIndex & ) );
84

85
    layout->setColumnStretch( 2, 1 );
86

87 88 89 90 91
    searchEdit = new SearchLineEdit( this );
    searchEdit->setMaximumWidth( 250 );
    searchEdit->setMinimumWidth( 80 );
    layout->addWidget( searchEdit, 0, 3 );
    CONNECT( searchEdit, textChanged( const QString& ),
92
             this, search( const QString& ) );
93
    layout->setColumnStretch( 3, 50 );
94

95
    /* Add item to the playlist button */
96
    addButton = new QToolButton;
97
    addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
98
    addButton->setMaximumWidth( 30 );
99
    BUTTONACT( addButton, popupAdd() );
100
    layout->addWidget( addButton, 0, 0 );
101

102
    /* Button to switch views */
103
    QToolButton *viewButton = new QToolButton( this );
104
    viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
105
    layout->addWidget( viewButton, 0, 4 );
106 107

    /* View selection menu */
108
    viewSelectionMapper = new QSignalMapper( this );
109 110 111 112
    CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );

    QActionGroup *actionGroup = new QActionGroup( this );

Jakob Leben's avatar
Jakob Leben committed
113 114 115 116 117 118 119
    for( int i = 0; i < VIEW_COUNT; i++ )
    {
        viewActions[i] = actionGroup->addAction( viewNames[i] );
        viewActions[i]->setCheckable( true );
        viewSelectionMapper->setMapping( viewActions[i], i );
        CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
    }
120

121
    BUTTONACT( viewButton, cycleViews() );
122 123 124 125
    QMenu *viewMenu = new QMenu( this );
    viewMenu->addActions( actionGroup->actions() );

    viewButton->setMenu( viewMenu );
126 127 128 129 130

    /* Saved Settings */
    getSettings()->beginGroup("Playlist");

    int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
131
    showView( i_viewMode );
132 133

    getSettings()->endGroup();
134

135 136
    CONNECT( THEMIM, leafBecameParent( input_item_t *),
             this, browseInto( input_item_t * ) );
137 138 139

    CONNECT( model, currentChanged( const QModelIndex& ),
             this, handleExpansion( const QModelIndex& ) );
140
    CONNECT( model, rootChanged(), this, handleRootChange() );
141 142
}

143 144 145
StandardPLPanel::~StandardPLPanel()
{
    getSettings()->beginGroup("Playlist");
146 147
    if( treeView )
        getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
Jakob Leben's avatar
Jakob Leben committed
148 149 150 151 152 153
    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 );
154 155 156
    getSettings()->endGroup();
}

157
/* Unused anymore, but might be useful, like in right-click menu */
158 159
void StandardPLPanel::gotoPlayingItem()
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
160
    currentView->scrollTo( model->currentIndex() );
161 162
}

163
void StandardPLPanel::handleExpansion( const QModelIndex& index )
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
164
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
165
    assert( currentView );
166
    currentView->scrollTo( index );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
167 168
}

169 170 171 172 173 174
void StandardPLPanel::handleRootChange()
{
    /* needed for popupAdd() */
    PLItem *root = model->getItem( QModelIndex() );
    currentRootId = root->id();

175
    browseInto();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    /* enable/disable adding */
    if( currentRootId == THEPL->p_playing->i_id )
    {
        addButton->setEnabled( true );
        addButton->setToolTip( qtr(I_PL_ADDPL) );
    }
    else if( THEPL->p_media_library &&
             currentRootId == THEPL->p_media_library->i_id )
    {
        addButton->setEnabled( true );
        addButton->setToolTip( qtr(I_PL_ADDML) );
    }
    else
        addButton->setEnabled( false );
}

193
/* PopupAdd Menu for the Add Menu */
194
void StandardPLPanel::popupAdd()
195
{
196
    QMenu popup;
197
    if( currentRootId == THEPL->p_playing->i_id )
198
    {
199
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
200
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
201
        popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
202
    }
203 204
    else if( THEPL->p_media_library &&
                currentRootId == THEPL->p_media_library->i_id )
205
    {
206
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
207
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
208
        popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
209
    }
210

211 212
    popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                        + QPoint( 0, addButton->height() ) );
213 214
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
215 216 217 218
void StandardPLPanel::popupPlView( const QPoint &point )
{
    QModelIndex index = currentView->indexAt( point );
    QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
219 220 221 222 223 224 225 226 227
    if( !index.isValid() ){
        QVLCMenu::PopupMenu( p_intf, true );
    }
    else
    {
        QItemSelectionModel *selection = currentView->selectionModel();
        QModelIndexList list = selection->selectedIndexes();
        model->popup( index, globalPoint, list );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
228 229
}

230
void StandardPLPanel::popupSelectColumn( QPoint pos )
231
{
232
    QMenu menu;
233
    assert( treeView );
234

235 236
    /* We do not offer the option to hide index 0 column, or
    * QTreeView will behave weird */
237
    int i, j;
238
    for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
239
    {
240 241 242
        QAction* option = menu.addAction(
            qfu( psz_column_title( i ) ) );
        option->setCheckable( true );
243
        option->setChecked( !treeView->isColumnHidden( j ) );
244 245
        selectColumnsSigMapper->setMapping( option, j );
        CONNECT( option, triggered(), selectColumnsSigMapper, map() );
246
    }
247
    menu.exec( QCursor::pos() );
248 249
}

250
void StandardPLPanel::toggleColumnShown( int i )
251
{
252
    treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
253
}
254

255
/* Search in the playlist */
256
void StandardPLPanel::search( const QString& searchText )
Clément Stenac's avatar
Clément Stenac committed
257
{
258 259 260 261
    bool flat = currentView == iconView || currentView == listView;
    model->search( searchText,
                   flat ? currentView->rootIndex() : QModelIndex(),
                   !flat );
Clément Stenac's avatar
Clément Stenac committed
262 263
}

264 265
/* Set the root of the new Playlist */
/* This activated by the selector selection */
266 267
void StandardPLPanel::setRoot( playlist_item_t *p_item )
{
Clément Stenac's avatar
Clément Stenac committed
268
    model->rebuild( p_item );
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
void StandardPLPanel::browseInto( const QModelIndex &index )
{
    if( currentView == iconView || currentView == listView )
    {
        currentRootIndexId = model->itemId( index );;
        currentView->setRootIndex( index );
    }

    locationBar->setIndex( index );
    model->search( QString(), index, false );
    searchEdit->clear();
}

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

291
/* Delete and Suppr key remove the selection
292
   FilterKey function and code function */
Clément Stenac's avatar
Clément Stenac committed
293 294 295 296 297 298 299 300 301 302 303 304 305
void StandardPLPanel::keyPressEvent( QKeyEvent *e )
{
    switch( e->key() )
    {
    case Qt::Key_Back:
    case Qt::Key_Delete:
        deleteSelection();
        break;
    }
}

void StandardPLPanel::deleteSelection()
{
306
    QItemSelectionModel *selection = currentView->selectionModel();
Clément Stenac's avatar
Clément Stenac committed
307 308 309 310
    QModelIndexList list = selection->selectedIndexes();
    model->doDelete( list );
}

311 312 313 314 315 316
void StandardPLPanel::createIconView()
{
    iconView = new PlIconView( model, this );
    iconView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( iconView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
317 318
    CONNECT( iconView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
319 320 321 322

    layout->addWidget( iconView, 1, 0, 1, -1 );
}

Jakob Leben's avatar
Jakob Leben committed
323 324 325 326 327 328 329 330 331 332 333 334 335
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 & ) );

    layout->addWidget( listView, 1, 0, 1, -1 );
}


336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
void StandardPLPanel::createTreeView()
{
    /* Create and configure the QTreeView */
    treeView = new QTreeView;

    treeView->setIconSize( QSize( 20, 20 ) );
    treeView->setAlternatingRowColors( true );
    treeView->setAnimated( true );
    treeView->setUniformRowHeights( true );
    treeView->setSortingEnabled( true );
    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 );

358 359 360
    /* setModel after setSortingEnabled(true), or the model will sort immediately! */
    treeView->setModel( model );

361 362 363 364 365 366 367 368 369
    if( getSettings()->contains( "headerStateV2" ) )
    {
        treeView->header()->restoreState(
                getSettings()->value( "headerStateV2" ).toByteArray() );
    }
    else
    {
        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
        {
370
            treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
371 372 373 374 375 376 377
            if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
            else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
        }
    }

    /* Connections for the TreeView */
    CONNECT( treeView, activated( const QModelIndex& ),
378
             this, activate( const QModelIndex& ) );
379 380 381 382 383 384 385 386 387 388 389 390 391 392
    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
             this, popupSelectColumn( QPoint ) );
    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );

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

    /* Finish the layout */
    layout->addWidget( treeView, 1, 0, 1, -1 );
}

393
void StandardPLPanel::showView( int i_view )
394
{
395 396 397 398 399 400 401
    switch( i_view )
    {
    case TREE_VIEW:
    {
        if( treeView == NULL )
            createTreeView();
        if( iconView ) iconView->hide();
Jakob Leben's avatar
Jakob Leben committed
402
        if( listView ) listView->hide();
403 404
        treeView->show();
        currentView = treeView;
Jakob Leben's avatar
Jakob Leben committed
405
        viewActions[i_view]->setChecked( true );
406 407 408
        break;
    }
    case ICON_VIEW:
409
    {
410
        if( iconView == NULL )
411 412
            createIconView();

413
        if( treeView ) treeView->hide();
414
        if( listView ) listView->hide();
415
        iconView->show();
416
        currentView = iconView;
Jakob Leben's avatar
Jakob Leben committed
417 418 419 420 421 422 423 424 425
        viewActions[i_view]->setChecked( true );
        break;
    }
    case LIST_VIEW:
    {
        if( listView == NULL )
            createListView();

        if( treeView ) treeView->hide();
426
        if( iconView ) iconView->hide();
Jakob Leben's avatar
Jakob Leben committed
427 428 429
        listView->show();
        currentView = listView;
        viewActions[i_view]->setChecked( true );
430
        break;
431
    }
432
    default: return;
433
    }
434 435

    browseInto();
436
}
437

438 439 440 441 442
void StandardPLPanel::cycleViews()
{
    if( currentView == iconView )
        showView( TREE_VIEW );
    else if( currentView == treeView )
Jakob Leben's avatar
Jakob Leben committed
443 444
        showView( LIST_VIEW );
    else if( currentView == listView )
445 446 447 448 449
        showView( ICON_VIEW );
    else
        assert( 0 );
}

450 451 452 453 454
void StandardPLPanel::wheelEvent( QWheelEvent *e )
{
    // Accept this event in order to prevent unwanted volume up/down changes
    e->accept();
}
455 456 457

void StandardPLPanel::activate( const QModelIndex &index )
{
458
    if( model->hasChildren( index ) )
459
    {
460 461
        if( currentView != treeView )
            browseInto( index );
462 463 464
    }
    else
    {
465 466 467
        playlist_Lock( THEPL );
        playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
        p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
468
        lastActivatedId = p_item->p_input->i_id;
469
        playlist_Unlock( THEPL );
470 471 472
        model->activateItem( index );
    }
}
473

474
void StandardPLPanel::browseInto( input_item_t *p_input )
475
{
Jakob Leben's avatar
Jakob Leben committed
476

477
    if( p_input->i_id != lastActivatedId ) return;
478 479 480

    playlist_Lock( THEPL );

481
    playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
482 483 484 485 486
    if( !p_item )
    {
        playlist_Unlock( THEPL );
        return;
    }
487

488
    QModelIndex index = model->index( p_item->i_id, 0 );
489 490 491

    playlist_Unlock( THEPL );

492
    if( currentView == treeView )
493
        treeView->setExpanded( index, true );
494 495
    else
        browseInto( index );
496

497
    lastActivatedId = -1;
498

499

500
}
501 502 503

LocationBar::LocationBar( PLModel *m )
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
504 505 506
    model = m;
    mapper = new QSignalMapper( this );
    CONNECT( mapper, mapped( int ), this, invoke( int ) );
507 508 509

    box = new QHBoxLayout;
    box->setSpacing( 0 );
Jakob Leben's avatar
Jakob Leben committed
510
    box->setContentsMargins( 0, 0, 0, 0 );
511
    setLayout( box );
512 513 514 515
}

void LocationBar::setIndex( const QModelIndex &index )
{
516 517
    qDeleteAll( buttons );
    buttons.clear();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
518
    QModelIndex i = index;
519
    bool bold = true;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
520 521 522 523 524 525 526
    while( true )
    {
        PLItem *item = model->getItem( i );

        char *fb_name = input_item_GetTitleFbName( item->inputItem() );
        QString text = qfu(fb_name);
        free(fb_name);
527
        QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
528
        btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
529
        box->insertWidget( 0, btn, bold ? 1 : 0 );
530
        buttons.append( btn );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
531 532 533 534

        mapper->setMapping( btn, item->id() );
        CONNECT( btn, clicked( ), mapper, map( ) );

535
        bold = false;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
536 537 538 539

        if( i.isValid() ) i = i.parent();
        else break;
    }
540 541
}

542 543 544 545 546
void LocationBar::setRootIndex()
{
    setIndex( QModelIndex() );
}

547 548
void LocationBar::invoke( int i_id )
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
549 550
    QModelIndex index = model->index( i_id, 0 );
    emit invoked ( index );
551
}
552

553 554
LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
  : b_arrow( arrow )
555 556 557 558
{
    QFont font;
    font.setBold( bold );
    setFont( font );
559
    setText( text );
560 561
}

Jakob Leben's avatar
Jakob Leben committed
562 563
#define PADDING 4

564 565 566 567 568 569
void LocationButton::paintEvent ( QPaintEvent * event )
{
    QStyleOptionButton option;
    option.initFrom( this );
    option.state |= QStyle::State_Enabled;
    QPainter p( this );
Jakob Leben's avatar
Jakob Leben committed
570

571 572
    if( underMouse() )
        style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
Jakob Leben's avatar
Jakob Leben committed
573 574 575 576 577 578 579

    int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;

    QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
    p.drawText( rect, Qt::AlignVCenter,
                fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );

580 581
    if( b_arrow )
    {
Jakob Leben's avatar
Jakob Leben committed
582 583
        option.rect.setX( margin );
        option.rect.setWidth( 8 );
584 585
        style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
    }
586 587 588 589
}

QSize LocationButton::sizeHint() const
{
Jakob Leben's avatar
Jakob Leben committed
590
    int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
591
    QSize s( fontMetrics().boundingRect( text() ).size() );
Jakob Leben's avatar
Jakob Leben committed
592 593
    s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
    s.setHeight( QPushButton::sizeHint().height() );
594 595
    return s;
}
Jakob Leben's avatar
Jakob Leben committed
596 597

#undef PADDING