standardpanel.cpp 10.9 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * standardpanel.cpp : The "standard" playlist panel : just a treeview
 ****************************************************************************
 * Copyright (C) 2000-2005 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * 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.
 *****************************************************************************/

Clément Stenac's avatar
Clément Stenac committed
24
#include "qt4.hpp"
25
#include "dialogs_provider.hpp"
Clément Stenac's avatar
Clément Stenac committed
26
#include "playlist_model.hpp"
27
#include "components/playlist/panels.hpp"
Clément Stenac's avatar
Clément Stenac committed
28 29
#include "util/customwidgets.hpp"

30 31
#include <vlc_intf_strings.h>

Clément Stenac's avatar
Clément Stenac committed
32
#include <QTreeView>
Clément Stenac's avatar
Clément Stenac committed
33 34
#include <QPushButton>
#include <QHBoxLayout>
35 36
#include <QVBoxLayout>
#include <QHeaderView>
Clément Stenac's avatar
Clément Stenac committed
37 38
#include <QKeyEvent>
#include <QModelIndexList>
Clément Stenac's avatar
Clément Stenac committed
39 40 41
#include <QToolBar>
#include <QLabel>
#include <QSpacerItem>
42
#include <QMenu>
43
#include <QSignalMapper>
Clément Stenac's avatar
Clément Stenac committed
44 45

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

47
StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
48
                                  intf_thread_t *_p_intf,
Clément Stenac's avatar
Clément Stenac committed
49
                                  playlist_t *p_playlist,
Clément Stenac's avatar
Clément Stenac committed
50
                                  playlist_item_t *p_root ):
51 52
                                  PLPanel( _parent, _p_intf )
{
53
    model = new PLModel( p_playlist, p_intf, p_root, -1, this );
54 55 56 57

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

58
    /* Create and configure the QTreeView */
Clément Stenac's avatar
Clément Stenac committed
59
    view = new QVLCTreeView( 0 );
Clément Stenac's avatar
Clément Stenac committed
60
    view->setModel(model);
61
    view->setIconSize( QSize( 20, 20 ) );
62
    view->setAlternatingRowColors( true );
63
    view->setAnimated( true );
64
    view->setSortingEnabled( true );
Clément Stenac's avatar
Clément Stenac committed
65
    view->setSelectionMode( QAbstractItemView::ExtendedSelection );
66 67 68 69
    view->setDragEnabled( true );
    view->setAcceptDrops( true );
    view->setDropIndicatorShown( true );
    view->setAutoScroll( true );
70

71 72
    /* Configure the size of the header */
    view->header()->resizeSection( 0, 200 );
73
    view->header()->resizeSection( 1, 80 );
74 75
    view->header()->setSortIndicatorShown( true );
    view->header()->setClickable( true );
76
    view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
77

78
    /* Connections for the TreeView */
Clément Stenac's avatar
Clément Stenac committed
79 80 81 82 83 84
    CONNECT( view, activated( const QModelIndex& ) ,
             model,activateItem( const QModelIndex& ) );
    CONNECT( view, rightClicked( QModelIndex , QPoint ),
             this, doPopup( QModelIndex, QPoint ) );
    CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
             this, handleExpansion( const QModelIndex& ) );
85
    CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
86
             this, popupSelectColumn( QPoint ) );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
87

88
    currentRootId = -1;
89
    CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
Clément Stenac's avatar
Clément Stenac committed
90

91
    /* Buttons configuration */
92 93
    QHBoxLayout *buttons = new QHBoxLayout;

94 95 96 97
    /* Add item to the playlist button */
    addButton = new QPushButton;
    addButton->setIcon( QIcon( ":/pixmaps/playlist_add.png" ) );
    addButton->setMaximumWidth( 30 );
98
    BUTTONACT( addButton, popupAdd() );
99 100
    buttons->addWidget( addButton );

101
    /* Random 2-state button */
102
    randomButton = new QPushButton( this );
103 104 105 106 107 108 109 110 111 112
    if( model->hasRandom() )
    {
        randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_on.png" ));
        randomButton->setToolTip( qtr( I_PL_RANDOM ));
    }
    else
    {
         randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_off.png" ) );
         randomButton->setToolTip( qtr( I_PL_NORANDOM ));
    }
