]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qmenuview.cpp
Qt: first attempt at a QMenuView
[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
72 /* */
73 void QMenuView::build( const QModelIndex &parent )
74 {
75     for( int i = 0; i < m_model->rowCount( parent ); i++ )
76     {
77         QModelIndex idx = m_model->index(i, 0, parent);
78         if( m_model->hasChildren( idx ) )
79         {
80             build( idx );
81         }
82         else
83         {
84             addAction( createActionFromIndex( idx ) );
85         }
86     }
87 }
88
89 /* Create individual actions */
90 QAction* QMenuView::createActionFromIndex( QModelIndex index )
91 {
92     QIcon icon = qvariant_cast<QIcon>( index.data( Qt::DecorationRole ) );
93     QAction * action = new QAction( icon, index.data().toString(), this );
94
95     /* Display in bold the active element */
96     if( index.data( VLCModel::IsCurrentRole ).toBool() )
97     {
98         QFont font; font.setBold ( true );
99         action->setFont( font );
100     }
101
102     /* Some items could be hypothetically disabled */
103     action->setEnabled( index.flags().testFlag( Qt::ItemIsEnabled ) );
104
105     /* */
106     QVariant variant; variant.setValue( index );
107     action->setData( variant );
108
109     return action;
110 }
111
112 /* QMenu action trigger */
113 void QMenuView::activate( QAction* action )
114 {
115     assert( m_model );
116
117     QVariant variant = action->data();
118     if( variant.canConvert<QModelIndex>() )
119     {
120         emit activated( variant.value<QModelIndex>());
121     }
122 }
123