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