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