]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
* Sorting
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "playlist_model.hpp"
25 #include "components/playlist/panels.hpp"
26 #include <QTreeView>
27 #include <QPushButton>
28 #include <QHBoxLayout>
29 #include <QVBoxLayout>
30 #include <QHeaderView>
31 #include <QKeyEvent>
32 #include "qt4.hpp"
33 #include <assert.h>
34 #include <QModelIndexList>
35
36 #include "util/views.hpp"
37
38 StandardPLPanel::StandardPLPanel( QWidget *_parent, intf_thread_t *_p_intf,
39                                   playlist_t *p_playlist,
40                                   playlist_item_t *p_root ):
41                                   PLPanel( _parent, _p_intf )
42 {
43     model = new PLModel( p_playlist, p_root, -1, this );
44     view = new QVLCTreeView( 0 );
45     view->setModel(model);
46     view->setIconSize( QSize(20,20) );
47     view->setAlternatingRowColors( true );
48     view->header()->resizeSection( 0, 300 );
49     view->header()->setSortIndicatorShown( true );
50     view->header()->setClickable( true );
51     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
52
53 //    connect( view->header(), SIGNAL( sectionClicked( int)), view, SLOT( sortByColumn(int)));
54
55     connect( view, SIGNAL( activated( const QModelIndex& ) ), model,
56              SLOT( activateItem( const QModelIndex& ) ) );
57
58     connect( view, SIGNAL( rightClicked( QModelIndex , QPoint ) ),
59              this, SLOT( doPopup( QModelIndex, QPoint ) ) );
60
61     connect( model,
62              SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
63              this, SLOT( handleExpansion( const QModelIndex& ) ) );
64
65     QVBoxLayout *layout = new QVBoxLayout();
66     layout->setSpacing( 0 ); layout->setMargin( 0 );
67
68     QHBoxLayout *buttons = new QHBoxLayout();
69     repeatButton = new QPushButton( this );
70     buttons->addWidget( repeatButton );
71     randomButton = new QPushButton( this );
72     buttons->addWidget( randomButton );
73
74     if( model->hasRandom() ) randomButton->setText( qtr( "Random" ) );
75     else randomButton->setText( qtr( "No random" ) );
76
77     if( model->hasRepeat() ) repeatButton->setText( qtr( "Repeat One" ) );
78     else if( model->hasLoop() ) repeatButton->setText( qtr( "Repeat All" ) );
79     else repeatButton->setText( qtr( "No Repeat" ) );
80
81     connect( repeatButton, SIGNAL( clicked() ), this, SLOT( toggleRepeat() ));
82     connect( randomButton, SIGNAL( clicked() ), this, SLOT( toggleRandom() ));
83
84     layout->addWidget( view );
85     layout->addLayout( buttons );
86     setLayout( layout );
87 }
88
89 void StandardPLPanel::toggleRepeat()
90 {
91     if( model->hasRepeat() )
92     {
93         model->setRepeat( false ); model->setLoop( true );
94         repeatButton->setText( qtr( "Repeat All" ) );
95     }
96     else if( model->hasLoop() )
97     {
98         model->setRepeat( false ) ; model->setLoop( false );
99         repeatButton->setText( qtr( "No Repeat" ) );
100     }
101     else
102     {
103         model->setRepeat( true );
104         repeatButton->setText( qtr( "Repeat One" ) );
105     }
106 }
107
108 void StandardPLPanel::toggleRandom()
109 {
110     bool prev = model->hasRandom();
111     model->setRandom( !prev );
112     randomButton->setText( prev ? qtr( "No Random" ) : qtr( "Random" ) );
113 }
114
115 void StandardPLPanel::handleExpansion( const QModelIndex &index )
116 {
117     QModelIndex parent;
118     if( model->isCurrent( index ) )
119     {
120         parent = index;
121         while( parent.isValid() )
122         {
123             view->setExpanded( parent, true );
124             parent = model->parent( parent );
125         }
126     }
127 }
128
129 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
130 {
131     assert( index.isValid() );
132     QItemSelectionModel *selection = view->selectionModel();
133     QModelIndexList list = selection->selectedIndexes();
134     model->popup( index, point, list );
135 }
136
137 void StandardPLPanel::setRoot( int i_root_id )
138 {
139     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
140     assert( p_item );
141     p_item = playlist_GetPreferredNode( THEPL, p_item );
142     assert( p_item );
143     model->rebuild( p_item );
144 }
145
146 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
147 {
148     switch( e->key() )
149     {
150     case Qt::Key_Back:
151     case Qt::Key_Delete:
152         deleteSelection();
153         break;
154     }
155 }
156
157 void StandardPLPanel::deleteSelection()
158 {
159     QItemSelectionModel *selection = view->selectionModel();
160     QModelIndexList list = selection->selectedIndexes();
161     model->doDelete( list );
162 }
163
164 StandardPLPanel::~StandardPLPanel()
165 {}