113 114 115
    BUTTONACT( randomButton, toggleRandom() );
    buttons->addWidget( randomButton );

116
    /* Repeat 3-state button */
117
    repeatButton = new QPushButton( this );
118
    if( model->hasRepeat() )
119 120 121 122 123 124 125 126
    {
        repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
        repeatButton->setToolTip( qtr( I_PL_REPEAT ));
    }
    else if( model->hasLoop() )
    {
        repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
        repeatButton->setToolTip( qtr( I_PL_LOOP ));
127
    }
128 129 130 131
    else
    {
        repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
        repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
132
    }
Clément Stenac's avatar
Clément Stenac committed
133
    BUTTONACT( repeatButton, toggleRepeat() );
134
    buttons->addWidget( repeatButton );
Clément Stenac's avatar
Clément Stenac committed
135

136 137 138 139
    /* A Spacer and the search possibilities */
    QSpacerItem *spacer = new QSpacerItem( 10, 20 );
    buttons->addItem( spacer );

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

143
    searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
144
    searchLine->setMinimumWidth( 80 );
Clément Stenac's avatar
Clément Stenac committed
145
    CONNECT( searchLine, textChanged(QString), this, search(QString));
Clément Stenac's avatar
Clément Stenac committed
146 147
    buttons->addWidget( searchLine ); filter->setBuddy( searchLine );

148 149
    QPushButton *clear = new QPushButton;
    clear->setText( qfu( "CL") );
150
    clear->setMaximumWidth( 30 );
151
    clear->setToolTip( qtr( "Clear" ));
Clément Stenac's avatar
Clément Stenac committed
152
    BUTTONACT( clear, clearFilter() );
153
    buttons->addWidget( clear );
Clément Stenac's avatar
Clément Stenac committed
154

155
    /* Finish the layout */
156
    layout->addWidget( view );
Clément Stenac's avatar
Clément Stenac committed
157
    layout->addLayout( buttons );
Clément Stenac's avatar
Clément Stenac committed
158
//    layout->addWidget( bar );
159 160 161
    setLayout( layout );
}

162
/* Function to toggle between the Repeat states */
Clément Stenac's avatar
Clément Stenac committed
163 164 165 166 167
void StandardPLPanel::toggleRepeat()
{
    if( model->hasRepeat() )
    {
        model->setRepeat( false ); model->setLoop( true );
168
        repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
169
        repeatButton->setToolTip( qtr( I_PL_LOOP ));
Clément Stenac's avatar
Clément Stenac committed
170 171 172 173
    }
    else if( model->hasLoop() )
    {
        model->setRepeat( false ) ; model->setLoop( false );
174
        repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
175
        repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
Clément Stenac's avatar
Clément Stenac committed
176 177 178 179
    }
    else
    {
        model->setRepeat( true );
180
        repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
181
        repeatButton->setToolTip( qtr( I_PL_REPEAT ));
Clément Stenac's avatar
Clément Stenac committed
182 183 184
    }
}

185
/* Function to toggle between the Random states */
Clément Stenac's avatar
Clément Stenac committed
186 187 188 189
void StandardPLPanel::toggleRandom()
{
    bool prev = model->hasRandom();
    model->setRandom( !prev );
190
    randomButton->setIcon( prev ?
191 192
                QIcon( ":/pixmaps/playlist_shuffle_off.png" ) :
                QIcon( ":/pixmaps/playlist_shuffle_on.png" ) );
193
    randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
Clément Stenac's avatar
Clément Stenac committed
194 195
}

Clément Stenac's avatar
Qt4:  
Clément Stenac committed
196 197 198
void StandardPLPanel::handleExpansion( const QModelIndex &index )
{
    if( model->isCurrent( index ) )
199
        view->scrollTo( index, QAbstractItemView::EnsureVisible );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
200 201
}

