standardpanel.cpp 27.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 33 34
#include "components/playlist/playlist_model.hpp" /* PLModel */
#include "components/playlist/views.hpp"          /* 3 views */
#include "components/playlist/selector.hpp"       /* PLSelector */
François Cartegnie's avatar
François Cartegnie committed
35
#include "util/animators.hpp"                     /* PixmapAnimator */
36 37
#include "menus.hpp"                              /* Popup */
#include "input_manager.hpp"                      /* THEMIM */
38
#include "dialogs_provider.hpp"                   /* THEDP */
39
#include "recents.hpp"                            /* RecentMRL */
40 41
#include "dialogs/playlist.hpp"                   /* Playlist Dialog */
#include "dialogs/mediainfo.hpp"                  /* MediaInfoDialog */
42
#include "util/qt_dirs.hpp"
43

44
#include <vlc_services_discovery.h>               /* SD_CMD_SEARCH */
45
#include <vlc_intf_strings.h>                     /* POP_ */
46

47 48 49 50 51 52
#define I_NEW_DIR \
    I_DIR_OR_FOLDER( N_("Create Directory"), N_( "Create Folder" ) )
#define I_NEW_DIR_NAME \
    I_DIR_OR_FOLDER( N_( "Enter name for new directory:" ), \
                     N_( "Enter name for new folder:" ) )

Edward Wang's avatar
Edward Wang committed
53 54 55 56 57 58
#define I_RENAME_DIR \
    I_DIR_OR_FOLDER( N_("Rename Directory"), N_( "Rename Folder" ) )
#define I_RENAME_DIR_NAME \
    I_DIR_OR_FOLDER( N_( "Enter a new name for the directory:" ), \
                     N_( "Enter a new name for the folder:" ) )

59
#include <QHeaderView>
60
#include <QMenu>
61
#include <QKeyEvent>
62
#include <QWheelEvent>
63
#include <QStackedLayout>
64
#include <QSignalMapper>
65
#include <QSettings>
66
#include <QStylePainter>
67 68 69
#include <QInputDialog>
#include <QDesktopServices>
#include <QUrl>
70
#include <QFont>
71

Clément Stenac's avatar
Clément Stenac committed
72
#include <assert.h>
Clément Stenac's avatar
Clément Stenac committed
73

François Cartegnie's avatar
François Cartegnie committed
74 75 76
/* local helper */
inline QModelIndex popupIndex( QAbstractItemView *view );

77
StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
78
                                  intf_thread_t *_p_intf,
79
                                  playlist_item_t *p_root,
80
                                  PLSelector *_p_selector,
81
                                  VLCModel *_p_model )
82 83 84 85
                : QWidget( _parent ),
                  model( _p_model ),
                  p_intf( _p_intf ),
                  p_selector( _p_selector )
86
{
87 88
    viewStack = new QStackedLayout( this );
    viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
89
    setMinimumWidth( 300 );
90

91 92 93 94
    iconView    = NULL;
    treeView    = NULL;
    listView    = NULL;
    picFlowView = NULL;
95

François Cartegnie's avatar
François Cartegnie committed
96 97
    currentRootIndexPLId  = -1;
    lastActivatedPLItemId     = -1;
Clément Stenac's avatar
Clément Stenac committed
98

99 100 101 102 103 104 105 106
    QList<QString> frames;
    frames << ":/util/wait1";
    frames << ":/util/wait2";
    frames << ":/util/wait3";
    frames << ":/util/wait4";
    spinnerAnimation = new PixmapAnimator( this, frames );
    CONNECT( spinnerAnimation, pixmapReady( const QPixmap & ), this, updateViewport() );

107
    /* Saved Settings */
108
    int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
109 110
    i_zoom = getSettings()->value( "Playlist/zoom", 0 ).toInt();

111
    showView( i_savedViewMode );
Jakob Leben's avatar
Jakob Leben committed
112

113 114
    DCONNECT( THEMIM, leafBecameParent( int ),
              this, browseInto( int ) );
115

116
    CONNECT( model, currentIndexChanged( const QModelIndex& ),
117
             this, handleExpansion( const QModelIndex& ) );
118
    CONNECT( model, rootIndexChanged(), this, browseInto() );
119

120
    setRootItem( p_root, false );
121 122
}

