standardpanel.cpp 11.3 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"
26
#include "components/playlist/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 140
    /* Goto */
    gotoPlayingButton = new QPushButton( qtr( "X" ), this );
    BUTTONACT( gotoPlayingButton, gotoPlayingItem() );
    buttons->addWidget( gotoPlayingButton );

141 142 143 144
    /* A Spacer and the search possibilities */
    QSpacerItem *spacer = new QSpacerItem( 10, 20 );
    buttons->addItem( spacer );

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

148
    searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
149
    searchLine->setMinimumWidth( 80 );
Clément Stenac's avatar
Clément Stenac committed
150
    CONNECT( searchLine, textChanged(QString), this, search(QString));
Clément Stenac's avatar
Clément Stenac committed
151 152
    buttons->addWidget( searchLine ); filter->setBuddy( searchLine );

153 154
    QPushButton *clear = new QPushButton;
    clear->setText( qfu( "CL") );
155
    clear->setMaximumWidth( 30 );
156
    clear->setToolTip( qtr( "Clear" ));
Clément Stenac's avatar
Clément Stenac committed
157
    BUTTONACT( clear, clearFilter() );
158
    buttons->addWidget( clear );
Clément Stenac's avatar
Clément Stenac committed
159

160
    /* Finish the layout */
161
    layout->addWidget( view );
Clément Stenac's avatar
Clément Stenac committed
162
    layout->addLayout( buttons );
Clément Stenac's avatar
Clément Stenac committed
163
//    layout->addWidget( bar );
164 165 166
    setLayout( layout );
}

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

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

201 202 203 204 205
void StandardPLPanel::gotoPlayingItem()
{
    view->scrollTo( view->currentIndex() );
}

Clément Stenac's avatar
Qt4:  
Clément Stenac committed
206 207 208
void StandardPLPanel::handleExpansion( const QModelIndex &index )
{
    if( model->isCurrent( index ) )
209
        view->scrollTo( index, QAbstractItemView::EnsureVisible );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
210 211
}

212 213 214 215 216 217 218
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 );
219
        addButton->setToolTip( qtr(I_PL_ADDPL) );
220
    }
221 222 223 224
    else if( ( THEPL->p_ml_category &&
                        currentRootId == THEPL->p_ml_category->i_id ) ||
             ( THEPL->p_ml_onelevel &&
                        currentRootId == THEPL->p_ml_onelevel->i_id ) )
225 226
    {
        addButton->setEnabled( true );
227
        addButton->setToolTip( qtr(I_PL_ADDML) );
228 229 230 231 232
    }
    else
        addButton->setEnabled( false );
}

233
/* PopupAdd Menu for the Add Menu */
234
void StandardPLPanel::popupAdd()
235
{
236
    QMenu popup;
237 238 239
    if( currentRootId == THEPL->p_local_category->i_id ||
        currentRootId == THEPL->p_local_onelevel->i_id )
    {
240 241 242
        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()) );
243
    }
244 245 246 247
    else if( ( THEPL->p_ml_category &&
                currentRootId == THEPL->p_ml_category->i_id ) ||
             ( THEPL->p_ml_onelevel &&
                currentRootId == THEPL->p_ml_onelevel->i_id ) )
248
    {
249 250 251
        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() ) );
252
    }
253 254
    popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                        + QPoint( 0, addButton->height() ) );
255 256
}

257
void StandardPLPanel::popupSelectColumn( QPoint pos )
258
{
259 260
    ContextUpdateMapper = new QSignalMapper(this);

261
    QMenu selectColMenu;
262 263

#define ADD_META_ACTION( meta ) { \
264 265 266 267 268 269 270
    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() );             \
}

271 272 273 274 275 276 277 278 279 280 281 282
    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
283

284
    selectColMenu.exec( QCursor::pos() );
285
}
286

287
/* ClearFilter LineEdit */
Clément Stenac's avatar
Clément Stenac committed
288 289 290 291 292
void StandardPLPanel::clearFilter()
{
    searchLine->setText( "" );
}

293
/* Search in the playlist */
Clément Stenac's avatar
Clément Stenac committed
294 295 296 297 298
void StandardPLPanel::search( QString searchText )
{
    model->search( searchText );
}

Clément Stenac's avatar
Clément Stenac committed
299 300
void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
{
301
    if( !index.isValid() ) return;
Clément Stenac's avatar
Clément Stenac committed
302 303 304 305 306
    QItemSelectionModel *selection = view->selectionModel();
    QModelIndexList list = selection->selectedIndexes();
    model->popup( index, point, list );
}

307 308
/* Set the root of the new Playlist */
/* This activated by the selector selection */
309 310
void StandardPLPanel::setRoot( int i_root_id )
{
311 312
    playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
                                                    VLC_TRUE );
313
    assert( p_item );
314 315
    p_item = playlist_GetPreferredNode( THEPL, p_item );
    assert( p_item );
Clément Stenac's avatar
Clément Stenac committed
316
    model->rebuild( p_item );
317 318
}

Clément Stenac's avatar
Clément Stenac committed
319 320 321 322 323
void StandardPLPanel::removeItem( int i_id )
{
    model->removeItem( i_id );
}

324 325
/* Delete and Suppr key remove the selection 
   FilterKey function and code function */
Clément Stenac's avatar
Clément Stenac committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
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 );
}

344 345
StandardPLPanel::~StandardPLPanel()
{}