202 203 204 205 206 207 208
void StandardPLPanel::setCurrentRootId( int _new )
{
    currentRootId = _new;
    if( currentRootId == THEPL->p_local_category->i_id ||
        currentRootId == THEPL->p_local_onelevel->i_id  )
    {
        addButton->setEnabled( true );
209
        addButton->setToolTip( qtr(I_PL_ADDPL) );
210
    }
211 212 213 214
    else if( ( THEPL->p_ml_category &&
                        currentRootId == THEPL->p_ml_category->i_id ) ||
             ( THEPL->p_ml_onelevel &&
                        currentRootId == THEPL->p_ml_onelevel->i_id ) )
215 216
    {
        addButton->setEnabled( true );
217
        addButton->setToolTip( qtr(I_PL_ADDML) );
218 219 220 221 222
    }
    else
        addButton->setEnabled( false );
}

223
/* PopupAdd Menu for the Add Menu */
224
void StandardPLPanel::popupAdd()
225
{
226
    QMenu popup;
227 228 229
    if( currentRootId == THEPL->p_local_category->i_id ||
        currentRootId == THEPL->p_local_onelevel->i_id )
    {
230 231 232
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
        popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
233
    }
234 235 236 237
    else if( ( THEPL->p_ml_category &&
                currentRootId == THEPL->p_ml_category->i_id ) ||
             ( THEPL->p_ml_onelevel &&
                currentRootId == THEPL->p_ml_onelevel->i_id ) )
238
    {
239 240 241
        popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
        popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
        popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
242
    }
243 244
    popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                        + QPoint( 0, addButton->height() ) );
245 246
}

247
void StandardPLPanel::popupSelectColumn( QPoint pos )
248
{
249 250
    ContextUpdateMapper = new QSignalMapper(this);

251
    QMenu selectColMenu;
252 253

#define ADD_META_ACTION( meta ) { \
254 255 256 257 258 259 260
    QAction* option = selectColMenu.addAction( qfu(VLC_META_##meta) );      \
    option->setCheckable( true );                                           \
    option->setChecked( model->shownFlags() & VLC_META_ENGINE_##meta );     \
    ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta );      \
    CONNECT( option, triggered(), ContextUpdateMapper, map() );             \
}

261 262 263 264 265 266 267 268 269 270 271 272
    CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );

    ADD_META_ACTION( TITLE );
    ADD_META_ACTION( ARTIST );
    ADD_META_ACTION( DURATION );
    ADD_META_ACTION( COLLECTION );
    ADD_META_ACTION( GENRE );
    ADD_META_ACTION( SEQ_NUM );
    ADD_META_ACTION( RATING );
    ADD_META_ACTION( DESCRIPTION );

#undef ADD_META_ACTION
273

274
    selectColMenu.exec( QCursor::pos() );
275
}
276

277
/* ClearFilter LineEdit */
Clément Stenac's avatar
Clément Stenac committed
278 279 280 281 282
void StandardPLPanel::clearFilter()
{
    searchLine->setText( "" );
}

283
/* Search in the playlist */
Clément Stenac's avatar
Clément Stenac committed
284 285 286 287 288
void StandardPLPanel::search( QString searchText )
{
    model->search( searchText );
}

Clément Stenac's avatar
Clément Stenac committed
289 290
void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
{
291
    if( !index.isValid() ) return;
Clément Stenac's avatar
Clément Stenac committed
292 293 294 295 296
    QItemSelectionModel *selection = view->selectionModel();
    QModelIndexList list = selection->selectedIndexes();
    model->popup( index, point, list );
}

297 298
void StandardPLPanel::setRoot( int i_root_id )
{
299 300
    playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
                                                    VLC_TRUE );
301
    assert( p_item );
302 303
    p_item = playlist_GetPreferredNode( THEPL, p_item );
    assert( p_item );
Clément Stenac's avatar
Clément Stenac committed
304
    model->rebuild( p_item );
305 306
}

Clément Stenac's avatar
Clément Stenac committed
307 308 309 310 311
void StandardPLPanel::removeItem( int i_id )
{
    model->removeItem( i_id );
}

312 313
/* Delete and Suppr key remove the selection 
   FilterKey function and code function */
Clément Stenac's avatar
Clément Stenac committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
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 );
}

332 333
StandardPLPanel::~StandardPLPanel()
{}