]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Some more cleanup and macros
[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 "qt4.hpp"
25 #include "playlist_model.hpp"
26 #include "components/playlist/panels.hpp"
27 #include "util/customwidgets.hpp"
28
29 #include <QTreeView>
30 #include <QPushButton>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
33 #include <QHeaderView>
34 #include <QKeyEvent>
35 #include <QModelIndexList>
36 #include <QToolBar>
37 #include <QLabel>
38 #include <QSpacerItem>
39
40 #include <assert.h>
41
42 StandardPLPanel::StandardPLPanel( QWidget *_parent, intf_thread_t *_p_intf,
43                                   playlist_t *p_playlist,
44                                   playlist_item_t *p_root ):
45                                   PLPanel( _parent, _p_intf )
46 {
47     model = new PLModel( p_playlist, p_root, -1, this );
48     view = new QVLCTreeView( 0 );
49     view->setModel(model);
50     view->setIconSize( QSize(20,20) );
51     view->setAlternatingRowColors( true );
52     view->header()->resizeSection( 0, 230 );
53     view->header()->setSortIndicatorShown( true );
54     view->header()->setClickable( true );
55     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
56
57     CONNECT( view, activated( const QModelIndex& ) ,
58              model,activateItem( const QModelIndex& ) );
59     CONNECT( view, rightClicked( QModelIndex , QPoint ),
60              this, doPopup( QModelIndex, QPoint ) );
61     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
62              this, handleExpansion( const QModelIndex& ) );
63
64     QVBoxLayout *layout = new QVBoxLayout();
65     layout->setSpacing( 0 ); layout->setMargin( 0 );
66
67     QHBoxLayout *buttons = new QHBoxLayout();
68
69     repeatButton = new QPushButton( 0 ); buttons->addWidget( repeatButton );
70     if( model->hasRepeat() ) repeatButton->setText( qtr( "Repeat One" ) );
71     else if( model->hasLoop() ) repeatButton->setText( qtr( "Repeat All" ) );
72     else repeatButton->setText( qtr( "No Repeat" ) );
73     BUTTONACT( repeatButton, toggleRepeat() );
74
75     randomButton = new QPushButton( 0 ); buttons->addWidget( randomButton );
76     if( model->hasRandom() ) randomButton->setText( qtr( "Random" ) );
77     else randomButton->setText( qtr( "No random" ) );
78     BUTTONACT( randomButton, toggleRandom() );
79
80     QSpacerItem *spacer = new QSpacerItem( 10, 20 );buttons->addItem( spacer );
81
82     QLabel *filter = new QLabel( qfu( "&Search:" ) + " " );
83     buttons->addWidget( filter );
84     searchLine = new  ClickLineEdit( qfu( "Playlist filter" ), 0 );
85     CONNECT( searchLine, textChanged(QString), this, search(QString));
86     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
87
88     QPushButton *clear = new QPushButton( qfu( "CL") );
89     buttons->addWidget( clear );
90     BUTTONACT( clear, clearFilter() );
91
92     layout->addWidget( view );
93     layout->addLayout( buttons );
94 //    layout->addWidget( bar );
95     setLayout( layout );
96 }
97
98 void StandardPLPanel::toggleRepeat()
99 {
100     if( model->hasRepeat() )
101     {
102         model->setRepeat( false ); model->setLoop( true );
103         repeatButton->setText( qtr( "Repeat All" ) );
104     }
105     else if( model->hasLoop() )
106     {
107         model->setRepeat( false ) ; model->setLoop( false );
108         repeatButton->setText( qtr( "No Repeat" ) );
109     }
110     else
111     {
112         model->setRepeat( true );
113         repeatButton->setText( qtr( "Repeat One" ) );
114     }
115 }
116
117 void StandardPLPanel::toggleRandom()
118 {
119     bool prev = model->hasRandom();
120     model->setRandom( !prev );
121     randomButton->setText( prev ? qtr( "No Random" ) : qtr( "Random" ) );
122 }
123
124 void StandardPLPanel::handleExpansion( const QModelIndex &index )
125 {
126     QModelIndex parent;
127     if( model->isCurrent( index ) )
128     {
129         parent = index;
130         while( parent.isValid() )
131         {
132             view->setExpanded( parent, true );
133             parent = model->parent( parent );
134         }
135     }
136 }
137
138 void StandardPLPanel::clearFilter()
139 {
140     searchLine->setText( "" );
141 }
142
143 void StandardPLPanel::search( QString searchText )
144 {
145     model->search( searchText );
146 }
147
148 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
149 {
150     if( !index.isValid() ) return;
151     QItemSelectionModel *selection = view->selectionModel();
152     QModelIndexList list = selection->selectedIndexes();
153     model->popup( index, point, list );
154 }
155
156 void StandardPLPanel::setRoot( int i_root_id )
157 {
158     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
159     assert( p_item );
160     p_item = playlist_GetPreferredNode( THEPL, p_item );
161     assert( p_item );
162     model->rebuild( p_item );
163 }
164
165 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
166 {
167     switch( e->key() )
168     {
169     case Qt::Key_Back:
170     case Qt::Key_Delete:
171         deleteSelection();
172         break;
173     }
174 }
175
176 void StandardPLPanel::deleteSelection()
177 {
178     QItemSelectionModel *selection = view->selectionModel();
179     QModelIndexList list = selection->selectedIndexes();
180     model->doDelete( list );
181 }
182
183 StandardPLPanel::~StandardPLPanel()
184 {}