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