]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/infopanels.cpp
Edit mediainfo so you can edit meta-data even when playitem changes in
[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     ReadOnly( true );
130 }
131
132 MetaPanel::~MetaPanel(){}
133
134 /**
135  * Save the MetaData, triggered by parent->save Button
136  **/
137 void MetaPanel::saveMeta()
138 {
139     ReadOnly( true );
140     playlist_t *p_playlist;
141     char psz[5];
142
143     meta_export_t p_export;
144     p_export.p_item = p_input;
145
146     if( p_input == NULL )
147         return;
148
149     /* we can write meta data only in a file */
150     if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
151         ( p_input->i_type == ITEM_TYPE_VFILE ) )
152         /* some audio files are detected as video files */
153     {
154         char *psz_uri = p_input->psz_uri;
155         if( !strncmp( psz_uri, "file://", 7 ) )
156             psz_uri += 7; /* strlen("file://") = 7 */
157
158         p_export.psz_file = strndup( psz_uri, PATH_MAX );
159     }
160     else
161         return;
162
163     /* now we read the modified meta data */
164     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
165     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
166     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
167
168     snprintf( psz, sizeof(psz), "%d", date_text->value() );
169     input_item_SetDate(  p_input, psz );
170
171     snprintf( psz, sizeof(psz), "%d", seqnum_text->value() );
172     input_item_SetTrackNum(  p_input, psz );
173
174     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
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::ReadOnly(bool readonly)
189 {
190     title_text->setReadOnly( readonly );
191     artist_text->setReadOnly( readonly );
192     collection_text->setReadOnly( readonly );
193     genre_text->setReadOnly( readonly );
194     date_text->setReadOnly( readonly );
195     seqnum_text->setReadOnly( readonly );
196     rating_text->setReadOnly( readonly );
197     language_text->setReadOnly( readonly );
198     setting_text->setReadOnly( readonly );
199     copyright_text->setReadOnly( readonly );
200     publisher_text->setReadOnly( readonly );
201     description_text->setReadOnly( readonly );
202
203 }
204
205 void MetaPanel::editMeta()
206 {
207     ReadOnly( false );
208 }
209
210 /**
211  * Update all the MetaData and art on an "item-changed" event
212  **/
213 void MetaPanel::update( input_item_t *p_item )
214 {
215     char *psz_meta; 
216 #define UPDATE_META( meta, widget ) {               \
217     psz_meta = input_item_Get##meta( p_item );      \
218     if( !EMPTY_STR( psz_meta ) )                    \
219         widget->setText( qfu( psz_meta ) );         \
220     else                                            \
221         widget->setText( "" ); }                    \
222     free( psz_meta );
223
224 #define UPDATE_META_INT( meta, widget ) {           \
225     psz_meta = input_item_Get##meta( p_item );      \
226     if( !EMPTY_STR( psz_meta ) )                    \
227         widget->setValue( atoi( psz_meta ) ); }     \
228     free( psz_meta );
229
230
231     /* Name / Title */
232     psz_meta = input_item_GetTitle( p_item );
233     if( !EMPTY_STR( psz_meta ) )
234         title_text->setText( qfu( psz_meta ) );
235     else if( !EMPTY_STR( p_item->psz_name ) )
236         title_text->setText( qfu( p_item->psz_name ) );
237     else title_text->setText( "" );
238     free( psz_meta );
239
240     /* URL / URI */
241     psz_meta = input_item_GetURL( p_item );
242     if( !EMPTY_STR( psz_meta ) )
243         emit uriSet( QString( psz_meta ) );
244     else if( !EMPTY_STR( p_item->psz_uri ) )
245         emit uriSet( QString( p_item->psz_uri ) );
246     free( psz_meta );
247
248     /* Other classic though */
249     UPDATE_META( Artist, artist_text );
250     UPDATE_META( Genre, genre_text );
251     UPDATE_META( Copyright, copyright_text );
252     UPDATE_META( Album, collection_text );
253     UPDATE_META( Description, description_text );
254     UPDATE_META( Language, language_text );
255     UPDATE_META( NowPlaying, nowplaying_text );
256     UPDATE_META( Publisher, publisher_text );
257     UPDATE_META( Setting, setting_text );
258
259     UPDATE_META_INT( Date, date_text );
260     UPDATE_META_INT( TrackNum, seqnum_text );
261     UPDATE_META_INT( Rating, rating_text );
262
263 #undef UPDATE_META_INT
264 #undef UPDATE_META
265
266     /* Art Urls */
267     psz_meta = input_item_GetArtURL( p_item );
268     if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
269     {
270         QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
271         art_cover->setPixmap( QPixmap( artUrl ) );
272     }
273     else
274         art_cover->setPixmap( QPixmap( ":/noart.png" ) );
275     free( psz_meta );
276 }
277
278 /*
279  * Clear all the metadata widgets
280  * Unused yet
281  */
282 void MetaPanel::clear(){}
283
284 /**
285  * Second Panel - Shows the extra metadata in a tree, non editable.
286  **/
287 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
288                                 intf_thread_t *_p_intf )
289                                 : QWidget( parent ), p_intf( _p_intf )
290 {
291      QGridLayout *layout = new QGridLayout(this);
292
293      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
294                  " are shown in this list.\n" ) );
295      topLabel->setWordWrap( true );
296      layout->addWidget( topLabel, 0, 0 );
297
298      extraMetaTree = new QTreeWidget( this );
299      extraMetaTree->setAlternatingRowColors( true );
300      extraMetaTree->setColumnCount( 2 );
301      extraMetaTree->header()->hide();
302 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
303  *                                             << qtr( "Value" ) );
304  * Useless, add this header if you think it would help the user          **
305  */
306
307      layout->addWidget( extraMetaTree, 1, 0 );
308 }
309
310 /**
311  * Update the Extra Metadata from p_meta->i_extras 
312  **/
313 void ExtraMetaPanel::update( input_item_t *p_item )
314 {
315     vlc_mutex_lock( &p_item->lock );
316     vlc_meta_t *p_meta = p_item->p_meta;
317     if( !p_meta )
318         return;
319     QStringList tempItem;
320
321     QList<QTreeWidgetItem *> items;
322     vlc_dictionary_t * p_dict = &p_meta->extra_tags;
323     char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
324     for (int i = 0; ppsz_allkey[i] ; i++ )
325     {
326         const char * psz_value = (const char *)vlc_dictionary_value_for_key(
327                 p_dict, ppsz_allkey[i] );
328         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
329         tempItem.append( qfu( psz_value ) );
330         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
331         free( ppsz_allkey[i] );
332     }
333     vlc_mutex_unlock( &p_item->lock );
334     free( ppsz_allkey );
335     extraMetaTree->addTopLevelItems( items );
336 }
337
338 /**
339  * Clear the ExtraMetaData Tree
340  **/
341 void ExtraMetaPanel::clear()
342 {
343     extraMetaTree->clear();
344 }
345
346 /**
347  * Third panel - Stream info
348  * Display all codecs and muxers info that we could gather.
349  **/
350 InfoPanel::InfoPanel( QWidget *parent,
351                       intf_thread_t *_p_intf )
352                       : QWidget( parent ), p_intf( _p_intf )
353 {
354      QGridLayout *layout = new QGridLayout(this);
355
356      QList<QTreeWidgetItem *> items;
357
358      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
359               " stream is made of.\n Muxer, Audio and Video Codecs, Subtitles "
360               "are shown." ) );
361      topLabel->setWordWrap( true );
362      layout->addWidget( topLabel, 0, 0 );
363
364      InfoTree = new QTreeWidget(this);
365      InfoTree->setColumnCount( 1 );
366      InfoTree->header()->hide();
367      layout->addWidget(InfoTree, 1, 0 );
368 }
369
370 InfoPanel::~InfoPanel()
371 {
372 }
373
374 /**
375  * Update the Codecs information on parent->update()
376  **/
377 void InfoPanel::update( input_item_t *p_item)
378 {
379     InfoTree->clear();
380     QTreeWidgetItem *current_item = NULL;
381     QTreeWidgetItem *child_item = NULL;
382
383     for( int i = 0; i< p_item->i_categories ; i++)
384     {
385         current_item = new QTreeWidgetItem();
386         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
387         InfoTree->addTopLevelItem( current_item );
388
389         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
390         {
391             child_item = new QTreeWidgetItem ();
392             child_item->setText( 0,
393                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
394                     + ": "
395                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
396
397             current_item->addChild(child_item);
398         }
399          InfoTree->setItemExpanded( current_item, true);
400     }
401 }
402
403 /**
404  * Clear the tree
405  **/
406 void InfoPanel::clear()
407 {
408     InfoTree->clear();
409 }
410
411 /**
412  * Save all the information to a file
413  * Not yet implemented.
414  **/
415 /*
416 void InfoPanel::saveCodecsInfo()
417 {
418
419 }
420 */
421
422 /**
423  * Fourth Panel - Stats
424  * Displays the Statistics for reading/streaming/encoding/displaying in a tree
425  */
426 InputStatsPanel::InputStatsPanel( QWidget *parent,
427                                   intf_thread_t *_p_intf )
428                                   : QWidget( parent ), p_intf( _p_intf )
429 {
430      QGridLayout *layout = new QGridLayout(this);
431
432      QList<QTreeWidgetItem *> items;
433
434      QLabel *topLabel = new QLabel( qtr( "Various statistics about the current"
435                  " media or stream.\n Played and streamed info are shown." ) );
436      topLabel->setWordWrap( true );
437      layout->addWidget( topLabel, 0, 0 );
438
439      StatsTree = new QTreeWidget(this);
440      StatsTree->setColumnCount( 3 );
441      StatsTree->header()->hide();
442
443 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
444     itemName =                                                                 \
445       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
446     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
447
448 #define CREATE_CATEGORY( catName, itemText ) {                           \
449     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
450     catName->setExpanded( true );                                        \
451     StatsTree->addTopLevelItem( catName );    }
452
453 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
454     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
455     catName->addChild( itemName ); }
456
457     /* Create the main categories */
458     CREATE_CATEGORY( input, qtr("Input") );
459     CREATE_CATEGORY( video, qtr("Video") );
460     CREATE_CATEGORY( streaming, qtr("Streaming") );
461     CREATE_CATEGORY( audio, qtr("Audio") );
462
463     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Read at media"),
464                            "0", input , "kB" );
465     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
466                            "0", input, "kb/s" );
467     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed"), "0", input, "kB") ;
468     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Stream bitrate"),
469                            "0", input, "kb/s" );
470
471     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded blocks"),
472                            "0", video, "" );
473     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed frames"),
474                            "0", video, "" );
475     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost frames"),
476                            "0", video, "" );
477
478     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent packets"), "0", streaming, "" );
479     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent bytes"),
480                            "0", streaming, "kB" );
481     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Sent bitrates"),
482                            "0", streaming, "kb/s" );
483
484     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded blocks"),
485                            "0", audio, "" );
486     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played buffers"),
487                            "0", audio, "" );
488     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost buffers"), "0", audio, "" );
489
490     input->setExpanded( true );
491     video->setExpanded( true );
492     streaming->setExpanded( true );
493     audio->setExpanded( true );
494
495     StatsTree->resizeColumnToContents( 0 );
496     StatsTree->setColumnWidth( 1 , 100 );
497
498     layout->addWidget(StatsTree, 1, 0 );
499 }
500
501 InputStatsPanel::~InputStatsPanel()
502 {
503 }
504
505 /**
506  * Update the Statistics
507  **/
508 void InputStatsPanel::update( input_item_t *p_item )
509 {
510     vlc_mutex_lock( &p_item->p_stats->lock );
511
512 #define UPDATE( widget, format, calc... ) \
513     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
514
515     UPDATE( read_media_stat, "%8.0f",
516             (float)(p_item->p_stats->i_read_bytes)/1000);
517     UPDATE( input_bitrate_stat, "%6.0f",
518                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
519     UPDATE( demuxed_stat, "%8.0f",
520                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
521     UPDATE( stream_bitrate_stat, "%6.0f",
522                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
523
524     /* Video */
525     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
526     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
527     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
528
529     /* Sout */
530     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
531     UPDATE( send_bytes_stat, "%8.0f",
532             (float)(p_item->p_stats->i_sent_bytes)/1000 );
533     UPDATE( send_bitrate_stat, "%6.0f",
534             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
535
536     /* Audio*/
537     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
538     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
539     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
540
541     vlc_mutex_unlock(& p_item->p_stats->lock );
542 }
543
544 void InputStatsPanel::clear()
545 {
546 }
547
548