]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/infopanels.cpp
Qt4 - Infopanels: cleanup, verification, updates of more widgets, block the edition...
[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, 5 );
73
74     /* Number - on the same line */
75     metaLayout->addWidget( new QLabel( qtr( "Track Number" )  + " :" ),
76                   line, 6 );
77     seqnum_text = new QLineEdit;
78     seqnum_text->setInputMask("0000");
79     seqnum_text->setAlignment( Qt::AlignRight );
80     metaLayout->addWidget( seqnum_text, line, 7, 1, 4 );
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, 2 );
89
90     /* Rating - on the same line */
91     metaLayout->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 3, 1, 2 );
92     rating_text = new QSpinBox; setSpinBounds( rating_text );
93     metaLayout->addWidget( rating_text, line, 5, 1, 2 );
94
95     /* Language on the same line */
96     metaLayout->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 7 );
97     language_text = new QLineEdit;
98     metaLayout->addWidget( language_text, line,  8, 1, 2 );
99     line++;
100
101     /* ART_URL */
102     art_cover = new QLabel( "" );
103     art_cover->setMinimumHeight( 128 );
104     art_cover->setMinimumWidth( 128 );
105     art_cover->setMaximumHeight( 128 );
106     art_cover->setMaximumWidth( 128 );
107     art_cover->setScaledContents( true );
108     art_cover->setPixmap( QPixmap( ":/noart.png" ) );
109     metaLayout->addWidget( art_cover, line, 8, 4, 2 );
110     
111 /* Settings is unused */
112 /*    l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
113     setting_text = new QLineEdit;
114     l->addWidget( setting_text, line, 6, 1, 4 ); */
115
116 /* Less used metadata */
117 #define ADD_META_2( string, widget ) {                             \
118     metaLayout->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
119     widget = new QLineEdit;                                      \
120     metaLayout->addWidget( widget, line, 1, 1, 7 );                       \
121     line++;            }
122     
123     /* Now Playing - Useful for live feeds (HTTP, DVB, ETC...) */
124     ADD_META_2( VLC_META_NOW_PLAYING, nowplaying_text );
125     nowplaying_text->setReadOnly( true );
126     ADD_META_2( VLC_META_PUBLISHER, publisher_text );
127     ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
128     ADD_META_2( "Comments:", description_text );
129
130 /* useless metadata */
131
132     //ADD_META_2( VLC_META_ENCODED_BY, encodedby_text );
133     /*  ADD_META( TRACKID )  Useless ? */
134     /*  ADD_URI - DO not show it, done outside */
135
136 #undef ADD_META
137 #undef ADD_META_2
138
139     CONNECT( title_text, textEdited( QString ), this, enterEditMode() );
140     CONNECT( description_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( date_text, textEdited( QString ), this, enterEditMode() );
145     CONNECT( seqnum_text, textEdited( QString ), this, enterEditMode() );
146 /*    CONNECT( rating_text, valueChanged( QString ), this, enterEditMode( QString ) );*/
147
148     /* We are not yet in Edit Mode */
149     b_inEditMode = false;
150 }
151
152 MetaPanel::~MetaPanel(){}
153
154 /**
155  * Update all the MetaData and art on an "item-changed" event
156  **/
157 void MetaPanel::update( input_item_t *p_item )
158 {
159     /* Don't update if you are in edit mode */
160     if( b_inEditMode ) return;
161         
162     char *psz_meta; 
163 #define UPDATE_META( meta, widget ) {               \
164     psz_meta = input_item_Get##meta( p_item );      \
165     if( !EMPTY_STR( psz_meta ) )                    \
166         widget->setText( qfu( psz_meta ) );         \
167     else                                            \
168         widget->setText( "" ); }                    \
169     free( psz_meta );
170
171 #define UPDATE_META_INT( meta, widget ) {           \
172     psz_meta = input_item_Get##meta( p_item );      \
173     if( !EMPTY_STR( psz_meta ) )                    \
174         widget->setValue( atoi( psz_meta ) ); }     \
175     free( psz_meta );
176
177     /* Name / Title */
178     psz_meta = input_item_GetTitle( p_item );
179     char *psz_name = input_item_GetName( p_item );
180     if( !EMPTY_STR( psz_meta ) )
181         title_text->setText( qfu( psz_meta ) );
182     else if( !EMPTY_STR( psz_name ) )
183         title_text->setText( qfu( psz_name ) );
184     else title_text->setText( "" );
185     free( psz_meta );
186     free( psz_name );
187
188     /* URL / URI */
189     psz_meta = input_item_GetURL( p_item );
190     if( !EMPTY_STR( psz_meta ) )
191     {
192         emit uriSet( QString( psz_meta ) );
193         free( psz_meta );
194     }
195     else
196     {
197         free( psz_meta );
198         psz_meta = input_item_GetURI( p_item );
199         if( !EMPTY_STR( psz_meta ) )
200             emit uriSet( QString( psz_meta ) );
201     }
202
203     /* Other classic though */
204     UPDATE_META( Artist, artist_text );
205     UPDATE_META( Genre, genre_text );
206     UPDATE_META( Copyright, copyright_text );
207     UPDATE_META( Album, collection_text );
208     UPDATE_META( Description, description_text );
209     UPDATE_META( Language, language_text );
210     UPDATE_META( NowPlaying, nowplaying_text );
211     UPDATE_META( Publisher, publisher_text );
212 //    UPDATE_META( Setting, setting_text );
213 //    UPDATE_META( EncodedBy, encodedby_text );
214
215     UPDATE_META( Date, date_text );
216     UPDATE_META( TrackNum, seqnum_text );
217     UPDATE_META_INT( Rating, rating_text );
218
219 #undef UPDATE_META_INT
220 #undef UPDATE_META
221
222     /* Art Urls */
223     psz_meta = input_item_GetArtURL( p_item );
224     if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
225     {
226         QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
227         art_cover->setPixmap( QPixmap( artUrl ) );
228     }
229     else
230         art_cover->setPixmap( QPixmap( ":/noart.png" ) );
231     free( psz_meta );
232 }
233
234 /**
235  * Save the MetaData, triggered by parent->save Button
236  **/
237 void MetaPanel::saveMeta()
238 {
239     playlist_t *p_playlist;
240     char psz[5];
241
242     meta_export_t p_export;
243     p_export.p_item = p_input;
244
245     if( p_input == NULL )
246         return;
247
248     /* we can write meta data only in a file */
249     vlc_mutex_lock( &p_input->lock );
250     int i_type = p_input->i_type;
251     vlc_mutex_unlock( &p_input->lock );
252     if( i_type == ITEM_TYPE_FILE )
253     {
254         char *psz_uri_orig = input_item_GetURI( p_input );
255         char *psz_uri = psz_uri_orig;
256         if( !strncmp( psz_uri, "file://", 7 ) )
257             psz_uri += 7; /* strlen("file://") = 7 */
258
259         p_export.psz_file = strndup( psz_uri, PATH_MAX );
260         free( psz_uri_orig );
261     }
262     else
263         return;
264
265     /* now we read the modified meta data */
266     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
267     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
268     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
269     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
270     input_item_SetTrackNum(  p_input, qtu( seqnum_text->text() ) );
271     input_item_SetDate(  p_input, qtu( date_text->text() ) );
272
273     input_item_SetCopyright( p_input, qtu( copyright_text->text() ) );
274     input_item_SetPublisher( p_input, qtu( publisher_text->text() ) );
275     input_item_SetDescription( p_input, qtu( description_text->text() ) );
276
277     p_playlist = pl_Yield( p_intf );
278     PL_LOCK;
279     p_playlist->p_private = &p_export;
280
281     module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
282     if( p_mod )
283         module_Unneed( p_playlist, p_mod );
284     PL_UNLOCK;
285     pl_Release( p_playlist );
286     
287     /* Reset the status of the mode. No need to emit any signal */
288     b_inEditMode = false;
289 }
290
291
292 bool MetaPanel::isInEditMode()
293 {
294     return b_inEditMode;
295 }
296
297 void MetaPanel::enterEditMode()
298 {
299     setEditMode( true );
300 }
301
302 void MetaPanel::setEditMode( bool b_editing )
303 {
304     b_inEditMode = b_editing;
305     if( b_editing )emit editing();
306 }
307
308 void MetaPanel::setInput( input_item_t *input )
309 {
310     if( b_inEditMode ) return;
311
312     p_input = input;
313 }
314
315 /*
316  * Clear all the metadata widgets
317  * Unused yet FIXME
318  */
319 void MetaPanel::clear(){
320     setEditMode( false );
321 }
322
323 /**
324  * Second Panel - Shows the extra metadata in a tree, non editable.
325  **/
326 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
327                                 intf_thread_t *_p_intf )
328                                 : QWidget( parent ), p_intf( _p_intf )
329 {
330      QGridLayout *layout = new QGridLayout(this);
331
332      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
333                  " are shown in this list.\n" ) );
334      topLabel->setWordWrap( true );
335      layout->addWidget( topLabel, 0, 0 );
336
337      extraMetaTree = new QTreeWidget( this );
338      extraMetaTree->setAlternatingRowColors( true );
339      extraMetaTree->setColumnCount( 2 );
340      extraMetaTree->header()->hide();
341 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
342  *                                             << qtr( "Value" ) );
343  * Useless, add this header if you think it would help the user          **
344  */
345
346      layout->addWidget( extraMetaTree, 1, 0 );
347 }
348
349 /**
350  * Update the Extra Metadata from p_meta->i_extras 
351  **/
352 void ExtraMetaPanel::update( input_item_t *p_item )
353 {
354     vlc_mutex_lock( &p_item->lock );
355     vlc_meta_t *p_meta = p_item->p_meta;
356     if( !p_meta )
357         return;
358     QStringList tempItem;
359
360     QList<QTreeWidgetItem *> items;
361     vlc_dictionary_t * p_dict = &p_meta->extra_tags;
362     char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
363     for (int i = 0; ppsz_allkey[i] ; i++ )
364     {
365         const char * psz_value = (const char *)vlc_dictionary_value_for_key(
366                 p_dict, ppsz_allkey[i] );
367         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
368         tempItem.append( qfu( psz_value ) );
369         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
370         free( ppsz_allkey[i] );
371     }
372     vlc_mutex_unlock( &p_item->lock );
373     free( ppsz_allkey );
374     extraMetaTree->addTopLevelItems( items );
375 }
376
377 /**
378  * Clear the ExtraMetaData Tree
379  **/
380 void ExtraMetaPanel::clear()
381 {
382     extraMetaTree->clear();
383 }
384
385 /**
386  * Third panel - Stream info
387  * Display all codecs and muxers info that we could gather.
388  **/
389 InfoPanel::InfoPanel( QWidget *parent,
390                       intf_thread_t *_p_intf )
391                       : QWidget( parent ), p_intf( _p_intf )
392 {
393      QGridLayout *layout = new QGridLayout(this);
394
395      QList<QTreeWidgetItem *> items;
396
397      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
398               " stream is made of.\n Muxer, Audio and Video Codecs, Subtitles "
399               "are shown." ) );
400      topLabel->setWordWrap( true );
401      layout->addWidget( topLabel, 0, 0 );
402
403      InfoTree = new QTreeWidget(this);
404      InfoTree->setColumnCount( 1 );
405      InfoTree->header()->hide();
406      layout->addWidget(InfoTree, 1, 0 );
407 }
408
409 InfoPanel::~InfoPanel()
410 {
411 }
412
413 /**
414  * Update the Codecs information on parent->update()
415  **/
416 void InfoPanel::update( input_item_t *p_item)
417 {
418     InfoTree->clear();
419     QTreeWidgetItem *current_item = NULL;
420     QTreeWidgetItem *child_item = NULL;
421
422     for( int i = 0; i< p_item->i_categories ; i++)
423     {
424         current_item = new QTreeWidgetItem();
425         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
426         InfoTree->addTopLevelItem( current_item );
427
428         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
429         {
430             child_item = new QTreeWidgetItem ();
431             child_item->setText( 0,
432                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
433                     + ": "
434                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
435
436             current_item->addChild(child_item);
437         }
438          InfoTree->setItemExpanded( current_item, true);
439     }
440 }
441
442 /**
443  * Clear the tree
444  **/
445 void InfoPanel::clear()
446 {
447     InfoTree->clear();
448 }
449
450 /**
451  * Save all the information to a file
452  * Not yet implemented.
453  **/
454 /*
455 void InfoPanel::saveCodecsInfo()
456 {
457
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( input, qtr("Input") );
498     CREATE_CATEGORY( video, qtr("Video") );
499     CREATE_CATEGORY( streaming, qtr("Streaming") );
500     CREATE_CATEGORY( audio, qtr("Audio") );
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 }