standardpanel.cpp 9.52 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
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

Clément Stenac's avatar
Clément Stenac committed
29
#include "qt4.hpp"
30
#include "dialogs_provider.hpp"
31

32
#include "components/playlist/playlist_model.hpp"
33
#include "components/playlist/panels.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 39
#include <QPushButton>
#include <QHBoxLayout>
40 41
#include <QVBoxLayout>
#include <QHeaderView>
Clément Stenac's avatar
Clément Stenac committed
42 43
#include <QKeyEvent>
#include <QModelIndexList>
Clément Stenac's avatar
Clément Stenac committed
44 45
#include <QLabel>
#include <QSpacerItem>
46
#include <QMenu>
47
#include <QSignalMapper>
Clément Stenac's avatar
Clément Stenac committed
48
#include <assert.h>
Clément Stenac's avatar
Clément Stenac committed
49

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

52
StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53
                                  intf_thread_t *_p_intf,
Clément Stenac's avatar
Clément Stenac committed
54
                                  playlist_t *p_playlist,
Clément Stenac's avatar
Clément Stenac committed
55
                                  playlist_item_t *p_root ):
56
                                  QWidget( _parent ), p_intf( _p_intf )
57
{
58
    model = new PLModel( p_playlist, p_intf, p_root, this );
59 60 61 62

    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing( 0 ); layout->setMargin( 0 );

63
    /* Create and configure the QTreeView */
64
    view = new QVLCTreeView;
Lukas Durfina's avatar
Lukas Durfina committed
65
    view->setModel( model );
66
    view->setIconSize( QSize( 20, 20 ) );
67
    view->setAlternatingRowColors( true );
68
    view->setAnimated( true );
69
    view->setSelectionBehavior( QAbstractItemView::SelectRows );
Clément Stenac's avatar
Clément Stenac committed
70
    view->setSelectionMode( QAbstractItemView::ExtendedSelection );
71 72 73
    view->setDragEnabled( true );
    view->setAcceptDrops( true );
    view->setDropIndicatorShown( true );
74
    view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
75
    view->setUniformRowHeights( true );
76
    view->setSortingEnabled( true );
77

78 79

    getSettings()->beginGroup("Playlist");
80
    if( getSettings()->contains( "headerStateV2" ) )
81
    {
82
        view->header()->restoreState(
83
                getSettings()->value( "headerStateV2" ).toByteArray() );
84 85 86
    }
    else
    {
87 88 89 90 91 92 93
        int m, c;
        for( m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
        {
            view->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
            if( m == COLUMN_TITLE ) view->header()->resizeSection( c, 200 );
            else if( m == COLUMN_DURATION ) view->header()->resizeSection( c, 80 );
        }
94
    }
95
    view->header()->setSortIndicatorShown( true );
96 97
    view->header()->setClickable( true );
    view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
98
    getSettings()->endGroup();
99

100
    /* Connections for the TreeView */
Clément Stenac's avatar
Clément Stenac committed
101 102 103 104
    CONNECT( view, activated( const QModelIndex& ) ,
             model,activateItem( const QModelIndex& ) );
    CONNECT( view, rightClicked( QModelIndex , QPoint ),
             this, doPopup( QModelIndex, QPoint ) );
105
    CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
106
             this, popupSelectColumn( QPoint ) );
107 108
    CONNECT( model, currentChanged( const QModelIndex& ),
             this, handleExpansion( const QModelIndex& ) );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
109

110
    currentRootId = -1;
Clément Stenac's avatar
Clément Stenac committed
111

112
    /* Buttons configuration */
113 114
    QHBoxLayout *buttons = new QHBoxLayout;

115 116
    /* Add item to the playlist button */
    addButton = new QPushButton;
117
    addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
118
    addButton->setMaximumWidth( 30 );
119
    BUTTONACT( addButton, popupAdd() );
120 121
    buttons->addWidget( addButton );

122
    /* Goto */
123
    gotoPlayingButton = new QPushButton;
124
    BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
125
            qtr( "Show the current item" ), gotoPlayingItem() );
126 127
    buttons->addWidget( gotoPlayingButton );

128 129 130 131
    /* A Spacer and the search possibilities */
    QSpacerItem *spacer = new QSpacerItem( 10, 20 );
    buttons->addItem( spacer );

132
    QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
Clément Stenac's avatar
Clément Stenac committed
133
    buttons->addWidget( filter );
134

135 136 137
    SearchLineEdit *search = new SearchLineEdit( this );
    buttons->addWidget( search );
    filter->setBuddy( search );
