1 /*****************************************************************************
3 ****************************************************************************
4 * Copyright © 2011 VLC authors and VideoLAN
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
25 #include "util/qmenuview.hpp"
27 #include "components/playlist/vlc_model.hpp" /* data( IsCurrentRole ) */
35 * This is a simple view, like a QListView, but displayed as a QMenu
36 * So far, this is quite limited.
38 * This is now linked to VLC's models. It should be splittable in the future.
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
45 Q_DECLARE_METATYPE(QModelIndex); // So we can store it in a QVariant
47 QMenuView::QMenuView( QWidget * parent )
52 /* Rebuild the Menu just before showing it */
53 CONNECT( this, aboutToShow(), this, rebuild() );
56 CONNECT( this, triggered(QAction*), this, activate(QAction*) );
60 void QMenuView::rebuild()
68 /* Rebuild from root */
69 build( QModelIndex() );
72 addAction( qtr( "Empty" ) )->setDisabled( true );
76 void QMenuView::build( const QModelIndex &parent )
78 for( int i = 0; i < m_model->rowCount( parent ); i++ )
80 QModelIndex idx = m_model->index(i, 0, parent);
81 if( m_model->hasChildren( idx ) )
87 addAction( createActionFromIndex( idx ) );
92 /* Create individual actions */
93 QAction* QMenuView::createActionFromIndex( QModelIndex index )
95 QIcon icon = qvariant_cast<QIcon>( index.data( Qt::DecorationRole ) );
96 QAction * action = new QAction( icon, index.data().toString(), this );
98 /* Display in bold the active element */
99 if( index.data( VLCModel::IsCurrentRole ).toBool() )
101 QFont font; font.setBold ( true );
102 action->setFont( font );
105 /* Some items could be hypothetically disabled */
106 action->setEnabled( index.flags().testFlag( Qt::ItemIsEnabled ) );
109 QVariant variant; variant.setValue( index );
110 action->setData( variant );
115 /* QMenu action trigger */
116 void QMenuView::activate( QAction* action )
120 QVariant variant = action->data();
121 if( variant.canConvert<QModelIndex>() )
123 emit activated( variant.value<QModelIndex>());