standardpanel.cpp 11.5 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 35
#include "util/customwidgets.hpp"

36 37
#include <vlc_intf_strings.h>

Clément Stenac's avatar
Clément Stenac committed
38
#include <QPushButton>
39
#include <QHeaderView>
Clément Stenac's avatar
Clément Stenac committed
40 41
#include <QKeyEvent>
#include <QModelIndexList>
Clément Stenac's avatar
Clément Stenac committed
42
#include <QLabel>
43
#include <QMenu>
44
#include <QSignalMapper>
45
#include <QWheelEvent>
46

Clément Stenac's avatar
Clément Stenac committed
47
#include <assert.h>
Clément Stenac's avatar
Clément Stenac committed
48

Rafaël Carré's avatar
Rafaël Carré committed
49 50
#include "sorting.h"

51
StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
52
                                  intf_thread_t *_p_intf,
Clément Stenac's avatar
Clément Stenac committed
53
                                  playlist_t *p_playlist,
Clément Stenac's avatar
Clément Stenac committed
54
                                  playlist_item_t *p_root ):
55
                                  QWidget( _parent ), p_intf( _p_intf )
56
{
57
    layout = new QGridLayout( this );
58
    layout->setSpacing( 0 ); layout->setMargin( 0 );
59
    setMinimumWidth( 300 );
60

61
    model = new PLModel( p_playlist, p_intf, p_root, this );
62 63
    CONNECT( model, currentChanged( const QModelIndex& ),
             this, handleExpansion( const QModelIndex& ) );
64

65
    iconView = NULL;
66
    treeView = NULL;
67

68
    currentRootId = -1;
Clément Stenac's avatar
Clément Stenac committed
69

70 71 72 73 74 75 76 77 78
    /* Title label */
    title = new QLabel;
    QFont titleFont;
    titleFont.setPointSize( titleFont.pointSize() + 6 );
    titleFont.setFamily( "Verdana" );
    title->setFont( titleFont );
    layout->addWidget( title, 0, 0 );

    /* A Spacer and the search possibilities */
79
    layout->setColumnStretch( 1, 10 );
80 81

    SearchLineEdit *search = new SearchLineEdit( this );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
82
    search->setMaximumWidth( 300 );
83
    layout->addWidget( search, 0, 4 );
84 85
    CONNECT( search, textChanged( const QString& ),
             this, search( const QString& ) );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
86
    layout->setColumnStretch( 4, 2 );
87

88 89
    /* Add item to the playlist button */
    addButton = new QPushButton;
90
    addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
91
    addButton->setMaximumWidth( 30 );
92
    BUTTONACT( addButton, popupAdd() );
93
    layout->addWidget( addButton, 0, 3 );
94

95 96 97 98
    QPushButton *viewButton = new QPushButton( this );
    viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
    layout->addWidget( viewButton, 0, 2 );
    BUTTONACT( viewButton, toggleView() );
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

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

    int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
    if( i_viewMode == ICON_VIEW )
    {
        createIconView();
        currentView = iconView;
    }
    else
    {
        createTreeView();
        currentView = treeView;
    }

    getSettings()->endGroup();
116 117
}

118 119 120
StandardPLPanel::~StandardPLPanel()
{
    getSettings()->beginGroup("Playlist");
121 122
    if( treeView )
        getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
123
    getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
124 125 126
    getSettings()->endGroup();
}

127
/* Unused anymore, but might be useful, like in right-click menu */
128 129
void StandardPLPanel::gotoPlayingItem()
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
130
    currentView->scrollTo( model->currentIndex() );
131 132
}

133
void StandardPLPanel::handleExpansion( const QModelIndex& index )
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
134
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
135
    assert( currentView );
136
    currentView->scrollTo( index );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
137 138
}

139
/* PopupAdd Menu for the Add Menu */
140
void StandardPLPanel::popupAdd()
141
{
142
    QMenu popup;
143 144 145
    if( currentRootId == THEPL->p_local_category->i_id ||
        currentRootId == THEPL->p_local_onelevel->i_id )
    {
146
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
147
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
148
        popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
149
    }
150 151 152 153
    else if( ( THEPL->p_ml_category &&
                currentRootId == THEPL->p_ml_category->i_id ) ||
             ( THEPL->p_ml_onelevel &&
                currentRootId == THEPL->p_ml_onelevel->i_id ) )
154
    {
155
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
156
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
157
        popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
158
    }
159

160 161
    popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                        + QPoint( 0, addButton->height() ) );
162 163
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
164 165 166 167 168 169 170 171 172
void StandardPLPanel::popupPlView( const QPoint &point )
{
    QModelIndex index = currentView->indexAt( point );
    QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
    QItemSelectionModel *selection = currentView->selectionModel();
    QModelIndexList list = selection->selectedIndexes();
    model->popup( index, globalPoint, list );
}

