]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qmenuview.cpp
Qt: Avoid using dangling pointers and fix memleaks
[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  * This is a simple view, like a QListView, but displayed as a QMenu
35  * So far, this is quite limited.
36  *
37  * This is now linked to VLC's models. It should be splittable in the future.
38  *
39  * TODO: - limit the width of the entries
40  *       - deal with a root item
41  ***/
42
43 Q_DECLARE_METATYPE(QPersistentModelIndex); // So we can store it in a QVariant
44
45 QMenuView::QMenuView( QWidget * parent, int _iMaxVisibleCount )
46           : QMenu( parent ), iMaxVisibleCount( _iMaxVisibleCount )
47 {
48     m_model = NULL;
49
50     /* Rebuild the Menu just before showing it */
51     CONNECT( this, aboutToShow(), this, rebuild() );
52
53     /* */
54     CONNECT( this, triggered(QAction*), this, activate(QAction*) );
55 }
56
57 /* */
58 void QMenuView::rebuild()
59 {
60     if( !m_model )
61         return;
62
63     /* Clear all Items */
64     clear();
65
66     /* Rebuild from root */
67     build( QModelIndex() );
68
69     if( isEmpty() )
70         addAction( qtr( "Empty" ) )->setDisabled( true );
71 }
72
73 /* */
74 void QMenuView::build( const QModelIndex &parent )
75 {
76     int i_count = iMaxVisibleCount == 0 ? m_model->rowCount( parent )
77                                          : __MIN( iMaxVisibleCount, m_model->rowCount( parent ) );
78     for( int i = 0; i < i_count; 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( QPersistentModelIndex( 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<QPersistentModelIndex>() )
122     {
123         emit activated( variant.value<QPersistentModelIndex>());
124     }
125 }
126