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