173
void StandardPLPanel::popupSelectColumn( QPoint pos )
174
{
175
    QMenu menu;
176
    assert( treeView );
177

178 179
    /* We do not offer the option to hide index 0 column, or
    * QTreeView will behave weird */
180
    int i, j;
181
    for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
182
    {
183 184 185
        QAction* option = menu.addAction(
            qfu( psz_column_title( i ) ) );
        option->setCheckable( true );
186
        option->setChecked( !treeView->isColumnHidden( j ) );
187 188
        selectColumnsSigMapper->setMapping( option, j );
        CONNECT( option, triggered(), selectColumnsSigMapper, map() );
189
    }
190
    menu.exec( QCursor::pos() );
191 192
}

193
void StandardPLPanel::toggleColumnShown( int i )
194
{
195
    treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
196
}
197

198
/* Search in the playlist */
199
void StandardPLPanel::search( const QString& searchText )
Clément Stenac's avatar
Clément Stenac committed
200 201 202 203
{
    model->search( searchText );
}

204 205
/* Set the root of the new Playlist */
/* This activated by the selector selection */
206 207 208
void StandardPLPanel::setRoot( playlist_item_t *p_item )
{
    QPL_LOCK;
209
    assert( p_item );
210

211 212
    playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
    if( p_pref_item ) p_item = p_pref_item;
213 214 215 216 217 218

    /* needed for popupAdd() */
    currentRootId = p_item->i_id;

    /* cosmetics, ..still need playlist locking.. */
    char *psz_title = input_item_GetName( p_item->p_input );
219
    title->setText( qfu(psz_title) );
220 221
    free( psz_title );

222 223
    QPL_UNLOCK;

224
    /* do THE job */
Clément Stenac's avatar
Clément Stenac committed
225
    model->rebuild( p_item );
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    /* enable/disable adding */
    if( p_item == THEPL->p_local_category ||
        p_item == THEPL->p_local_onelevel )
    {
        addButton->setEnabled( true );
        addButton->setToolTip( qtr(I_PL_ADDPL) );
    }
    else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
              ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
    {
        addButton->setEnabled( true );
        addButton->setToolTip( qtr(I_PL_ADDML) );
    }
    else
        addButton->setEnabled( false );
242 243
}

Clément Stenac's avatar
Clément Stenac committed
244 245 246 247 248
void StandardPLPanel::removeItem( int i_id )
{
    model->removeItem( i_id );
}

249
/* Delete and Suppr key remove the selection
250
   FilterKey function and code function */
Clément Stenac's avatar
Clément Stenac committed
251 252 253 254 255 256 257 258 259 260 261 262 263
void StandardPLPanel::keyPressEvent( QKeyEvent *e )
{
    switch( e->key() )
    {
    case Qt::Key_Back:
    case Qt::Key_Delete:
        deleteSelection();
        break;
    }
}

void StandardPLPanel::deleteSelection()
{
264
    QItemSelectionModel *selection = currentView->selectionModel();
Clément Stenac's avatar
Clément Stenac committed
265 266 267 268
    QModelIndexList list = selection->selectedIndexes();
    model->doDelete( list );
}

269 270 271 272 273 274
void StandardPLPanel::createIconView()
{
    iconView = new PlIconView( model, this );
    iconView->setContextMenuPolicy( Qt::CustomContextMenu );
    CONNECT( iconView, customContextMenuRequested( const QPoint & ),
             this, popupPlView( const QPoint & ) );
275 276
    CONNECT( iconView, activated( const QModelIndex & ),
             this, activate( const QModelIndex & ) );
277 278 279 280 281 282 283 284 285 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 311 312

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

void StandardPLPanel::createTreeView()
{
    /* Create and configure the QTreeView */
    treeView = new QTreeView;
    treeView->setModel( model );

    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 );

    if( getSettings()->contains( "headerStateV2" ) )
    {
        treeView->header()->restoreState(
                getSettings()->value( "headerStateV2" ).toByteArray() );
    }
    else
    {
        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
        {
313
            treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
314 315 316 317 318 319 320
            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& ),
321
             this, activate( const QModelIndex& ) );
322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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 );
}

336 337
void StandardPLPanel::toggleView()
{
338
    if( treeView && treeView->isVisible() )
339
    {
340
        if( iconView == NULL )
341 342
            createIconView();

343 344
        treeView->hide();
        iconView->show();
345
        currentView = iconView;
346 347 348
    }
    else
    {
349 350 351
        if( treeView == NULL )
            createTreeView();

352 353
        iconView->hide();
        treeView->show();
354
        currentView = treeView;
355
    }
356
}
357

358 359 360 361 362
void StandardPLPanel::wheelEvent( QWheelEvent *e )
{
    // Accept this event in order to prevent unwanted volume up/down changes
    e->accept();
}
363 364 365 366 367 368 369 370 371 372 373 374 375

void StandardPLPanel::activate( const QModelIndex &index )
{
    if( model->hasChildren( index ) && currentView == iconView )
    {
        iconView->setRootIndex( index );
        title->setText( index.data().toString() );
    }
    else
    {
        model->activateItem( index );
    }
}