]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/infopanels.cpp
qt4: forgot that file in previous commit
[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 <QStringList>
34 #include <QGridLayout>
35 #include <QLineEdit>
36 #include <QLabel>
37 #include <QSpinBox>
38 #include <QTabWidget>
39
40 /************************************************************************
41  * Single panels
42  ************************************************************************/
43
44 /**
45  * First Panel - Meta Info
46  * All the usual MetaData are displayed and can be changed.
47  **/
48 MetaPanel::MetaPanel( QWidget *parent,
49                       intf_thread_t *_p_intf )
50                       : QWidget( parent ), p_intf( _p_intf )
51 {
52     QGridLayout *l = new QGridLayout( this );
53     int line = 0; /* Counter for GridLayout */
54     p_input = NULL;
55
56 #define ADD_META( string, widget ) {                             \
57     l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
58     widget = new QLineEdit;                                      \
59     l->addWidget( widget, line, 1, 1, 9 );                       \
60     line++;            }
61
62     /* Title, artist and album*/
63     ADD_META( VLC_META_TITLE, title_text ); /* OK */
64     ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
65     ADD_META( VLC_META_COLLECTION, collection_text ); /* OK */
66
67     /* Genre Name */
68     /* FIXME List id3genres.h is not includable yet ? */
69     genre_text = new QLineEdit;
70     l->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
71     l->addWidget( genre_text, line, 1, 1, 6 );
72
73     /* Date (Should be in years) */
74     date_text = new QSpinBox; setSpinBounds( date_text );
75     l->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 7 );
76     l->addWidget( date_text, line, 8, 1, 2 );
77     line++;
78
79     /* Number and Rating */
80     l->addWidget( new QLabel( qtr( "Track number/Position" )  + " :" ),
81                   line, 0 );
82     seqnum_text = new QSpinBox; setSpinBounds( seqnum_text );
83     l->addWidget( seqnum_text, line, 1, 1, 4 );
84
85     l->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 5 );
86     rating_text = new QSpinBox; setSpinBounds( rating_text) ;
87     l->addWidget( rating_text, line, 6, 1, 4 );
88     line++;
89
90     /* Now Playing ? */
91     ADD_META( VLC_META_NOW_PLAYING, nowplaying_text );
92
93     /* Language and settings */
94     l->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 0 );
95     language_text = new QLineEdit;
96     l->addWidget( language_text, line, 1, 1, 4 );
97     l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
98     setting_text = new QLineEdit;
99     l->addWidget( setting_text, line, 6, 1, 4 );
100     line++;
101
102     /* ART_URL */
103     art_cover = new QLabel( "" );
104     art_cover->setMinimumHeight( 128 );
105     art_cover->setMinimumWidth( 128 );
106     art_cover->setMaximumHeight( 128 );
107     art_cover->setMaximumWidth( 128 );
108     art_cover->setScaledContents( true );
109     art_cover->setPixmap( QPixmap( ":/noart.png" ) );
110     l->addWidget( art_cover, line, 8, 4, 2 );
111
112 #define ADD_META_2( string, widget ) {                             \
113     l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
114     widget = new QLineEdit;                                      \
115     l->addWidget( widget, line, 1, 1, 7 );                       \
116     line++;            }
117
118     ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
119     ADD_META_2( VLC_META_PUBLISHER, publisher_text );
120
121     ADD_META_2( VLC_META_ENCODED_BY, publisher_text );
122     ADD_META_2( VLC_META_DESCRIPTION, description_text );
123
124     /*  ADD_META( TRACKID )  Useless ? */
125     /*  ADD_URI - DO not show it, done outside */
126
127 #undef ADD_META
128 #undef ADD_META_2
129 }
130
131 MetaPanel::~MetaPanel(){}
132
133 /**
134  * Save the MetaData, triggered by parent->save Button
135  **/
136 void MetaPanel::saveMeta()
137 {
138     playlist_t *p_playlist;
139     char psz[5];
140
141     meta_export_t p_export;
142     p_export.p_item = p_input;
143
144     if( p_input == NULL )
145         return;
146
147     /* we can write meta data only in a file */
148     if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
149         ( p_input->i_type == ITEM_TYPE_VFILE ) )
150         /* some audio files are detected as video files */
151     {
152         char *psz_uri = p_input->psz_uri;
153         if( !strncmp( psz_uri, "file://", 7 ) )
154             psz_uri += 7; /* strlen("file://") = 7 */
155
156         p_export.psz_file = strndup( psz_uri, PATH_MAX );
157     }
158     else
159         return;
160
161     /* now we read the modified meta data */
162     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
163     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
164     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
165
166     snprintf( psz, sizeof(psz), "%d", date_text->value() );
167     input_item_SetDate(  p_input, psz );
168
169     snprintf( psz, sizeof(psz), "%d", seqnum_text->value() );
170     input_item_SetTrackNum(  p_input, psz );
171
172     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
173
174     p_playlist = pl_Yield( p_intf );
175
176     PL_LOCK;
177     p_playlist->p_private = &p_export;
178
179     module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
180     if( p_mod )
181         module_Unneed( p_playlist, p_mod );
182     PL_UNLOCK;
183     pl_Release( p_playlist );
184 }
185
186 /**
187  * Update all the MetaData and art on an "item-changed" event
188  **/
189 void MetaPanel::update( input_item_t *p_item )
190 {
191     const char *psz_meta; 
192 #define UPDATE_META( meta, widget ) {               \
193     psz_meta = input_item_Get##meta( p_item );      \
194     if( !EMPTY_STR( psz_meta ) )                    \
195         widget->setText( qfu( psz_meta ) );         \
196     else                                            \
197         widget->setText( "" ); }
198
199 #define UPDATE_META_INT( meta, widget ) {           \
200     psz_meta = input_item_Get##meta( p_item );      \
201     if( !EMPTY_STR( psz_meta ) )                    \
202         widget->setValue( atoi( psz_meta ) ); }
203
204     /* Name / Title */
205     psz_meta = input_item_GetTitle( p_item );
206     if( !EMPTY_STR( psz_meta ) )
207         title_text->setText( qfu( psz_meta ) );
208     else if( !EMPTY_STR( p_item->psz_name ) )
209         title_text->setText( qfu( p_item->psz_name ) );
210     else title_text->setText( "" );
211
212     /* URL / URI */
213     psz_meta = input_item_GetURL( p_item );
214     if( !EMPTY_STR( psz_meta ) )
215         emit uriSet( QString( psz_meta ) );
216     else if( !EMPTY_STR( p_item->psz_uri ) )
217         emit uriSet( QString( p_item->psz_uri ) );
218
219     /* Other classic though */
220     UPDATE_META( Artist, artist_text );
221     UPDATE_META( Genre, genre_text );
222     UPDATE_META( Copyright, copyright_text );
223     UPDATE_META( Album, collection_text );
224     UPDATE_META( Description, description_text );
225     UPDATE_META( Language, language_text );
226     UPDATE_META( NowPlaying, nowplaying_text );
227     UPDATE_META( Publisher, publisher_text );
228     UPDATE_META( Setting, setting_text );
229
230     UPDATE_META_INT( Date, date_text );
231     UPDATE_META_INT( TrackNum, seqnum_text );
232     UPDATE_META_INT( Rating, rating_text );
233
234 #undef UPDATE_META_INT
235 #undef UPDATE_META
236
237     /* Art Urls */
238     psz_meta = input_item_GetArtURL( p_item );
239     if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
240     {
241         QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
242         art_cover->setPixmap( QPixmap( artUrl ) );
243     }
244     else
245         art_cover->setPixmap( QPixmap( ":/noart.png" ) );
246 }
247
248 /*
249  * Clear all the metadata widgets
250  * Unused yet
251  */
252 void MetaPanel::clear(){}
253
254 /**
255  * Second Panel - Shows the extra metadata in a tree, non editable.
256  **/
257 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
258                                 intf_thread_t *_p_intf )
259                                 : QWidget( parent ), p_intf( _p_intf )
260 {
261      QGridLayout *layout = new QGridLayout(this);
262
263      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
264                  " are shown in this list.\n" ) );
265      topLabel->setWordWrap( true );
266      layout->addWidget( topLabel, 0, 0 );
267
268      extraMetaTree = new QTreeWidget( this );
269      extraMetaTree->setAlternatingRowColors( true );
270      extraMetaTree->setColumnCount( 2 );
271      extraMetaTree->header()->hide();
272 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
273  *                                             << qtr( "Value" ) );
274  * Useless, add this header if you think it would help the user          **
275  */
276
277      layout->addWidget( extraMetaTree, 1, 0 );
278 }
279
280 /**
281  * Update the Extra Metadata from p_meta->i_extras 
282  **/
283 void ExtraMetaPanel::update( input_item_t *p_item )
284 {
285     vlc_meta_t *p_meta = p_item->p_meta;
286     if( !p_meta )
287         return;
288     QStringList tempItem;
289
290     QList<QTreeWidgetItem *> items;
291     vlc_dictionary_t * p_dict = &p_meta->extra_tags;
292     char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
293     for (int i = 0; ppsz_allkey[i] ; i++ )
294     {
295         const char * psz_value = (const char *)vlc_dictionary_value_for_key(
296                 p_dict, ppsz_allkey[i] );
297         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
298         tempItem.append( qfu( psz_value ) );
299         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
300         free( ppsz_allkey[i] );
301     }
302     free( ppsz_allkey );
303     extraMetaTree->addTopLevelItems( items );
304 }
305
306 /**
307  * Clear the ExtraMetaData Tree
308  **/
309 void ExtraMetaPanel::clear()
310 {
311     extraMetaTree->clear();
312 }
313
314 /**
315  * Third panel - Stream info
316  * Display all codecs and muxers info that we could gather.
317  **/
318 InfoPanel::InfoPanel( QWidget *parent,
319                       intf_thread_t *_p_intf )
320                       : QWidget( parent ), p_intf( _p_intf )
321 {
322      QGridLayout *layout = new QGridLayout(this);
323
324      QList<QTreeWidgetItem *> items;
325
326      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
327               " stream is made of.\n Muxer, Audio and Video Codecs, Subtitles "
328               "are shown." ) );
329      topLabel->setWordWrap( true );
330      layout->addWidget( topLabel, 0, 0 );
331
332      InfoTree = new QTreeWidget(this);
333      InfoTree->setColumnCount( 1 );
334      InfoTree->header()->hide();
335      layout->addWidget(InfoTree, 1, 0 );
336 }
337
338 InfoPanel::~InfoPanel()
339 {
340 }
341
342 /**
343  * Update the Codecs information on parent->update()
344  **/
345 void InfoPanel::update( input_item_t *p_item)
346 {
347     InfoTree->clear();
348     QTreeWidgetItem *current_item = NULL;
349     QTreeWidgetItem *child_item = NULL;
350
351     for( int i = 0; i< p_item->i_categories ; i++)
352     {
353         current_item = new QTreeWidgetItem();
354         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
355         InfoTree->addTopLevelItem( current_item );
356
357         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
358         {
359             child_item = new QTreeWidgetItem ();
360             child_item->setText( 0,
361                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
362                     + ": "
363                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
364
365             current_item->addChild(child_item);
366         }
367          InfoTree->setItemExpanded( current_item, true);
368     }
369 }
370
371 /**
372  * Clear the tree
373  **/
374 void InfoPanel::clear()
375 {
376     InfoTree->clear();
377 }
378
379 /**
380  * Save all the information to a file
381  * Not yet implemented.
382  **/
383 /*
384 void InfoPanel::saveCodecsInfo()
385 {
386
387 }
388 */
389
390 /**
391  * Fourth Panel - Stats
392  * Displays the Statistics for reading/streaming/encoding/displaying in a tree
393  */
394 InputStatsPanel::InputStatsPanel( QWidget *parent,
395                                   intf_thread_t *_p_intf )
396                                   : QWidget( parent ), p_intf( _p_intf )
397 {
398      QGridLayout *layout = new QGridLayout(this);
399
400      QList<QTreeWidgetItem *> items;
401
402      QLabel *topLabel = new QLabel( qtr( "Various statistics about the current"
403                  " media or stream.\n Played and streamed info are shown." ) );
404      topLabel->setWordWrap( true );
405      layout->addWidget( topLabel, 0, 0 );
406
407      StatsTree = new QTreeWidget(this);
408      StatsTree->setColumnCount( 3 );
409      StatsTree->header()->hide();
410
411 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
412     itemName =                                                                 \
413       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
414     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
415
416 #define CREATE_CATEGORY( catName, itemText ) {                           \
417     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
418     catName->setExpanded( true );                                        \
419     StatsTree->addTopLevelItem( catName );    }
420
421 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
422     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
423     catName->addChild( itemName ); }
424
425     /* Create the main categories */
426     CREATE_CATEGORY( input, qtr("Input") );
427     CREATE_CATEGORY( video, qtr("Video") );
428     CREATE_CATEGORY( streaming, qtr("Streaming") );
429     CREATE_CATEGORY( audio, qtr("Audio") );
430
431     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Read at media"),
432                            "0", input , "kB" );
433     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
434                            "0", input, "kb/s" );
435     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed"), "0", input, "kB") ;
436     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Stream bitrate"),
437                            "0", input, "kb/s" );
438
439     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded blocks"),
440                            "0", video, "" );
441     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed frames"),
442                            "0", video, "" );
443     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost frames"),
444                            "0", video, "" );
445
446     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent packets"), "0", streaming, "" );
447     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent bytes"),
448                            "0", streaming, "kB" );
449     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Sent bitrates"),
450                            "0", streaming, "kb/s" );
451
452     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded blocks"),
453                            "0", audio, "" );
454     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played buffers"),
455                            "0", audio, "" );
456     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost buffers"), "0", audio, "" );
457
458     input->setExpanded( true );
459     video->setExpanded( true );
460     streaming->setExpanded( true );
461     audio->setExpanded( true );
462
463     StatsTree->resizeColumnToContents( 0 );
464     StatsTree->setColumnWidth( 1 , 100 );
465
466     layout->addWidget(StatsTree, 1, 0 );
467 }
468
469 InputStatsPanel::~InputStatsPanel()
470 {
471 }
472
473 /**
474  * Update the Statistics
475  **/
476 void InputStatsPanel::update( input_item_t *p_item )
477 {
478     vlc_mutex_lock( &p_item->p_stats->lock );
479
480 #define UPDATE( widget, format, calc... ) \
481     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
482
483     UPDATE( read_media_stat, "%8.0f",
484             (float)(p_item->p_stats->i_read_bytes)/1000);
485     UPDATE( input_bitrate_stat, "%6.0f",
486                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
487     UPDATE( demuxed_stat, "%8.0f",
488                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
489     UPDATE( stream_bitrate_stat, "%6.0f",
490                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
491
492     /* Video */
493     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
494     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
495     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
496
497     /* Sout */
498     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
499     UPDATE( send_bytes_stat, "%8.0f",
500             (float)(p_item->p_stats->i_sent_bytes)/1000 );
501     UPDATE( send_bitrate_stat, "%6.0f",
502             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
503
504     /* Audio*/
505     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
506     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
507     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
508
509     vlc_mutex_unlock(& p_item->p_stats->lock );
510 }
511
512 void InputStatsPanel::clear()
513 {
514 }
515
516