]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Put the dock/undock back in the menu
[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 "dialogs_provider.hpp"
26 #include "playlist_model.hpp"
27 #include "components/playlist/panels.hpp"
28 #include "util/customwidgets.hpp"
29
30 #include <QTreeView>
31 #include <QPushButton>
32 #include <QHBoxLayout>
33 #include <QVBoxLayout>
34 #include <QHeaderView>
35 #include <QKeyEvent>
36 #include <QModelIndexList>
37 #include <QToolBar>
38 #include <QLabel>
39 #include <QSpacerItem>
40 #include <QMenu>
41
42 #include <assert.h>
43
44 StandardPLPanel::StandardPLPanel( BasePlaylistWidget *_parent,
45                                   intf_thread_t *_p_intf,
46                                   playlist_t *p_playlist,
47                                   playlist_item_t *p_root ):
48                                   PLPanel( _parent, _p_intf )
49 {
50     model = new PLModel( p_playlist, p_root, -1, this );
51     view = new QVLCTreeView( 0 );
52     view->setModel(model);
53     view->setIconSize( QSize(20,20) );
54     view->setAlternatingRowColors( true );
55     view->header()->resizeSection( 0, 230 );
56     view->header()->resizeSection( 2, 60 );
57     view->header()->setSortIndicatorShown( true );
58     view->header()->setClickable( true );
59     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
60
61     CONNECT( view, activated( const QModelIndex& ) ,
62              model,activateItem( const QModelIndex& ) );
63     CONNECT( view, rightClicked( QModelIndex , QPoint ),
64              this, doPopup( QModelIndex, QPoint ) );
65     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
66              this, handleExpansion( const QModelIndex& ) );
67
68     currentRootId = -1;
69     CONNECT( parent, rootChanged(int), this, setCurrentRootId( int ) );
70
71     QVBoxLayout *layout = new QVBoxLayout();
72     layout->setSpacing( 0 ); layout->setMargin( 0 );
73
74     QHBoxLayout *buttons = new QHBoxLayout();
75
76     addButton = new QPushButton( "+", this );
77     addButton->setMaximumWidth( 25 );
78     BUTTONACT( addButton, add() );
79     buttons->addWidget( addButton );
80
81     repeatButton = new QPushButton( 0 ); buttons->addWidget( repeatButton );
82     if( model->hasRepeat() ) repeatButton->setText( qtr( "R1" ) );
83     else if( model->hasLoop() ) repeatButton->setText( qtr( "RA" ) );
84     else repeatButton->setText( qtr( "NR" ) );
85     BUTTONACT( repeatButton, toggleRepeat() );
86
87     randomButton = new QPushButton( 0 ); buttons->addWidget( randomButton );
88     if( model->hasRandom() ) randomButton->setText( qtr( " RND" ) );
89     else randomButton->setText( qtr( "NRND" ) );
90     BUTTONACT( randomButton, toggleRandom() );
91
92     QSpacerItem *spacer = new QSpacerItem( 10, 20 );buttons->addItem( spacer );
93
94     QLabel *filter = new QLabel( qfu( "&Search:" ) + " " );
95     buttons->addWidget( filter );
96     searchLine = new  ClickLineEdit( qfu( "Playlist filter" ), 0 );
97     CONNECT( searchLine, textChanged(QString), this, search(QString));
98     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
99
100     QPushButton *clear = new QPushButton( qfu( "CL") );
101     clear->setMaximumWidth( 30 );
102     buttons->addWidget( clear );
103     BUTTONACT( clear, clearFilter() );
104
105     layout->addWidget( view );
106     layout->addLayout( buttons );
107 //    layout->addWidget( bar );
108     setLayout( layout );
109 }
110
111 void StandardPLPanel::toggleRepeat()
112 {
113     if( model->hasRepeat() )
114     {
115         model->setRepeat( false ); model->setLoop( true );
116         repeatButton->setText( qtr( "Repeat All" ) );
117     }
118     else if( model->hasLoop() )
119     {
120         model->setRepeat( false ) ; model->setLoop( false );
121         repeatButton->setText( qtr( "No Repeat" ) );
122     }
123     else
124     {
125         model->setRepeat( true );
126         repeatButton->setText( qtr( "Repeat One" ) );
127     }
128 }
129
130 void StandardPLPanel::toggleRandom()
131 {
132     bool prev = model->hasRandom();
133     model->setRandom( !prev );
134     randomButton->setText( prev ? qtr( "No Random" ) : qtr( "Random" ) );
135 }
136
137 void StandardPLPanel::handleExpansion( const QModelIndex &index )
138 {
139     QModelIndex parent;
140     if( model->isCurrent( index ) )
141     {
142         parent = index;
143         while( parent.isValid() )
144         {
145             view->setExpanded( parent, true );
146             parent = model->parent( parent );
147         }
148     }
149 }
150
151 void StandardPLPanel::setCurrentRootId( int _new )
152 {
153     currentRootId = _new;
154     if( currentRootId == THEPL->p_local_category->i_id ||
155         currentRootId == THEPL->p_local_onelevel->i_id  )
156     {
157         addButton->setEnabled( true );
158         addButton->setToolTip( qtr("Add to playlist" ) );
159     }
160     else if( currentRootId == THEPL->p_ml_category->i_id ||
161              currentRootId == THEPL->p_ml_onelevel->i_id )
162     {
163         addButton->setEnabled( true );
164         addButton->setToolTip( qtr("Add to media library" ) );
165     }
166     else
167         addButton->setEnabled( false );
168 }
169
170 void StandardPLPanel::add()
171 {
172     QMenu *popup = new QMenu();
173     if( currentRootId == THEPL->p_local_category->i_id ||
174         currentRootId == THEPL->p_local_onelevel->i_id )
175     {
176         popup->addAction( "Add file", THEDP, SLOT( simplePLAppendDialog() ) );
177         popup->addAction( "Advanced add", THEDP, SLOT( PLAppendDialog() ) );
178     }
179     else if( currentRootId == THEPL->p_ml_category->i_id ||
180              currentRootId == THEPL->p_ml_onelevel->i_id )
181     {
182         popup->addAction( "Add file", THEDP, SLOT( simpleMLAppendDialog() ) );
183         popup->addAction( "Advanced add", THEDP, SLOT( MLAppendDialog() ) );
184         popup->addAction( "Directory", THEDP, SLOT( openMLDirectory() ) );
185     }
186     popup->popup( QCursor::pos() );
187 }
188
189 void StandardPLPanel::clearFilter()
190 {
191     searchLine->setText( "" );
192 }
193
194 void StandardPLPanel::search( QString searchText )
195 {
196     model->search( searchText );
197 }
198
199 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
200 {
201     if( !index.isValid() ) return;
202     QItemSelectionModel *selection = view->selectionModel();
203     QModelIndexList list = selection->selectedIndexes();
204     model->popup( index, point, list );
205 }
206
207 void StandardPLPanel::setRoot( int i_root_id )
208 {
209     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
210     assert( p_item );
211     p_item = playlist_GetPreferredNode( THEPL, p_item );
212     assert( p_item );
213     model->rebuild( p_item );
214 }
215
216 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
217 {
218     switch( e->key() )
219     {
220     case Qt::Key_Back:
221     case Qt::Key_Delete:
222         deleteSelection();
223         break;
224     }
225 }
226
227 void StandardPLPanel::deleteSelection()
228 {
229     QItemSelectionModel *selection = view->selectionModel();
230     QModelIndexList list = selection->selectedIndexes();
231     model->doDelete( list );
232 }
233
234 StandardPLPanel::~StandardPLPanel()
235 {}