123 124 125
StandardPLPanel::~StandardPLPanel()
{
    getSettings()->beginGroup("Playlist");
126 127
    if( treeView )
        getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
128
    getSettings()->setValue( "view-mode", currentViewIndex() );
129
    getSettings()->setValue( "zoom", i_zoom );
130 131 132
    getSettings()->endGroup();
}

133
/* Unused anymore, but might be useful, like in right-click menu */
134 135
void StandardPLPanel::gotoPlayingItem()
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
136
    currentView->scrollTo( model->currentIndex() );
137 138
}

139
void StandardPLPanel::handleExpansion( const QModelIndex& index )
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
140
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
141
    assert( currentView );
François Cartegnie's avatar
François Cartegnie committed
142
    if( currentRootIndexPLId != -1 && currentRootIndexPLId != model->itemId( index.parent(), PLAYLIST_ID ) )
143
        browseInto( index.parent() );
144
    currentView->scrollTo( index );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
145 146
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
147 148 149
void StandardPLPanel::popupPlView( const QPoint &point )
{
    QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
150 151 152 153 154 155 156 157 158 159
    QModelIndex index = currentView->indexAt( point );
    if ( !index.isValid() )
    {
        currentView->clearSelection();
    }
    else if ( ! currentView->selectionModel()->selectedIndexes().contains( index ) )
    {
        currentView->selectionModel()->select( index, QItemSelectionModel::Select );
    }

160
    if( !popup( globalPoint ) ) THEDP->setPopupMenu();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
161 162
}

163
/*********** Popup *********/
François Cartegnie's avatar
François Cartegnie committed
164
bool StandardPLPanel::popup( const QPoint &point )
165
{
François Cartegnie's avatar
François Cartegnie committed
166
    QModelIndex index = popupIndex( currentView ); /* index for menu logic only. Do not store.*/
167
    VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
168

François Cartegnie's avatar
François Cartegnie committed
169
#define ADD_MENU_ENTRY( icon, title, act ) \
170 171
    if ( model->isSupportedAction( act, index ) )\
    {\
172 173
    action = menu.addAction( icon, title ); \
    container.action = act; \
174 175
    action->setData( QVariant::fromValue( container ) );\
    }
176 177 178 179

    /* */
    QMenu menu;
    QAction *action;
François Cartegnie's avatar
François Cartegnie committed
180
    VLCModelSubInterface::actionsContainerType container;
181 182 183

    /* Play/Stream/Info static actions */

184 185
    ADD_MENU_ENTRY( QIcon( ":/menu/play" ), qtr(I_POP_PLAY),
                    VLCModelSubInterface::ACTION_PLAY )
186

187 188
    ADD_MENU_ENTRY( QIcon( ":/menu/stream" ), qtr(I_POP_STREAM),
                    VLCModelSubInterface::ACTION_STREAM )
189

190 191
    ADD_MENU_ENTRY( QIcon(), qtr(I_POP_SAVE),
                    VLCModelSubInterface::ACTION_SAVE );
192

193 194
    ADD_MENU_ENTRY( QIcon( ":/menu/info" ), qtr(I_POP_INFO),
                    VLCModelSubInterface::ACTION_INFO );
195

196 197 198 199 200 201
    menu.addSeparator();

    ADD_MENU_ENTRY( QIcon( ":/type/folder-grey" ), qtr(I_POP_EXPLORE),
                    VLCModelSubInterface::ACTION_EXPLORE );

    QIcon addIcon( ":/buttons/playlist/playlist_add" );
202

203 204 205
    ADD_MENU_ENTRY( addIcon, qtr(I_POP_NEWFOLDER),
                    VLCModelSubInterface::ACTION_CREATENODE )

Edward Wang's avatar
Edward Wang committed
206 207 208
    ADD_MENU_ENTRY( QIcon(), qtr(I_POP_RENAMEFOLDER),
                    VLCModelSubInterface::ACTION_RENAMENODE )

209
    menu.addSeparator();
210
    /* In PL or ML, allow to add a file/folder */
211 212
    ADD_MENU_ENTRY( addIcon, qtr(I_PL_ADDF),
                    VLCModelSubInterface::ACTION_ENQUEUEFILE )
213

214 215
    ADD_MENU_ENTRY( addIcon, qtr(I_PL_ADDDIR),
                    VLCModelSubInterface::ACTION_ENQUEUEDIR )
216

217 218
    ADD_MENU_ENTRY( addIcon, qtr(I_OP_ADVOP),
                    VLCModelSubInterface::ACTION_ENQUEUEGENERIC )
219

220 221
    ADD_MENU_ENTRY( QIcon(), qtr(I_PL_ADDPL),
                    VLCModelSubInterface::ACTION_ADDTOPLAYLIST );
222

223 224 225 226
    menu.addSeparator();
    ADD_MENU_ENTRY( QIcon(), qtr( I_PL_SAVE ),
                    VLCModelSubInterface::ACTION_SAVETOPLAYLIST );

227 228 229 230
    menu.addSeparator();

    /* Item removal */

231 232 233 234 235
    ADD_MENU_ENTRY( QIcon( ":/buttons/playlist/playlist_remove" ), qtr(I_POP_DEL),
                    VLCModelSubInterface::ACTION_REMOVE );

    ADD_MENU_ENTRY( QIcon( ":/toolbar/clear" ), qtr("Clear the playlist"),
                    VLCModelSubInterface::ACTION_CLEAR );
236 237 238 239

    menu.addSeparator();

    /* Playlist sorting */
240
    if ( model->isSupportedAction( VLCModelSubInterface::ACTION_SORT, index ) )
241
    {
242 243 244 245 246 247 248 249 250 251
        QMenu *sortingMenu = new QMenu( qtr( "Sort by" ) );
        /* Choose what columns to show in sorting menu, not sure if this should be configurable*/
        QList<int> sortingColumns;
        sortingColumns << COLUMN_TITLE << COLUMN_ARTIST << COLUMN_ALBUM << COLUMN_TRACK_NUMBER << COLUMN_URI;
        container.action = VLCModelSubInterface::ACTION_SORT;
        foreach( int Column, sortingColumns )
        {
            action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Ascending") );
            container.column = model->columnFromMeta(Column) + 1;
            action->setData( QVariant::fromValue( container ) );
252

253 254 255 256 257
            action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Descending") );
            container.column = -1 * (model->columnFromMeta(Column)+1);
            action->setData( QVariant::fromValue( container ) );
        }
        menu.addMenu( sortingMenu );
258 259 260 261
    }

    /* Zoom */
    QMenu *zoomMenu = new QMenu( qtr( "Display size" ) );
262 263
    zoomMenu->addAction( qtr( "Increase" ), this, SLOT( increaseZoom() ) );
    zoomMenu->addAction( qtr( "Decrease" ), this, SLOT( decreaseZoom() ) );
264 265
    menu.addMenu( zoomMenu );

François Cartegnie's avatar
François Cartegnie committed
266
    CONNECT( &menu, triggered( QAction * ), this, popupAction( QAction * ) );
267

268 269 270 271 272 273 274 275 276 277 278
    menu.addMenu( StandardPLPanel::viewSelectionMenu( this ) );

    /* Display and forward the result */
    if( !menu.isEmpty() )
    {
        menu.exec( point ); return true;
    }
    else return false;

#undef ADD_MENU_ENTRY
}
279

