]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: playlist, set a minimum width
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2009 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          JB Kempf
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "qt4.hpp"
30 #include "dialogs_provider.hpp"
31
32 #include "components/playlist/playlist_model.hpp"
33 #include "components/playlist/standardpanel.hpp"
34 #include "util/customwidgets.hpp"
35
36 #include <vlc_intf_strings.h>
37
38 #include <QPushButton>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
41 #include <QHeaderView>
42 #include <QKeyEvent>
43 #include <QModelIndexList>
44 #include <QLabel>
45 #include <QSpacerItem>
46 #include <QMenu>
47 #include <QSignalMapper>
48 #include <assert.h>
49
50 #include "sorting.h"
51
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53                                   intf_thread_t *_p_intf,
54                                   playlist_t *p_playlist,
55                                   playlist_item_t *p_root ):
56                                   QWidget( _parent ), p_intf( _p_intf )
57 {
58     QGridLayout *layout = new QGridLayout( this );
59     layout->setSpacing( 0 ); layout->setMargin( 0 );
60     setMinimumWidth( 300 );
61
62     model = new PLModel( p_playlist, p_intf, p_root, this );
63
64     /* Create and configure the QTreeView */
65     view = new QVLCTreeView;
66     view->setModel( model );
67
68     view->setIconSize( QSize( 20, 20 ) );
69     view->setAlternatingRowColors( true );
70     view->setAnimated( true );
71     view->setUniformRowHeights( true );
72     view->setSortingEnabled( true );
73     view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
74     view->header()->setSortIndicatorShown( true );
75     view->header()->setClickable( true );
76     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
77
78     view->setSelectionBehavior( QAbstractItemView::SelectRows );
79     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
80     view->setDragEnabled( true );
81     view->setAcceptDrops( true );
82     view->setDropIndicatorShown( true );
83
84     /* Saved Settings */
85     getSettings()->beginGroup("Playlist");
86     if( getSettings()->contains( "headerStateV2" ) )
87     {
88         view->header()->restoreState(
89                 getSettings()->value( "headerStateV2" ).toByteArray() );
90     }
91     else
92     {
93         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
94         {
95             view->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
96             if( m == COLUMN_TITLE ) view->header()->resizeSection( c, 200 );
97             else if( m == COLUMN_DURATION ) view->header()->resizeSection( c, 80 );
98         }
99     }
100     getSettings()->endGroup();
101
102     /* Connections for the TreeView */
103     CONNECT( view, activated( const QModelIndex& ) ,
104              model,activateItem( const QModelIndex& ) );
105     CONNECT( view, rightClicked( QModelIndex , QPoint ),
106              this, doPopup( QModelIndex, QPoint ) );
107     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
108              this, popupSelectColumn( QPoint ) );
109     CONNECT( model, currentChanged( const QModelIndex& ),
110              this, handleExpansion( const QModelIndex& ) );
111
112     currentRootId = -1;
113
114     /* Title label */
115     title = new QLabel;
116     QFont titleFont;
117     titleFont.setPointSize( titleFont.pointSize() + 6 );
118     titleFont.setFamily( "Verdana" );
119     title->setFont( titleFont );
120     layout->addWidget( title, 0, 0 );
121
122     /* A Spacer and the search possibilities */
123     layout->setColumnStretch( 1, 10 );
124
125     SearchLineEdit *search = new SearchLineEdit( this );
126     search->setMaximumWidth( 160 );
127     layout->addWidget( search, 0, 4 );
128     CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
129
130     /* Add item to the playlist button */
131     addButton = new QPushButton;
132     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
133     addButton->setMaximumWidth( 30 );
134     BUTTONACT( addButton, popupAdd() );
135     layout->addWidget( addButton, 0, 3 );
136
137     /* Finish the layout */
138     layout->addWidget( view, 1, 0, 1, -1 );
139
140     selectColumnsSigMapper = new QSignalMapper( this );
141     CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
142 }
143
144 StandardPLPanel::~StandardPLPanel()
145 {
146     getSettings()->beginGroup("Playlist");
147     getSettings()->setValue( "headerStateV2", view->header()->saveState() );
148     getSettings()->endGroup();
149 }
150
151 /* Unused anymore, but might be useful, like in right-click menu */
152 void StandardPLPanel::gotoPlayingItem()
153 {
154     view->scrollTo( model->currentIndex() );
155 }
156
157 void StandardPLPanel::handleExpansion( const QModelIndex& index )
158 {
159     view->scrollTo( index );
160 }
161
162 /* PopupAdd Menu for the Add Menu */
163 void StandardPLPanel::popupAdd()
164 {
165     QMenu popup;
166     if( currentRootId == THEPL->p_local_category->i_id ||
167         currentRootId == THEPL->p_local_onelevel->i_id )
168     {
169         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
170         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
171         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
172     }
173     else if( ( THEPL->p_ml_category &&
174                 currentRootId == THEPL->p_ml_category->i_id ) ||
175              ( THEPL->p_ml_onelevel &&
176                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
177     {
178         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
179         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
180         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
181     }
182
183     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
184                         + QPoint( 0, addButton->height() ) );
185 }
186
187 void StandardPLPanel::popupSelectColumn( QPoint pos )
188 {
189     QMenu menu;
190
191     /* We do not offer the option to hide index 0 column, or
192     * QTreeView will behave weird */
193     int i, j;
194     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
195     {
196         QAction* option = menu.addAction(
197             qfu( psz_column_title( i ) ) );
198         option->setCheckable( true );
199         option->setChecked( !view->isColumnHidden( j ) );
200         selectColumnsSigMapper->setMapping( option, j );
201         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
202     }
203     menu.exec( QCursor::pos() );
204 }
205
206 void StandardPLPanel::toggleColumnShown( int i )
207 {
208     view->setColumnHidden( i, !view->isColumnHidden( i ) );
209 }
210
211 /* Search in the playlist */
212 void StandardPLPanel::search( const QString& searchText )
213 {
214     model->search( searchText );
215 }
216
217 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
218 {
219     QItemSelectionModel *selection = view->selectionModel();
220     QModelIndexList list = selection->selectedIndexes();
221     model->popup( index, point, list );
222 }
223
224 /* Set the root of the new Playlist */
225 /* This activated by the selector selection */
226 void StandardPLPanel::setRoot( playlist_item_t *p_item )
227 {
228     QPL_LOCK;
229     assert( p_item );
230
231     playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
232     if( p_pref_item ) p_item = p_pref_item;
233
234     /* needed for popupAdd() */
235     currentRootId = p_item->i_id;
236
237     /* cosmetics, ..still need playlist locking.. */
238     char *psz_title = input_item_GetName( p_item->p_input );
239     title->setText( qfu(psz_title) );
240     free( psz_title );
241
242     QPL_UNLOCK;
243
244     /* do THE job */
245     model->rebuild( p_item );
246
247     /* enable/disable adding */
248     if( p_item == THEPL->p_local_category ||
249         p_item == THEPL->p_local_onelevel )
250     {
251         addButton->setEnabled( true );
252         addButton->setToolTip( qtr(I_PL_ADDPL) );
253     }
254     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
255               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
256     {
257         addButton->setEnabled( true );
258         addButton->setToolTip( qtr(I_PL_ADDML) );
259     }
260     else
261         addButton->setEnabled( false );
262 }
263
264 void StandardPLPanel::removeItem( int i_id )
265 {
266     model->removeItem( i_id );
267 }
268
269 /* Delete and Suppr key remove the selection
270    FilterKey function and code function */
271 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
272 {
273     switch( e->key() )
274     {
275     case Qt::Key_Back:
276     case Qt::Key_Delete:
277         deleteSelection();
278         break;
279     }
280 }
281
282 void StandardPLPanel::deleteSelection()
283 {
284     QItemSelectionModel *selection = view->selectionModel();
285     QModelIndexList list = selection->selectedIndexes();
286     model->doDelete( list );
287 }
288