]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/infopanels.cpp
Prevents segfault if no file are in the playlist (thanks j-b)
[vlc] / modules / gui / qt4 / components / infopanels.cpp
1 /*****************************************************************************
2  * infopanels.cpp : Panels for the information dialogs
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "qt4.hpp"
26 #include "components/infopanels.hpp"
27
28 #include <QTreeWidget>
29 #include <QListView>
30 #include <QPushButton>
31 #include <QHeaderView>
32 #include <QList>
33 #include <QGridLayout>
34 #include <QLineEdit>
35 #include <QLabel>
36 #include <QSpinBox>
37 #include <QTabWidget>
38
39 /************************************************************************
40  * Single panels
41  ************************************************************************/
42
43 /* First Panel - Meta Info */
44
45 MetaPanel::MetaPanel( QWidget *parent, intf_thread_t *_p_intf ) :
46                                     QWidget( parent ), p_intf( _p_intf )
47 {
48     int line = 0;
49     QGridLayout *l = new QGridLayout( this );
50     p_input = NULL;
51
52 #define ADD_META( string, widget ) {                             \
53     l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
54     widget = new QLineEdit;                                      \
55     l->addWidget( widget, line, 1, 1, 9 );                       \
56     line++;            }
57
58     ADD_META( VLC_META_TITLE, title_text ); /* OK */
59     ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
60     ADD_META( VLC_META_COLLECTION, collection_text ); /* OK */
61
62     /* Genre Name */ /* FIXME List id3genres.h is not includable yet ? */
63     genre_text = new QLineEdit;
64     l->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
65     l->addWidget( genre_text, line, 1, 1, 6 );
66     /* Date (Should be in years) */
67     date_text = new QSpinBox; setSpinBounds( date_text );
68     l->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 7 );
69     l->addWidget( date_text, line, 8, 1, 2 );
70     line++;
71
72     /* Number and Rating */
73     l->addWidget( new QLabel( qtr( "Track number/Position" )  + " :" ),
74                   line, 0 );
75     seqnum_text = new QSpinBox; setSpinBounds( seqnum_text );
76     l->addWidget( seqnum_text, line, 1, 1, 4 );
77
78     l->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 5 );
79     rating_text = new QSpinBox; setSpinBounds( rating_text) ;
80     l->addWidget( rating_text, line, 6, 1, 4 );
81     line++;
82
83     ADD_META( VLC_META_NOW_PLAYING, nowplaying_text );
84
85     /* Language and settings */
86     l->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 0 );
87     language_text = new QLineEdit;
88     l->addWidget( language_text, line, 1, 1, 4 );
89     l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
90     setting_text = new QLineEdit;
91     l->addWidget( setting_text, line, 6, 1, 4 );
92     line++;
93
94     /* ART_URL */
95     //    ADD_META( VLC_META_URL, setting_text );
96     art_cover = new QLabel( "" );
97     art_cover->setMinimumHeight( 128 );
98     art_cover->setMinimumWidth( 128 );
99     art_cover->setMaximumHeight( 128 );
100     art_cover->setMaximumWidth( 128 );
101     art_cover->setScaledContents( true );
102     art_cover->setPixmap( QPixmap( ":/noart.png" ) );
103
104     l->addWidget( art_cover, line, 8, 4, 2 );
105
106 #define ADD_META_B( string, widget ) {                             \
107     l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
108     widget = new QLineEdit;                                      \
109     l->addWidget( widget, line, 1, 1, 7 );                       \
110     line++;            }
111
112     ADD_META_B( VLC_META_COPYRIGHT, copyright_text );
113     ADD_META_B( VLC_META_PUBLISHER, publisher_text );
114
115     ADD_META_B( VLC_META_ENCODED_BY, publisher_text );
116     ADD_META_B( VLC_META_DESCRIPTION, description_text ); // Comment Two lines?
117
118     QPushButton *write = new QPushButton( qtr( "&Write" ) );
119     l->addWidget( write, line, 0 );
120
121     BUTTONACT( write, saveMeta() );
122
123     /*  ADD_META( TRACKID)  DO NOT SHOW it */
124     /*  ADD_URI - DO not show it, done outside */
125
126 #undef ADD_META
127 #undef ADD_META_B
128
129 //  CONNECT( model,  artSet( QString ) , this, setArt( QString ) );
130 }
131
132 MetaPanel::~MetaPanel()
133 {
134 }
135
136 void MetaPanel::saveMeta()
137 {
138     playlist_t *p_playlist;
139
140     meta_export_t p_export;
141     p_export.p_item = p_input;
142
143     if( p_input == NULL )
144         return;
145
146     /* we can write meta data only in a file */
147     if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
148         ( p_input->i_type == ITEM_TYPE_VFILE ) )
149         /* some audio files are detected as video files */
150     {
151         char *psz_uri = p_input->psz_uri;
152         if( !strncmp( psz_uri, "file://", 7 ) )
153             psz_uri += 7; /* strlen("file://") = 7 */
154
155         p_export.psz_file = strndup( psz_uri, PATH_MAX );
156     }
157     else
158         return;
159
160     /* now we read the modified meta data */
161     free( p_input->p_meta->psz_artist );
162     p_input->p_meta->psz_artist = strdup( artist_text->text().toUtf8() );
163     free( p_input->p_meta->psz_album );
164     p_input->p_meta->psz_album = strdup( collection_text->text().toUtf8() );
165     free( p_input->p_meta->psz_genre );
166     p_input->p_meta->psz_genre = strdup( genre_text->text().toUtf8() );
167     free( p_input->p_meta->psz_date );
168     p_input->p_meta->psz_date = (char*) malloc(5);
169     snprintf( p_input->p_meta->psz_date, 5, "%d", date_text->value() );
170     free( p_input->p_meta->psz_tracknum );
171     p_input->p_meta->psz_tracknum = (char*) malloc(5);
172     snprintf( p_input->p_meta->psz_tracknum, 5, "%d", seqnum_text->value() );
173     free( p_input->p_meta->psz_title );
174     p_input->p_meta->psz_title = strdup(  title_text->text().toUtf8() );
175
176     p_playlist = pl_Yield( p_intf );
177
178     PL_LOCK;
179     p_playlist->p_private = &p_export;
180
181     module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
182     if( p_mod )
183         module_Unneed( p_playlist, p_mod );
184     PL_UNLOCK;
185     pl_Release( p_playlist );
186 }
187
188 void MetaPanel::update( input_item_t *p_item )
189 {
190     char *psz_meta;
191 #define UPDATE_META( meta, widget ) {               \
192     psz_meta = p_item->p_meta->psz_##meta;          \
193     if( !EMPTY_STR( psz_meta ) )                    \
194         widget->setText( qfu( psz_meta ) );         \
195     else                                            \
196         widget->setText( "" );          }
197
198 #define UPDATE_META_INT( meta, widget ) {           \
199     psz_meta = p_item->p_meta->psz_##meta;          \
200     if( !EMPTY_STR( psz_meta ) )                    \
201         widget->setValue( atoi( psz_meta ) ); }
202
203     /* Name / Title */
204     psz_meta = p_item->p_meta->psz_title;
205     if( !EMPTY_STR( psz_meta ) )
206         title_text->setText( qfu( psz_meta ) );
207     else if( !EMPTY_STR( p_item->psz_name ) )
208         title_text->setText( qfu( p_item->psz_name ) );
209     else title_text->setText( "" );
210
211     /* URL / URI */
212     psz_meta = p_item->p_meta->psz_url;
213     if( !EMPTY_STR( psz_meta ) )
214         emit uriSet( QString( psz_meta ) );
215     else if( !EMPTY_STR( p_item->psz_uri ) )
216         emit uriSet( QString( p_item->psz_uri ) );
217
218     /* Other classic though */
219     UPDATE_META( artist, artist_text );
220     UPDATE_META( genre, genre_text );
221     UPDATE_META( copyright, copyright_text );
222     UPDATE_META( album, collection_text );
223     UPDATE_META( description, description_text );
224     UPDATE_META( language, language_text );
225     UPDATE_META( nowplaying, nowplaying_text );
226     UPDATE_META( publisher, publisher_text );
227     UPDATE_META( setting, setting_text );
228
229     UPDATE_META_INT( date, date_text );
230     UPDATE_META_INT( tracknum, seqnum_text );
231     UPDATE_META_INT( rating, rating_text );
232
233 #undef UPDATE_META
234     psz_meta = p_item->p_meta->psz_arturl;
235     if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
236     {
237         QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
238         art_cover->setPixmap( QPixmap( artUrl ) );
239     }
240     else
241         art_cover->setPixmap( QPixmap( ":/noart.png" ) );
242 }
243
244 void MetaPanel::clear(){}
245
246 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent, intf_thread_t *_p_intf ) :
247                                         QWidget( parent ), p_intf( _p_intf )
248 {
249      QGridLayout *layout = new QGridLayout(this);
250      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
251                  " are shown in this list.\n" ) );
252      topLabel->setWordWrap( true );
253      layout->addWidget( topLabel, 0, 0 );
254
255      extraMetaTree = new QTreeWidget( this );
256      extraMetaTree->setAlternatingRowColors( true );
257      extraMetaTree->setColumnCount( 2 );
258
259      extraMetaTree->header()->hide();
260 /*     QStringList *treeHeaders;
261      treeHeaders << qtr( "Test1" ) << qtr( "Test2" ); */
262
263      layout->addWidget( extraMetaTree, 1, 0 );
264 }
265
266 void ExtraMetaPanel::update( input_item_t *p_item )
267 {
268     vlc_meta_t *p_meta = p_item->p_meta;
269     QStringList tempItem;
270
271     QList<QTreeWidgetItem *> items;
272     for (int i = 0; i < p_meta->i_extra; i++ )
273     {
274         tempItem.append( qfu( p_meta->ppsz_extra_name[i] ) + " : ");
275         tempItem.append( qfu( p_meta->ppsz_extra_value[i] ) );
276         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
277     }
278     extraMetaTree->addTopLevelItems( items );
279 }
280
281 void ExtraMetaPanel::clear(){}
282
283 /* Second Panel - Stats */
284
285 InputStatsPanel::InputStatsPanel( QWidget *parent, intf_thread_t *_p_intf ) :
286                                   QWidget( parent ), p_intf( _p_intf )
287 {
288      QGridLayout *layout = new QGridLayout(this);
289      StatsTree = new QTreeWidget(this);
290      QList<QTreeWidgetItem *> items;
291      QLabel *topLabel = new QLabel( qtr( "Various statistics about the current"
292                  " media or stream.\n Played and streamed info are shown." ) );
293      topLabel->setWordWrap( true );
294      layout->addWidget( topLabel, 0, 0 );
295
296      StatsTree->setColumnCount( 3 );
297      StatsTree->header()->hide();
298
299 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
300     itemName =                                                                 \
301       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
302     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
303
304 #define CREATE_CATEGORY( catName, itemText ) {                           \
305     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
306     catName->setExpanded( true );                                        \
307     StatsTree->addTopLevelItem( catName );    }
308
309 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
310     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
311     catName->addChild( itemName ); }
312
313     /* Create the main categories */
314     CREATE_CATEGORY( input, qtr("Input") );
315     CREATE_CATEGORY( video, qtr("Video") );
316     CREATE_CATEGORY( streaming, qtr("Streaming") );
317     CREATE_CATEGORY( audio, qtr("Audio") );
318
319     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Read at media"),
320                            "0", input , "kB" );
321     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
322                            "0", input, "kb/s" );
323     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed"), "0", input, "kB") ;
324     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Stream bitrate"),
325                            "0", input, "kb/s" );
326
327     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded blocks"),
328                            "0", video, "" );
329     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed frames"),
330                            "0", video, "" );
331     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost frames"),
332                            "0", video, "" );
333
334     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent packets"), "0", streaming, "" );
335     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent bytes"),
336                            "0", streaming, "kB" );
337     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Sent bitrates"),
338                            "0", streaming, "kb/s" );
339
340     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded blocks"),
341                            "0", audio, "" );
342     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played buffers"),
343                            "0", audio, "" );
344     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost buffers"), "0", audio, "" );
345
346     input->setExpanded( true );
347     video->setExpanded( true );
348     streaming->setExpanded( true );
349     audio->setExpanded( true );
350
351     StatsTree->resizeColumnToContents( 0 );
352     StatsTree->setColumnWidth( 1 , 100 );
353
354     layout->addWidget(StatsTree, 1, 0 );
355 }
356
357 InputStatsPanel::~InputStatsPanel()
358 {
359 }
360
361 void InputStatsPanel::update( input_item_t *p_item )
362 {
363     vlc_mutex_lock( &p_item->p_stats->lock );
364
365 #define UPDATE( widget, format, calc... ) \
366     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
367
368     UPDATE( read_media_stat, "%8.0f",
369             (float)(p_item->p_stats->i_read_bytes)/1000);
370     UPDATE( input_bitrate_stat, "%6.0f",
371                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
372     UPDATE( demuxed_stat, "%8.0f",
373                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
374     UPDATE( stream_bitrate_stat, "%6.0f",
375                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
376
377     /* Video */
378     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
379     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
380     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
381
382     /* Sout */
383     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
384     UPDATE( send_bytes_stat, "%8.0f",
385             (float)(p_item->p_stats->i_sent_bytes)/1000 );
386     UPDATE( send_bitrate_stat, "%6.0f",
387             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
388
389     /* Audio*/
390     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
391     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
392     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
393
394     vlc_mutex_unlock(& p_item->p_stats->lock );
395 }
396
397 void InputStatsPanel::clear()
398 {
399 }
400
401 /* Third panel - Stream info */
402
403 InfoPanel::InfoPanel( QWidget *parent, intf_thread_t *_p_intf ) :
404                                       QWidget( parent ), p_intf( _p_intf )
405 {
406      QGridLayout *layout = new QGridLayout(this);
407      InfoTree = new QTreeWidget(this);
408      QList<QTreeWidgetItem *> items;
409
410      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
411               " stream is made of.\n Muxer, Audio and Video Codecs, Subtitles "
412               "are shown." ) );
413      topLabel->setWordWrap( true );
414      layout->addWidget( topLabel, 0, 0 );
415
416      InfoTree->setColumnCount( 1 );
417      InfoTree->header()->hide();
418      layout->addWidget(InfoTree, 1, 0 );
419 }
420
421 InfoPanel::~InfoPanel()
422 {
423 }
424
425 void InfoPanel::update( input_item_t *p_item)
426 {
427     InfoTree->clear();
428     QTreeWidgetItem *current_item = NULL;
429     QTreeWidgetItem *child_item = NULL;
430
431     for( int i = 0; i< p_item->i_categories ; i++)
432     {
433         current_item = new QTreeWidgetItem();
434         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
435         InfoTree->addTopLevelItem( current_item );
436
437         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
438         {
439             child_item = new QTreeWidgetItem ();
440             child_item->setText( 0,
441                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
442                     + ": "
443                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
444
445             current_item->addChild(child_item);
446         }
447          InfoTree->setItemExpanded( current_item, true);
448     }
449 }
450
451 void InfoPanel::clear()
452 {
453     InfoTree->clear();
454 }