]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/vlc_model.cpp
Qt: VLCModel/TreeView: delegate cover rendering (fix #10206)
[vlc] / modules / gui / qt4 / components / playlist / vlc_model.cpp
1 /*****************************************************************************
2  * vlc_model.cpp : base for playlist and ml model
3  ****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team and AUTHORS
5  * $Id$
6  *
7  * Authors: Srikanth Raju <srikiraju#gmail#com>
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 "vlc_model.hpp"
25 #include "input_manager.hpp"                            /* THEMIM */
26 #include "pixmaps/types/type_unknown.xpm"
27
28 VLCModelSubInterface::VLCModelSubInterface()
29 {
30     sigs = new VLCModelSignalsHandler( this );
31 }
32
33 VLCModelSubInterface::~VLCModelSubInterface()
34 {
35     delete sigs;
36 }
37
38 int VLCModelSubInterface::columnFromMeta( int meta_col )
39 {
40     int meta = 1, column = 0;
41
42     while( meta != meta_col && meta != COLUMN_END )
43     {
44         meta <<= 1;
45         column++;
46     }
47
48     return column;
49 }
50
51 VLCModel::VLCModel( intf_thread_t *_p_intf, QObject *parent )
52     : QAbstractItemModel( parent ), VLCModelSubInterface(), p_intf(_p_intf)
53 {
54     /* Icons initialization */
55 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
56     ADD_ICON( UNKNOWN , QPixmap( type_unknown_xpm ) );
57     ADD_ICON( FILE, ":/type/file" );
58     ADD_ICON( DIRECTORY, ":/type/directory" );
59     ADD_ICON( DISC, ":/type/disc" );
60     ADD_ICON( CDDA, ":/type/cdda" );
61     ADD_ICON( CARD, ":/type/capture-card" );
62     ADD_ICON( NET, ":/type/net" );
63     ADD_ICON( PLAYLIST, ":/type/playlist" );
64     ADD_ICON( NODE, ":/type/node" );
65 #undef ADD_ICON
66 }
67
68 VLCModel::~VLCModel()
69 {
70
71 }
72
73 QString VLCModel::getMeta( const QModelIndex & index, int meta )
74 {
75     return index.model()->index( index.row(), columnFromMeta( meta ), index.parent() ).
76         data().toString();
77 }
78
79 QPixmap VLCModel::getArtPixmap( const QModelIndex & index, const QSize & size )
80 {
81     QString artUrl = index.sibling( index.row(),
82                      VLCModel::columnFromMeta(COLUMN_COVER) ).data().toString();
83     QPixmap artPix;
84
85     QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
86
87     if( !QPixmapCache::find( key, artPix ))
88     {
89         if( artUrl.isEmpty() || !artPix.load( artUrl ) )
90         {
91             key = QString("noart%1%2").arg(size.width()).arg(size.height());
92             if( !QPixmapCache::find( key, artPix ) )
93             {
94                 artPix = QPixmap( ":/noart" ).scaled( size,
95                                                       Qt::KeepAspectRatio,
96                                                       Qt::SmoothTransformation );
97                 QPixmapCache::insert( key, artPix );
98             }
99         }
100         else
101         {
102             artPix = artPix.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
103             QPixmapCache::insert( key, artPix );
104         }
105     }
106
107     return artPix;
108 }
109
110 QVariant VLCModel::headerData( int section, Qt::Orientation orientation,
111                               int role ) const
112 {
113     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
114         return QVariant();
115
116     int meta_col = columnToMeta( section );
117
118     if( meta_col == COLUMN_END ) return QVariant();
119
120     return QVariant( qfu( psz_column_title( meta_col ) ) );
121 }
122
123 int VLCModel::columnToMeta( int _column )
124 {
125     int meta = 1, column = 0;
126
127     while( column != _column && meta != COLUMN_END )
128     {
129         meta <<= 1;
130         column++;
131     }
132
133     return meta;
134 }
135
136 int VLCModel::metaToColumn( int _meta )
137 {
138     int meta = 1, column = 0;
139
140     while( meta != COLUMN_END )
141     {
142         if ( meta & _meta )
143             break;
144         meta <<= 1;
145         column++;
146     }
147
148     return column;
149 }
150
151 int VLCModel::itemId( const QModelIndex &index, int type ) const
152 {
153     AbstractPLItem *item = getItem( index );
154     if ( !item ) return -1;
155     return item->id( type );
156 }
157
158 AbstractPLItem *VLCModel::getItem( const QModelIndex &index ) const
159 {
160     if( index.isValid() )
161         return static_cast<AbstractPLItem*>( index.internalPointer() );
162     else return NULL;
163 }
164
165 QString VLCModel::getURI( const QModelIndex &index ) const
166 {
167     AbstractPLItem *item = getItem( index );
168     if ( !item ) return QString();
169     return item->getURI().toString();
170 }
171
172 input_item_t * VLCModel::getInputItem( const QModelIndex &index ) const
173 {
174     AbstractPLItem *item = getItem( index );
175     if ( !item ) return NULL;
176     return item->inputItem();
177 }
178
179 QString VLCModel::getTitle( const QModelIndex &index ) const
180 {
181     AbstractPLItem *item = getItem( index );
182     if ( !item ) return QString();
183     return item->getTitle();
184 }
185
186 bool VLCModel::isCurrent( const QModelIndex &index ) const
187 {
188     AbstractPLItem *item = getItem( index );
189     if ( !item ) return false;
190     return item->inputItem() == THEMIM->currentInputItem();
191 }
192
193 int VLCModel::columnCount( const QModelIndex & ) const
194 {
195     return columnFromMeta( COLUMN_END );
196 }
197
198 void VLCModel::ensureArtRequested( const QModelIndex &index )
199 {
200     if ( index.isValid() && hasChildren( index ) )
201     {
202         int i_art_policy = var_GetInteger( THEPL, "album-art" );
203         if ( i_art_policy != ALBUM_ART_ALL ) return;
204         int nbnodes = rowCount( index );
205         QModelIndex child;
206         for( int row = 0 ; row < nbnodes ; row++ )
207         {
208             child = index.child( row, COLUMN_COVER );
209             if ( child.isValid() && child.data().toString().isEmpty() )
210                 THEMIM->getIM()->requestArtUpdate( getInputItem( child ) );
211         }
212     }
213 }
214