François Cartegnie's avatar
François Cartegnie committed
280 281
void StandardPLPanel::popupAction( QAction *action )
{
282
    VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
283 284
    VLCModelSubInterface::actionsContainerType a =
            action->data().value<VLCModelSubInterface::actionsContainerType>();
François Cartegnie's avatar
François Cartegnie committed
285
    QModelIndexList list = currentView->selectionModel()->selectedRows();
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    QModelIndex index = popupIndex( currentView );
    char *path = NULL;
    OpenDialog *dialog;
    QString temp;
    QStringList uris;
    bool ok;

    /* first try to complete actions requiring missing parameters thru UI dialogs */
    switch( a.action )
    {
    case VLCModelSubInterface::ACTION_INFO:
        /* locally handled only */
        if( index.isValid() )
        {
            input_item_t* p_input = model->getInputItem( index );
            MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
            mid->setParent( PlaylistDialog::getInstance( p_intf ),
                            Qt::Dialog );
            mid->show();
        }
        break;

    case VLCModelSubInterface::ACTION_EXPLORE:
        /* locally handled only */
        temp = model->getURI( index );
311
        if( ! temp.isEmpty() ) path = make_path( temp.toLatin1().constData() );
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        if( path == NULL ) return;
        QDesktopServices::openUrl(
                    QUrl::fromLocalFile( QFileInfo( qfu( path ) ).absolutePath() ) );
        free( path );
        break;

    case VLCModelSubInterface::ACTION_STREAM:
        /* locally handled only */
        temp = model->getURI( index );
        if ( ! temp.isEmpty() )
            THEDP->streamingDialog( NULL, temp, false );
        break;

    case VLCModelSubInterface::ACTION_SAVE:
        /* locally handled only */
        temp = model->getURI( index );
        if ( ! temp.isEmpty() )
            THEDP->streamingDialog( NULL, temp );
        break;

    case VLCModelSubInterface::ACTION_CREATENODE:
        temp = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
            qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
            QLineEdit::Normal, QString(), &ok);
        if ( !ok ) return;
        model->createNode( index, temp );
        break;

Edward Wang's avatar
Edward Wang committed
340 341 342 343 344 345 346 347
    case VLCModelSubInterface::ACTION_RENAMENODE:
        temp = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
            qtr( I_RENAME_DIR ), qtr( I_RENAME_DIR_NAME ),
            QLineEdit::Normal, model->getTitle( index ), &ok);
        if ( !ok ) return;
        model->renameNode( index, temp );
        break;

