]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/info_panels.cpp
Qt: Better media info panel
[vlc] / modules / gui / qt4 / components / info_panels.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  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31 #include "components/info_panels.hpp"
32 #include "components/interface_widgets.hpp"
33
34 #include <assert.h>
35 #include <vlc_url.h>
36 #include <vlc_meta.h>
37
38 #include <QTreeWidget>
39 #include <QHeaderView>
40 #include <QList>
41 #include <QStringList>
42 #include <QGridLayout>
43 #include <QLineEdit>
44 #include <QLabel>
45 #include <QSpinBox>
46
47 /************************************************************************
48  * Single panels
49  ************************************************************************/
50
51 /**
52  * First Panel - Meta Info
53  * All the usual MetaData are displayed and can be changed.
54  **/
55 MetaPanel::MetaPanel( QWidget *parent,
56                       intf_thread_t *_p_intf )
57                       : QWidget( parent ), p_intf( _p_intf )
58 {
59     QGridLayout *metaLayout = new QGridLayout( this );
60     metaLayout->setVerticalSpacing( 12 );
61
62     int line = 0; /* Counter for GridLayout */
63     p_input = NULL;
64
65 #define ADD_META( string, widget ) {                                      \
66     metaLayout->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
67     widget = new QLineEdit;                                               \
68     metaLayout->addWidget( widget, line, 1, 1, 9 );                       \
69     line++;            }
70
71     /* Title, artist and album*/
72     ADD_META( VLC_META_TITLE, title_text ); /* OK */
73     ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
74     ADD_META( VLC_META_ALBUM, collection_text ); /* OK */
75
76     /* Genre Name */
77     /* TODO List id3genres.h is not includable yet ? */
78     genre_text = new QLineEdit;
79     metaLayout->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
80     metaLayout->addWidget( genre_text, line, 1, 1, 3 );
81
82     /* Number - on the same line */
83     metaLayout->addWidget( new QLabel( qtr( VLC_META_TRACK_NUMBER )  + " :" ),
84                   line, 5, 1, 2  );
85     seqnum_text = new QLineEdit;
86     seqnum_text->setInputMask("0000");
87     seqnum_text->setAlignment( Qt::AlignRight );
88     metaLayout->addWidget( seqnum_text, line, 7, 1, 3 );
89     line++;
90
91     /* Date (Should be in years) */
92     date_text = new QLineEdit;
93     date_text->setInputMask("0000");
94     date_text->setAlignment( Qt::AlignRight );
95     metaLayout->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 0 );
96     metaLayout->addWidget( date_text, line, 1, 1, 3 );
97
98     /* Rating - on the same line */
99     /*
100     metaLayout->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 4, 1, 2 );
101     rating_text = new QSpinBox; setSpinBounds( rating_text );
102     metaLayout->addWidget( rating_text, line, 6, 1, 1 );
103     */
104     /* Language on the same line */
105     metaLayout->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 5, 1, 2 );
106     language_text = new QLineEdit;
107     language_text->setReadOnly( true );
108     metaLayout->addWidget( language_text, line,  7, 1, 3 );
109     line++;
110
111     /* ART_URL */
112     art_cover = new CoverArtLabel( this, p_intf );
113     metaLayout->addWidget( art_cover, line, 8, 4, 2, Qt::AlignRight );
114
115 /* Settings is unused */
116 /*    l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
117     setting_text = new QLineEdit;
118     l->addWidget( setting_text, line, 6, 1, 4 ); */
119
120 /* Less used metadata */
121 #define ADD_META_2( string, widget ) {                                    \
122     metaLayout->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
123     widget = new QLineEdit;                                               \
124     metaLayout->addWidget( widget, line, 1, 1, 7 );                       \
125     line++;            }
126
127     /* Now Playing - Useful for live feeds (HTTP, DVB, ETC...) */
128     ADD_META_2( VLC_META_NOW_PLAYING, nowplaying_text );
129     nowplaying_text->setReadOnly( true );
130     ADD_META_2( VLC_META_PUBLISHER, publisher_text );
131     ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
132     ADD_META_2( N_("Comments"), description_text );
133
134 /* useless metadata */
135
136     //ADD_META_2( VLC_META_ENCODED_BY, encodedby_text );
137     /*  ADD_META( TRACKID )  Useless ? */
138     /*  ADD_URI - DO not show it, done outside */
139
140     metaLayout->setColumnStretch( 1, 2 );
141     metaLayout->setColumnMinimumWidth ( 1, 80 );
142     metaLayout->setRowStretch( line, 10 );
143 #undef ADD_META
144 #undef ADD_META_2
145
146     CONNECT( title_text, textEdited( QString ), this, enterEditMode() );
147     CONNECT( artist_text, textEdited( QString ), this, enterEditMode() );
148     CONNECT( collection_text, textEdited( QString ), this, enterEditMode() );
149     CONNECT( genre_text, textEdited( QString ), this, enterEditMode() );
150     CONNECT( seqnum_text, textEdited( QString ), this, enterEditMode() );
151
152     CONNECT( date_text, textEdited( QString ), this, enterEditMode() );
153     CONNECT( description_text, textEdited( QString ), this, enterEditMode() );
154 /*    CONNECT( rating_text, valueChanged( QString ), this, enterEditMode( QString ) );*/
155
156     /* We are not yet in Edit Mode */
157     b_inEditMode = false;
158 }
159
160 /**
161  * Update all the MetaData and art on an "item-changed" event
162  **/
163 void MetaPanel::update( input_item_t *p_item )
164 {
165     if( !p_item )
166     {
167         clear();
168         return;
169     }
170
171     /* Don't update if you are in edit mode */
172     if( b_inEditMode ) return;
173     else p_input = p_item;
174
175     char *psz_meta;
176 #define UPDATE_META( meta, widget ) {               \
177     psz_meta = input_item_Get##meta( p_item );      \
178     if( !EMPTY_STR( psz_meta ) )                    \
179         widget->setText( qfu( psz_meta ) );         \
180     else                                            \
181         widget->setText( "" ); }                    \
182     free( psz_meta );
183
184 #define UPDATE_META_INT( meta, widget ) {           \
185     psz_meta = input_item_Get##meta( p_item );      \
186     if( !EMPTY_STR( psz_meta ) )                    \
187         widget->setValue( atoi( psz_meta ) ); }     \
188     free( psz_meta );
189
190     /* Name / Title */
191     psz_meta = input_item_GetTitleFbName( p_item );
192     if( psz_meta )
193     {
194         title_text->setText( qfu( psz_meta ) );
195         free( psz_meta );
196     }
197     else
198         title_text->setText( "" );
199
200     /* URL / URI */
201     psz_meta = input_item_GetURL( p_item );
202     if( !EMPTY_STR( psz_meta ) )
203         emit uriSet( qfu( psz_meta ) );
204     else
205     {
206         free( psz_meta );
207         psz_meta = input_item_GetURI( p_item );
208         if( !EMPTY_STR( psz_meta ) )
209             emit uriSet( qfu( psz_meta ) );
210     }
211     free( psz_meta );
212
213     /* Other classic though */
214     UPDATE_META( Artist, artist_text );
215     UPDATE_META( Genre, genre_text );
216     UPDATE_META( Copyright, copyright_text );
217     UPDATE_META( Album, collection_text );
218     UPDATE_META( Description, description_text );
219     UPDATE_META( Language, language_text );
220     UPDATE_META( NowPlaying, nowplaying_text );
221     UPDATE_META( Publisher, publisher_text );
222 //    UPDATE_META( Setting, setting_text );
223 //FIXME this is wrong if has Publisher and EncodedBy fields
224     UPDATE_META( EncodedBy, publisher_text );
225
226     UPDATE_META( Date, date_text );
227     UPDATE_META( TrackNum, seqnum_text );
228 //    UPDATE_META_INT( Rating, rating_text );
229
230 #undef UPDATE_META_INT
231 #undef UPDATE_META
232
233     // If a artURL is available as a local file, directly display it !
234
235     QString file;
236     char *psz_art = input_item_GetArtURL( p_item );
237     if( psz_art && !strncmp( psz_art, "file://", 7 ) &&
238                 decode_URI( psz_art + 7 ) )
239 #ifdef WIN32
240         file = qfu( psz_art + 8 ); // Remove extra / on Win32 URI.
241 #else
242         file = qfu( psz_art + 7 );
243 #endif
244     free( psz_art );
245
246     art_cover->showArtUpdate( file );
247
248 }
249
250 /**
251  * Save the MetaData, triggered by parent->save Button
252  **/
253 void MetaPanel::saveMeta()
254 {
255     if( p_input == NULL )
256         return;
257
258     /* now we read the modified meta data */
259     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
260     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
261     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
262     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
263     input_item_SetTrackNum(  p_input, qtu( seqnum_text->text() ) );
264     input_item_SetDate(  p_input, qtu( date_text->text() ) );
265
266     input_item_SetCopyright( p_input, qtu( copyright_text->text() ) );
267     input_item_SetPublisher( p_input, qtu( publisher_text->text() ) );
268     input_item_SetDescription( p_input, qtu( description_text->text() ) );
269
270     playlist_t *p_playlist = pl_Hold( p_intf );
271     input_item_WriteMeta( VLC_OBJECT(p_playlist), p_input );
272     pl_Release( p_intf );
273
274     /* Reset the status of the mode. No need to emit any signal because parent
275        is the only caller */
276     b_inEditMode = false;
277 }
278
279
280 bool MetaPanel::isInEditMode()
281 {
282     return b_inEditMode;
283 }
284
285 void MetaPanel::enterEditMode()
286 {
287     msg_Dbg( p_intf, "Entering Edit MetaData Mode" );
288     setEditMode( true );
289 }
290
291 void MetaPanel::setEditMode( bool b_editing )
292 {
293     b_inEditMode = b_editing;
294     if( b_editing )emit editing();
295 }
296
297 /*
298  * Clear all the metadata widgets
299  */
300 void MetaPanel::clear()
301 {
302     title_text->clear();
303     artist_text->clear();
304     genre_text->clear();
305     copyright_text->clear();
306     collection_text->clear();
307     seqnum_text->clear();
308     description_text->clear();
309     date_text->clear();
310     language_text->clear();
311     nowplaying_text->clear();
312     publisher_text->clear();
313
314     setEditMode( false );
315     emit uriSet( "" );
316 }
317
318 /**
319  * Second Panel - Shows the extra metadata in a tree, non editable.
320  **/
321 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
322                                 intf_thread_t *_p_intf )
323                                 : QWidget( parent ), p_intf( _p_intf )
324 {
325      QGridLayout *layout = new QGridLayout(this);
326
327      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
328                  " are shown in this panel.\n" ) );
329      topLabel->setWordWrap( true );
330      layout->addWidget( topLabel, 0, 0 );
331
332      extraMetaTree = new QTreeWidget( this );
333      extraMetaTree->setAlternatingRowColors( true );
334      extraMetaTree->setColumnCount( 2 );
335      extraMetaTree->resizeColumnToContents( 0 );
336      extraMetaTree->header()->hide();
337 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
338  *                                             << qtr( "Value" ) );
339  * Useless, add this header if you think it would help the user          **
340  */
341
342      layout->addWidget( extraMetaTree, 1, 0 );
343 }
344
345 /**
346  * Update the Extra Metadata from p_meta->i_extras
347  **/
348 void ExtraMetaPanel::update( input_item_t *p_item )
349 {
350     if( !p_item )
351     {
352         clear();
353         return;
354     }
355
356     QList<QTreeWidgetItem *> items;
357
358     extraMetaTree->clear();
359
360     vlc_mutex_lock( &p_item->lock );
361     vlc_meta_t *p_meta = p_item->p_meta;
362     if( !p_meta )
363     {
364         vlc_mutex_unlock( &p_item->lock );
365         return;
366     }
367
368     char ** ppsz_allkey = vlc_meta_CopyExtraNames( p_meta);
369
370     for( int i = 0; ppsz_allkey[i] ; i++ )
371     {
372         const char * psz_value = vlc_meta_GetExtra( p_meta, ppsz_allkey[i] );
373         QStringList tempItem;
374         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
375         tempItem.append( qfu( psz_value ) );
376         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
377         free( ppsz_allkey[i] );
378     }
379     vlc_mutex_unlock( &p_item->lock );
380     free( ppsz_allkey );
381
382     extraMetaTree->addTopLevelItems( items );
383     extraMetaTree->resizeColumnToContents( 0 );
384 }
385
386 /**
387  * Clear the ExtraMetaData Tree
388  **/
389 void ExtraMetaPanel::clear()
390 {
391     extraMetaTree->clear();
392 }
393
394 /**
395  * Third panel - Stream info
396  * Display all codecs and muxers info that we could gather.
397  **/
398 InfoPanel::InfoPanel( QWidget *parent,
399                       intf_thread_t *_p_intf )
400                       : QWidget( parent ), p_intf( _p_intf )
401 {
402      QGridLayout *layout = new QGridLayout(this);
403
404      QList<QTreeWidgetItem *> items;
405
406      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
407               " stream is made of.\nMuxer, Audio and Video Codecs, Subtitles "
408               "are shown." ) );
409      topLabel->setWordWrap( true );
410      layout->addWidget( topLabel, 0, 0 );
411
412      InfoTree = new QTreeWidget(this);
413      InfoTree->setColumnCount( 1 );
414      InfoTree->setColumnWidth( 0, 20000 );
415      InfoTree->header()->hide();
416 //     InfoTree->header()->setStretchLastSection(false);
417 //     InfoTree->header()->setResizeMode(QHeaderView::ResizeToContents);
418      layout->addWidget(InfoTree, 1, 0 );
419 }
420
421 /**
422  * Update the Codecs information on parent->update()
423  **/
424 void InfoPanel::update( input_item_t *p_item)
425 {
426     if( !p_item )
427     {
428         clear();
429         return;
430     }
431
432     InfoTree->clear();
433     QTreeWidgetItem *current_item = NULL;
434     QTreeWidgetItem *child_item = NULL;
435
436     for( int i = 0; i< p_item->i_categories ; i++)
437     {
438         current_item = new QTreeWidgetItem();
439         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
440         InfoTree->addTopLevelItem( current_item );
441
442         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
443         {
444             child_item = new QTreeWidgetItem ();
445             child_item->setText( 0,
446                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
447                     + ": "
448                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
449
450             current_item->addChild(child_item);
451         }
452          InfoTree->setItemExpanded( current_item, true);
453     }
454 }
455
456 /**
457  * Clear the tree
458  **/
459 void InfoPanel::clear()
460 {
461     InfoTree->clear();
462 }
463
464 /**
465  * Save all the information to a file
466  * Not yet implemented.
467  **/
468 /*
469 void InfoPanel::saveCodecsInfo()
470 {}
471 */
472
473 /**
474  * Fourth Panel - Stats
475  * Displays the Statistics for reading/streaming/encoding/displaying in a tree
476  */
477 InputStatsPanel::InputStatsPanel( QWidget *parent,
478                                   intf_thread_t *_p_intf )
479                                   : QWidget( parent ), p_intf( _p_intf )
480 {
481      QGridLayout *layout = new QGridLayout(this);
482
483      QList<QTreeWidgetItem *> items;
484
485      QLabel *topLabel = new QLabel( qtr( "Current"
486                  " media / stream " "statistics") );
487      topLabel->setWordWrap( true );
488      layout->addWidget( topLabel, 0, 0 );
489
490      StatsTree = new QTreeWidget(this);
491      StatsTree->setColumnCount( 3 );
492      StatsTree->header()->hide();
493
494 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
495     itemName =                                                                 \
496       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
497     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
498
499 #define CREATE_CATEGORY( catName, itemText ) {                           \
500     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
501     catName->setExpanded( true );                                        \
502     StatsTree->addTopLevelItem( catName );    }
503
504 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
505     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
506     catName->addChild( itemName ); }
507
508     /* Create the main categories */
509     CREATE_CATEGORY( audio, qtr("Audio") );
510     CREATE_CATEGORY( video, qtr("Video") );
511     CREATE_CATEGORY( input, qtr("Input/Read") );
512     CREATE_CATEGORY( streaming, qtr("Output/Written/Sent") );
513
514     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Media data size"),
515                            "0", input , "kB" );
516     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
517                            "0", input, "kb/s" );
518     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed data size"), "0", input, "kB") ;
519     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Content bitrate"),
520                            "0", input, "kb/s" );
521     CREATE_AND_ADD_TO_CAT( corrupted_stat, qtr("Discarded (corrupted)"),
522                            "0", input, "" );
523     CREATE_AND_ADD_TO_CAT( discontinuity_stat, qtr("Dropped (discontinued)"),
524                            "0", input, "" );
525
526     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded"),
527                            "0", video, qtr("blocks") );
528     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed"),
529                            "0", video, qtr("frames") );
530     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost"),
531                            "0", video, qtr("frames") );
532
533     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent"), "0", streaming, qtr("packets") );
534     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent"),
535                            "0", streaming, "kB" );
536     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Upstream rate"),
537                            "0", streaming, "kb/s" );
538
539     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded"),
540                            "0", audio, qtr("blocks") );
541     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played"),
542                            "0", audio, qtr("buffers") );
543     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost"), "0", audio, qtr("buffers") );
544
545 #undef CREATE_AND_ADD_TO_CAT
546 #undef CREATE_CATEGORY
547 #undef CREATE_TREE_ITEM
548
549     input->setExpanded( true );
550     video->setExpanded( true );
551     streaming->setExpanded( true );
552     audio->setExpanded( true );
553
554     StatsTree->resizeColumnToContents( 0 );
555     StatsTree->setColumnWidth( 1 , 200 );
556
557     layout->addWidget(StatsTree, 1, 0 );
558 }
559
560 /**
561  * Update the Statistics
562  **/
563 void InputStatsPanel::update( input_item_t *p_item )
564 {
565     assert( p_item );
566     vlc_mutex_lock( &p_item->p_stats->lock );
567
568 #define UPDATE( widget, format, calc... ) \
569     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
570
571     UPDATE( read_media_stat, "%8.0f",
572             (float)(p_item->p_stats->i_read_bytes)/1000);
573     UPDATE( input_bitrate_stat, "%6.0f",
574                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
575     UPDATE( demuxed_stat, "%8.0f",
576                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
577     UPDATE( stream_bitrate_stat, "%6.0f",
578                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
579     UPDATE( corrupted_stat, "%5i", p_item->p_stats->i_demux_corrupted );
580     UPDATE( discontinuity_stat, "%5i", p_item->p_stats->i_demux_discontinuity );
581
582     /* Video */
583     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
584     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
585     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
586
587     /* Sout */
588     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
589     UPDATE( send_bytes_stat, "%8.0f",
590             (float)(p_item->p_stats->i_sent_bytes)/1000 );
591     UPDATE( send_bitrate_stat, "%6.0f",
592             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
593
594     /* Audio*/
595     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
596     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
597     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
598
599 #undef UPDATE
600
601     vlc_mutex_unlock(& p_item->p_stats->lock );
602 }
603
604 void InputStatsPanel::clear()
605 {
606 }
607