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