348 349 350 351 352 353 354 355 356 357 358 359 360
    case VLCModelSubInterface::ACTION_ENQUEUEFILE:
        uris = THEDP->showSimpleOpen();
        if ( uris.isEmpty() ) return;
        uris.sort();
        foreach( const QString &file, uris )
            a.uris << qtu( toURI( toNativeSeparators( file ) ) );
        action->setData( QVariant::fromValue( a ) );
        if ( model->action( action, list ) )
            foreach( const QString &file, a.uris )
                RecentsMRL::getInstance( p_intf )->addRecent( file );
        break;

    case VLCModelSubInterface::ACTION_ENQUEUEDIR:
361
        temp = DialogsProvider::getDirectoryDialog( p_intf );
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        if ( temp.isEmpty() ) return;
        a.uris << temp;
        action->setData( QVariant::fromValue( a ) );
        model->action( action, list );
        break;

    case VLCModelSubInterface::ACTION_ENQUEUEGENERIC:
        dialog = OpenDialog::getInstance( this, p_intf, false, SELECT, true, true );
        dialog->showTab( OPEN_FILE_TAB );
        dialog->exec(); /* make it modal */
        a.uris = dialog->getMRLs( false );
        a.options = dialog->getOptions();
        if ( a.uris.isEmpty() ) return;
        action->setData( QVariant::fromValue( a ) );
        if ( model->action( action, list ) )
            foreach( const QString &file, a.uris )
                RecentsMRL::getInstance( p_intf )->addRecent( file );
        break;

381 382 383
    case VLCModelSubInterface::ACTION_SAVETOPLAYLIST:
        THEDP->savePlayingToPlaylist();
        break;
384 385 386
    default:
        model->action( action, list );
    }
François Cartegnie's avatar
François Cartegnie committed
387 388
}

389 390 391 392 393
QMenu* StandardPLPanel::viewSelectionMenu( StandardPLPanel *panel )
{
    QMenu *viewMenu = new QMenu( qtr( "Playlist View Mode" ) );
    QSignalMapper *viewSelectionMapper = new QSignalMapper( viewMenu );
    CONNECT( viewSelectionMapper, mapped( int ), panel, showView( int ) );
394

395
    QActionGroup *viewGroup = new QActionGroup( viewMenu );
396 397 398 399 400
# define MAX_VIEW StandardPLPanel::VIEW_COUNT
    for( int i = 0; i < MAX_VIEW; i++ )
    {
        QAction *action = viewMenu->addAction( viewNames[i] );
        action->setCheckable( true );
401
        viewGroup->addAction( action );
402 403
        viewSelectionMapper->setMapping( action, i );
        CONNECT( action, triggered(), viewSelectionMapper, map() );
404
        if( panel->currentViewIndex() == i )
405 406
            action->setChecked( true );
    }
407
    return viewMenu;
408 409
}

François Cartegnie's avatar
François Cartegnie committed
410 411 412 413 414 415 416 417 418
inline QModelIndex popupIndex( QAbstractItemView *view )
{
    QModelIndexList list = view->selectionModel()->selectedIndexes();
    if ( list.isEmpty() )
        return QModelIndex();
    else
        return list.first();
}

