1 /*****************************************************************************
2 * vlc_model.cpp : base for playlist and ml model
3 ****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team and AUTHORS
7 * Authors: Srikanth Raju <srikiraju#gmail#com>
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 *****************************************************************************/
24 #include "vlc_model.hpp"
25 #include "input_manager.hpp" /* THEMIM */
26 #include "pixmaps/types/type_unknown.xpm"
28 VLCModelSubInterface::VLCModelSubInterface()
32 VLCModelSubInterface::~VLCModelSubInterface()
36 int VLCModelSubInterface::columnFromMeta( int meta_col )
38 int meta = 1, column = 0;
40 while( meta != meta_col && meta != COLUMN_END )
49 VLCModel::VLCModel( intf_thread_t *_p_intf, QObject *parent )
50 : QAbstractItemModel( parent ), VLCModelSubInterface(), p_intf(_p_intf)
52 /* Icons initialization */
53 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
54 ADD_ICON( UNKNOWN , QPixmap( type_unknown_xpm ) );
55 ADD_ICON( FILE, ":/type/file" );
56 ADD_ICON( DIRECTORY, ":/type/directory" );
57 ADD_ICON( DISC, ":/type/disc" );
58 ADD_ICON( CARD, ":/type/capture-card" );
59 ADD_ICON( STREAM, ":/type/stream" );
60 ADD_ICON( PLAYLIST, ":/type/playlist" );
61 ADD_ICON( NODE, ":/type/node" );
70 QString VLCModel::getMeta( const QModelIndex & index, int meta )
72 return index.model()->index( index.row(), columnFromMeta( meta ), index.parent() ).
76 QPixmap VLCModel::getArtPixmap( const QModelIndex & index, const QSize & size )
78 QString artUrl = index.sibling( index.row(),
79 VLCModel::columnFromMeta(COLUMN_COVER) ).data().toString();
82 QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
84 if( !QPixmapCache::find( key, artPix ))
86 if( artUrl.isEmpty() || !artPix.load( artUrl ) )
88 key = QString("noart%1%2").arg(size.width()).arg(size.height());
89 if( !QPixmapCache::find( key, artPix ) )
91 artPix = QPixmap( ":/noart" ).scaled( size,
93 Qt::SmoothTransformation );
94 QPixmapCache::insert( key, artPix );
99 artPix = artPix.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
100 QPixmapCache::insert( key, artPix );
107 QVariant VLCModel::headerData( int section, Qt::Orientation orientation,
110 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
113 int meta_col = columnToMeta( section );
115 if( meta_col == COLUMN_END ) return QVariant();
117 return QVariant( qfu( psz_column_title( meta_col ) ) );
120 int VLCModel::columnToMeta( int _column )
122 int meta = 1, column = 0;
124 while( column != _column && meta != COLUMN_END )
133 int VLCModel::metaToColumn( int _meta )
135 int meta = 1, column = 0;
137 while( meta != COLUMN_END )
148 int VLCModel::itemId( const QModelIndex &index, int type ) const
150 AbstractPLItem *item = getItem( index );
151 if ( !item ) return -1;
152 return item->id( type );
155 AbstractPLItem *VLCModel::getItem( const QModelIndex &index ) const
157 if( index.isValid() )
158 return static_cast<AbstractPLItem*>( index.internalPointer() );
162 QString VLCModel::getURI( const QModelIndex &index ) const
164 AbstractPLItem *item = getItem( index );
165 if ( !item ) return QString();
166 return item->getURI().toString();
169 input_item_t * VLCModel::getInputItem( const QModelIndex &index ) const
171 AbstractPLItem *item = getItem( index );
172 if ( !item ) return NULL;
173 return item->inputItem();
176 QString VLCModel::getTitle( const QModelIndex &index ) const
178 AbstractPLItem *item = getItem( index );
179 if ( !item ) return QString();
180 return item->getTitle();
183 bool VLCModel::isCurrent( const QModelIndex &index ) const
185 AbstractPLItem *item = getItem( index );
186 if ( !item ) return false;
187 return item->inputItem() == THEMIM->currentInputItem();
190 int VLCModel::columnCount( const QModelIndex & ) const
192 return columnFromMeta( COLUMN_END );
195 void VLCModel::ensureArtRequested( const QModelIndex &index )
197 if ( index.isValid() && hasChildren( index ) )
199 int i_art_policy = var_GetInteger( THEPL, "album-art" );
200 bool b_access = var_InheritBool( THEPL, "metadata-network-access" );
201 if ( i_art_policy != ALBUM_ART_ALL && ! b_access ) return;
202 int nbnodes = rowCount( index );
204 for( int row = 0 ; row < nbnodes ; row++ )
206 child = index.child( row, COLUMN_COVER );
207 if ( child.isValid() && child.data().toString().isEmpty() )
208 THEMIM->getIM()->requestArtUpdate( getInputItem( child ), false );