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