419
void StandardPLPanel::popupSelectColumn( QPoint )
420
{
421
    QMenu menu;
422
    assert( treeView );
423

424
    /* We do not offer the option to hide index 0 column, or
425 426
     * QTreeView will behave weird */
    for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
427
    {
428
        QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
429
        option->setCheckable( true );
430
        option->setChecked( !treeView->isColumnHidden( j ) );
431 432
        selectColumnsSigMapper->setMapping( option, j );
        CONNECT( option, triggered(), selectColumnsSigMapper, map() );
433
    }
434
    menu.exec( QCursor::pos() );
435 436
}

437
void StandardPLPanel::toggleColumnShown( int i )
438
{
439
    treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
440
}
441

442
/* Search in the playlist */
443
void StandardPLPanel::search( const QString& searchText )
Clément Stenac's avatar
Clément Stenac committed
444
{
445 446
    int type;
    QString name;
447 448 449 450
    bool can_search;
    p_selector->getCurrentItemInfos( &type, &can_search, &name );

    if( type != SD_TYPE || !can_search )
451
    {
452 453 454
        bool flat = ( currentView == iconView ||
                      currentView == listView ||
                      currentView == picFlowView );
François Cartegnie's avatar
François Cartegnie committed
455
        model->filter( searchText,
456 457 458
                       flat ? currentView->rootIndex() : QModelIndex(),
                       !flat );
    }
459 460
}

461
void StandardPLPanel::searchDelayed( const QString& searchText )
462 463 464
{
    int type;
    QString name;
465 466
    bool can_search;
    p_selector->getCurrentItemInfos( &type, &can_search, &name );
467

468
    if( type == SD_TYPE && can_search )
469
    {
470
        if( !name.isEmpty() && !searchText.isEmpty() )
471 472
            playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
                                              qtu( searchText ) );
473
    }
Clément Stenac's avatar
Clément Stenac committed
474 475
}

476 477
/* Set the root of the new Playlist */
/* This activated by the selector selection */
478
void StandardPLPanel::setRootItem( playlist_item_t *p_item, bool b )
479
{
480
    Q_UNUSED( b );
481
    model->rebuild( p_item );
482 483
}

484 485
void StandardPLPanel::browseInto( const QModelIndex &index )
{
486
    if( currentView == iconView || currentView == listView || currentView == picFlowView )
487
    {
488

489
        currentView->setRootIndex( index );
490 491 492

        /* When going toward root in LocationBar, scroll to the item
           that was previously as root */
François Cartegnie's avatar
François Cartegnie committed
493
        QModelIndex newIndex = model->indexByPLID(currentRootIndexPLId,0);
494 495 496 497 498 499
        while( newIndex.isValid() && (newIndex.parent() != index) )
            newIndex = newIndex.parent();
        if( newIndex.isValid() )
            currentView->scrollTo( newIndex );

        /* Store new rootindexid*/
François Cartegnie's avatar
François Cartegnie committed
500 501
        currentRootIndexPLId = model->itemId( index, PLAYLIST_ID );

502
        model->ensureArtRequested( index );
503 504
    }

505
    emit viewChanged( index );
506 507
}

508
void StandardPLPanel::browseInto()
509
{
François Cartegnie's avatar
François Cartegnie committed
510 511
    browseInto( (currentRootIndexPLId != -1 && currentView != treeView) ?
                 model->indexByPLID( currentRootIndexPLId, 0 ) :
512
                 QModelIndex() );
513 514
}

515
void StandardPLPanel::wheelEvent( QWheelEvent *e )
Clément Stenac's avatar
Clément Stenac committed
516
{
517 518 519 520 521 522 523
    if( e->modifiers() & Qt::ControlModifier ) {
        int numSteps = e->delta() / 8 / 15;
        if( numSteps > 0)
            increaseZoom();
        else if( numSteps < 0)
            decreaseZoom();
    }
524 525 526 527
    // Accept this event in order to prevent unwanted volume up/down changes
    e->accept();
}

