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