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