528
bool StandardPLPanel::eventFilter ( QObject *obj, QEvent * event )
529 530
{
    if (event->type() == QEvent::KeyPress)
Clément Stenac's avatar
Clément Stenac committed
531
    {
532 533
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if( keyEvent->key() == Qt::Key_Delete ||
534
            keyEvent->key() == Qt::Key_Backspace )
535 536 537 538
        {
            deleteSelection();
            return true;
        }
Clément Stenac's avatar
Clément Stenac committed
539
    }
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    else if ( event->type() == QEvent::Paint )
    {/* Warn! Don't filter events from anything else than views ! */
        if ( model->rowCount() == 0 && p_selector->getCurrentItemCategory() == PL_ITEM_TYPE )
        {
            QWidget *viewport = qobject_cast<QWidget *>( obj );
            QStylePainter painter( viewport );
            QPixmap dropzone(":/dropzone");
            QRect rect = viewport->geometry();
            QSize size = rect.size() / 2 - dropzone.size() / 2;
            rect.adjust( 0, size.height(), 0 , 0 );
            painter.drawItemPixmap( rect, Qt::AlignHCenter, dropzone );
            /* now select the zone just below the drop zone and let Qt center
               the text by itself */
            rect.adjust( 0, dropzone.size().height() + 10, 0, 0 );
            rect.setRight( viewport->geometry().width() );
            rect.setLeft( 0 );
            painter.drawItemText( rect,
                                  Qt::AlignHCenter,
                                  palette(),
                                  true,
Christoph Miebach's avatar
Typo  
Christoph Miebach committed
560
                                  qtr("Playlist is currently empty.\n"
561
                                      "Drop a file here or select a "
Christoph Miebach's avatar
Typo  
Christoph Miebach committed
562
                                      "media source from the left."),
563 564
                                  QPalette::Text );
        }
565 566 567 568 569 570 571 572 573 574 575 576 577 578
        else if ( spinnerAnimation->state() == PixmapAnimator::Running )
        {
            if ( currentView->model()->rowCount() )
                spinnerAnimation->stop(); /* Trick until SD emits events */
            else
            {
                QWidget *viewport = qobject_cast<QWidget *>( obj );
                QStylePainter painter( viewport );
                QPixmap *spinner = spinnerAnimation->getPixmap();
                QPoint point = viewport->geometry().center();
                point -= QPoint( spinner->size().width() / 2, spinner->size().height() / 2 );
                painter.drawPixmap( point, *spinner );
            }
        }
579
    }
580
    return false;
Clément Stenac's avatar
Clément Stenac committed
581 582 583 584
}

void StandardPLPanel::deleteSelection()
{
François Cartegnie's avatar
François Cartegnie committed
585
    QModelIndexList list = currentView->selectionModel()->selectedIndexes();
Clément Stenac's avatar
Clément Stenac committed
586 587 588
    model->doDelete( list );
}

589 590 591 592 593 594
void StandardPLPanel::createIconView()
{
    iconView = new PlIconView( model, this );
    iconView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( iconView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
595 596
    CONNECT( iconView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
597
    iconView->installEventFilter( this );
598
    iconView->viewport()->installEventFilter( this );
599
    viewStack->addWidget( iconView );
600 601
}

Jakob Leben's avatar
Jakob Leben committed
602 603 604 605 606 607 608 609
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 & ) );
610
    listView->installEventFilter( this );
611
    listView->viewport()->installEventFilter( this );
612
    viewStack->addWidget( listView );
Jakob Leben's avatar
Jakob Leben committed
613 614
}

615 616 617 618 619 620 621 622 623 624 625 626
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 );
}

627 628 629
void StandardPLPanel::createTreeView()
{
    /* Create and configure the QTreeView */
630
    treeView = new PlTreeView( model, this );
631

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

634 635
    /* Connections for the TreeView */
    CONNECT( treeView, activated( const QModelIndex& ),
636
             this, activate( const QModelIndex& ) );
637 638 639 640
    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
             this, popupSelectColumn( QPoint ) );
    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
641
    treeView->installEventFilter( this );
642
    treeView->viewport()->installEventFilter( this );
643 644 645 646 647 648

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

649
    viewStack->addWidget( treeView );
650 651
}

652 653
void StandardPLPanel::updateZoom( int i )
{
654 655
    if ( i < 5 - QApplication::font().pointSize() ) return;
    if ( i > 3 + QApplication::font().pointSize() ) return;
656 657 658 659 660 661 662 663 664 665
    i_zoom = i;
#define A_ZOOM( view ) \
    if ( view ) \
    qobject_cast<AbstractPlViewItemDelegate*>( view->itemDelegate() )->setZoom( i_zoom )
    /* Can't iterate as picflow & tree aren't using custom delegate */
    A_ZOOM( iconView );
    A_ZOOM( listView );
#undef A_ZOOM
}

