standardpanel.cpp 9.83 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
/*****************************************************************************
 * standardpanel.cpp : The "standard" playlist panel : just a treeview
 ****************************************************************************
4
 * Copyright (C) 2000-2009 VideoLAN
5
 * $Id$
zorglub's avatar
zorglub committed
6
7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          JB Kempf <jb@videolan.org>
zorglub's avatar
zorglub committed
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
zorglub's avatar
zorglub committed
28

zorglub's avatar
zorglub committed
29
#include "qt4.hpp"
zorglub's avatar
zorglub committed
30
#include "dialogs_provider.hpp"
31

32
#include "components/playlist/playlist_model.hpp"
33
#include "components/playlist/standardpanel.hpp"
zorglub's avatar
zorglub committed
34
35
#include "util/customwidgets.hpp"

zorglub's avatar
zorglub committed
36
37
#include <vlc_intf_strings.h>

zorglub's avatar
zorglub committed
38
#include <QPushButton>
zorglub's avatar
zorglub committed
39
#include <QHeaderView>
zorglub's avatar
zorglub committed
40
41
#include <QKeyEvent>
#include <QModelIndexList>
zorglub's avatar
zorglub committed
42
#include <QLabel>
zorglub's avatar
zorglub committed
43
#include <QMenu>
44
#include <QSignalMapper>
45

zorglub's avatar
zorglub committed
46
#include <assert.h>
zorglub's avatar
zorglub committed
47

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

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

60
61
    model = new PLModel( p_playlist, p_intf, p_root, this );

62
    /* Create and configure the QTreeView */
63
    view = new QVLCTreeView;
lukas.durfina's avatar
lukas.durfina committed
64
    view->setModel( model );
65
    view2 = NULL;
66

67
    view->setIconSize( QSize( 20, 20 ) );
zorglub's avatar
zorglub committed
68
    view->setAlternatingRowColors( true );
69
    view->setAnimated( true );
70
71
72
73
74
75
76
    view->setUniformRowHeights( true );
    view->setSortingEnabled( true );
    view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
    view->header()->setSortIndicatorShown( true );
    view->header()->setClickable( true );
    view->header()->setContextMenuPolicy( Qt::CustomContextMenu );

77
    view->setSelectionBehavior( QAbstractItemView::SelectRows );
zorglub's avatar
zorglub committed
78
    view->setSelectionMode( QAbstractItemView::ExtendedSelection );
79
80
81
    view->setDragEnabled( true );
    view->setAcceptDrops( true );
    view->setDropIndicatorShown( true );
82

83
    /* Saved Settings */
84
    getSettings()->beginGroup("Playlist");
85
    if( getSettings()->contains( "headerStateV2" ) )
86
    {
87
        view->header()->restoreState(
88
                getSettings()->value( "headerStateV2" ).toByteArray() );
89
90
91
    }
    else
    {
92
        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
93
94
95
96
97
        {
            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 );
        }
98
    }
99
    getSettings()->endGroup();
100

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

zorglub's avatar
zorglub committed
111
    currentRootId = -1;
zorglub's avatar
zorglub committed
112

113
114
115
116
117
118
119
120
121
    /* 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 */
122
    layout->setColumnStretch( 1, 10 );
123
124

    SearchLineEdit *search = new SearchLineEdit( this );
125
    search->setMaximumWidth( 200 );
126
    layout->addWidget( search, 0, 4 );
127
128
129
    CONNECT( search, textChanged( const QString& ),
             this, search( const QString& ) );
    layout->setColumnStretch( 4, 1 );
130

131
132
    /* Add item to the playlist button */
    addButton = new QPushButton;
133
    addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
134
    addButton->setMaximumWidth( 30 );
135
    BUTTONACT( addButton, popupAdd() );
136
    layout->addWidget( addButton, 0, 3 );
zorglub's avatar
zorglub committed
137

138
139
140
141
142
    QPushButton *viewButton = new QPushButton( this );
    viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
    layout->addWidget( viewButton, 0, 2 );
    BUTTONACT( viewButton, toggleView() );

143
    /* Finish the layout */
144
    layout->addWidget( view, 1, 0, 1, -1 );
145
146

    selectColumnsSigMapper = new QSignalMapper( this );
147
148
    CONNECT( selectColumnsSigMapper, mapped( int ),
             this, toggleColumnShown( int ) );
zorglub's avatar
zorglub committed
149
150
}

151
152
153
154
155
156
157
StandardPLPanel::~StandardPLPanel()
{
    getSettings()->beginGroup("Playlist");
    getSettings()->setValue( "headerStateV2", view->header()->saveState() );
    getSettings()->endGroup();
}

158
/* Unused anymore, but might be useful, like in right-click menu */
159
160
void StandardPLPanel::gotoPlayingItem()
{
161
    view->scrollTo( model->currentIndex() );
162
163
}

164
void StandardPLPanel::handleExpansion( const QModelIndex& index )
zorglub's avatar
Qt4:    
zorglub committed
165
{
166
    view->scrollTo( index );
zorglub's avatar
Qt4:    
zorglub committed
167
168
}

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

190
191
    popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                        + QPoint( 0, addButton->height() ) );
zorglub's avatar
zorglub committed
192
193
}

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

198
199
    /* We do not offer the option to hide index 0 column, or
    * QTreeView will behave weird */
200
    int i, j;
201
    for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
202
    {
203
204
205
206
207
208
        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() );
209
    }
210
    menu.exec( QCursor::pos() );
211
212
}

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

218
/* Search in the playlist */
ivoire's avatar
ivoire committed
219
void StandardPLPanel::search( const QString& searchText )
zorglub's avatar
zorglub committed
220
221
222
223
{
    model->search( searchText );
}

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

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

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

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

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

249
250
    QPL_UNLOCK;

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

    /* 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 );
zorglub's avatar
zorglub committed
269
270
}

zorglub's avatar
zorglub committed
271
272
273
274
275
void StandardPLPanel::removeItem( int i_id )
{
    model->removeItem( i_id );
}

276
/* Delete and Suppr key remove the selection
277
   FilterKey function and code function */
zorglub's avatar
zorglub committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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 );
}

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
void StandardPLPanel::toggleView()
{
    if( view && view->isVisible() )
    {
        if( view2 == NULL )
        {
            view2 = new QListView;
            view2->setModel( model );
            view2->setViewMode( QListView::IconMode );
            view2->setMovement( QListView::Snap );
            layout->addWidget( view2, 1, 0, 1, -1 );
        }
        view->hide();
        view2->show();
    }
    else
    {
        view2->hide();
        view->show();
    }


}