138
    CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
Clément Stenac's avatar
Clément Stenac committed
139

140 141 142 143 144 145 146
    /* Title label */
    title = new QLabel;
    QFont titleFont;
    titleFont.setPointSize( titleFont.pointSize() + 6 );
    titleFont.setFamily( "Verdana" );
    title->setFont( titleFont );

147
    /* Finish the layout */
148
    layout->addWidget( title );
149
    layout->addWidget( view );
Clément Stenac's avatar
Clément Stenac committed
150
    layout->addLayout( buttons );
Clément Stenac's avatar
Clément Stenac committed
151
//    layout->addWidget( bar );
152
    setLayout( layout );
153 154 155

    selectColumnsSigMapper = new QSignalMapper( this );
    CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
156 157
}

158 159
void StandardPLPanel::gotoPlayingItem()
{
160
    view->scrollTo( model->currentIndex() );
161 162
}

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

168
/* PopupAdd Menu for the Add Menu */
169
void StandardPLPanel::popupAdd()
170
{
171
    QMenu popup;
172 173 174
    if( currentRootId == THEPL->p_local_category->i_id ||
        currentRootId == THEPL->p_local_onelevel->i_id )
    {
175
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
176
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
177
        popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
178
    }
179 180 181 182
    else if( ( THEPL->p_ml_category &&
                currentRootId == THEPL->p_ml_category->i_id ) ||
             ( THEPL->p_ml_onelevel &&
                currentRootId == THEPL->p_ml_onelevel->i_id ) )
183
    {
184
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
185
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
186
        popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
187
    }
188

189 190
    popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                        + QPoint( 0, addButton->height() ) );
191 192
}

193
void StandardPLPanel::popupSelectColumn( QPoint pos )
194
{
195
    QMenu menu;
196

197 198
    /* We do not offer the option to hide index 0 column, or
    * QTreeView will behave weird */
199
    int i, j;
200
    for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
201
    {
202 203 204 205 206 207
        QAction* option = menu.addAction(
            qfu( psz_column_title( i ) ) );
        option->setCheckable( true );
        option->setChecked( !view->isColumnHidden( j ) );
        selectColumnsSigMapper->setMapping( option, j );
        CONNECT( option, triggered(), selectColumnsSigMapper, map() );
208
    }
209
    menu.exec( QCursor::pos() );
210 211
}

212
void StandardPLPanel::toggleColumnShown( int i )
213
{
214
    view->setColumnHidden( i, !view->isColumnHidden( i ) );
215
}
216

217
/* Search in the playlist */
218
void StandardPLPanel::search( const QString& searchText )
Clément Stenac's avatar
Clément Stenac committed
219 220 221 222
{
    model->search( searchText );
}

Clément Stenac's avatar
Clément Stenac committed
223 224 225 226 227 228 229
void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
{
    QItemSelectionModel *selection = view->selectionModel();
    QModelIndexList list = selection->selectedIndexes();
    model->popup( index, point, list );
}

230 231
/* Set the root of the new Playlist */
/* This activated by the selector selection */
232 233 234
void StandardPLPanel::setRoot( playlist_item_t *p_item )
{
    QPL_LOCK;
235
    assert( p_item );
236

237 238
    playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
    if( p_pref_item ) p_item = p_pref_item;
239 240 241 242 243 244

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

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

248 249
    QPL_UNLOCK;

250
    /* do THE job */
Clément Stenac's avatar
Clément Stenac committed
251
    model->rebuild( p_item );
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

    /* 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 );
268 269
}

Clément Stenac's avatar
Clément Stenac committed
270 271 272 273 274
void StandardPLPanel::removeItem( int i_id )
{
    model->removeItem( i_id );
}

275
/* Delete and Suppr key remove the selection
276
   FilterKey function and code function */
Clément Stenac's avatar
Clément Stenac committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
void StandardPLPanel::keyPressEvent( QKeyEvent *e )
{
    switch( e->key() )
    {
    case Qt::Key_Back:
    case Qt::Key_Delete:
        deleteSelection();
        break;
    }
}

void StandardPLPanel::deleteSelection()
{
    QItemSelectionModel *selection = view->selectionModel();
    QModelIndexList list = selection->selectedIndexes();
    model->doDelete( list );
}

295
StandardPLPanel::~StandardPLPanel()
296
{
297
    getSettings()->beginGroup("Playlist");
298
    getSettings()->setValue( "headerStateV2", view->header()->saveState() );
299 300 301
    getSettings()->endGroup();
}