666
void StandardPLPanel::showView( int i_view )
667
{
668
    bool b_treeViewCreated = false;
669

670 671 672
    switch( i_view )
    {
    case ICON_VIEW:
673
    {
674
        if( iconView == NULL )
675
            createIconView();
676
        currentView = iconView;
Jakob Leben's avatar
Jakob Leben committed
677 678 679 680 681 682 683
        break;
    }
    case LIST_VIEW:
    {
        if( listView == NULL )
            createListView();
        currentView = listView;
684
        break;
685
    }
686 687 688 689 690 691 692
    case PICTUREFLOW_VIEW:
    {
        if( picFlowView == NULL )
            createCoverView();
        currentView = picFlowView;
        break;
    }
693 694 695 696
    default:
    case TREE_VIEW:
    {
        if( treeView == NULL )
697
        {
698
            createTreeView();
699 700
            b_treeViewCreated = true;
        }
701 702 703
        currentView = treeView;
        break;
    }
704
    }
705

François Cartegnie's avatar
François Cartegnie committed
706
    currentView->setModel( model );
707

708 709 710 711 712 713 714 715
    /* 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() );
716 717 718 719 720 721
            /* if there is allready stuff in playlist, we don't sort it and we reset
               sorting */
            if( model->rowCount() )
            {
                treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
            }
722 723 724 725 726 727 728 729 730 731 732 733
        }
        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 );
            }
        }
    }

734
    updateZoom( i_zoom );
735
    viewStack->setCurrentWidget( currentView );
736
    browseInto();
737
    gotoPlayingItem();
738
}
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
void StandardPLPanel::setWaiting( bool b )
{
    if ( b )
    {
        spinnerAnimation->setLoopCount( 20 ); /* Trick until SD emits an event */
        spinnerAnimation->start();
    }
    else
        spinnerAnimation->stop();
}

void StandardPLPanel::updateViewport()
{
    /* A single update on parent widget won't work */
    currentView->viewport()->repaint();
}

757
int StandardPLPanel::currentViewIndex() const
758 759 760 761 762 763 764
{
    if( currentView == treeView )
        return TREE_VIEW;
    else if( currentView == iconView )
        return ICON_VIEW;
    else if( currentView == listView )
        return LIST_VIEW;
765 766
    else
        return PICTUREFLOW_VIEW;
767 768
}

769 770 771 772 773
void StandardPLPanel::cycleViews()
{
    if( currentView == iconView )
        showView( TREE_VIEW );
    else if( currentView == treeView )
Jakob Leben's avatar
Jakob Leben committed
774 775
        showView( LIST_VIEW );
    else if( currentView == listView )
776
#ifndef NDEBUG
777 778
        showView( PICTUREFLOW_VIEW  );
    else if( currentView == picFlowView )
779
#endif
780 781 782 783 784
        showView( ICON_VIEW );
    else
        assert( 0 );
}

785 786
void StandardPLPanel::activate( const QModelIndex &index )
{
787
    if( currentView->model() == model )
788
    {
789
        /* If we are not a leaf node */
François Cartegnie's avatar
François Cartegnie committed
790
        if( !index.data( VLCModelSubInterface::IsLeafNodeRole ).toBool() )
791 792 793 794 795 796 797
        {
            if( currentView != treeView )
                browseInto( index );
        }
        else
        {
            playlist_Lock( THEPL );
François Cartegnie's avatar
François Cartegnie committed
798
            playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index, PLAYLIST_ID ) );
799 800 801 802 803
            if ( p_item )
            {
                p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
                lastActivatedPLItemId = p_item->i_id;
            }
804
            playlist_Unlock( THEPL );
805
            if ( p_item && index.isValid() )
François Cartegnie's avatar
François Cartegnie committed
806
                model->activateItem( index );
807
        }
808 809
    }
}
810

François Cartegnie's avatar
François Cartegnie committed
811
void StandardPLPanel::browseInto( int i_pl_item_id )
812
{
François Cartegnie's avatar
François Cartegnie committed
813
    if( i_pl_item_id != lastActivatedPLItemId ) return;
814

François Cartegnie's avatar
François Cartegnie committed
815
    QModelIndex index = model->indexByPLID( i_pl_item_id, 0 );
816

817
    if( currentView == treeView )
818
        treeView->setExpanded( index, true );
819 820
    else
        browseInto( index );
821

François Cartegnie's avatar
François Cartegnie committed
822
    lastActivatedPLItemId = -1;
823
}