]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qmenuview.cpp
Qt: avoid a crash on displaying the chapters for some mp4
[vlc] / modules / gui / qt4 / util / qmenuview.cpp
1 /*****************************************************************************
2  * QMenuView
3  ****************************************************************************
4  * Copyright © 2011 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb@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 "util/qmenuview.hpp"
26
27 #include "components/playlist/vlc_model.hpp" /* data( IsCurrentRole ) */
28
29 #include <QFont>
30 #include <QVariant>
31 #include <assert.h>
32
33
34 /***
35  * This is a simple view, like a QListView, but displayed as a QMenu
36  * So far, this is quite limited.
37  *
38  * This is now linked to VLC's models. It should be splittable in the future.
39  *
40  * TODO: - limit the number of the menu, as a Q_PROPERTY
41  *       - limit the width of the entries
42  *       - deal with a root item
43  ***/
44
45 Q_DECLARE_METATYPE(QModelIndex); // So we can store it in a QVariant
46
47 QMenuView::QMenuView( QWidget * parent )
48           : QMenu( parent )
49 {
50     m_model = NULL;
51
52     /* Rebuild the Menu just before showing it */
53     CONNECT( this, aboutToShow(), this, rebuild() );
54
55     /* */
56     CONNECT( this, triggered(QAction*), this, activate(QAction*) );
57 }
58
59 /* */
60 void QMenuView::rebuild()
61 {
62     if( !m_model )
63         return;
64
65     /* Clear all Items */
66     clear();
67
68     /* Rebuild from root */
69     build( QModelIndex() );
70
71     if( isEmpty() )
72         addAction( qtr( "Empty" ) )->setDisabled( true );
73 }
74
75 /* */
76 void QMenuView::build( const QModelIndex &parent )
77 {
78     for( int i = 0; i < m_model->rowCount( parent ); i++ )
79     {
80         QModelIndex idx = m_model->index(i, 0, parent);
81         if( m_model->hasChildren( idx ) )
82         {
83             build( idx );
84         }
85         else
86         {
87             addAction( createActionFromIndex( idx ) );
88         }
89     }
90 }
91
92 /* Create individual actions */
93 QAction* QMenuView::createActionFromIndex( QModelIndex index )
94 {
95     QIcon icon = qvariant_cast<QIcon>( index.data( Qt::DecorationRole ) );
96     QAction * action = new QAction( icon, index.data().toString(), this );
97
98     /* Display in bold the active element */
99     if( index.data( VLCModel::IsCurrentRole ).toBool() )
100     {
101         QFont font; font.setBold ( true );
102         action->setFont( font );
103     }
104
105     /* Some items could be hypothetically disabled */
106     action->setEnabled( index.flags().testFlag( Qt::ItemIsEnabled ) );
107
108     /* */
109     QVariant variant; variant.setValue( index );
110     action->setData( variant );
111
112     return action;
113 }
114
115 /* QMenu action trigger */
116 void QMenuView::activate( QAction* action )
117 {
118     assert( m_model );
119
120     QVariant variant = action->data();
121     if( variant.canConvert<QModelIndex>() )
122     {
123         emit activated( variant.value<QModelIndex>());
124     }
125 }
126